I'm trying to simply center justify and align two bootstrap columns inside a container. The columns are in different rows.
The grid layout is
<body style="height: 100vh;">
<div class="container">
<div class="row">
<div class="col-6">
<div class="card">
hello
</div>
</div>
</div>
<div class="row">
<div class="col-4">
<div class="card">
world
</div>
</div>
</div>
</div>
</body>
the cards are just to outline the containers.
I'm aware there are MANY questions on vertical alignment, and I may have missed the one that contains the answer I need. But my take away from most of them is that the parent needs to have some height.
The following works just fine if you have only one row, because you can center the column in the 100% height row. But if you have two rows, it doesn't so well, since each column is centered in its own row, and the rows are each the height of the window.
<body style="height: 100vh;">
<div class="container h-100">
<div class="row h-100 justify-content-center align-items-center">
<div class="col-6">
<div class="card">
hello
</div>
</div>
</div>
<div class="row h-100 justify-content-center">
<div class="col-4">
<div class="card">
world
</div>
</div>
</div>
</div>
</body>
I wish I could just move the justify-content-center align-items-center up to the container, but alas that doesn't seem to do anything.
So, please help me, how can I place these flush, one on top of the other, smack in the middle of the screen?
Here's a good way to do this using Bootstrap's build in flexboxes.
<div class="container d-flex h-100 flex-column">
<div class="flex-grow-1"></div>
<div class="row justify-content-center">
<div class="col-3">
<div class="card">ITEM A</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-3">
<div class="card">ITEM B</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-3">
<div class="card">ITEM C</div>
</div>
</div>
<div class="flex-grow-1"></div>
</div>
We have two buffer flex rows, with flex-grow-1. Each of these rows will expand (with the same weight) to fill the top and bottom portions equally. Each of the content rows in the middle will have the height of their content.
The end result is the ITEM cards centered both vertically and horizontally on the screen. Because they are within individual rows, you can adjust margins/padding as necessary (by default they cards end up next to each other vertically.
Example screenshot of the above code in bootstrap
You could potentially do this one with one row + column and an internal flexbox, but that gives you less control over how the individual cards appear in relation to each other.
EDIT: d-flex makes the container a flex container around it's child rows, and flex-column makes the children render and stretch vertically rather than horizontally.
Simplest solution is to make the rows 50% height instead...
<body style="height: 100vh;">
<div class="container h-100">
<div class="row h-100 justify-content-center align-items-center">
<div class="col-6">
<div class="card">
hello
</div>
</div>
</div>
<div class="row h-100 justify-content-center align-items-center">
<div class="col-4">
<div class="card">
world
</div>
</div>
</div>
</div>
</body>
Demo: https://www.codeply.com/go/7POJtgNaQI
Or, the flexbox method would be to use Bootstrap flex-grow-1 class. Also you don't need the height on the body. Just use min-vh-100 on the container...
<div class="container d-flex flex-column min-vh-100">
<div class="row flex-grow-1 justify-content-center align-items-center">
<div class="col-6">
<div class="card">
hello
</div>
</div>
</div>
<div class="row flex-grow-1 justify-content-center align-items-center">
<div class="col-4">
<div class="card">
world
</div>
</div>
</div>
</div>
https://www.codeply.com/go/n1eL2Jakuo
Always give the height through the inner element height, If I will give the height on container class element, it won't work, so let the container depend on the inner element's height, So try like this.
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container mt-3">
<div class="col-12" style="min-height: 180px;">
<div
class="row bg-success position-absolute w-100 h-100 d-flex flex-row justify-content-center align-items-center">
<div class="col-6 pr-2">
<div class="card text-center">
hello
</div>
</div>
<div class="col-6 pl-2">
<div class="card text-center">
world
</div>
</div>
</div>
</div>
</div>
Hope you will understand this tiny mess, and so it would help you.
Related
i wanna ask something about display flex
<div class="container mx-auto">
<div class="d-flex">
<div class="bg-red-300 w-100 mr-3">halo</div>
<div class="bg-red-300 w-50 mr-3">halo</div>
</div>
</div>
how to display flex can manage width flex item with width 100% and 50% ?
Thankyou :)
In BS, if you want to have that kind of 2:1 ratio of width of elements, you'd have to use the grid system. Your above code will turn into:
<div class="container mx-auto">
<div class="row">
<div class="col-8 bg-red-300 mr-3">halo</div>
<div class="col-4 bg-red-300 mr-3">halo</div>
</div>
</div>
Here's how to use Flex's Bootstrap,
The 'col' has to be used to give the width of the box
<div class="container">
<div class="row">
<div class="col-6 bg-red-300">halo</div>
<div class="col-6 bg-red-300">halo</div>
</div>
</div>
enter image description here
grid break space between 2 column.
<section>
<div class="container mt-2">
<div class="row justify-content-around">
<div class="col-md-8 bg-light justify-content-around">
<h1>Helo</h1>
</div>
<div class="col-md-4 bg-light">
<h1>Hello</h1>
</div>
</div>
</div>
Anyone help to break two grid column.
You need to use display inline-block for having the divs in a single row.
Try
.justify-content-around div {
display:inline-block
}
If you mean by " the space " the padding added to each div in your row.
Then you can remove it by adding the class no-gutters to the row div.
<div class="row justify-content-around no-gutters">
and the space between the two divs is now removed.
if that is not what you mean please provide a screenshot of your desired result.
<section>
<div class="container mt-2">
<div class="row justify-content-around">
<div class="col-md-6 bg-light">
<h1>Helo</h1>
</div>
<div class="col-md-6 bg-light">
<h1>Hello</h1>
</div>
</div>
</div>
This question already has answers here:
Center flex item in container, when surrounded by other flex items
(2 answers)
Closed 4 years ago.
With utilising d-flex, justify-content-center and flew-grow-1, I achieved the desired result with 3 divs.
<div class="row bg-primary d-flex justify-content-center">
<div class="col-auto">
LEFT
</div>
<div class="col-auto flex-grow-1 text-center">
Middle
</div>
<div class="col-auto">
Right
</div>
</div>
I want to achieve the same effect with 2 divs while Middle one is forced to be the exact center. However, if I try to remove the 'Right' element, it can't calculate the center.
<div class="row bg-primary d-flex justify-content-center">
<div class="col-auto">
LEFT
</div>
<div class="col-auto flex-grow-1 text-center">
Middle
</div>
</div>
Is there a way to force "Middle" to be exact center and Left or Right divs take space from Middle as they need? Kind of like all the positions of Left and Middle are same as in the first image but 'Right' div is not there.
Fiddle http://jsfiddle.net/4a7tLf0m/
I can achieve the same effect with giving specified column size (col-3 instead of col-auto) but I don't like the fact that I need to be explicit. Is this the only way to go?
Keep the right/left div as invisible when not required. That way middle will always occupy the exact same position as when the other 2 divs are not there.
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="container">
<div class="row bg-primary d-flex justify-content-center">
<div class="col-auto">
LEFT
</div>
<div class="col-auto flex-grow-1 text-center">
Middle
</div>
<div class="col-auto">
Right
</div>
</div>
<hr/>
<div class="row bg-primary d-flex justify-content-center">
<div class="col-auto">
LEFT
</div>
<div class="col-auto flex-grow-1 text-center">
Middle
</div>
<div class="col-auto invisible">
Right
</div>
</div>
<hr/>
<div class="row bg-primary d-flex justify-content-center">
<div class="col-auto invisible">
LEFT
</div>
<div class="col-auto flex-grow-1 text-center">
Middle
</div>
<div class="col-auto">
Right
</div>
</div>
</div>
I have looked at the documentation of boostrap 4.
To center the content horizontally we could use justify-content-center class.
for example
i have 4 sub elements in a div like images and text, i want to center them all at once but its not working using above class. is it achievable?
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="content col-12 col-sm-6 d-flex justify-content-center ">
<div class="photo-wrapper">
<img src="#" alt="">
</div>
<div class="text-wrapper">
<div class="title">
Title
</div>
<div class="position">
Position
</div>
<div class="phones">
Phone
</div>
</div>
</div>
<div class="col-12 col-sm-6 p-0 d-flex justify-content-center justify-content-sm-start">
See profile
</div>
It should look like below image but it doesn't.
Image link
You have to use it in a row class, for instance:
<div class="row justify-content-center">
<div class="col-md-2"></div>
<div class="col-md-2"></div>
<div class="col-md-2"></div>
</div>
I have a simple table-esque layout in Bootstrap 3 as such (it's a "property grid):
<div class="row">
<div class="col-md-3">
<div id="propGrid">
<div class="row pgTable">
<div class="col col-md-12">
<div class="row pgGroupRow">
<div class="col col-md-12 pgGroupCell">Editor</div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">Font<span class="pgTooltip" title="The font editor to use">[?]</span></div>
<div class="col col-md-7 pgCell"><input type="text" id="pg0font" value="Consolas" <="" input=""></div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">Font size</div>
<div class="col col-md-7 pgCell"><span class="ui-spinner ui-corner-all ui-widget ui-widget-content"><input type="text" id="pg0fontSize" value="14" style="width:50px" aria-valuemin="0" aria-valuemax="20" aria-valuenow="14" autocomplete="off" class="ui-spinner-input" role="spinbutton"><a tabindex="-1" aria-hidden="true" class="ui-spinner-button ui-spinner-up ui-corner-tr"></a><a tabindex="-1" aria-hidden="true" class="ui-spinner-button ui-spinner-down ui-corner-br"></a></span></div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">Font color</div>
<div class="col col-md-7 pgCell"><input type="text" id="pg0fontColor" value="#a3ac03" <="" input=""></div>
</div>
<div class="row pgGroupRow">
<div class="col col-md-12 pgGroupCell">Plugins</div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">jQuery<span class="pgTooltip" title="Whether or not to include jQuery on the page">[?]</span></div>
<div class="col col-md-7 pgCell"><input type="checkbox" id="pg0jQuery" value="jQuery" checked=""></div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">modernizr<span class="pgTooltip" title="Whether or not to include modernizr on the page">[?]</span></div>
<div class="col col-md-7 pgCell"><input type="checkbox" id="pg0modernizr" value="modernizr"></div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">Framework<span class="pgTooltip" title="Whether to include any additional framework">[?]</span></div>
<div class="col col-md-7 pgCell"><select id="pg0framework"><option value="None">None</option><option value="angular" selected="">AngularJS</option><option value="backbone">Backbone.js</option></select></div>
</div>
<div class="row pgGroupRow">
<div class="col col-md-12 pgGroupCell">Other</div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">iHaveNoMeta</div>
<div class="col col-md-7 pgCell"><input type="text" id="pg0iHaveNoMeta" value="Never mind..." <="" input=""></div>
</div>
<div class="row pgRow">
<div class="col col-md-5 pgCell">I am read only</div>
<div class="col col-md-7 pgCell"><label for="pg0iAmReadOnly" title="Label types use a label tag for read-only properties">I am a label which is not editable</label></div>
</div>
</div>
</div>
</div>
</div>
</div>
I've been trying with no success to vertically center the contents of the left column against the height of the right column.
Check out the Code pen at
http://codepen.io/anon/pen/rrvNaV
and take a look at the last row in particular.
I've tried adjusting the padding, but since the content of the right column has variable height, that doesn't seem like a good solution. The line-height adjustment I usually use seem to fail for similar reasons.
Any ideas on how to vertically center the contents of both columns, given it's row height (row height being determined by the content height in either column or a particular row)?
Vertically centering float-based content is very problematic. There are a handful of hacks to do it (using transformations or negative bottom margins), but it is far easier to use display table or flexbox for the layout.
Change the row to flexbox like this:
.pgRow {
background:yellow;
display: flex; /* make the row a flex container */
align-items: center; /* vertically center each flex item in the container */
}
Be sure to remove the height from the .va! Generally, you should not set heights on containers in CSS; doing so will only introduce a load of other problems.
For more options, check out http://howtocenterincss.com/. To get a handle on flexbox, check out this article on CSS Tricks. To really get a handle on all these things and how they work together (shameless plug alert), checkout my book.
I added align-items-center to my row class.
Example:
<div class="container">
<div class="row align-items-center">
<div class="col">
One of three columns
</div>
<div class="col">
One of three columns
</div>
<div class="col">
One of three columns
</div>
</div>
</div>
The example's from the Bootstrap docs here