Mastering Inset for Creative CSS Layouts
What is "inset"
"Inset" is a keyword used in CSS properties like padding
, margin
, border-width
, and box-shadow
to specify that the following values should create an inset effect.
How it works
Normally, padding
, margin
, and border-width
values increase the size of the element outwards from its content box. With "inset", these values instead create margins, padding, or borders that intrude inwards from the element's border box.
For example:
.inset-box {
padding: inset 10px; /* Creates 10px padding that goes inside the border */
border: inset 5px solid red; /* Creates a 5px red border that goes inside the content */
}
Box-shadow with inset
The box-shadow
property can also use "inset" to create an inner shadow effect. Here, "inset" is placed before the shadow values:
.inset-shadow {
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5); /* Creates a dark inner shadow */
}
- MDN web docs on box-shadow
[MDN box shadow ON Mozilla developer.mozilla.org] - MDN web docs on border
[MDN border ON Mozilla developer.mozilla.org] - MDN web docs on margin
[MDN margin ON Mozilla developer.mozilla.org] - MDN web docs on padding
[MDN padding ON Mozilla developer.mozilla.org]
Inset padding
.inset-box {
width: 200px;
height: 100px;
background-color: lightblue;
padding: inset 20px 30px 10px 5px; /* Creates different inset padding on all sides */
border: 1px solid black;
}
This code creates a box with a light blue background. It applies different inset padding values for each side: 20px on top, 30px on right and left, and 10px on the bottom. The black border helps visualize the inset effect.
Inset border
.inset-border {
width: 150px;
height: 75px;
border: inset 10px solid green;
}
This code creates a box with a green inset border of 10px width. The green border goes inwards from the element's border box.
Inset shadow
.inset-shadow {
width: 100px;
height: 100px;
background-color: lightgray;
box-shadow: inset 5px 5px 10px rgba(0, 0, 0, 0.3); /* Creates a dark inner shadow */
}
This code creates a light gray box with an inner shadow effect. The inset
keyword is used before the shadow values to create a dark shadow that goes inwards from the box's edges.
Combined inset properties
.special-box {
width: 250px;
height: 150px;
background-color: lightcoral;
padding: inset 15px; /* Uniform inset padding on all sides */
border: inset 3px solid orange;
box-shadow: inset -5px -5px 5px rgba(255, 165, 0, 0.5); /* Inner glowing shadow */
}
This code creates a box with various inset properties combined. It has uniform inset padding, an orange inset border, and a glowing inner shadow effect achieved with a positive rgba
value for the shadow.
Negative values for margin, padding, and border-width
While not exactly the same as "inset", you can use negative values for margin
, padding
, and border-width
properties to create an illusion of inward positioning. However, this can be less intuitive and might cause layout issues in some cases.
Example
.not-inset {
padding: -10px; /* Creates outward padding that overlaps the border visually */
border: 1px solid black;
}
Pseudo-elements with positioning
For more complex inner effects, you can use pseudo-elements like ::before
or ::after
positioned absolutely within the element. This allows for more granular control over the inner element's placement and styling.
Example (Inner shadow)
.shadow-box {
position: relative; /* Needed for absolute positioning of pseudo-element */
width: 100px;
height: 100px;
}
.shadow-box::before {
content: "";
position: absolute;
top: 5px;
right: 5px;
bottom: 5px;
left: 5px;
background-color: rgba(0, 0, 0, 0.3); /* Creates a dark inner shadow */
}
Choosing the right approach
- Remember, "inset" is generally more performant and easier to maintain compared to workarounds.
- For more complex effects like inner shadows or specific positioning, pseudo-elements offer greater control.
- If you need a simple inward effect like padding or a basic border, negative values might suffice.
- Maintainability: Consider the code complexity and maintainability of different solutions.
- Browser support: Ensure your chosen approach is supported by the target browsers.