- The CSS box model looks at all the HTML elements as boxes
- We can set width and height in CSS as follows
#box {
height: 70px;
width: 70px;
}
-
Note that the total width/height is calculated as follows:
-
Total height = height + top/bottom padding + top/bottom border + top/bottom margin
- We can set margin and padding as follows:
.box{
margin: 3px; /* Sets top, bottom, left & right values*/
padding: 4px; /* Sets top, bottom, left & right values*/
}
.boxMain{
margin: 7px 0px 2px 11px; /*top, right, bottom, left*/
}
.boxLast{
margin: 7px 3px; /*(top & bottom) (left & right)*/
}
We can also set individual margins/padding like this:
- margin-top: 70px
- margin-bottom: 3px
- margin-left: 8px
- margin-right: 9px
Same goes with padding also
- We can set the border as follows
.bx{
border-width: 2px;
border-style: solid;
border-color: red;
}
- Shorthand for above codes,
set border: 2px solid red;
- We can set border-radius to create rounded borders
.div2{
border-radius: 7px;
}
- When two margins from different elements overlap, the equivalent margin is the greater of the two. This is called margin collapse.
- Determines what out of padding and border is included in elements width and height
- Can be content-box or border-box
- Include only content in width/height
.div1{
box-sizing: border-box;
}
- The content width and height includes, content + padding + border
- Create a website layout. Add a header box, one content box, and one footer.
- Add borders and margins to question 1 (website layout)
- Did the margin collapse between the content box and footer?
- Add the box-sizing property to the content box. What changes did you notice?