0

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!

5
  • Can you change the HTML? It wouldn't affect the visual result but it would make it much easier to solve your problem. Commented Oct 21, 2017 at 1:44
  • It is a re-design of an existing site which means it is possible but it would be a lot of extra work. What would i have to change?
    – Dennis
    Commented Oct 21, 2017 at 1:46
  • You could solve this easily using nth-of-type() if only you could make a container for each of the three blocks. Commented Oct 21, 2017 at 1:46
  • I would definitely change the HTML a bit as the code that you have would require a lot of 'hacks' to get it to work. I will answer the question bellow. Commented Oct 21, 2017 at 1:47
  • If each of those "groups" of boxes is preceded by a 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; }
    – C3roe
    Commented Oct 21, 2017 at 1:51

2 Answers 2

2

Your HTML is not helping. Ideally, you would have a div wrapping all the divs with class .box. Something like:

.boxesWrapper {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}

.box{
  border: 1px dotted black;
  margin-right: 10px;
  width: 100px;
}
<body>

  <p>Headline</p>
  <div class="boxesWrapper">
    <div class="box">content</div>
    <div class="box">content</div>
    <div class="box">content</div>
  </div>
  <p>Headline</p>
  <div class="boxesWrapper">
    <div class="box">content</div>
    <div class="box">content</div>
  </div>

  <p>Headline</p>
  <div class="boxesWrapper">
    <div class="box">content</div>
    <div class="box">content</div>
    <div class="box">content</div>
  </div>

</body>

0
1

You can solve it by using nth-of-type() selector if you can add a container around each group:

body {
  width: 1020px;
  margin: 0 auto;
}

.box {
  width: 500px;
  float: left;
  background: red;
}

.container div.box:nth-of-type(odd) {
  margin-right: 10px;
}

.clear {
  clear: both;
  height: 0;
  font-size: 1px;
  line-height: 0;
}
<body>

  <div class="container">
    <p>Headline</p>
    <div class="box">content</div>
    <div class="box">content</div>
    <div class="box">content</div>
    <div class="clear"></div>
  </div>

  <div class="container">
    <p>Headline</p>
    <div class="box">content</div>
    <div class="box">content</div>
    <div class="clear"></div>
  </div>

  <div class="container">
    <p>Headline</p>
    <div class="box">content</div>
    <div class="box">content</div>
    <div class="box">content</div>
    <div class="clear"></div>
  </div>

</body>

Not the answer you're looking for? Browse other questions tagged or ask your own question.