0

I want to create three colored boxes inside a box, here is the HTML/CSS code. When I inspect my code in browser the width would fit the parent box however the last colored box still position itself below the other boxes which should not happen.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  width: 1000px;
  height: 500px;
  border: 10px solid black;
  margin: 0 auto;
}

.green {
  display: inline-block;
  box-sizing: border-box;
  width: 33.3333%;
  height: 400px;
  padding: 20px;
  background-color: green;
}

.red {
  display: inline-block;
  box-sizing: border-box;
  width: 33.3333%;
  height: 400px;
  padding: 20px;
  background-color: red;
}

.blue {
  display: inline-block;
  box-sizing: border-box;
  width: 33.3333%;
  height: 400px;
  padding: 20px;
  background-color: blue;
}
<div class="container">
  <div class="box green"></div>
  <div class="box red"></div>
  <div class="box blue"></div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272

1 Answers1

0

Because inline elements are sensitive to the white space in your code. Just remove it:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  width: 1000px;
  height: 500px;
  border: 10px solid black;
  margin: 0 auto;
}

.green {
  display: inline-block;
  box-sizing: border-box;
  width: 33.3333%;
  height: 400px;
  padding: 20px;
  background-color: green;
}

.red {
  display: inline-block;
  box-sizing: border-box;
  width: 33.3333%;
  height: 400px;
  padding: 20px;
  background-color: red;
}

.blue {
  display: inline-block;
  box-sizing: border-box;
  width: 33.3333%;
  height: 400px;
  padding: 20px;
  background-color: blue;
}
<div class="container">
  <div class="box green"></div><div class="box red"></div><div class="box blue"></div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272