I am trying to vertically align two divs in a parent div.
The vertical align is straightforward, but I am also trying float the divs, one left and one right.
Is this possible to do?
.outer {
background: red;
height: 300px;
display: flex;
align-items: center;
}
.inner_right {
background: blue;
float: right;
}
.inner_left {
background: yellow;
float: left;
}
<div class="outer">
<div class="inner_right">
RIGHT MIDDLE
</div>
<div class="inner_left">
LEFT MIDDLE
</div>
</div>
https://jsfiddle.net/xh8rbnmh/
body { margin: 0; }
.outer {
display: flex;
align-items: center;
justify-content: space-between; /* 1 */
background: red;
height: 300px;
}
.inner_right {
order: 1; /* 2 */
/* float: right; */ /* 3 */
background: aqua;
}
.inner_left {
/* float: left; */ /* 3 */
background: yellow;
}
<div class="outer">
<div class="inner_right">RIGHT MIDDLE</div>
<div class="inner_left">LEFT MIDDLE</div>
</div>
methods for aligning flex items on main axis
the flex order property can move elements around the screen
floats are ignored in a flex formatting context
Simple. You need to put your left div first in the markup. Then simply add margin: auto to the right div.
Note that if you need to retain the original markup (with the right div first, then the left div), flexbox allows you to order the divs using the intuitive order: property on each div.
I've updated the fiddle here: https://jsfiddle.net/v6facjnp/4/
This is alternative solution not using flexbox - noticed that margin has to be height of element.
.outer {
background: red;
height: 300px;
position:relative;
}
.inner_right {
background: blue;
position:absolute;
right:0px;
top:50%;
margin-top: -18px;
}
.inner_left {
background: yellow;
position:absolute;
top:50%;
left:0px;
margin-top: -18px;
}
First lets fix, some: left at the left, right at the right
<div class="outer">
<div class="inner_left">
LEFT MIDDLE
</div>
<div class="inner_right">
RIGHT MIDDLE
</div>
</div>
Second: Flex makes the elements to behave like blocks, discarding the float property. So we use margins and justify
.outer {
background: red;
height: 300px;
display: flex;
align-items: center;
justify-content:flex-end; //Move all items to the right
}
.inner_right {
background: blue;
}
.inner_left {
background: yellow;
margin-right:auto;//Move this to the left
}
Related
I have to align two contents in div as per below screenshot. I do not want to use flex as I am completely new to it. I have bootstrap3 library in my project
first content exactly at center
second content should be at extreme right. very minimum space at right corner will be ok.
I checked, float:right but it is not aligning second content at extreme right. how to fix?
Use display: flex and add invisible element. Then use justify-content: space-between; to align the items one on the left, one center and one on the right.
If you need vertical center use align-items: center;
Learn more on flex here. The bootstrap documentation does great job visualizing more configurations.
.parent {
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid black;
width: 200px;
height: 40px;
}
.left-child {
visibility: hidden;
}
.center-child {
background: red;
width: 10px;
height: 20px;
}
.right-child {
background: red;
width: 10px;
height: 20px;
}
<div class="parent">
<div class="left-child"></div>
<div class="center-child"></div>
<div class="right-child"></div>
</div>
In my answer, you have to change the margin-top of the right content as the font-size or height of the center content
<style type="text/css">
.center{
text-align:center;
margin:auto;
display:block;
}
.right{
float:right;
margin-top:-18px;
}
</style>
<span class="center">Center</span>
<span class="right">Right</span>
I'd like to create a responsive page with a fixed html structure so I can just adjust the css. I would like to create rows with vertically and horizontally centered texts. The divs should fully stretch across the parent div.
My HTML...
<body>
<div class="parent">
<div class="d1">
one
</div>
<div class="d2">
two
</div>
<div class="d3">
three
</div>
</div>
</body>
My CSS...
body {
background-color: lightyellow;
height: 100%;
width: 100%;
margin: 0px;
}
.parent {
background-color: lightblue;
height: 100%;
}
.d1, .d2, .d3 {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
height: 100px;
}
.d2 {
background-color: lightgreen;
}
However here I am setting d1, d2 and d3 to the height of 100px and not 100% of the parent div. Example here: https://jsfiddle.net/bLf2sxq0/
My second idea was to use display: table for the parent which results in table-rows for the childs but then I end up with the same stretching issue plus the texts are not vertically centered. Here the css would be like this ...
body {
background-color: lightyellow;
height: 100%;
width: 100%;
margin: 0px;
}
.parent {
width: 100%;
height: 100%;
display: table;
background-color: lightblue;
}
.d1, .d2, .d3 {
text-align: center;
height: 100px;
}
.d2 {
background-color: lightgreen;
}
Example here: https://jsfiddle.net/qmbzkwr2/
Is there a way to stretch the divs vertically along the parent and keep the texts centered vertically and horizontally within the divs? So I would not have width 100px but something like calc(100%/3) or any other solution to do this? Or maybe by using the flex grow option? Easiest way would do it :)
Thanks for any help!
You're on the right track. Use flexbox to stretch and fill items vertically and evenly. Remember to set parent containers (e.g. body, html) to height: 100%.
From here, if you want control over some items, use flex on any individual item, like flex: 1 1 300px on class .d2 for example.
Codepen
body, html {
height: 100%;
}
.parent {
background-color: lightblue;
height: 100%;
display: flex;
flex-direction: column;
}
.d1, .d2, .d3 {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
}
.d2 {
background-color: lightgreen;
}
<div class="parent">
<div class="d1">
<div class="d11">
one
</div>
</div>
<div class="d2">
two
</div>
<div class="d3">
three
</div>
</div>
I am building on the question originally asked here How to center horizontal table-cell with a slight modification.
Basically, DIVs need to be centered as they are now, however, I also need to vertically align all the content in the cell in the middle.
Changing vertical-align: middle; for .column does NOTHING. If I change display: inline-block; for .column to display: table-cell, it will align content in the middle, but then .column DIVs are no longer centered and widths are all broken (currently all a evenly set to 25%). Setting margin:auto; or text-align on parent does nothing.
I've been running around this for days. Your help is appreciated.
/* Setting the container to be a table with maximum width and height */
#container {
display: table;
height: 100%;
width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */
.section {
display: table-row;
min-height: 1px;
}
/* We need one extra container, setting it to full width */
.columns-container {
display: table-cell;
height: 100%;
width:100%;
text-align: center;
}
/* Creating columns */
.column {
display: inline-block;
vertical-align: middle;
min-height: 150px;
width: 25%;
text-align: left;
}
#a {
background-color: pink;
}
#b {
background-color: lightgreen;
}
#c {
background-color: lightblue;
}
<div id="container">
<div class="section">
<div class="columns-container">
<div class="column" id="a"> Contents A </div>
<div class="column" id="b"> Contents B </div>
<div class="column" id="c"> Contents C </div>
</div>
</div>
</div>
You could do it like the follows, it uses CSS3 Transforms, see the browser support details. And be aware of the white spaces thing on inline block.
JsFiddle demo
.container {
text-align: center;
margin: 0 auto;
}
.column {
display: inline-block;
width: 150px;
height: 150px;
}
.column > div {
position: relative;
top: 50%;
transform: translateY(-50%);
}
#a { background-color: pink; }
#b { background-color: lightgreen; }
#c { background-color: lightblue; }
<div class="container">
<div class="column" id="a"><div>Contents A</div></div>
<div class="column" id="b"><div>Contents B</div></div>
<div class="column" id="c"><div>Contents C</div></div>
</div>
setting your .column's line-height to the height of the element is step one; so: line-height:150px vertically aligns the content.
Then, simply edit the text-align:left style declaration you have set on .column to text-align:center finishes the vertically alignment in this case.
here's a fiddle:
http://jsfiddle.net/jalbertbowdenii/t2xgL3rm/
<!DOCTYPE html>
<html>
<head>
<title>Width issue</title>
<style type="text/css">
body {
margin: 0;
}
#left {
width: 50%;
background: lightblue;
display: inline-block;
}
#right {
width: 50%;
background: orange;
display: inline-block;
}
</style>
</head>
<body>
<div id="left">Left</div>
<div id="right">Right</div>
</body>
</html>
JSFiddle: http://jsfiddle.net/5EcPK/
The above code is trying to place the #left div and the #right div, side by side, in a single row. But as you can see in the above JSFiddle URL, this is not the case.
I am able to resolve the issue reducing the width of one of the divs to 49%. See http://jsfiddle.net/mUKSC/ . But this is not an ideal solution because a small gap appears between the two divs.
Another way I am able to solve the problem is by floating both the divs. See http://jsfiddle.net/VptQm/ . This works fine.
But my original question remains. Why when both the divs are kept as inline-block elements, they do not fit side by side?
Update: as it's 2021, use flexbox or even better - CSS grid layout instead of inline-block.
When using inline-block elements, there will always be an whitespace issue between those elements (that space is about ~ 4px wide).
So, your two divs, which both have 50% width, plus that whitespace(~ 4px) is more than 100% in width, and so it breaks. Example of your problem:
body{
margin: 0; /* removing the default body margin */
}
div{
display: inline-block;
width: 50%;
}
.left{
background-color: aqua;
}
.right{
background-color: gold;
}
<div class="left">foo</div>
<div class="right">bar</div>
There is a few ways to fix that:
1. No space between those elements
body{
margin: 0; /* removing the default body margin */
}
div{
display: inline-block;
width: 50%;
}
.left{
background-color: aqua;
}
.right{
background-color: gold;
}
<div class="left">foo</div><div class="right">bar</div>
2. Using HTML comments
body{
margin: 0; /* removing the default body margin */
}
div{
display: inline-block;
width: 50%;
}
.left{
background-color: aqua;
}
.right{
background-color: gold;
}
<div class="left">foo</div><!--
--><div class="right">bar</div>
3. Set the parents font-size to 0, and then adding some value to inline-block elements
body{
margin: 0; /* removing the default body margin */
}
.parent{
font-size: 0; /* parent value */
}
.parent > div{
display: inline-block;
width: 50%;
font-size: 16px; /* some value */
}
.left{
background-color: aqua;
}
.right{
background-color: gold;
}
<div class="parent">
<div class="left">foo</div>
<div class="right">bar</div>
</div>
4. Using a negative margin between them (not preferable)
body{
margin: 0; /* removing the default body margin */
}
div{
display: inline-block;
width: 50%;
margin-right: -4px; /* negative margin */
}
.left{
background-color: aqua;
}
.right{
background-color: gold;
}
<div class="left">foo</div>
<div class="right">bar</div>
5. Dropping closing angle
body{
margin: 0; /* removing the default body margin */
}
div{
display: inline-block;
width: 50%;
}
.left{
background-color: aqua;
}
.right{
background-color: gold;
}
<div class="left">foo</div
><div class="right">bar</div>
<hr>
<div class="left">foo</div><div class="right">
bar</div>
6. Skipping certain HTML closing tags (thanks #thirtydot for the reference)
body{
margin: 0; /* removing the default body margin */
}
ul{
margin: 0; /* removing the default ul margin */
padding: 0; /* removing the default ul padding */
}
li{
display: inline-block;
width: 50%;
}
.left{
background-color: aqua;
}
.right{
background-color: gold;
}
<ul>
<li class="left">foo
<li class="right">bar
</ul>
References:
Fighting the Space Between Inline Block Elements on CSS Tricks
Remove Whitespace Between Inline-Block Elements by David Walsh
How to remove the space between inline-block elements?
As #MarcosPĆ©rezGude said, the best way is to use rem, and add some default value to font-size on the html tag (like in HTML5Boilerplate). Example:
html{
font-size: 1em;
}
.ib-parent{ /* ib -> inline-block */
font-size: 0;
}
.ib-child{
display: inline-block;
font-size: 1rem;
}
good answer in css3 is:
white-space: nowrap;
in parent node, and :
white-space: normal;
vertical-align: top;
in div (or other) at 50%
exemple : http://jsfiddle.net/YpTMh/19/
EDIT:
there is another way with :
font-size: 0;
for parent node and override it in child node
EDIT 2021 : personaly, I recommand use flexbox now : https://the-echoplex.net/flexyboxes/
It's because the whitespace between your two divs is being interpreted as a space. If you put your <div> tags in line as shown below the problem is corrected:
<div id="left"></div><div id="right"></div>
Because there is a space between the elements. If you remove all whitespace, they will fit.
<div id="left">Left</div><div id="right">Right</div>
Either make them block instead of inline-block. This will render divs ignoring spaces between them.
display:block;
or remove space between tags
<div id='left'></div><div id='right'></div>
or add
margin: -1en;
to one of the divs in order to mitigate space taken by single space rendered.
Please check below code:
body {
margin: 0;
}
#left {
width: 50%;
background: lightblue;
display: inline-block;
float:left;
}
#right {
width: 50%;
background: orange;
display: inline-block;
float:left;
}
<div id="left">Left</div>
<div id="right">Right</div>
It can be done by adding the css display:inline to the div that holds the inline elements.
While removing the white space using margin with a negative value it becomes necessary to add it to this particular element. As adding it to a class will affect places where this class has been used.
So it would be safer to use display:inline;
Flexbox example - this would be used for the parent class holding the two side by side elements.
.parentclass {
display: flex;
justify-content: center;
align-items: center;
}
Taken from Vertically centering a div inside another div
add float: left; to both div tags.
div {
float: left;
}
I am trying to use display:inline-block to build 3 columns.
It works fine in the beginning, but when I add content to the first column it affects the rest of the layout and renders the rest of the columns at a lower level.
What can I do to avoid this?
.cont {
width: 500px;
height: 200px;
background: #666666;
}
.col {
display: inline-block;
width: 30%;
background: pink;
}
<div class="cont">
<div class="col">
test<br><br><br>
</div>
<div class="col">
col2
</div>
<div class="col">
col3
</div>
</div>
You should add vertical-align: top; CSS declaration to align the columns vertically at the top:
.cont span {
display: inline-block;
vertical-align: top; /* Vertically align the inline-block elements */
height:100%;
line-height: 100%;
width: 33.33%; /* Just for Demo */
outline: 1px dashed red; /* Just for Demo */
}
Here is a online demo.
Honestly, I'm not a fan of using inline-block to create columns on the page, because of the white spaces between them.
The float was being used for a while, but nowadays flex box or CSS grid can be an option.
You just have to set a width of 33% on the the column, this will constraint it to take up 33% of the entire width of the div.
http://jsfiddle.net/Ge6g7/1/
.cont {
height:60px;
background: #ffff88;
}
.cont span {
display: inline-block;
height:100%;
line-height: 100%;
width: 33%; /* Added Css */
}