CSS Box Model

 



 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





Related Posts:

  • CSS-simplified-series.html INTRODUCTIONIntroduction to CSS Ways to Add CSSCSS SelectorsCSS CommentsCSS PropertiesCSS ColorsCSS BackgroundsCSS BorderCSS MarginCSS Padd… Read More
  • CSS BordersBorder property helps in creating borders around the textCSS border properties allow you to specify the style, width, and color of an element's border… Read More
  • Introduction to CSSWhat is CSS?CSS is a language that we use to style an HTML document.It describes how elements will be displayed.CSS stands for Cascading Style Sheets.… Read More
  • CSS BackgroundsCSS BackgroundsCSS background properties are used to add background effects for elementsWe will learn below following CSS background properties:backgr… Read More
  • CSS ColorCSS ColorsColor property is used to set the color of the text.Syntax: {color: value;}Now there are many ways of giving this value to the colour. Some … Read More

0 Comments:

Post a Comment

Do leave your comments