Sure! Let's break down the CSS Box Model properties you mentioned—padding, margin, border, border-radius, and box-shadow—with detailed syntax and examples.
The Box Model consists of four main parts:
- Content – The actual text or elements inside the box.
- Padding – Space between the content and the border.
- Border – A line surrounding the padding and content.
- Margin – Space outside the border, separating the element from others.
Here's a visual representation of the box model:
- Padding adds space inside the element, between the content and the border.
- It can be set individually for top, right, bottom, and left or shorthand for all sides.
padding: 20px; /* Same padding on all sides */
padding: 10px 20px; /* 10px top & bottom, 20px left & right */
padding: 10px 15px 20px 25px; /* Top, Right, Bottom, Left */- Margin creates space outside the element, pushing it away from other elements.
- Works the same way as padding with shorthand and individual values.
margin: 20px; /* Same margin on all sides */
margin: 10px 20px; /* 10px top & bottom, 20px left & right */
margin: 10px 15px 20px 25px; /* Top, Right, Bottom, Left */- A border surrounds the element and has three properties:
- Width (thickness in px, em, rem, etc.)
- Style (solid, dashed, dotted, etc.)
- Color (any color value)
border: 2px solid black; /* 2px thick, solid black border */
border: 3px dashed red; /* 3px thick, dashed red border */
border: 4px dotted blue; /* 4px thick, dotted blue border */
border: 5px double green; /* 5px thick, double-lined green border */- You can also define borders separately:
border-top: 3px solid black;
border-right: 2px dashed red;
border-bottom: 4px dotted blue;
border-left: 5px double green;- The border-radius property makes corners rounded.
- It can be applied uniformly or to specific corners.
border-radius: 10px; /* All corners rounded by 10px */
border-radius: 10px 20px; /* Top-left & bottom-right: 10px, Top-right & bottom-left: 20px */
border-radius: 10px 15px 20px 25px; /* TL, TR, BR, BL */
border-radius: 50%; /* Makes a perfect circle if width and height are equal */- The box-shadow property adds a shadow around the element.
- It has the following values:
- Horizontal offset (move shadow left/right)
- Vertical offset (move shadow up/down)
- Blur radius (higher value = softer edges)
- Spread radius (increases shadow size)
- Color (shadow color)
box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.3);
/* 5px right, 5px down, 10px blur, semi-transparent black */
box-shadow: -10px 10px 15px rgba(255, 0, 0, 0.5);
/* 10px left, 10px down, 15px blur, semi-transparent red */
box-shadow: 0px 0px 20px 5px blue;
/* Glowing blue effect */.card {
width: 300px;
height: 200px;
padding: 20px;
margin: 30px auto;
border: 3px solid black;
border-radius: 15px;
box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.3);
background-color: white;
}- The card has padding inside to separate content from the border.
- Margin centers it with spacing.
- A 3px solid black border surrounds it.
- 15px border-radius makes the corners rounded.
- Box-shadow adds depth for a modern look.
- Padding = Space inside the element.
- Margin = Space outside the element.
- Border = The outline of the element.
- Border-radius = Makes corners rounded.
- Box-shadow = Adds a shadow effect for depth.
