Basic Layout
CSS is not only responsible for how elements look, it's also responsible for how elements are sized and positioned too!
This can be one of the most complicated and challenging parts of using CSS. Because of this, this tutorial will only cover the basics.
Display Modes
Each element is either inline-level or block-level
- Inline-level elements appear in-line with text, not disrupting the flow of the page
- Block-level elements do not appear inline, instead breaking the flow of text
By default, each element is either a block-level (like <p>
elements) or inline-level element (like <span>
elements). However you can also change the display mode of elements using the display
CSS rule!
display: block;
display: inline;
The display
rule can also be used to prevent elements from being displayed using the none
value!
display: none;
If you want to prevent an element from being displayed without hiding its children, the contents
value can be used!
display: contents;
Sizing
The Box Model
You can imagine each element as a box. The size of this box is determined by several things:
- Content: The size of the contents inside the box
- Padding: The distance between the contents of the box and the border of the box
- Border: The thickness of the border
- Margin: The distance between this box and other boxes
These parameters are used to determine the size of elements, along with how they're spaced.
Absolute Length Units
When setting a length value, such as the amount of padding or the thickness of a border, you must specify a unit of length.
suffix | unit | length | example |
---|---|---|---|
px | pixels | 1px | 60px |
cm | centimeters | 37.8px | 5cm |
in | inches | 96px | 1.5in |
pt | points | 1/72in | 8pt |
Note
While CSS allows you to use physical units like inches and centimeters, generally they shouldn't be used.
It is typically better to use px
, or some of the relative units below
Relative Length Units
Some length units change depending on which element uses them!
Percentage
The percentage %
unit represents a percentage of the parent element's width or height!
em Unit
The em
unit is relative to the parent element's font size.
It is multiplied by the parent element's font size to calculate the new size
- 1em = parent size/default size
- 2em = twice the size of the parent
- 0.5em = half the size of the parent
- etc
The rem
unit is relative to the root element's font size.
Width and Height
To set the width and height of the content box, the width
and height
rules can be used.
width: <length>;
height: <length>;
Note
These rules set the size of the content box. This means that the element can exceed the set size if it has padding or a border!
If the padding and border should be included, use the box-sizing
rule
box-sizing: content-box; /* default value */
/* to */
box-sizing: border-box;
Warning
These two rules cannot be used on inline elements!
If you need to set the width or height of an inline element, use the display: block
or display: inline-block
rules!
display: inline-block;
width: 20%;
height: 20%;
Margin and Padding
To set the margin or padding of an element, the margin
and padding
rules can be used
margin: <length>;
padding: <length>;
To set the vertical and horizontal margin or padding separately, two values can be specified.
margin: <vertical> <horizontal>;
padding: <vertical> <horizontal>;
/* for example */
margin: 20px 40px; /* top and bottom: 20px, left and right: 40px */
You can also set the padding and margin for each side individually
margin: <top> <right> <bottom> <left>;
padding: <top> <right> <bottom> <left>;
/* or you can set them using individual css rules */
margin-left: <length>;
margin-right: <length>;
margin-top: <length>;
margin-bottom: <length>;
/* ... etc */
Positioning
Positioning elements in CSS can be quite complex. Each method (aside from float) could be an entire page. Because of this, only float is included in this tutorial.
Float
One of the most basic positioning rules you can use is float
.
Float allows you to position an element inside its parent while also allowing inline text to flow around it!
float: left;
float: right;
float: none;
Without Float:
Using Float: