Hello Dev Community! 👋
It is officially Day 5 of my journey toward the MERN stack! Yesterday, I learned how to add colors and fonts using CSS selectors. Today, I tackled the absolute core of CSS layout design: The Box Model.
Before today, whenever I tried to move an element, it would randomly overlap or break the layout. Today, I finally understood why that happens.
🧠Key Learnings From Day 5
I learned that the browser treats every single HTML element as a rectangular box. This box consists of four distinct layers:
- Content: The actual text, image, or video inside the element.
- Padding: The transparent space inside the element, between the content and its border. (Great for making buttons look spacious!).
-
Border: The line wrapped around the padding and content (I experimented with
border-radiusto make smooth, rounded corners). - Margin: The transparent space outside the element, separating it from other surrounding boxes.
âš¡ The Life-Saving Property: box-sizing: border-box;
One major headache I faced was that adding padding or borders increased the actual width of my elements, ruining my layout math.
Then I learned about a universal reset that senior developers use. Adding this to the top of my CSS file fixed everything instantly:
css
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
Top comments (0)