Remove 1px gap when using display:flex with Bootstrap - css

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?
.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>

The gap is caused by the clearfix gap - content: " " - which is on pseudo elements of the bootstrap .row class.
To prevent the gap:
Remove the "row" class as well as the parents "container" class
or
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>

Related

how to give row's elements margin without breaking the row?

How can I add margin between elements without breaking the row in this fiddle
This question has been asked before here However I couldn't find a proper answer, because adding padding is not possible in my case.
.row {
background: #f8f9fa;
margin-top: 20px;
}
.item {
border: solid 1px #6c757d;
padding: 10px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-6 item m-1">2</div>
<div class="col-6 item">4</div>
</div>
<div class="row">
<div class="col-8 item m-2">2</div>
<div class="col-4 item">1</div>
</div>
</div>
well u can do this by adding a new div inside ur col
.row {
background: #f8f9fa;
margin-top: 20px;
}
.item {
border: solid 1px #6c757d;
padding: 10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<!--
Bootstrap docs: https://getbootstrap.com/docs
-->
<div class="container">
<div class="row">
<div class="col-6"><div class="m-1 item">2</div></div>
<div class="col-6"><div class="m-1 item">4</div></div>
</div>
<div class="row">
<div class="col-8 "><div class="m-2 item">4</div></div>
<div class="col-4"><div class="m-2 item">6</div></div>
</div>
</div>

CSS text side by side same height

I am trying to make this text
but how can I make it same height and make inline each other
<div class="row">
<div class="col-xs-12 text-center">
<p style="display:inline">初期費用 <br> 月額費用 <br>手続き料</p>
<h3 style="display:inline">0<span>円</span></h3>
</div>
</div>
Try this:
p, h3{
display: inline-block;
}
h3 {
color: skyblue;
font-size: 70px;
}
<div class="row">
<div class="col-xs-12 text-center">
<p>初期費用 <br> 月額費用 <br>手続き料</p>
<h3>0<span>円</span></h3>
</div>
</div>
Using display flex
.col-xs-12 {
display: flex;
align-items: center;
padding: 0;
}
p {
font-size: 16px;
line-height: 1.5;
margin-right: 8px;
}
h3 {
font-size: 102px;
margin: 0;
}
h3 span {
font-size: 32px
}
<div class="row">
<div class="col-xs-12 text-center container">
<p>初期費用 <br> 月額費用 <br>手続き料</p>
<h3>0<span>円</span></h3>
</div>
</div>
Here you go with a solution
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<div class="row">
<div class="col-xs-12 text-center">
<div style="display: flex;">
<p style="display: inline-flex">初期費用 <br> 月額費用 <br>手続き料</p>
<h3 style="display: inline-flex">0<span>円</span></h3>
</div>
</div>
</div>
Solution using flex & inline-flex

How to span rows in Bootstrap?

I'm trying to achieve a layout like below in Bootstrap but am having a difficult time with it. I feel dumb asking this but it's my first time using Bootstrap and I couldn't find a similar example on here.
Thanks!
I thought maybe something like this, but div C clears div B and ends up way too far down the page.
<div class="container">
<div class="row">
<div class="col-md-8">
A
</div>
<div class="col-md-4">
B
</div>
</div>
<div class="row">
<div class="col-md-8">
C
</div>
</div>
</div>
If you need a pure bootstrap solution you need to add col-xs-12 to make it 100% on mobiles and col-sm-6 to make it 50% on desktop. The add pull-left and pull-right to avoid the B panel to clear and move C below everything
.bg-danger, .bg-primary {
height: 200px;
}
.bg-success {
height: 400px;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row container-fluid">
<div class="col-sm-6 col-xs-12 bg-danger pull-left"></div>
<div class="col-sm-6 col-xs-12 bg-success pull-right"></div>
<div class="col-sm-6 col-xs-12 bg-primary pull-left"></div>
</div>
</div></body>
</html>
Click full page to see the difference
Here we have an explanation about the grid system.
http://getbootstrap.com/css/#grid
Here's a simple solution:
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12">
</div>
<div class="col-sm-12">
</div>
</div>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
The page is split in half with the two outer columns "col-sm-6", with one of these columns containing two inner columns that span it's entire width
A simple solution if you want essentailly the green box to come in between.
Check this Bootply for responsive-ness check.
Snippet here:
.something {
height: 100px;
margin-top: 10px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="something bg-danger"></div>
</div>
<div class="col-md-6">
<div class="something bg-success"></div>
</div>
<div class="col-md-6">
<div class="something bg-primary"></div>
</div>
</div>
</div>
Here is another example where the Green box will come below the rest two boxes..:
.something {
height: 100px;
margin-top: 10px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div clas="col-md-6">
<div class="col-md-12">
<div class="something bg-danger"></div>
</div>
<div class="col-md-12">
<div class="something bg-primary"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="something bg-success"></div>
</div>
</div>
</div>
You should use Pure CSS Flexbox for this.
Have a look at the snippet below (use full screen for desktop mode):
.box-holder {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
flex-wrap: wrap;
width: 300px;
height: 280px;
}
.box {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 100px;
border: 1px solid #000;
color: #fff;
margin: 15px;
font-size: 40px;
font-weight: 200;
}
.a {
background: red;
}
.b {
align-self: flex-start;
order: 1;
background: green;
height: 240px;
margin: 0;
}
.c {
background: blue;
}
/* On Mobiles */
#media screen and (max-width: 700px) {
.box-holder {
width: auto;
height: auto;
}
.b {
align-self: center !important;
order: 0;
margin: 15px;
}
}
<div class="box-holder">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
</div>
Hope this helps!
I hope this helps..:)
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-sm-3"><!--div for the left side abc pattern starts-->
<div class="row">
<div class="col-sm-12">
"standing a"
</div>
</div>
<div class="row">
<div class="col-sm-12">
"standing b - adjust the height of this block"
</div>
</div>
<div class="row">
<div class="col-sm-12">
"standing c"
</div>
</div>
</div><!-- div for the left side abc pattern ends -->
<div class="col-sm-6"><!-- div for the right side abc pattern starts -->
<div class="row">
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12">
"block a"
</div>
</div>
<div class="row">
<div class="col-sm-12">
"block b"
</div>
</div>
</div>
<div class="col-sm-6">
"block c"
</div>
</div>
</div><!-- div for the right side abc pattern ends -->
</div><!-- row closed here -->
</div>

Bootstrap complex column position ordering

Is it possible to pull/push divs of a column from another column in Bootstrap grid system? For example:
At desktop view: it's like this way:
Parent Column 1 | Parent Column 2
First Content | Second Content
Third Content | Fourth Content
At mobile view, it's like this way:
Parent Column 1
First Content
Second Content
Parent Column 2
Third Content
Fourth Content
Please take a look on this fiddle to understand what I am trying to mean.Thanks in advance.
You can achieve it by using bootstrap's grid.
.first {
background: yellow;
height: 300px;
}
.second {
background: red;
height: 100px;
color: white;
}
.third {
background: green;
height: 300px;
color: white;
}
.fourth {
background: blue;
height: 100px;
color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-sm-6 col-xs-12 first">
FIRST
</div>
<div class="col-sm-6 col-xs-12 second">
SECOND
</div>
<div class="col-sm-6 col-xs-12 third">
THIRD
</div>
<div class="col-sm-6 col-xs-12 fourth">
FOURTH
</div>
</div>
</div>
Or you can use Masonry JS:
var $container = $('.masonry-container');
$container.masonry({
columnWidth: '.item',
itemSelector: '.item'
});
.first {
background: yellow;
height: 300px;
}
.second {
background: red;
height: 100px;
color: white;
}
.third {
background: green;
height: 300px;
color: white;
}
.fourth {
background: blue;
height: 100px;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/4.1.1/masonry.pkgd.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row masonry-container">
<div class="col-sm-6 col-xs-12 first item">
FIRST
</div>
<div class="col-sm-6 col-xs-12 second item">
SECOND
</div>
<div class="col-sm-6 col-xs-12 third item">
THIRD
</div>
<div class="col-sm-6 col-xs-12 fourth item">
FOURTH
</div>
</div>
</div>
I think the issue is more of a float problem because your columns have variable height. Create a special class to pull the 2nd and 3rd columnd right on larger screens..
#media (min-width: 992px) {
.pull-md-right {
float:right;
}
}
And, use it like this..
<div class="row">
<div class="col-md-6">
<div class="first">First</div>
</div>
<div class="col-md-6 pull-md-right">
<div class="second">Second</div>
</div>
<div class="col-md-6 pull-md-right">
<div class="third">Third</div>
</div>
<div class="col-md-6">
<div class="fourth">Fourth</div>
</div>
</div>
http://codeply.com/go/CYteNdheKS
In Bootstrap 4, responsive floats are included so you won't need the extra CSS.

CSS: Getting a placeholder image take the entire size of the DIV without distortion

I want to place a png as a background image for a div that acts as a placeholder:
https://jsbin.com/hohegiyazu/edit?html,css,output
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css" rel="stylesheet">
.container {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.placeholder {
background-image:url(/images/placeholder.png);
background-size:100% 100%;
height:50px;
flex:1;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
</div>
</div>
</body>
</html>
I get this effect:
The image is distorted according to the width of the element and the left and right margins are not equal.
I want to achieve this effect:
Is it possible?
One way is to change your background-size property to cover
.item {
background-size: cover;
}
It will make sure the image cover the full background, without stretch, though this still depends a lot on how the image actually looks like as well as how the element using it is set up.
Update after question edit
With positioning, i.e. center center, you can control what part of the image is of more value and have it centered or left aligned or ...
.container {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.placeholder {
background: url(http://placehold.it/300) center center;
background-size: cover;
height:50px;
flex:1;
margin: 1px;
}
<div class="container">
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
</div>
</div>
Update after 2:nd question edit
You can use a pseudo element, no extra markup, with background color and rounded corners. This will save you the extra http request, load the page faster (which is really recommended) and you can easily change color and border.
And you can use an image if you really need one
.container {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.placeholder {
position: relative;
height:50px;
flex:1;
margin: 1px;
padding: 5px;
color: red;
}
.placeholder::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius: 14px;
z-index: -1;
background: lightgray;
/* with image
background: url(http://placehold.it/300) center center / cover;
*/
}
<div class="container">
<div class="row">
<div class="placeholder">Hey, there</div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
</div>
</div>
The reason for this is that your PNG seem to include the white margins. So when you stretch it horizontally, the margins are proportionally stretched too and therefore become bigger.
I think you should use the border-image property as described here: https://css-tricks.com/understanding-border-image/
That way, you will be able to define which part of your image can be stretched and which part remains the same width/height, allowing you to keep corners and margins as you want them.

Resources