So far, every element has taken its turn from top to bottom. Now let's put things side by side, pin them to corners and stack them on top of each other.
flag
What you'll learn
How display decides if an element sits on its own line, inside a line, or is hidden.
How to move elements and pin them to a spot with position, and which one is in front with z-index.
What to do with content that does not fit, and how to wrap text around a picture.
waving_handLet's move things around
The browser places elements one after another, from top to bottom, in the same order as the HTML. This is called the normal flow.
The properties in this lesson change that: they put elements in a row, move them, pin them to a spot, or put one on top of another.
push_pin
Think of a school bulletin board. Most notes are pinned in order, one after another (static).
Someone bumps one note a little, and its empty spot is still there (relative). An urgent notice is pinned to an exact corner of the board (absolute).
When two notes overlap, the one pinned last is on top, unless you choose otherwise (z-index).
Club meeting on Fridayposition: static;
Lost: blue umbrellaposition: relative; top: 18px; left: 12px;
Lost: blue umbrellaposition: relative; top: 18px; left: 12px;
URGENT: No classes Mondayposition: absolute; top: 0; right: 0;
Science fair sign-upz-index: 1;
Art contest winnersz-index: 2;
Now let's see the CSS ↓
Club meeting on Fridayposition: static;
Lost: blue umbrellaposition: relative; top: 18px; left: 12px;
Lost: blue umbrellaposition: relative; top: 18px; left: 12px;
URGENT: No classes Mondayposition: absolute; top: 0; right: 0;
Science fair sign-upz-index: 1;
Art contest winnersz-index: 2;
The dashed outline shows the spot the "Lost" note came from. With relative, that space stays empty. With absolute, the "Urgent" note leaves no empty space behind.
searchA closer look
display: block, inline, inline-block and none
Every element has a display value. Block elements, like <div>, <p>, <h1> and <li>, start on a new line and take the full width.
Inline elements, like <a> and <span>, sit inside a line of text, and they skip width and height.
inline-block mixes the two: the element sits in a row, but width, height and padding all work. none hides the element completely.
CSS Lessons 7 and 9 add two more values, flex and grid.
position and its four helpers
position decides how an element is placed. static is the default: the element stays in the normal flow.
The other values use four helpers, top, right, bottom and left, to say where it goes:
relative moves the element from its usual spot. The space it came from stays empty.
absolute takes the element out of the flow and places it inside the nearest parent that has a position, usually one with position: relative.
fixed places the element on the screen itself, so it stays put while you scroll, like a chat button.
z-index: who is in front
When positioned elements overlap, the one that comes later in the HTML is drawn on top. z-index changes that: a higher number sits in front of a lower one.
It works on elements that have a position other than static.
overflow: content that doesn't fit
When a box has a set height and its content needs more room, overflow decides what happens: visible lets it spill out (the default), hidden cuts it off, and auto adds a scroll bar only when needed.
float: wrapping text around a picture
float: left; pushes an element to the left side of its box, and the text that comes after it wraps around its right side, like a photo in a newspaper story.
Floats were once used to build whole page layouts. Today, flexbox and grid (CSS Lessons 7 to 10) do that job, and float is mostly used for pictures in text.
constructionLet's build it
Let's arrange a Front Page Bytes page with a menu and a story. The starting CSS uses only what you learned in CSS Lessons 1 to 4. Each code box opens on the CSS tab with the new lines highlighted. Click HTML to see the page being styled.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <ul class="menu"> <li>News</li> <li>Games</li> <li>Deals</li> </ul> <div class="story"> <span class="badge">NEW</span> <img class="photo" src="/storage/assets/WebcoreIcon.png" alt="Front Page Bytes logo"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999. Many shops expect long lines, so some will hand out numbers at the door.</p> </div> </body></html>
What you see
description(no title)
List items are block elements, so each one starts on a new line. display: inline-block; lets them sit side by side, while their padding still works.
Scroll the HTML tab to see the list.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <ul class="menu"> <li>News</li> <li>Games</li> <li>Deals</li> </ul> <div class="story"> <span class="badge">NEW</span> <img class="photo" src="/storage/assets/WebcoreIcon.png" alt="Front Page Bytes logo"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999. Many shops expect long lines, so some will hand out numbers at the door.</p> </div> </body></html>
What you see
description(no title)
float: left; moves the photo to the left side of the story, and the headline and text wrap around it. margin-right keeps the text from touching the photo.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <ul class="menu"> <li>News</li> <li>Games</li> <li>Deals</li> </ul> <div class="story"> <span class="badge">NEW</span> <img class="photo" src="/storage/assets/WebcoreIcon.png" alt="Front Page Bytes logo"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999. Many shops expect long lines, so some will hand out numbers at the door.</p> </div> </body></html>
What you see
description(no title)
position: absolute; lifts the "NEW" badge out of the flow and places it 8 pixels from the top and 8 pixels from the right.
Measured from where? From the nearest parent with a position. That's why .story gets position: relative;: it becomes the frame the badge is pinned to.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <ul class="menu"> <li>News</li> <li>Games</li> <li>Deals</li> </ul> <div class="story"> <span class="badge">NEW</span> <img class="photo" src="/storage/assets/WebcoreIcon.png" alt="Front Page Bytes logo"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999. Many shops expect long lines, so some will hand out numbers at the door.</p> </div> </body></html>
What you see
description(no title)
max-height: 130px; limits how tall the story can get, and overflow: auto; adds a scroll bar for the rest. Scroll inside the story in the mini browser to read it all.
sticky_note_2Quick notes
displayHow an element sits
.menu li { display: inline-block; }
How to use
blockStarts on a new line and takes the full width. Like <div>, <p> and <h1>.
inlineSits inside a line of text. Width and height are skipped. Like <a> and <span>.
inline-blockSits in a line, but width, height and padding all work.
noneHides the element. It takes up no space at all.
Examples
A menu with its items in one row.
.menu li { display: inline-block;}
A link that fills its whole line, so it is easy to tap.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <ul class="menu"> <li>News</li> <li>Games</li> <li>Deals</li> </ul> <div class="story"> <span class="badge">NEW</span> <img class="photo" src="/storage/assets/WebcoreIcon.png" alt="Front Page Bytes logo"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999. Many shops expect long lines, so some will hand out numbers at the door.</p> </div> </body></html>
What you see
description(no title)
.story has no position, so the badge looks further up for a positioned parent and finds none. It ends up pinned to the corner of the whole page, far from its story.
tips_and_updatesTip: Add position: relative; to the box the badge belongs in, here .story.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <ul class="menu"> <li>News</li> <li>Games</li> <li>Deals</li> </ul> <div class="story"> <span class="badge">NEW</span> <img class="photo" src="/storage/assets/WebcoreIcon.png" alt="Front Page Bytes logo"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999. Many shops expect long lines, so some will hand out numbers at the door.</p> </div> </body></html>
What you see
description(no title)
The plan was to bring the photo in front of the badge. But the photo has no position, so z-index: 5; does nothing, and the badge still covers its corner.
tips_and_updatesTip: Give the photo a position too: position: relative; z-index: 5;
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <ul class="menu"> <li>News</li> <li>Games</li> <li>Deals</li> </ul> <div class="story"> <span class="badge">NEW</span> <img class="photo" src="/storage/assets/WebcoreIcon.png" alt="Front Page Bytes logo"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999. Many shops expect long lines, so some will hand out numbers at the door.</p> </div> </body></html>
What you see
description(no title)
The story is only 90 pixels tall, and overflow: hidden; cuts off everything below that. Most of the story disappears, with no way to read it.
tips_and_updatesTip: Use overflow: auto; so readers can scroll, or leave the height out.
quizTest your knowledge
Edit the CSS on the left. Open the HTML tab to see the class names you can use. The mini browser updates as you type, and each goal gets a check mark when it's done.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Put the .menu li items in a row with display: inline-block.not done yet
Float the .photo to the left.not done yet
Pin the .badge with position: absolute.not done yet
display decides how an element sits: block (own line), inline (inside a line), inline-block (in a row, with sizes) or none (hidden).
position with top, right, bottom and left moves or pins an element. An absolute element is placed inside the nearest positioned parent, and z-index decides which one is in front.
overflow handles content that doesn't fit, and float lets text wrap around a picture.
menu_bookReferences
Lesson content prepared by the WebCore F.E. team for beginner learners. The following references were used in preparing this lesson.
W3C. CSS Display Module Level 3 (official specification).
Open reference