CSS - margin-block Property



The logical block start and end margins for an element are specified by the margin-block CSS shorthand property.

  • The writing mode, directionality, and text orientation of the element determine how these logical margins are converted into physical margins.

  • Depending on the values provided for writing-mode, direction, and text-orientation, this property refers to either the margin-top and margin-bottom or margin-right and margin-left properties.

  • The margin-block property allows for one or two values. With one value, it applies the same margin to both start and end. When two values are specified, the first one applies to the start, and the second one applies to the end.

Constituent Properties

This property is a shorthand for the following CSS properties:

Possible Values

The following list covers all the possible values of margin-block property.

  • <length> - The fixed value of the margin's size.

  • <percentage> - The percentage of the margin measured in relation to the contained block's inline size, or the writing-mode-defined width in a horizontal language.

  • auto - The browser chooses an appropriate margin to apply. This value, for instance, may occasionally be utilized to center an element.

Applies to

All elements, except elements with table display types other than table-caption, table and inline-table. It also applies to ::first-letter.

Syntax

margin-block = <'margin-top'>{1,2}  

CSS margin-block - Length value

The following example demonstrates the usage of margin-block with length value.

<html>
<head>
<style>
   body {
      background-color: #dfe4ed;
   }
   #customDIV {
      height: 400px;
      background-color: #edecb7;
      padding: 20px;
   }
   .marginDemo {
      width: 250px;
      background-color: #65f252;
      border: 2px dashed #4682B4;
      margin-block: 25px;
   }
   .marginBox {
      width: 250px;
      background-color: #f5d902;
      text-align: center;
      padding: 10px;
   }
</style>
</head>
<body>
<h1>Explore the margin-block property</h1>
<div id="customDIV">
   <p>Yellow div elements emphasize margins around the green box.</p>
   <div class="marginDemo">box</div>
      <div class="marginBox">
         Example of margin-block property.
      </div>
   <div class="marginDemo">box</div>
</div>
</body>
</html>

CSS margin-block - Percentage Value

The following example demonstrates the usage of margin-block with percentage value.

<html>
<head>
<style>
   body {
      background-color: #F1ECEC;
   }
   #customDIV {
      height: 400px;
      background-color: #FFF7B0;
      padding: 20px;
   }
   .marginDemo {
      width: 250px;
      background-color: #8B78B6;
      border: 2px dashed #4682B4;
      margin-block: 4% 6%; 
   }
   .marginBox {
      width: 250px;
      background-color: #F3994D;
      text-align: center;
      padding: 10px;
   }
</style>
</head>
<body>
<h1>Explore the margin-block property</h1>
<div id="customDIV">
   <p>Orange div elements emphasize margins around the purple box.</p>
      <div class="marginBox">box</div>
      <div class="marginDemo">
         Example of margin-block property.
      </div>
   <div class="marginBox">box</div>
</div>
</body>
</html>

CSS margin-block - Using auto value

The following example demonstrates the usage of margin-block with value auto.

<html>
<head>
<style>
   body {
      background-color: #F2EFEF;
   }
   #customDIV {
      height: 400px;
      background-color: #FFEEA8;
      padding: 20px;
   }
   .marginDemo {
      width: 250px;
      background-color: #6495ED;
      border: 4px dashed #4682B4;
      margin-block: 2rem auto;
   }
   .marginBox {
      width: 250px;
      background-color: #FFA07A;
      text-align: center;
      padding: 10px;
   }
</style>
</head>
<body>
<h1>Explore the margin-block property</h1>
<div id="customDIV">
<p>Orange div elements emphasize margins around the blue box.</p>
   <div class="marginBox">box</div>
      <div class="marginDemo">
         Example of margin-block property.
      </div>
   <div class="marginBox">box</div>
</div>
</body>
</html>

CSS margin-block - Using writing-mode

The following example demonstrates the usage of margin-block with length value and writing-mode: vertical-rl.

<html>
<head>
<style>
   body {
      background-color: #E3EAE9;
   }
   #customDIV {
      height: 500px;
      background-color: #FFEACB;
      padding: 20px;
   }
   .marginDemo {
      width: 50px;
      background-color: #87CEEB;
      border: 4px dashed #2E517F;
      margin-block: 20px 20px;
      writing-mode: vertical-rl;
   }
   .marginBox {
      width: 150px;
      background-color: #FF9A8B;
      text-align: center;
      padding: 10px;
   }
</style>
</head>
<body>
<h1>Explore the margin-block property</h1>
<div id="customDIV">
   <p>Orange div elements emphasize margins around the blue box.</p>
   <div class="marginBox">box</div>
      <div class="marginDemo">
         Example of margin-block property
      </div>
   <div class="marginBox">box</div>
</div>
</body>
</html>
Advertisements