i have the following code on one of my websites:
body {
width: 1020px;
margin: 0 auto;
}
.box {
width: 500px;
float: left;
}
.clear {
clear: both;
height: 0;
font-size: 1px;
line-height: 0;
}
<body>
<p>Headline</p>
<div class="box">content</div>
<div class="box">content</div>
<div class="box">content</div>
<div class="clear"></div>
<p>Headline</p>
<div class="box">content</div>
<div class="box">content</div>
<div class="clear"></div>
<p>Headline</p>
<div class="box">content</div>
<div class="box">content</div>
<div class="box">content</div>
<div class="clear"></div>
</body>
I would like to add a margin-right of 10px to every box div that is on the left side so that there is a gap between the two divs that are next to each other.
I have tried to solve this with :nth-child() but it wont work because there are other elements like the p tags and the clearing elements in between.
Is there a way to solve this with just css?
I can not change the structure or the type of the elements by the way!
nth-of-type()
if only you could make a container for each of the three blocks.p
element, and the number of boxes in each group is limited to a "reasonable" amount (say rather a dozen, than hundreds), then you could get a way with simply selecting every first box that comes after a p, every box that comes after a box than comes after a box after a p (3rd), and so on:p + .box, p + .box + .box + .box, ... { margin-right: 10px; }