CSS Box Model

DevJunctionPoint
0

 



 In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of margins, borders, padding, and the actual content. The image below illustrates the box model

Explanation of the different parts:
  • Content - The content of the box, where text and images appear
  • Padding - Clears an area around the content. The padding is transparent
  • Border - A border that goes around the padding and content
  • Margin - Clears an area outside the border. The margin is transparent
index.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Box Model</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>Box Model</h1>
    <p>The CSS box model is essentially a box that wraps around every HTML element. It consists of: borders, padding,
        margins, and the actual content</p>
    <div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, ducimus? Lorem ipsum, dolor sit amet
        consectetur adipisicing elit. Magnam quae distinctio sequi aliquam, quis reiciendis dignissimos odio nostrum
        enim cupiditate rem repellat, quibusdam perferendis commodi consectetur eveniet, nemo sunt velit.</div>
</body>

</html>

style.css
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

div {
    background-color: brown;
    width: 300px;
    border: 15px solid yellow;
    padding: 50px;
    margin: 30px;
}

Output:



Width and Height of an Element

  • In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
  • Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add padding, borders, and margins

Here is the calculation:
300px (width)
+ 100px (left + right padding)
+ 30px (left + right border)
+ 60px (left + right margin)
= 490px

  • The total width of an element should be calculated like this:
  • Total element width = width + left padding + right padding + left border + right border + left margin + right margin
  • The total height of an element should be calculated like this:
  • Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin





Tags

Post a Comment

0Comments

Do leave your comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!
To Top