Understanding CSS Logical Properties

azam4code

Azam

Posted on June 26, 2023

Understanding CSS Logical Properties

CSS logical properties are a powerful tool for building flexible, responsive web layouts. By using logical properties instead of traditional physical properties in your CSS code, you can create designs that adapt automatically to different screen sizes and orientations, without having to write multiple versions of the same layout rules. Logical properties allow you to express your layout logic in terms of start/end instead of left/right or top/bottom, which makes it easier to create layouts that work in any language or writing direction. With logical properties, you can simplify your code, reduce redundancy, and build more efficient, dynamic websites that are ready for the modern web.

CSS Logical Properties provide a way to define layout rules based on the logical (start/end) values rather than the physical (left/right) values. This makes it much easier to create layouts that work across different writing modes such as LTR (left-to-right) and RTL (right-to-left).

Here's an example of using logical properties for a simple layout:

<div class="container">
  <div class="box"></div>
  <div class="box"></div>
</div>
Enter fullscreen mode Exit fullscreen mode
.container {
  display: flex;
  flex-direction: column;
}

.box {
  height: 50px;
  width: 50px;
  background-color: red;
  margin: 10px;

  /* Logical Properties */
  margin-inline-start: 20px;
  margin-inline-end: 20px;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have a box layout with two boxes stacked on top of each other in a container. We've set the container to be a flexbox with a vertical direction, which means that the boxes will stack on top of each other instead of side-by-side.

To space the boxes out evenly, we could use margin-left and margin-right properties in the box styles. However, this can cause problems if the text direction changes, such as with languages that are read right-to-left instead of left-to-right.

Instead, we've used CSS logical properties called margin-inline-start and margin-inline-end. These properties behave like margin-left and margin-right, but adjust themselves based on the writing mode. This means that if the text direction is left-to-right, margin-inline-start will act like margin-left. If the text direction is right-to-left, margin-inline-start will act like margin-right.

This makes it much easier to create layouts that work well across different languages and writing modes without having to adjust the styles manually.

Another Example

<div class="box">
  <p>Hello World!</p>
</div>
Enter fullscreen mode Exit fullscreen mode
.box {
  width: 50%;
  height: 200px;
  padding-inline: 10%;
  margin-inline: auto;
  border-block-end: 2px solid black;
}

p {
  padding-block: 1em;
  margin-inline: 0;
} 

Enter fullscreen mode Exit fullscreen mode

In this example, we have a div element with the class of .box that contains a p element. We're using all the available CSS logical properties to style them.

  • width: Sets the width of the box to 50% of its containing block.
  • height: Sets the height of the box to 200 pixels.
  • padding-inline: Sets the horizontal padding of the box to 10% of its containing block. This property replaces padding-left and padding-right
  • margin-inline: Centers the box horizontally by setting the left and right margins to auto. This property replaces margin-left and margin-right.
  • border-block-end: Adds a 2-pixel thick black border to the bottom of the box. This property replaces border-bottom.
  • padding-block: Adds vertical padding of 1em to the p element. This property replaces padding-top and padding-bottom.
  • margin-inline: Removes the default left and right margins from the p element. This property replaces margin-left and margin-right.

By using logical properties, we can write more maintainable and flexible code that adapts better to different screen sizes and orientations.

💖 💪 🙅 🚩
azam4code
Azam

Posted on June 26, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related