CSS
Properties
box-sizing
box-sizing defines hwo the width and height
of an element are calculated
There are two main values for box-sizing:
border-box and content-box.
box-sizing: content-box;(default):- The width and height properties include only the content. Padding, border and margin are not included in the width and height.
Example:
.content-box {
width: 200px;
padding: 20px;
border: 10px solid black;
box-sizing: content-box;
}In this example, the total width of the element will be 200px (content) + 40px (padding) + 20px (border) = 260px.
box-sizing: border-box;- The width snf height properties include the content, padding and border, but not the margin.
- This makes it easier to manage the size of elements, as the total width and height are consistent.
With the above CSS example, the total width of the element will be exactly 200px, as the padding and border are included within the specified width;
Example:
In this example, the .content-box element
will have a total width grater than 200px due to the padding
and border, while the .border-box element will
have a total width of exactly 200px, including the padding
and border.
What are some common use cases for using
box-sizing: border-box; in web design?
Using box-sizing: border-box; in web design
us quite common due to its practical benefits in managing
element dimensions. Here are some common use cases:
- Consistent Layouts:
- When you set
box-sizing: border-box;, the padding and border are included in the element's total width and height. THis makes it easier to create consistent layouts, as the total size of elements remains predictable.
- When you set
- Responsive Design:
- In responsive design, managing the dimensions of
elements across different screen sizes is crucial.
border-boxhelps ensure that the elements resize as expected without unexpected overflow or layout shifts dues to padding and borders.
- In responsive design, managing the dimensions of
elements across different screen sizes is crucial.
- Grid Systems:
- When building grid systems, using
border-boxensures that columns and rows align correctly without needing to account for padding and borders separately. This simplifies the math involved in creating fluid grids.
- When building grid systems, using
- Form Elements:
- Form elements like input fields, buttons, and text areas
often need precise sizing to fit within their containers.
Using
border-boxensures that these elements maintain their intended size, regardless of padding and border.
- Form elements like input fields, buttons, and text areas
often need precise sizing to fit within their containers.
Using
- Global Reset:
- Many developers use a global CSS reset or normalize
stylesheet to set
box-sizing: border-box;for all elements. This creates a consistent box model across the entire application, reducing the likelihood of layout issues.
- Many developers use a global CSS reset or normalize
stylesheet to set
Example of a global reset using
box-sizing: border-box;:
/* Apply border-box to all elements */
*,
*::before,
*::after {
box-sizing: border-box;
}Using CSS custom properties (variables)
https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
the :root pseudo-class, can be referenced globally:
:root {
--main-bg-color: brown;
}
Referencing custom properties with var()
details {
background-color: var(--main-bg-color);
}
apply rule only when inside an element
CSS descendant selector (a space) between the parent element and the descendant element
article p {
/* CSS rules */
}