Tip 46/72

Logical Properties

Learn when and how to use logical properties.

Logical properties in CSS allow you to create layouts that adapt to different writing modes and text directions. This can be particularly useful for creating more flexible and adaptable designs.

What is a Logical Property?

Logical properties are CSS properties that adapt to the writing mode and text direction of the document. Instead of using physical properties like margin-left or padding-right, you use logical properties like margin-inline-start or padding-inline-end.

<div class="flex ps-11 pe-11">
    <div class="flex-1">First</div>
    <div class="flex-1">Second</div>
</div>

In this example:

  • ps-11 is the logical equivalent of padding-left in left-to-right (LTR) writing mode and padding-right in right-to-left (RTL) writing mode.
  • pe-11 is the logical equivalent of padding-right in LTR writing mode and padding-left in RTL writing mode.

Difference with Physical Properties

Physical properties are fixed and do not adapt to the writing mode or text direction. For example, margin-left always applies a margin to the left side of an element, regardless of the writing mode.

Logical properties, on the other hand, adapt to the writing mode and text direction. This makes them more flexible and suitable for creating layouts that need to support multiple languages and writing modes.

<!-- Physical Properties -->
<div class="mx-11"></div>
<div class="px-11"></div>

<!-- Logical Properties -->
<div class="ps-11 pe-11"></div>
<div class="ms-11 me-11"></div>

Why Use Logical Properties?

Logical properties provide several benefits:

  • Flexibility: They adapt to different writing modes and text directions, making your layouts more flexible.
  • Internationalization: They make it easier to create layouts that support multiple languages and writing modes.
  • Consistency: They provide a consistent way to apply styles, regardless of the writing mode or text direction.

When to Use Logical Properties

Use logical properties when you need to create layouts that support multiple languages and writing modes. They are particularly useful for:

  • Internationalization: Creating layouts that need to support both LTR and RTL languages.
  • Responsive Design: Adapting layouts to different writing modes and text directions.
  • Consistency: Ensuring consistent styling across different writing modes and text directions.
<div class="ps-11 pe-11"></div>

See the How to create an Input with icon tip for practical examples of using logical properties in TailwindCSS.

Conclusion

Logical properties in TailwindCSS provide a powerful way to create flexible and adaptable layouts. By leveraging these properties, you can create designs that support multiple languages and writing modes, enhancing the overall user experience.