Metadata
- Date :: 19-04-2025
- Tags :: web-dev
Notes
๐ Letโs Learn About CSS Display Property
๐ What is CSS Again?
CSS stands for Cascading Style Sheets โ itโs like dressing up your website to make it look pretty ๐ ๐ฝ๐ป.
So far, we learned how to:
-
Color things ๐จ
-
Move them around ๐บ
-
Put boxes on top of each other ๐ฆ
Now weโre going to learn how to arrange those boxes properly using something called the Display Property!
๐ง What is the Display Property?
Think of it like this: every HTML element (like paragraphs, divs, images, etc.) is a box.
And the display property tells each box how it behaves โ whether itโs greedy, polite, shy, or flexible.
๐ญ The 4 Main Types of Display
Letโs meet the 4 display types like theyโre characters in a story!
1. Block (The Greedy One ๐)
-
Takes up the entire row (the full width of the screen)
-
Doesnโt like to share space with anyone else
-
Pushes the next box to the next line
๐ฆ Example: A <p> (paragraph) or <div> is block by default
๐งช What happens if you have two block elements?
<p>Hello</p>
<p>World</p>๐ They will appear one below the other like:
Hello
World
๐ง Block boxes can have width and height set easily!
2. Inline (The Shy One ๐ค)
-
Only takes up as much space as needed
-
Shares space with others on the same line
-
Cannot set its width or height โ it sizes itself based on its content!
๐ฆ Example: A <span> or <a> tag is inline by default
<p>Hello <span>beautiful</span> world</p>๐ง span is great for coloring or styling just part of the text.
3. Inline-Block (The Best of Both Worlds ๐)
-
Stays on the same line like inline
-
But you can still set width and height like block
๐ฏ This one is super useful when you want little blocks next to each other but still want to control their size.
๐งฑ Imagine lining up LEGO blocks next to each other โ thatโs inline-block!
4. None (The Invisible One ๐ซฅ)
-
Hides the element completely
-
Itโs like telling your toy: โShhh, hide under the bed!โ
-
It wonโt show up on screen and wonโt take any space
Use it when:
-
You want to hide something like a secret message ๐
-
Make a task disappear when itโs done in a to-do list โ
๐๏ธ Letโs Try Them with Examples!
Imagine we have 3 boxes โ red, green, and blue โ all with width: 200px and height: 200px.
You want to arrange them side by side like this:
๐ฅ ๐ฉ ๐ฆ
To do that, youโd use:
display: inline-block;โ This lets them:
-
Appear next to each other
-
Keep their shape and size
๐งช Experiment Time!
Thereโs a cool practice website:
๐ https://appbrewery.github.io/css-display
It shows you how each display type behaves. Try changing the width and height to see what happens!
๐ง Hereโs What Happens When You Change Display:
| Display Type | Takes Full Width? | Can Sit Next to Others? | Can Set Width/Height? |
|---|---|---|---|
block | โ Yes | โ No | โ Yes |
inline | โ No | โ Yes | โ No |
inline-block | โ No | โ Yes | โ Yes |
none | โ (Itโs hidden!) | โ (Itโs hidden!) | โ (Itโs hidden!) |
๐งฉ Challenge Time!
Youโre given 3 boxes (paragraphs) with width and height set to 200px.
You need to:
-
Make them line up horizontally
-
Make them line up vertically
๐ฏ Solution 1: Horizontal Layout
To make the boxes line up side by side:
p {
display: inline-block;
}โจ Now the boxes will appear on the same line (if thereโs enough space on screen!)
๐ฏ Solution 2: Vertical Layout
To make the boxes stack up like a tower:
p {
display: block;
}๐งฑ Now the boxes each take their own row and stack on top of each other.
๐ค Recap - What Did We Learn?
| Concept | Think of it likeโฆ | What it does |
|---|---|---|
block | Big greedy box ๐ฆ | Takes the whole line |
inline | Little text hug ๐ค | Sits inside text, only as big as it needs to be |
inline-block | LEGO brick ๐งฑ | Can sit with others & have its own shape |
none | Ninja mode ๐ฅท | Completely hidden |