Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Topics

CSS margin-inline Property

Posted in CSS Tutorial
Updated on Oct 03, 2024
By Mari Selvan
👁️ 25 - Views
⏳ 4 mins
💬 1 Comment
CSS margin-inline Property

Photo Credit to CodeToFun

🙋 Introduction

The margin-inline property in CSS is a shorthand property that sets the margin on the inline-start and inline-end sides of an element.

Unlike the margin-left and margin-right properties, which set margins relative to the physical left and right, margin-inline adjusts margins based on the logical order of content. This makes it especially useful in multilingual and internationalized web design, where text direction can vary between left-to-right (LTR) and right-to-left (RTL) languages.

💡 Syntax

The syntax for the margin-inline property can be written in several ways, depending on the number of values specified:

Syntax
Copied
Copy To Clipboard
element {
  margin-inline: start end;
}
  • If one value is provided, it applies to both margin-inline-start and margin-inline-end.
  • If two values are provided, the first value applies to margin-inline-start and the second to margin-inline-end.

🎛️ Default Value

The default value of margin-inline is 0, meaning no margin is applied to the inline-start and inline-end sides by default.

🏠 Property Values

ValueDescription
lengthSpecifies the margin in units such as pixels (px), ems (em), etc. Example: 10px.
percentageSpecifies the margin as a percentage of the containing element's width. Example: 5%.
autoThe browser calculates a suitable margin.
initialSets the property to its default value.
inheritInherits the margin value from the parent element.

📄 Example

In this example, we'll apply different margins to the inline-start and inline-end sides of a paragraph element.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS margin-inline Example</title>
  <style>
    p {
      margin-inline: 20px 40px;
      border: 1px solid #ccc;
      padding: 10px;
    }
  </style>
</head>
<body>
  <h1>Paragraph with Custom Inline Margins</h1>
  <p>
    This paragraph has a margin of 20px on the inline-start side and 40px on the inline-end side. 
    In an LTR layout, the margins are applied to the left and right, respectively.
  </p>
</body>
</html>

🖥️ Browser Compatibility

The margin-inline property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. It is always a good practice to test your website across different browsers to ensure compatibility.

🎉 Conclusion

The margin-inline property is a versatile tool for managing margins in a way that respects the text direction of different languages.

By using this property, web developers can create layouts that adapt gracefully to both LTR and RTL content. Experiment with different values and see how margin-inline can enhance your web design projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy