CSS Properties
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.
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
Value | Description |
---|---|
length | Specifies the margin using units like px, em, rem, etc. |
percentage | Specifies the margin as a percentage of the containing block's width. |
percentage | The browser calculates the margin. |
percentage | Inherits the margin value from the parent element. |
initial | Sets the property to its default value. |
unset | Resets 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.
<!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:
Author
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
If you have any doubts regarding this article (CSS margin-block Property), please comment here. I will help you immediately.