I am trying to center a div within its parent div like so:
<div id="wrapper">
<div id="yourdiv">
<div id='one'>1</div>
<div id='two'>2</div>
<div id='one'>1</div>
<div id='two'>2</div>
<div id='one'>1</div>
<div id='two'>2</div>
</div>
</div>
#wrapper {
background-color: green;
text-align: center;
}
#yourdiv {
background-color: red;
display: inline-block;
}
#yourdiv > div
{
float: left;
width: 200px;
}
#one {
background-color: blue;
}
#two {
background-color: brown;
}
This works fine when the child div does not 'wrap' as you can see below.
However, once there are too many child elements to fit the page width, the elements wrap to a second row and 'yourdiv' fills its parent width like so:
The child elements are no longer centered on the page because 'yourdiv' now fills its parents width. How can I fix it so that 'yourdiv' shrinks its width to its content so that the child elements are centred no matter how many there are?
EDIT:
The desired result is:
I recommend using flexbox.
To center your elements horizontally use
display: flex;
justify-content: center;
flex-wrap: wrap;
on the container.
Play around with flex-grow, min-width and max-width on the children to tune your layout.
Turn your css to flex and it's too much easier. Your just have to set #yourdiv flex-wrap: wrap; to make it wrap when overflow and justify-content: center; to center child element:
#wrapper {
background-color: green;
text-align: center;
}
#yourdiv {
background-color: red;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
#yourdiv>div {
float: left;
width: 200px;
}
#one {
background-color: blue;
}
#two {
background-color: brown;
}
<div id="wrapper">
<div id="yourdiv">
<div id='one'>1</div>
<div id='two'>2</div>
<div id='one'>1</div>
<div id='two'>2</div>
<div id='one'>1</div>
<div id='two'>2</div>
</div>
</div>
You should use Flexbox for those cases, you should read those good articles:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
and play this game to understand Flexbox while playing a game:
https://flexboxfroggy.com/
#wrapper {
background-color: green;
text-align: center;
}
#yourdiv {
background-color: red;
display: flex;
justify-content: center;
}
#yourdiv > div {
min-width: 100px;
}
.one {
background-color: blue;
}
.two {
background-color: brown;
}
<div id="wrapper">
<div id="yourdiv">
<div class="one">1</div>
<div class="two">2</div>
<div class="one">1</div>
<div class="two">2</div>
<div class="one">1</div>
<div class="two">2</div>
<div class="one">1</div>
<div class="two">2</div>
<div class="one">1</div>
<div class="two">2</div>
</div>
</div>
Related
I stumbled upon an issue with image centering within a flexbox with direction:column.
Imagine you have two elements within a flexbox, where the first one contains an image:
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
.image-container {
flex: 1;
align-self: center;
.img {
height: 100%;
}
}
.another-flex-child {
flex: none;
background-color: red;
}
}
I would expect the image to be center horizontally within the div, but it appears the left border of the image is exactly at the center of the div.
When I replace the image with another div which contains some text it is placed as expected.
Can anybody explain to me whats happening there?
Checkout this fiddle
Because your <div> that contains the image (and has align-self: center on it) is by default a block-level element, and has a width of 100% by default. As such, it is constrained in relation to the parent.
In order to have your image centered correctly, you'll want to add display: contents with:
container .image-container {
display: inline;
}
This can be seen in the following:
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.container .image-container {
flex: 1;
align-self: center;
display: contents;
}
.container .image-container .img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
The issue is that you are using an SVG with no intrinsic dimension and only an intrinsic ratio so it's like your image has a width equal to 0 which make its centred container with a width equal to 0, too.
Here is before using height:100%
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
border:2px solid blue;
}
.img {
/*height: 100%;*/
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
After setting height:100% the image will fill all the space and will keep its ratio but you will have an overflow because the browser will not go back to calculate the width of the container again:
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
border:2px solid blue;
}
.img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" >
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
To avoid this give the image a width and make sure to add min-height:0 to the container to allow it to shrink
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
border:2px solid blue;
min-height:0;
}
.img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" width="250">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
If you were initially using an image with intrinsic dimension you won't have this issue and you don't need to define a width. You will only need to add min-height:0 to avoid the overflow:
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
border:2px solid blue;
min-height:0;
}
.img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://picsum.photos/id/1/400/400">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
Note that the above doesn't work the same way in Firefox and you will need to add text-aling:center to make sure it works the same everywhere:
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
border:2px solid blue;
text-align:center;
min-height:0;
}
.img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://picsum.photos/id/1/400/400">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
You will notice that the difference is related to the width calculation of the container which a bit complex due to the use of height:100%
Things may get worse if the size of the image is very small:
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
border:2px solid blue;
text-align:center;
min-height:0;
}
.img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://picsum.photos/id/1/50/50">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
In Firefox text-align:center will do nothing and you may need a nested flexbox container
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
justify-content:center;
border:2px solid blue;
display:flex;
min-height:0;
}
.img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://picsum.photos/id/1/50/50">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
The below is almost the same issue you were having with the initial SVG that can fixed with this same code but it won't remove the overflow:
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
display:flex;
justify-content:center;
border:2px solid blue;
}
.img {
height: 100%;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" >
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
Another intresting thing to note is that your initial code may work fine if you add height:100% to the container making the calculation of the nested height easier:
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
}
.image-container {
flex: 1;
align-self: center;
border:2px solid blue;
box-sizing:border-box;
height:100%;
}
.img {
height: 100%;
display:block;
}
.another-flex-child {
flex: none;
background-color: red;
}
.spacer {
height: 20px;
}
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" >
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
<div class="spacer"></div>
<div class="container">
<div class="image-container">
<div>Properly centered content</div>
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
Add the justify-content like below:
.image-container {
flex: 1;
align-self: center;
justify-content:center;
}
it should works
How to create two containers with equal width even if there are a lot of flex items inside one of them? I could add overflow hidden to both containers but it seems to be workaround rather than solution of the problem
<div class="parent">
<div class="child1">
blabla
</div>
<div class="child2">
<div class="container">
<div class="subcontainer">aaaaaaaaaaaaaaaaaaaaaaa</div>
<div class="subcontainer">bbbbbbbbbbbbbbbbbbbbbbb</div>
<div class="subcontainer">ccccccccccccccccccccccc</div>
<div class="subcontainer">dwadawdwdaadwawadawddwaw</div>
<div class="subcontainer">dddddddddddddddddddddddd</div>
<div class="subcontainer">eeeeeeeeeeeeeeeeeeeeeeee</div>
</div>
</div>
</div>
.parent {
display: flex;
}
.child1, .child2 {
flex: 1;
flex-basis: 50%;
/* overflow: hidden */
}
.container {
display: flex;
}
.child1 {
border: 1px solid red;
}
.child2 {
border: 1px solid green;
}
https://jsfiddle.net/0e7n6g8b/
Simply add flex-wrap:wrap; to your container div.
.container {
display: flex;
flex-wrap:wrap;
}
Thanks
I am trying to design a section which 3 image. I can get the two images to display by block easily. I can float the third image to the right and adjust the height easily. However my issue is it does not align side by side.Below is an example of what I am trying to achieve
This is an example of what I have so far
.image-one,
.image-two {
width: 250px;
background-color: green;
display: block;
margin-top: 10px;
}
.image-three {
float: right;
background-color: lightblue;
width: 250px;
height: 200px;
}
<div class="container">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
<div class="image-three"> Image three </div>
</div>
You should be able to simple add flex to the container, and then add the content within a left and a right div.
Here's a working example:
.container {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.image-one,
.image-two {
width: 250px;
height: 95px;
background-color: green;
margin-bottom: 10px;
}
.image-three {
background-color: lightblue;
width: 240px;
height: 200px;
margin-left: 10px;
}
<div class="container">
<div class="left">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
</div>
<div class="right">
<div class="image-three"> Image three </div>
</div>
</div>
You can use flexbox for this:
.container {
display: flex;
flex-direction: column; /* align items in columns */
flex-wrap: wrap; /* wrap to a new column when height is reached */
justify-content: space-between; /* add spacing in between top and bottom image */
height: 210px; /* height of your 2 images plus and spacing you want */
width: 510px; /* width of 2 columns plus any spacing */
}
.image-one,
.image-two {
width: 250px;
height: 100px;
background-color: green;
display: block;
}
.image-three {
background-color: lightblue;
width: 250px;
height: 210px; /* I would make this image the height of the other 2 plus spacing */
align-self:flex-end; /* align this to the right of the container */
}
<div class="container">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
<div class="image-three"> Image three </div>
</div>
Maybe you can add some internal divs like this:
<div class="container">
<div class="container-left">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
</div>
<div class="container-right">
<div class="image-three"> Image three </div>
</div>
</div>
Then, add css to container-left and container-right to properly set the width and the float. Like this:
.container-left, .container-right{
width:250px;
float:left;
}
Why don't you make use of bootstrap columns?
HTML
<div class="container">
<div class="row main-row">
<div class="col-6 left-col">
<div class="row left-col-top">
<!-- Top left image here -->
</div>
<div class="row left-col-bottom">
<!-- Bottom left image here -->
</div>
</div>
<div class="col-6 right-col">
</div>
</div>
</div>
CSS
.main-row {
height:300px;
}
.left-col-top {
background-color:blue;
height:50%;
}
.left-col-bottom {
background-color:red;
height:50%;
}
.right-col {
background-color:green;
height:100%;
}
Easy flexbox solution :)
#main, #left {
display:flex;
}
#left {
flex-direction: column;
flex: 1;
}
.section {
flex: 1;
margin: 2px;
background-color: green;
}
<div id="main">
<div id="left">
<div class="section">Hello</div>
<div class="section">Hello</div>
</div>
<div id="right" class="section">Hello</div>
</div>
I want to create one flex container, which has 3 child items. However, I want two of the child items to be columns, and the third one to be a row which runs below of the the columns (like a footer). Is it possible?
This is probably best achieved by using two divs, one flex-box, and one that isn't: see below:
#wrapper {
display: flex;
background-color: lightblue;
}
#a {
background-color: lightgreen;
}
#b {
background-color: lightgray;
}
#c {
background-color: lightyellow;
}
<div id="wrapper">
<div id="a">This is a</div>
<div id="b">This is b</div>
</div>
<div id="c">This is c</div>
You can do something like THIS
.container {
display: flex;
flex-direction: column;
width: 100%;
}
.col-container {
display: flex;
flex-direction: row;
}
.col {
margin: 10px;
height: 200px;
width: 200px;
background-color: blue;
}
.row {
height: 100px;
width: 100%;
background-color: red;
}
<div class="container">
<div class="col-container">
<div class="col"></div>
<div class="col"></div>
</div>
<div class="row"></div>
</div>
Is there a more flexbox-ish way to right-align "Contact" than to use position: absolute?
.main {
display: flex;
}
.a,
.b,
.c {
background: #efefef;
border: 1px solid #999;
}
.b {
flex: 1;
text-align: center;
}
.c {
position: absolute;
right: 0;
}
<h2>With title</h2>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<h2>Without title</h2>
<div class="main">
<div class="a">Home</div>
<!--<div class="b">Some title centered</div>-->
<div class="c">Contact</div>
</div>
http://jsfiddle.net/vqDK9/
A more flex approach would be to use an auto left margin (flex items treat auto margins a bit differently than when used in a block formatting context).
.c {
margin-left: auto;
}
Updated fiddle:
.main { display: flex; }
.a, .b, .c { background: #efefef; border: 1px solid #999; }
.b { flex: 1; text-align: center; }
.c {margin-left: auto;}
<h2>With title</h2>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<h2>Without title</h2>
<div class="main">
<div class="a">Home</div>
<!--<div class="b">Some title centered</div>-->
<div class="c">Contact</div>
</div>
<h1>Problem</h1>
<p>Is there a more flexbox-ish way to right align "Contact" than to use position absolute?</p>
Here you go. Set justify-content: space-between on the flex container.
.main {
display: flex;
justify-content: space-between;
}
.a, .b, .c { background: #efefef; border: 1px solid #999; }
.b { text-align: center; }
<h2>With title</h2>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<h2>Without title</h2>
<div class="main">
<div class="a">Home</div>
<!-- <div class="b">Some title centered</div> -->
<div class="c">Contact</div>
</div>
You can also use a filler to fill the remaining space.
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="filler"></div>
<div class="c">Contact</div>
</div>
.filler{
flex-grow: 1;
}
I have updated the solution with 3 different versions. This because of the discussion of the validity of using an additional filler element. If you run the code snipped you see that all solutions do different things. For instance setting the filler class on item b will make this item fill the remaining space. This has the benefit that there is no 'dead' space that is not clickable.
<div class="mainfiller">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="filler"></div>
<div class="c">Contact</div>
</div>
<div class="mainfiller">
<div class="a">Home</div>
<div class="filler b">Some title centered</div>
<div class="c">Contact</div>
</div>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<style>
.main { display: flex; justify-content: space-between; }
.mainfiller{display: flex;}
.filler{flex-grow:1; text-align:center}
.a, .b, .c { background: yellow; border: 1px solid #999; }
</style>
Or you could just use justify-content: flex-end
.main { display: flex; }
.c { justify-content: flex-end; }
If you want to use flexbox for this, you should be able to, by doing this (display: flex on the container, flex: 1 on the items, and text-align: right on .c):
.main { display: flex; }
.a, .b, .c {
background: #efefef;
border: 1px solid #999;
flex: 1;
}
.b { text-align: center; }
.c { text-align: right; }
...or alternatively (even simpler), if the items don't need to meet, you can use justify-content: space-between on the container and remove the text-align rules completely:
.main { display: flex; justify-content: space-between; }
.a, .b, .c { background: #efefef; border: 1px solid #999; }
Here's a demo on Codepen to allow you to quickly try the above.
As easy as
.main {
display: flex;
flex-direction:row-reverse;
}
margin-left: auto works well. But clean flex box solution would be space-between in the main class. Space between works well if there is two or more elements. I have added a solution for single element as well.
.main { display: flex; justify-content: space-between; }
.a, .b, .c { background: #efefef; border: 1px solid #999; padding: 0.25rem; margin: 0.25rem;}
.b { flex: 1; text-align: center; }
.c-wrapper {
display: flex;
flex: 1;
justify-content: flex-end;
}
.c-wrapper2 {
display: flex;
flex: 1;
flex-flow: row-reverse;
}
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<div class="main">
<div class="a">Home</div>
<div class="c">Contact</div>
</div>
<div class="main">
<div class="c-wrapper">
<a class="c" href="#">Contact</a>
<a class="c" href="#">Contact2</a>
</div>
</div>
<div class="main">
<div class="c-wrapper2">
<span class="c">Contact</span>
<span class="c">Contact2</span>
</div>
</div>
Add the following CSS class to your stylesheet:
.my-spacer {
flex: 1 1 auto;
}
Place an empty element between the element on the left and the element you wish to right-align:
<span class="my-spacer"></span>
If you need one item to be left aligned (like a header) but then multiple items right aligned (like 3 images), then you would do something like this:
h1 {
flex-basis: 100%; // forces this element to take up any remaining space
}
img {
margin: 0 5px; // small margin between images
height: 50px; // image width will be in relation to height, in case images are large - optional if images are already the proper size
}
Here's what that will look like (only relavent CSS was included in snippet above)
'justify-content: flex-end' worked within price box container.
.price-box {
justify-content: flex-end;
}
For those using Angular and Flex-Layout, use the following on the flex-item container:
<div fxLayout="row" fxLayoutAlign="flex-end">
See fxLayoutAlign docs here and the full fxLayout docs here.
I find that adding 'justify-content: flex-end' to the flex container solves the problem while 'justify-content: space-between' doesnt do anything.
Example code based on answer by TetraDev
Images on right:
* {
outline: .4px dashed red;
}
.main {
display: flex;
flex-direction: row;
align-items: center;
}
h1 {
flex-basis: 100%;
}
img {
margin: 0 5px;
height: 30px;
}
<div class="main">
<h1>Secure Payment</h1>
<img src="https://i.stack.imgur.com/i65gn.png">
<img src="https://i.stack.imgur.com/i65gn.png">
</div>
Images on left:
* {
outline: .4px dashed red;
}
.main {
display: flex;
flex-direction: row;
align-items: center;
}
h1 {
flex-basis: 100%;
text-align: right;
}
img {
margin: 0 5px;
height: 30px;
}
<div class="main">
<img src="https://i.stack.imgur.com/i65gn.png">
<img src="https://i.stack.imgur.com/i65gn.png">
<h1>Secure Payment</h1>
</div>