I'm trying to show two divs next to each other using Bootstrap, but there is a distance between them. How can i place them exactly next to each other.
The code:
<div class="col-lg-8 col-lg-offset-2 centered">
<div style="float: left; border: 1px solid; width: 227px; height: 50px;"></div>
<div style="float: right; border: 1px solid;width: 227px; height: 50px;"></div>
</div>
Image illustration:
Look into grids in Bootstrap.
You could do something like this:
<div class="row">
<div class="col-xs-6">div 1</div>
<div class="col-xs-6">div 2</div>
</div>
Adding to Lschessinger's answer you could use offset to center the blocks. Look here under Offsetting columns
Maybe this is what you're looking for?
<style>
.border {border: 1px solid #CCC;}
</style>
<div class="row">
<div class="col-xs-2 border col-xs-offset-4">div 1</div>
<div class="col-xs-2 border">div 2</div>
</div>
Or if you have to stick to your code with the inline styles and specific widths then maybe this, you can increase the width between by increasing the width 454px to 464px for a 10px gap, and so on:
<div class="col-lg-12">
<div style="width: 454px;" class="center-block">
<div style="border: 1px solid; width: 227px; height: 50px;" class="pull-left"></div>
<div style="border: 1px solid; width: 227px; height: 50px;" class="pull-right"></div>
</div>
</div>
One approach is to have a row containing two div elements. The elements will seemingly stack next to each other due to the fact that Bootstrap is built with flexbox.
In the following example I'm using the class justify-content-center to center both elements. And col-auto that will size columns based on the natural width of their content.
div{border:1px solid blue}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<div class="container">
<div class="row justify-content-center p-3">
<div class="col-auto">
content one
</div>
<div class="col-auto">
content two
</div>
</div>
</div>
Align the elements to the left by using justify-content-start
div{border:1px solid blue}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<div class="container">
<div class="row justify-content-start p-3">
<div class="col-auto">
content one
</div>
<div class="col-auto">
content two
</div>
</div>
</div>
Align the elements to the right by using justify-content-end
div{border:1px solid blue}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<div class="container">
<div class="row justify-content-end p-3">
<div class="col-auto">
content one
</div>
<div class="col-auto">
content two
</div>
</div>
</div>
Note: This codes do not have breakpoints. If you want to add breakpoints use this format: col-{breakpoint}-auto or/and justify-content-{breakpoint}-auto
Related
I'd like to be able to get a layout using Bootstrap that looks as follows:
My code is:
<div class="container">
<div class="row">
<div class="col-12">foo</div>
</div>
<div class="row">
<div class="col-6">bar</div>
<div class="col-6">baz</div>
</div>
Unfortunately, I'm getting something that looks like this (notice that "foo" in row 1 is aligned with "bar" in row 2):
How do I achieve the desired result?
Thanks!
It seems you want to only center the text on the .col-12 from the first .row which you can easily achieve by simply adding the text-center class to that .col-12 element.
Note: the class text-center is a Bootstrap class.
/** just to visually show the changes */
.col-6,
.col-12 {
border: 1px solid red
}
.col-12 {
margin-bottom: 20px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-12 text-center">foo</div>
</div>
<div class="row">
<div class="col-6">bar</div>
<div class="col-6">baz</div>
</div>
</div>
In case you have an img not a text on the .col-12 element, you might simply add the d-inline-block and your image should be centered thanks to text-ce,ter class that we already applied.
/** just to visually show the changes */
.col-6,
.col-12 {
border: 1px solid red;
}
.col-12 {
margin-bottom: 20px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-12 text-center">
<img src="https://via.placeholder.com/150" width="150" class="img-fluid d-inline-block">
</div>
</div>
<div class="row">
<div class="col-6">bar</div>
<div class="col-6">baz</div>
</div>
</div>
Note: the use of d-inline-block is not really required but i used it to ensure cross-browser behavior because each browser might treat the images in a different way. Most of the browsers already set the display property of an image to inline-block.
The class img-fluid is a Bootstrap class that allows to maintain a responsive image. Learn more about Bootstrap Responsive Images on Bootstrap Docs (BS v4.0).
Learn more about the display property on MDN.
According to your code to make it center, you can align the text center and that's all.
<div class="container">
<div class="row text-center">
<div class="col-12">foo</div>
</div>
<div class="row text-center">
<div class="col-6">bar</div>
<div class="col-6">baz</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-12 text-center">foo</div>
</div>
<div class="row">
<div class="col-6">bar</div>
<div class="col-6">baz</div>
</div>
In bootstrap 4 I have the following code:
<div class="container-fluid">
<div class="row no-gutter">
<div class="d-none d-md-flex col-md-12 flex-wrap">
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
</div>
</div>
</div>
Issue I have is as soon as there is a 'wrapped item', it appears to get stuck to the bottom of the items above. When I try to apply a margin-bottom:5px to each of the divs, it appears to have no effect. What is the correct way to handle this?
A few things in your code...
why use float with flex? Bootstrap 4 uses flex instead of float (which was used by bootstrap 3)
the divs in pink are from your code with classes removed to show the effect in the browser/snippet
the divs in blue show the solution with flex (using bootstrap 4 classes)
margin-bottom works in both
working snippet below:
.flex-wrap>div {
width: 100px;
height: 50px;
display: block;
}
.my-divs {
float: left;
margin-bottom: 10px;
background: lightpink;
}
.my-flex-div {
background: lightblue;
margin-bottom: 5px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row no-gutter">
<div class=" d-md-flex col-md-12 flex-wrap">
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
</div>
<hr/>
<div class="d-flex flex-wrap">
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
</div>
</div>
</div>
It is most likely your use of float that is causing the problem. However this whole layout can be achieved using only Bootstrap default classes and no additional CSS.
First, there is no need for a row/col layout if you are only using a col-12 full-width single column. Simply remove the row no-gutter and col-12 classes.
Second, BS4 includes utility classes for margin. Read about them here.
Try the example below. I have used multiple background colors for clarity.
.flex-wrap div {
width: 100px;
height: 50px;
}
<div class="container-fluid">
<div class="d-flex flex-wrap">
<div class="mb-5 bg-primary">Div</div>
<div class="mb-5 bg-secondary">Div</div>
<div class="mb-5 bg-warning">Div</div>
<div class="mb-5 bg-success">Div</div>
<div class="mb-5 bg-danger">Div</div>
</div>
</div>
Here is a working codepen example.
So I'm trying to center my green divs vertically which are the ones with the class myCol by using align-items-center on the parent row but it's not centering it and I have no idea why.
Why does it behave like that?
.myRow {
height: 30vh;
border: orange dotted 2px;
}
.myCell {
width: 100px;
height: 100px;
background: green;
border: blue dotted 3px;
}
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<div class="container">
<div class="row align-items-center myRow">
<div class="col-3 myCell">
</div>
</div>
<div class="row myRow">
<div class="col-4 myCell">
</div>
</div>
</div>
The problem with your code is that you are using Bootstrap version 2 whereas you should be using Bootstrap version 4.
Just replace the links for bootstrap script and css file and it should work.
You are using bootstrap version 2, but align-items-center class appear in bootstrap version 4.
Replace your html code to:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
<div class="row align-items-center myRow">
<div class="col-3 myCell">
</div>
</div>
<div class="row myRow">
<div class="col-4 myCell">
</div>
</div>
</div>
I have a problem, all I want to do is at a certain width (using the #media) I want to change my left div to the right side under the one that is already there. I tried everything, including changing the display, positioning using left margin but due to the parent container it doesn't work. The only thing that I thought about is to make the same left div on the right side and to make the display to none, but I am pretty sure that's not the way to deal with it. I use Bootstrap 4.
This is the HTML:
.main{
background-color: #aaa;
box-shadow: 3px 3px 20px #000000;
border-radius: 20px;
margin-left: 20px;
margin-right: 20px
}
.left{
background-color: #aaa;
max-width: 200px;
height: 1%;
border-radius: 20px;
}
.right{
background-color: #aaa;
max-width: 200px;
height: 1%;
border-radius: 20px;
}
.right-content{
width: 100%;
text-align: center;
float: center;
margin-top: 10px;
}
.parent-container {
display: flex;
}
<div class="parent-container">
<div class="container left">
<div class="row">
Left content
</div>
</div>
<div class="container card main py-3">
<div class="container card">
<div class="card-body">
<div class="row">
<div class="col-lg-12 text-center">
<h2>TITLE2</h2>
</div>
</div>
<div class="row">
<div class="col-md-12 text-center">
<a href="#" class="link1">
<img src="2.jpg">
<h3>Title3</h3>
</a>
</div>
</div>
</div>
</div>
<div class="container right">
<div class="row">
<div class="right-content">
Right content here
</div>
</div>
</div>
</div>
To achieve the effect you want, you need to use Bootstrap 4 order-* classes. order-last in the code snippet below re-orders the column to come last by default but the order-md-first makes the column appear first on screens that are medium (md) or larger.
There are other ways of using the order classes to achieve the same result. This is just one of the possible options.
But there are many other and fundamental mistakes in your code. Here are a few pointers:
Do NOT nest containers inside other containers. Nest row-column pairs inside columns instead
Do NOT put any content directly into a .row. Bootstrap rows aren't designed for that. Put one or more columns (.col-*) into a row and put all your content inside that column(s).
Bootstrap 4 is flexbox-based by default and has classes to do almost everything you'll ever need in terms of positioning and spacing. Use those native Bootstrap classes instead of unnecessary custom css hacks.
Here's a working code snippet (click the "run code snippet" button below and expand to full page):
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="container-fluid">
<div class="row">
<div class="col-12 col-md-3 col-xl-2 bg-light order-last order-md-first py-3">
Left div content <br>
Left div content <br>
Left div content <br>
</div>
<div class="col-12 col-md-9 col-xl-10 bg-primary py-3">
<div class="card py-3">
<div class="card-body">
<div class="row">
<div class="col text-center">
<h2>Right div TITLE</h2>
</div>
</div>
<div class="row">
<div class="col text-center">
<a href="#">
<img class="img-fluid" src="https://placeimg.com/888/88/tech">
<h3>Right div subtitle</h3>
</a>
</div>
</div>
</div>
<div class="row">
<div class="col text-center">
Right div content here
</div>
</div>
</div>
</div>
</div>
</div>
Notice: That code doesn't contain any of your custom css. If you want to add some custom css to that, start adding it line by line to see where your custom css breaks Bootstrap.
Also, the background classes bg-light and bg-primary in the code snippet above are just for making things easier to see.
I am trying to put 5 div elements next to each other and also center them. However, I cant make it work. Here is my code
<div style="width:200px;margin-right:20px;">
<div class="panel panel-default text-center" style="border: 1px solid #fab05d;border-radius:0px;">
<div class="panel-heading" style="background-color:#fab05d;color:#ffffff !important;border-radius:0px;">
<h1>2017</h1>
<p style="line-height: 14pt; margin-top:3px;margin-right:25px;">EARLY BIRD*</p>
<p style="clear:both;line-height: 16pt;">GOLD PACKAGE <br>REGISTRATION**</p>
</div>
<div class="panel-body" style="padding-top:5px;">
<p style="color:#929393;font-size:15px;"><strong>$x.00 TICKET</strong></p>
<div style="display:inline-block;margin:auto;padding-top:1px;margin-top:1px;" class="text-center">
<input type="image" src="http://localhost/wordpress/wp-content/uploads/2017/03/gold_remove.png" id="gold_r" style="">
<input type="text" class="text-center" id='goldval' name="quantity" size="5" onchange="gold_change()" style="vertical-align:top;font-weight:bold;border-radius:5px;border:1px solid #929393;">
<input type="image" src="http://localhost" id="gold_a" style="">
</div>
</div>
</div>
I have 5 of them. They are basically the same, only the content is different. All 5 divs are inside container-fluid (I am using bootsrap). I have tried to add display:inline-block to my container but it doesn't work. Also, I used float:left but in that case, I can't properly align my divs when the screen size changes. I tried to use the extended version of Bootstrap grid when you can create equal 5 columns but in that case, my div's content gets messed up. Can anyone please give me some solution that will work for all screen sizes? Thanks
Try this in a larger screen, I think this will work. The snippet's screen is very small that's why it does not align together. Use Media Queries to adjust the div's size so that it can accommodate smaller screen size
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<div class="row">
<div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2">
</div>
<div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2">
</div>
<div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2">
</div>
<div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2">
</div>
<div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2">
</div>
</div>
</div>
This may help you!
.col-xs-2{
background:green;
color:#FFF;
}
.col-half-offset{
margin-left:4.166666667%
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" style="border: 1px solid black">
<div class="col-xs-2" id="p1">One</div>
<div class="col-xs-2 col-half-offset" id="p2">Two</div>
<div class="col-xs-2 col-half-offset" id="p3">Three</div>
<div class="col-xs-2 col-half-offset" id="p4">Four</div>
<div class="col-xs-2 col-half-offset" id="p5">Five</div>
<div>lorem</div>
</div>
</div>