CSS Borders, Margins, and Padding
Hey friends 👋 welcome back. In the last tutorial, we explored colors and text styling. Today, we’re going to talk about something very important in web design — borders, margins, and padding. These three things control the spacing and structure of your web pages.
Think of them like this:
-
Content → the actual text or image.
-
Padding → the space between the content and the border.
-
Border → the line that surrounds the element.
-
Margin → the space outside the border, separating the element from others.
This is often called the box model in CSS.
Borders
Borders are lines that surround an element. You can control their width, style, and color.
Example:
Border styles include: solid, dashed, dotted, double, groove, ridge, inset, and outset.
Padding
Padding is the space inside the border — it pushes the content away from the edges.
Example:
You can set padding for each side:
Or shorthand:
Margins
Margins create space outside the border, pushing elements away from each other.
Example:
Just like padding, you can set margins for each side:
Or shorthand:
The Box Model
Borders, padding, and margins together create the box model, which controls how every element is spaced and sized on a page. Understanding this is one of the most important steps to becoming good at CSS.
Try It Yourself
Create a new file called boxmodel.html. Inside it, write a heading and a paragraph. Add a border to both, give them some padding, and use margins to separate them from each other. Experiment with different numbers like 10px, 20px, or 50px and see how it changes the layout.
Great job 🎉 You just learned about borders, margins, and padding in CSS. In the next tutorial, we’ll move on to CSS Backgrounds — where we’ll learn how to use background colors, gradients, and even images to make our websites stand out. 🚀
0 Comments