6/25/21

CSS Tutorials : CSS Margin Property

CSS Margin

 CSS Margin property is used to give space around Html elements. You can give some space to your Html elements using this CSS property. It is completely transparent you can’t see any background color when you give this property to elements.

You can also provide margins of Html elements independently such as left, right, bottom and top.   

You can provide the margin of an Html element by using values like - auto, %, length, etc. we can talk about it after some time with a full example. 

The Margin CSS Property basically have 5 property these are: - 

  1. Margin: - This CSS property will apply when the user wants to give margin value in one declaration
  2. Margin-left: - This CSS property will only apply on the left side of the Html element.
  3. Margin-top: - This CSS property is used to set the margin on the top side of the Html element.
  4. Margin-bottom: - This CSS property is used to set a margin on the bottom side of the Html element.
  5. Margin-right: - This CSS property will only apply on the right side of the Html element.

Now, the margin CSS Property also has a different value these values are: -

  1. Auto: - This margin value is set by the browser automatically
  2. Length: - This margin value is set by the user in px, cm, pt, etc
  3. %: - This margin value is set into %( percentages ) Like :- 25%, 30% etc
  4. Inherit: - This margin value is used to inherit margin from its parent element.

Let’s see through An amazing example of this topic.


<html>

<head>
    <title> CSS Margin Property </title>
    <style>
        .container{
            displayflex;
        }
       .box
        width216px;
        height209px;
        margin30px 30px 30px 30px;
       }
       #box1 {
            border3px solid red;
            background-colorrgb(204157157);
       }
       #box2{
            border3px solid green;
            background-colorrgb(157204182);
       }
       #box3{
            border3px solid blue;
            background-colorrgb(219230121);
       }
       #box4{
            border3px solid purple;
            background-colorrgb(163158233);
       }
    </style>
</head>

<body>
  <div class = "container">
      <p class=  "box" id="box1"> Box 1 </p>
      <p class = "box" id="box2"> Box 2 </p>
      <p class = "box" id="box3"> Box 3 </p>
      <p class = "box" id="box4"> Box 4 </p>
  </div>
</body>

</html>

Output :-


In the above example, we have seen the margin CSS property. We provide margin property to all the boxes in a single declaration. Now we are going to give different CSS property to all boxes.

But before the example let see an example in which we didn't give any margin in the Html document. Look at the image below in this image you will see that all the boxes are combined together. There is no space between them. 


Now, look at the example below. In this example, we applied different margin properties to the different boxes. 

Example: - 
<html>
<head>
    <title> CSS Margin Property </title>
    <style>
        *{
            margin0%;
            background-colorrgb(209190190);
        }
        .container{
            displayflex;
        }
       .box
        width216px;
        height209px;
        margin-left30px;
       }
       #box1 {
            border3px solid red;
            background-colorrgb(204157157);
       }
       #box2{
            border3px solid green;
            background-colorrgb(157204182);
            margin-right50px;
       }
       #box3{
            border3px solid blue;
            background-colorrgb(219230121);
            margin-top100px;
       }
       #box4{
            border3px solid purple;
            background-colorrgb(163158233);
       }
    </style>
</head>

<body>
  <div class = "container">
      <p class=  "box" id="box1"> Box 1 </p>
      <p class = "box" id="box2"> Box 2 </p>
      <p class = "box" id="box3"> Box 3 </p>
      <p class = "box" id="box4"> Box 4 </p>
  </div>
</body>
</html>

Output: -


People Also Read: - CSS Tutorial : CSS Border Radius Property

People Also Read: -  CSS Tutorials :- CSS Background Property

0 comments:

Post a Comment

Like us on facebook

blogger templatesblogger widgets

Follow on Twitter

Linkedin

Categories

Mad About Computer. Powered by Blogger.