DEV Community

Cover image for Quick CSS Quiz #1
Abdelrahman Ismail
Abdelrahman Ismail

Posted on β€’ Edited on

11 2

Quick CSS Quiz #1

CSS might be a total mystery to you or a topic that’s near and dear to your heart. Whether you think of CSS as a "necessary evil" or think of it as the power to control the world of technology, CSS is a must-have for anyone working on web applications. A deep knowledge of CSS can be the difference between a beautiful, polished web application and one that just feels like "meh".

In this series, I try to focus on CSS weird parts, throw quiz and its answer, hoping to give a better understanding of how CSS works in depth.

Problem:

Having the following code snippet, for two adjacent siblings divs, #div1 has a margin of 50px, #div2 has a margin of 20px.
what is the distance between those two div?

<div id="div1" style="margin: 50px;">A</div>
<div id="div2" style="margin: 20px;">B</div>
Enter fullscreen mode Exit fullscreen mode

Options:

  • 20px
  • 30px
  • 50px
  • 70px

The answer:

you can find the answer below, but please give yourself a couple of minutes to think about it, to make sure you benefit from this quiz.






πŸ‘‡πŸ‘‡πŸ‘‡





The correct answer is: "50px" πŸŽ‰πŸŽ‰
If you wonder why this is the correct answer, please read the discussions below.

Cover image original artwork by: Sergi Delgado

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (5)

Collapse
 
ismail9k profile image
Abdelrahman Ismail β€’

First of all, because the div is a block-level element it will take the full width of the container, so the distance between the two divs is the vertical distance.
Due to "margin collapsing", the top and bottom margins are collapsed into a single margin whose size is the largest of the individual margins, which is 50px.

Collapse
 
ygalanter profile image
ygalanter β€’

Adjacent margins overlap, not combine, hence 50.

Collapse
 
ismail9k profile image
Abdelrahman Ismail β€’

πŸ˜„πŸ‘πŸ‘πŸŽ‰πŸŽ‰

Collapse
 
maxart2501 profile image
Massimo Artizzu β€’

Hey folks, remember that margin collapsing happens only vertically, or better on the block axis.

Also, it doesn't apply to flex or grid items.

Collapse
 
ismail9k profile image
Abdelrahman Ismail β€’

Thank you for sharing this πŸ€—πŸ€—

Image of Checkly

4 Playwright Locators Explained: Which One Should You Use?

- locator('.cta'): Fast but brittle
- getByText('Click me'): User-facing, but can miss broken accessibility
- getByRole('button', { name: 'Click me' }): Most robust, best for a11y
- getByTestId('cta-button'): Stable, but ignores UX

Watch video

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay