Logical properties in 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;
grammar
Users can follow the syntax below to use logical properties in CSS.
padding-block-start: value; margin-inline-end: value;
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>
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>
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

It'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.

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's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

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.

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

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

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
