Table of Contents
Understanding "Inline" and "Block" Directions
Writing Mode and Directionality
Complete List of Logical Properties and Values
Sizing
Borders
Margin and Padding
Positioning
Text Alignment
Border Radius, Floats, and Other Properties
Further Exploration
Conclusion
Home Web Front-end CSS Tutorial CSS Logical Properties and Values

CSS Logical Properties and Values

Mar 21, 2025 am 10:21 AM

Logical CSS Properties: A Comprehensive Guide

With cross-browser support for logical properties reaching a critical mass, now's the ideal time to explore their advantages. These properties are invaluable for multilingual websites, offering significant improvements in code efficiency and maintainability. Even for single-language sites, the streamlined syntax provides worthwhile benefits.

For instance, centering an element often involves this cumbersome code:

<code>.thing {
  margin-left: auto;
  margin-right: auto;
}</code>
Copy after login

While margin: 0 auto; offers a shorter alternative, it affects all margins. The margin-inline logical property provides a more precise solution, targeting only left and right margins.

Understanding "Inline" and "Block" Directions

The margin-inline property elegantly sets both margin-left and margin-right. Similarly, margin-block manages margin-top and margin-bottom. This simplification extends to border and padding properties. For example, border-inline applies borders only to the sides, avoiding individual directional specifications.

This approach shifts the focus from physical (left, right, top, bottom) to logical (inline, block) directions. Inline handles horizontal positioning, while block handles vertical positioning.

However, this relationship changes with variations in writing direction.

Writing Mode and Directionality

The examples above illustrate CSS logical properties—alternatives to traditional properties that abstract away physical directions.

CSS, initially designed for left-to-right (LTR) languages like English, doesn't inherently support right-to-left (RTL) languages like Arabic. HTML's dir attribute addresses this:

<div dir="rtl">...</div>
Copy after login

CSS offers an equivalent (direction: rtl;), though the HTML attribute is preferred for robustness.

Languages like Chinese, Japanese, Korean, and Mongolian can be written horizontally (LTR or RTL) or vertically. While horizontal writing is prevalent, vertical writing is more common in Japanese websites, sometimes mixed with horizontal text. Vertical writing orientations vary: Chinese, Japanese, and Korean typically start top-right, while Mongolian starts top-left. CSS's writing-mode property handles this:

  • horizontal-tb: Default LTR/RTL, top-to-bottom.
  • vertical-rl: RTL, top-to-bottom (Chinese, Japanese, Korean).
  • vertical-lr: LTR, top-to-bottom (Mongolian).

Logical properties provide context-aware CSS. Spacing and layout adapt to both writing-mode and direction, enabling cross-language CSS reuse. This contrasts with relying on automatic translation, offering a superior user experience and allowing for region-specific content customization while maintaining consistent visual styling.

The image below illustrates the limitations of physical properties. Using margin-left (red), LTR spacing is correct, but RTL spacing is flawed. Logical properties resolve this.

CSS Logical Properties and Values

Logical properties automatically adjust to the language context. In LTR languages, margin-inline-start sets the left margin; in RTL languages, it sets the right margin. For vertical text, it adjusts accordingly, placing the margin at the reading start point. The inline direction adapts to the element's writing-mode.

Complete List of Logical Properties and Values

Numerous CSS properties have logical equivalents. Adrian Roselli's visualization tool helps compare physical and logical properties under default LTR horizontal settings.

The following tables map physical and logical properties (using LTR horizontal mapping as a reference). Remember, the context-sensitive nature of logical properties is key.

Sizing

In horizontal mode, inline-size sets width, block-size sets height; in vertical mode, this reverses. Cross-browser support is excellent.

Borders

Excellent cross-browser support exists for logical border properties. Examples demonstrate border-inline-start, border-block-start, and border-block-end. Individual border color, width, and style properties also have logical counterparts, along with shorthand properties.

Margin and Padding

Logical margin and padding properties mirror each other, offering comprehensive cross-browser support and shorthands.

Positioning

Logical positioning offsets are available. inset-block-start maps to top (horizontal), inset-inline-start maps to left (LTR horizontal), and their behavior adapts to different writing modes. Modern browsers support these, with recent Safari inclusion. inset provides a shorthand for all four offsets. Note that inset is a shorthand for physical values, not logical properties.

Text Alignment

Logical text alignment (text-align: start, text-align: end) has strong browser support, adapting to LTR/RTL contexts.

Border Radius, Floats, and Other Properties

Logical border-radius properties have developing browser support. Logical float values have limited support. Proposed logical properties for overflow and resize have poor support.

Further Exploration

For deeper dives, consider these resources:

  • “RTL Styling 101” (Ahmad Shadeed): Comprehensive guide to RTL styling.
  • text-combine-upright (CSS-Tricks): For vertical text manipulation.
  • Web Awards for Horizontal and Vertical Writings: Real-world examples of vertical typography.

Conclusion

While not requiring immediate codebase overhauls, adopting logical properties offers significant advantages. Excellent browser support and improved code clarity make them a worthwhile addition to any CSS workflow.

The above is the detailed content of CSS Logical Properties and Values. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles