Daniel Moss Daniel Moss - 3 years ago 155
CSS Question

Detecting if a div above has margin in CSS?

I currently have two elements:

.item-one {
margin-bottom: 24px;
}
..other-elements..
.item-two {
margin-top: 48px;
}


These items are fine on their own, but say, if I removed the
..other elements..
between them, so they'd be one after another, I would be left with a total margin (visually) of 72px.

The
..other elements..
have a consistent 24px, because of the flow, but my app is dynamic and users can switch places of elements.

Unfortunately, this is highly undesirable as it'd result in bad looking spaces.

I'd like to write something like

.item-two [if-div-is-previous -> .item-one] {
//this will set the margin to 0 only if preceded by .item-one
margin-top: 0px;
}


In short, I'm trying to keep a consistent distance between all the elements on the page of 24 px, even when items that have different margins than 24px are one after another.

Answer Source
.item-one + .item-two {  } //if .item-two is next to .item-one
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download