Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove 1px gap when using display:flex with Bootstrap

In some browsers, such as Safari 9, the following bootstrap grid is leaving a 1px gap on either side of the row element. Why is that?

Screenshot

.a {background-color:#eee}
.b {background-color:#ddd}
.row {background-color:red}

.vertical-align {
   display: flex;
   align-items: center;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-xs-6 a">Hello</div>
        <div class="col-xs-6 b">World</div>
    </div>
</div>
<br><br>
<div class="container">
    <div class="row vertical-align">
        <div class="col-xs-6 a">Hello</div>
        <div class="col-xs-6 b">World</div>
    </div>
</div>
like image 276
greener Avatar asked Oct 14 '14 14:10

greener


People also ask

How do I get rid of flexbox extra whitespace?

You have to remove overflow-x: hidden; from html,boy leave default value. flex-wrap: 100vh; is wrong it should be flex-wrap: wrap | nowrap; read flex-wrap.

Does gap work with Flex?

Examples. The gap property is designed for use in grid, flex and multi-column layouts.

How do I add a gap between bootstrap Flex items?

justify-content: space-around , justify-content: space-evenly , justify-content: space-between . They provide spacing between elements and should help. Show activity on this post. If you are using bootstrap you need to add class.


1 Answers

The gap is caused by the clearfix gap - content: " " - which is on pseudo elements of the bootstrap .row class.

To prevent the gap:

  1. Remove the "row" class as well as the parents "container" class

  or

  1. Remove the clearfix pseudo element with:
div.vertical-align:before, div.vertical-align:after { display: none }

Note: Placing "div" before the class selector - .vertical-align - prevents the need for !important

The two examples

Without removing the row class

.a {
  background-color: #eee
}
.b {
  background-color: #ddd
}
.row {
  background-color: red
}
.vertical-align {
  display: flex;
  align-items: center;
}
div.vertical-align:before,
div.vertical-align:after {
  display: none;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-xs-6 a">Hello</div>
    <div class="col-xs-6 b">World</div>
  </div>
</div>
<br>
<br>
<div class="container">
  <div class="row vertical-align">
    <div class="col-xs-6 a">Hello</div>
    <div class="col-xs-6 b">World</div>
  </div>
</div>

With the removal of the row class.

The class - .container - also needs to be removed.

.a {
  background-color: #eee
}
.b {
  background-color: #ddd
}
.row {
  background-color: red
}
.vertical-align {
  display: flex;
  align-items: center;
}
div.vertical-align:before,
div.vertical-align:after {
  display: none;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-xs-6 a">Hello</div>
    <div class="col-xs-6 b">World</div>
  </div>
</div>
<br>
<br>
<div>
  <div class="vertical-align">
    <div class="col-xs-6 a">Hello</div>
    <div class="col-xs-6 b">World</div>
  </div>
</div>
like image 84
misterManSam Avatar answered Oct 21 '22 01:10

misterManSam