
CSS Table Properties: Layout, Borders, Captions, and Spacing
12/10/2019
CSS Pseudo-Classes and Pseudo-Elements: A Practical Guide
12/10/2019
12. CSS layout and positioning
Building a web page means deciding where to place images, text, tables, and other elements. The following table summarizes the CSS properties covered in this lesson.
| Property | Description | Values |
|---|---|---|
display | How the box participates in layout | inline | block | inline-block | none |
position | Positioning scheme | static | relative | absolute | fixed | sticky |
top right bottom left | Insets from the top, right, bottom, and left | length | percentage | auto |
float | Float direction | left | right | none |
clear | Clearance from preceding floats | none | left | right | both |
z-index | Stacking order | auto | integer |
box-sizing | Whether dimensions include borders and padding | content-box | border-box |
visibility | Shows or hides the box while preserving its space | visible | hidden |
The inset properties top, bottom, left, and right take effect when the box uses relative, absolute, fixed, or sticky positioning. Their reference depends on the positioning scheme. Values can be lengths such as px, em, and rem, or percentages where appropriate.
12.1. Display
Values covered here: none, inline, block, and inline-block.
- none: The element is hidden and no space is reserved for it.
- inline: The box flows on the same line as the surrounding content. For ordinary non-replaced inline elements, width, height, and vertical margins do not affect layout.
- block: The box starts on a new line in normal block layout. It accepts dimensions and vertical margins.
- inline-block: The box flows inline while accepting dimensions and vertical margins.
Display example
Apply these rules to different elements and compare the results.
.a { display: none; }
.b { display: inline; width: 100px; height: 50px;}
.c { display: block; }
.d { display: inline-block; width: 100px; height: 50px;}
12.2. Position
Values: static, relative, fixed, absolute, and sticky.
- static: The element follows normal flow. The inset properties do not move it.
- relative: The box can move relative to its original position while keeping its original space in the layout.
- fixed: The box is usually positioned relative to the viewport and stays put as you scroll. Certain ancestor properties, such as a transform, can change its containing block.
- absolute: Removes the box from normal flow. Its containing block is usually the nearest positioned ancestor. If none of its ancestors establishes one, the initial containing block is used.
- sticky: The element remains in normal flow and can stick at a specified inset within its scroll context and containing block.
12.2.1. Example: position: static
Apply these rules to different elements. They keep their normal positions because inset offsets have no effect on a static box.
.a { position: static; }
.b { position: static; }
.c { position: static; }
12.2.2. Example: position: relative
Apply the rule to an element. It moves 20px to the right and 10px down while retaining the space it originally occupied.
.box2 { position: relative; left: 20px; top: 10px; }
12.2.3. Example: position: fixed
Scroll the example and watch the fixed box stay in place relative to the viewport.
.box2 { position: fixed; top: 130px; left: 100px; }
12.2.4. Example: position: absolute
The .b box leaves normal flow and takes its position from the .a container. Change .a to position: static to see which containing block the child uses next. The inset values determine its placement.
.a {
position: relative; /*change this value to static to see the difference*/
width: 300px;
height: 300px;
border: 2px dotted purple;
}
.b {
position: absolute; /*The positioned ancestor provides the containing block*/
background-color: #EEEEEE;
top: 60px;
right: 0px;
width: 200px;
height: 80px;
border: 3px solid green;
}
12.2.5. Example: position: sticky
Sticky positioning keeps an element in the page flow until it reaches its specified threshold. For example, a sidebar banner can stay visible once it reaches the top of the scroll area, using CSS alone.
Set an inset such as top to tell the browser where the menu or container should stick. It stops sticking when it reaches the edge of its containing block.
The next example keeps the current letter heading at the top while you scroll through an alphabetical list.
p {
position: sticky;
top: 0px;
background-color: #e1f4fc;
padding: 20px;
font-weight: bold;
}
ul{
margin: 20px 0;
}
li{
padding-left: 20px;
}
12.3. Float
Values: left, right, and none.
A float moves to the specified side of its container and lets nearby inline content wrap around it.
Float example
Apply these rules to several boxes and compare the results.
.a { float: left; }
.b { float: right; }
.c { float: right; }
.d { clear: both; }
12.4. Clear
Values: none, left, right, and both.
The clear property moves a box past preceding floats on the specified side or on both sides. The floated elements stay floated.
Clear example
Float a box to the left, then apply clear to a paragraph to make it start below the float.
p{ clear: both; }
12.5. Z-index
Values: auto or an integer.
Within a stacking context, a higher applicable z-index can place one box above another. The diagram shows how the layers overlap.

Z-index example
Apply these rules to overlapping positioned boxes and compare their stacking order.
.a { z-index: 3;}
.b { z-index: 1;}
.c { z-index: 2;}
12.6. Box sizing
With the default CSS box model, the declared width and height apply to the content. Padding and borders add to those dimensions, so allow room for them when sizing the box.
Values: content-box and border-box.
- content-box: A declared width of 100px gives the content 100px. The padding and borders are added to the visible width.
- border-box: A declared width of 100px includes padding and borders, and the content area shrinks to fit. This makes the outer dimensions easier to predict.
Box-sizing example
Create a full-width container and put three images in a row. Set each width to 33.333% and observe that the three widths add up to approximately 100%.
Add padding to the images. With content-box, the total exceeds the container width. Then use box-sizing: border-box to include the padding in the widths.
box-sizing: border-box;
The unprefixed declaration is sufficient in modern browsers. Older examples may show a prefixed declaration for historical compatibility.
12.7. Visibility
The visibility property controls whether a box is visible while keeping its place in the layout.
Values covered here: visible and hidden.
Visibility example
Compare the examples. display: none removes the box from layout, while visibility: hidden hides it and preserves its space.
.a {display:none;}
.b {visibility: hidden;}