Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS margin-block Property

Posted in CSS Tutorial
Updated on Jul 29, 2024
By Mari Selvan
👁️ 9 - Views
⏳ 4 mins
💬 1 Comment
CSS margin-block Property

Photo Credit to CodeToFun

🙋 Introduction

The margin-block property in CSS is a shorthand property that allows you to set the margin on the block start and block end sides of an element. In a block-level context, the block start refers to the top, and the block end refers to the bottom. However, in a writing mode where text flows vertically, these terms adapt accordingly.

This property provides a convenient way to control vertical margins in layouts, respecting the text direction and writing mode of the document.

💡 Syntax

The syntax for the margin-block property allows you to specify values for both the block start and block end margins.

Syntax
Copied
Copy To Clipboard
element {
  margin-block: <margin-start> <margin-end>;
}
  • <margin-start>: The margin for the block start side (top in horizontal writing modes).
  • <margin-end>: The margin for the block end side (bottom in horizontal writing modes).

If only one value is provided, it applies to both the block start and block end sides.

🎛️ Default Value

The default value for the margin-block property is 0, meaning there is no margin applied unless specified otherwise.

🏠 Property Values

ValueDescription
lengthSpecifies the margin using units like px, em, rem, etc.
percentageSpecifies the margin as a percentage of the containing block's width.
percentageThe browser calculates the margin.
percentageInherits the margin value from the parent element.
initialSets the property to its default value.
unsetResets the property to either inherit or initial, depending on the context.

📄 Example

In this example, we'll set a margin of 20px at the top and 10px at the bottom 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-block Example</title>
  <style>
    p {
      margin-block: 20px 10px;
    }
  </style>
</head>
<body>
  <h1>Using margin-block Property</h1>
  <p>This paragraph has a margin of 20px at the top and 10px at the bottom.</p>
</body>
</html>

🖥️ Browser Compatibility

The margin-block property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. As always, it is advisable to test your website across different browsers to ensure consistent behavior.

🎉 Conclusion

The margin-block property is a useful shorthand for setting vertical margins on block-level elements, especially in documents with diverse writing modes and directions.

It simplifies layout management by providing a unified way to handle block margins, adapting to the document's writing mode. By using this property, you can create more adaptable and responsive layouts.

👨‍💻 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