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 TypeTakes 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:

  1. Make them line up horizontally

  2. 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?

ConceptThink of it likeโ€ฆWhat it does
blockBig greedy box ๐Ÿ“ฆTakes the whole line
inlineLittle text hug ๐Ÿค—Sits inside text, only as big as it needs to be
inline-blockLEGO brick ๐ŸงฑCan sit with others & have its own shape
noneNinja mode ๐ŸฅทCompletely hidden

References