You can use margin-right
, for example, to push a next sibling as seen below:
#parent {
background: tan;
font-size: 0;
}
#parent * {
display: inline-block;
width: 50px;
height: 50px;
}
#child1 {
background: teal;
margin-right: 100px;
}
#child2 {
background: olive;
}
<div id="parent">
<div id="child1"></div>
<div id="child2"></div>
</div>
I know there is no previous sibling selector in CSS. However, I wonder if there's a workaround so #child2
can pull #child1
to a distance of say, 20px
. Here's a before-after screenshot:
Note: I don't want to give a margin
value to #child1
or alter the HTML. The effect should be achieved using #child2
only.