Logical properties in CSS

王林
Release: 2023-09-14 10:29:02
forward
1198 people have browsed it

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!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!