I have two DIV elements. I will call them DIV A
and DIV B
. I am creating a space between the two DIVs
of 20px. Example margin-top: 20px
. Here is a diagram to show that.
Let’s say if DIV A
is not present in the page, or is removed. In that case, I don’t want to have 20px spacing from DIV B
to it’s parent DIV
. I just want to use whatever the default spacing is.
I am wondering if there is a way to automatically change spacing for this scenario using CSS?
2
Answers
Use
margin-bottom: 20px
on div A. In this way, if div A is not present the 20px spacing will also not be present.Use
display: flex;
+flex-direction: column
on the container. Then you can create the spacing by adding thegap
property:Alternatively you could use a combination of CSS-Selectors: