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 id="Using-the-i-margins-and-paddings-logical-properties-i-in-CSS"> 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 id="Using-the-i-Logical-border-i-properties-in-CSS"> 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 id="Using-the-i-margin-inline-property-i-to-set-the-inline-margin"> 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!

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust


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

Dreamweaver Mac version
Visual web development tools

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
