2

I want to stack divs ( 2 divs in 1 row )which are 50% width side by side without using float:left because it will taken the div out of the normal flow. Flexbox is also not preferred because of limited browser support. How can I get the desired view without changing the width?

my code

*, *:before, *:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  -o-box-sizing: border-box;
  box-sizing: border-box;
}

.col {
  display: inline-block;
  width: 50%;
}

.col-content {
  padding: 0.5em;
  background-color: #ddd;
  border: 1px solid #aaa;
  color: #333;
}
<div class="container">
  <div class="col">
    <p class="col-content">
      col Item 1
    </p>
  </div>
 
  <div class="col">
    <p class="col-content">
      col Item 2
    </p>
  </div>
  
  <div class="col">
    <p class="col-content">
      col Item 3
    </p>
  </div>
 
  <div class="col">
    <p class="col-content">
      col Item 4
    </p>
  </div>
</div>

2 Answers2

1

Strange but true - you can just remove the spaces between the elements from the HTML - like this:

*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  -o-box-sizing: border-box;
  box-sizing: border-box;
}
.col {
  display: inline-block;
  width: 50%;
}
.col-content {
  padding: 0.5em;
  background-color: #ddd;
  border: 1px solid #aaa;
  color: #333;
}
<div class="container">
  <div class="col">
    <p class="col-content">
      col Item 1
    </p>
  </div><!--
 
  --><div class="col">
    <p class="col-content">
      col Item 2
    </p>
  </div><!--
  
   --><div class="col">
    <p class="col-content">
      col Item 3
    </p>
  </div><!--
 
   --><div class="col">
    <p class="col-content">
      col Item 4
    </p>
  </div>
</div>

You can also try a margin of -4px on each inline-block element

sideroxylon
  • 4,338
  • 1
  • 22
  • 40
  • Unorthodox way. but still working perfect. But Its seems the problem will comeback if I try to intent the HTML code. –  Feb 02 '16 at 11:42
  • It should be fine as long as the width of the inline blocks is half the width of the container. – sideroxylon Feb 02 '16 at 11:44
0

Remove spaces between col div ending tag and starting tag.

For Example:

<div class="col">
    <p class="col-content">
      col Item 1
    </p>
</div>
<div class="col">
    <p class="col-content">
      col Item 2
    </p>
</div>

to

<div class="col">
    <p class="col-content">
      col Item 1
    </p>
</div><div class="col">
    <p class="col-content">
      col Item 2
    </p>
</div>

Try this snippet:

*, *:before, *:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  -o-box-sizing: border-box;
  box-sizing: border-box;
}

.col {
  display: inline-block;
  width: 50%;
}

.col-content {
  padding: 0.5em;
  background-color: #ddd;
  border: 1px solid #aaa;
  color: #333;
}
<div class="container">
  <div class="col">
    <p class="col-content">
      col Item 1
    </p>
  </div><div class="col">
    <p class="col-content">
      col Item 2
    </p>
  </div><div class="col">
    <p class="col-content">
      col Item 3
    </p>
  </div><div class="col">
    <p class="col-content">
      col Item 4
    </p>
  </div>
</div>
Santhosh B
  • 162
  • 3