DEV Community

Cover image for #css #webdev #beginners #codenewbie
Ali Hamza
Ali Hamza

Posted on

#css #webdev #beginners #codenewbie

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:

  1. Content: The actual text, image, or video inside the element.
  2. Padding: The transparent space inside the element, between the content and its border. (Great for making buttons look spacious!).
  3. Border: The line wrapped around the padding and content (I experimented with border-radius to make smooth, rounded corners).
  4. 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;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)