Table of Contents
Why should we use logical properties instead of normal CSS properties?
grammar
Example 1 (margin and padding logical properties)
Example 3
Home Web Front-end CSS Tutorial Logical properties in CSS

Logical properties in CSS

Sep 14, 2023 am 10:29 AM

CSS 中的逻辑属性

In CSS, logical properties allow developers to define styles based on the logical structure of a web page rather than the physical layout. This means we can apply CSS based on text direction or content flow.

Mainly use logical attributes to set the margins, padding and borders of HTML elements. It contains different variations of margin, padding, and border properties.

Logical properties can be defined based on block-level and inline dimensions.

  • Block dimension − The block dimension represents the perpendicular direction of the content flow. For example, in English text direction is left to right. So, block dimensions handle the top and bottom of the element.

  • Inline dimensions - Inline dimensions represent the same orientation as the content or text orientation. For English, left and right are inline dimensions.

Let’s look at some commonly used logical properties in CSS.

  • Border-block − It sets the upper and lower borders.

  • Border-inline − Set left and right borders.

  • Border-block-start − It sets the top border.

  • Border-block-end − It sets the bottom border.

  • Margin-inline − It sets the left and right margins.

  • Padding-inline − It sets the left and right padding.

  • Padding-inline-start − It sets the left padding.

  • Margin-inline-end − It sets the bottom padding.

  • Border-inline-end-width − It sets the width of the right border.

  • Border-block-start-style − It sets the style of the top border.

In the above properties, users can observe that we require to use 'block' for top and bottom and 'inline' for left and right. Also, 'start' for left and top, and 'end' for right and bottom .

Why should we use logical properties instead of normal CSS properties?

By observing the functions of the above properties, the first question that comes to mind is whether we can use ordinary CSS properties to achieve the same style, and why we should use logical properties. Here are your answers.

Sometimes, we need to set left and right margins for HTML elements. We can do this using the '0 auto' value of the margin attribute, or using the CSS properties of margin-left and margin-right respectively. When using '0 auto' we will also change the values ​​of the top and bottom margins if they have been applied previously. Therefore, it is better to use the 'margin-inline' CSS property.

margin: 0 auto;
or
margin-left: auto;
margin-right: auto;
or
margin-inline: auto;
Copy after login

grammar

Users can follow the syntax below to use logical properties in CSS.

padding-block-start: value;
margin-inline-end: value;
Copy after login

In the above syntax, we use logical properties just like other CSS properties.

Example 1 (margin and padding logical properties)

In the example below, we create two div elements and add text inside them. In CSS, we used the "padding-block-start", "padding-inline-start" and "margin-block-end" logical CSS properties to set the top and left padding and bottom margin for the first div.

Additionally, we used the ‘margin-inline-end’ logical CSS property to add right padding to the div element.

<html>
<head>
   <style>
      .text {
         padding-block-start: 20px;
         padding-inline-start: 30px;
         margin-block-end: 50px;
         color: green;
         background-color: red;
         width: 300px;
         font-size: 2rem;
      }
      .text1 {
         width: 300px;
         font-size: 2rem;
         padding-block-start: 20px;
         padding-inline-start: 10px;
         margin-inline-end: 50px;
         color: blue;
         background-color: yellow;
      }
   </style>
</head>
<body>
   <h3> Using the <i> margins and paddings logical properties </i> in CSS </h3>
   <div class = "text"> This is a text. </div>
   <div class = "text1"> This is another text div element. </div>
</body>
</html>
Copy after login

Example 2

In the example below, we have demonstrated the logical CSS properties related to the border. We used the 'border-block-start' to apply the top border and the 'border-block-end' to apply the bottom border. Furthermore, we used the 'border-inline-start' to apply the left border and 'border-inline-end' to apply the right border.

In the output, users can observe the different borders for the different sides of the div element.

<html>
<head>
   <style>
      .sample {
         border-block-start: 3px dotted blue;
         border-block-end: 5px solid green;
         border-inline-start: 10px double red;
         border-inline-end: 5px groove yellow;
         padding: 10px;
         width: 300px;
         height: 200px;
      }
      .left {color: red;}
      .right {color: yellow;}
      .top {color: blue;}
      .bottom {color: green;}
   </style>
</head>
<body>
   <h2> Using the <i> Logical border </i> properties in CSS </h2>
   <div class = "sample">
      Observe the border of the div.
      <p class = "left"> border inline start </p>
      <p class = "right"> border inline end </p>
      <p class = "top"> border block start </p>
      <p class = "bottom"> border block end </p>
   </div>
</body>
</html>
Copy after login

Example 3

is translated as:

Example 3

In the example below, we applied the CSS logical properties related to the margin and padding in the flexbox. Here, we have created three div elements inside the container div element. After that, we used the 'padding-inline' to apply right and left padding in the container div element.

<html>
<head>
   <style>
      .container {
         display: flex;
         flex-direction: row;
         justify-content: space-between;
         padding-inline: 40px;
         width: 500px;
         background-color: bisque;
         font-size: 2rem;
      }
      .item {flex: 1;}
   </style>
</head>
<body>
   <h3> Using the <i> margin-inline property </i> to set the inline margin </h3>
   <div class = "container">
      <div class = "item"> First </div>
      <div class = "item"> second </div>
      <div class = "item"> Third </div>
   </div>
</body>
</html>
Copy after login

Users learned to use logical attributes in CSS. Most logical properties relate to margins, padding, and borders. However, some logical properties related to overflow also exist and can be consulted by developers on the Internet. When working with logical properties, users need to focus on the "Block" and "Inline" dimensions as well as the "Start" and "End" directions.

The above is the detailed content of Logical properties in CSS. 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)

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

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.

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.

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:

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.

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

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles