Is it possible to use display: flex; to align all items to the left (flex-wrap: wrap; align-items: flex-start; justify-content: flex-start;) but the container it self to the center (like margin: 0 auto;)?
It seems like flex-containers are always scaled to 100% width, why the automatic margin won't work. Does anybody have an idea how to achieve what I try?
.container {
width: auto;
margin: 0 auto;
display: -webkit-flex;
display: -moz-flex;
display: -ms-flexbox; // IE10 uses -ms-flexbox
display: -ms-flex; // IE11
display: flex;
-webkit-flex-wrap: wrap;
-moz-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-justify-content: flex-start;
-moz-justify-content: flex-start;
-ms-justify-content: flex-start;
justify-content: flex-start;
-webkit-align-items: flex-start;
-moz-align-items: flex-start;
-ms-align-items: flex-start;
align-items: flex-start;
}
.item {
display: block;
width: 220px;
}
EDIT: Important! While the container has an auto width!
Yes, using text-align:center on a parent (or body) and display:inline-flex instead of display:flex.
This operates much the same as the difference between display:inline-block and display:block.
MDN says:
The element behaves like an inline element and lays out its content according to the flexbox model.
body {
background: #eee;
text-align: center;
}
.inner {
display: inline-flex;
width: auto;
/* stated but not required */
background: #ccc;
padding: 15px;
}
p {
padding: 15px;
border: 1px solid red;
}
<div class="inner">
<p>1</p>
<p>2</p>
<p>3</p>
</div>
For the parent, you can use display: inline-flex , which has the same effect as display: inline-block compared to display: block.
The flex won't claim the whole page width anymore.
You can find more information about flex here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Related
I have a parent CSS property which seems cant be overwritten by !important. what other options to I have? I'm trying to get rid of the margin-left: 30 property.
Div code
.difference-ul {
margin-left: 0 !important;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
The parent CSS property below which is applying to the above div class
.Rte ul, .Rte ol {
margin-left: 30px;
padding-left: 0;
list-style-position: outside;
}
Try using your code like this:
.Rte ul, .Rte ol .difference-ul {
margin-left: 0 !important;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
I'm trying to create a horizontal menu. My understanding of flexbox is that if I want to have elements stack horizontally, I should apply the CSS rules
display: flex
flex-direction: row;
However, in the following page, the boxes stack vertically
div#navcontainer {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
div.navitem {
color: #292929;
height: 45px;
width: 150px;
padding: 5px;
border-color: 292929;
border-width: 2px;
border-style: solid;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
<div id="header">
<div id="navcontainer">
<div class="navitem">home</div>
<div class="navitem">about</div>
<div class="navitem">services</div>
<div class="navitem">contact</div>
</div>
</div>
How do I make css flex-items which are themselves flex containers of other elements stack vertically?
Set display: flex to #navcontainer. You can also get it without flexbox. Use display: inline-block and percentage values for boxes.
Horizontal flex direction is a default direction, so flex-direction: row is superfluous. Right now you have a horizontal list which will contain vertical flex boxes.
#navcontainer {
display: flex;
flex-wrap: nowrap;
}
.navitem {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
I have a row of two elements inside of a flex container which are centered using the CSS properties -webkit-flex-flow: row wrap; and justify-content: space-around;. Above this row I want to have a div with text which is vertically aligned with the left most div in the row.
Is it possible to do this using only CSS with the requirement that the elements keep their display: flex; property?
Here is my html:
<div class="flex-container">
<div class="info-box">
</div>
<div class="type-one">
</div>
<div class="type-one">
</div>
</div>
and here is the css:
.flex-container {
padding: 0;
margin: 0;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: space-around;
}
.type-one{
width: 45%;
height: 50px;
background: tomato;
text-align: left;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: flex-start;
}
.info-box{
width: 100%;
height: 10px;
background: tomato;
margin-bottom: 3px;
}
Here is a fiddle example. You can see how the top row starts all the way from the left (since it has flex-start alignment), but I want it to start at the location where the leftmost element in the second row starts. Is this possible with the given requirements?
Edit: I realized that I can add a margin-left of 2.5% to the info-box or make its width 95%, but I would prefer a solution which is relative to the type-one elements so that if I change their width the info-box will automatically realign to them.
To have them align on the left edge, set the left/right margins of the parent element to match wherever you want the columns in the middle to start. Change justify-content from space-around to space-between so that the left spacing of the middle columns won't change, and use the width of those elements to create space between them.
.flex-container {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 0;
margin: 0;
width: 95%;
margin: auto;
}
.type-one{
height: 50px;
background: tomato;
text-align: left;
width: 47.5%;
}
.type-two{
width: 100%;
margin-top: 10px;
font-size: 15px;
text-align: center;
height: 20px;
background: tomato;
}
.info-box{
width: 100%;
height: 10px;
background: tomato;
margin-bottom: 3px;
}
<div class="flex-container">
<div class="info-box"></div>
<div class="type-one"></div><div class="type-one"></div>
<div class="type-two"></div>
</div>
I am having an issue where I am trying to use flex to show divs left, center, and right. Although I run into an issue where the center column isn't in-line with the div above it. When I change the flex to flex: 1, it does put each column in line but leaves an empty space to the right of my furthest right div. Can someone offer some advice or tips on how to correct this? I have seen similar questions about flex, but nothing the specifically addressed this concern. I have provided some of the code I am using currently. Thank you in advance!
.container {
display: flex;
width: 100%;
justify-content: space-between;
}
.item {
flex: 1;
}
<body>
<div class="container">
<div class="item">Hello World</div>
<div class="item">It is me</div>
<div class="item">BYE</div>
</div>
<div class="container">
<div class="item">Hello World, again!</div>
<div class="item">It is me, again?</div>
<div class="item">BYE</div>
</div>
</body>
You need to swap
justify-content: space-between;
for
justify-content: space-around;
Working Example:
.container {
display: flex;
justify-content: space-around;
}
.item {
flex: 1 1 33%;
margin: 6px;
padding: 6px;
color: rgb(255,255,255);
background-color: rgb(255,0,0);
font-weight: bold;
}
<div class="container">
<div class="item">Hello World</div>
<div class="item">It is me</div>
<div class="item">BYE</div>
</div>
<div class="container">
<div class="item">Hello World, again!</div>
<div class="item">It is me, again?</div>
<div class="item">BYE</div>
</div>
Please check the code. There is no empty space on right. padding: 10px for body and .container have margin-bottom: 30px; also .item have margin-bottom: 10px;. I think you need to learn more about the flex box.
body
{
margin: 0;
padding: 10px;
background-color: #898989;
}
.container
{
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
margin-bottom: 30px;
border: 2px solid #000;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-flex: 0;
-webkit-flex: 0 0 100%;
-moz-box-flex: 0;
-ms-flex: 0 0 100%;
flex: 0 0 100%;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: start;
-webkit-justify-content: flex-start;
-moz-box-pack: start;
-ms-flex-pack: start;
justify-content: flex-start;
}
.container .item
{
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
margin-bottom: 10px;
border: 5px solid #f0f;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-flex: 1;
-webkit-flex-grow: 1;
-moz-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
<body>
<div class="container">
<div class="item">Hello World</div>
<div class="item">It is me</div>
<div class="item">BYE</div>
</div>
<div class="container">
<div class="item">Hello World, again!</div>
<div class="item">It is me, again?</div>
<div class="item">BYE</div>
</div>
</body>
If I understood the question correctly:
.item {
flex: 1;
text-align: center;
}
If you instead mean centering the entire div, use:
.container {
margin: 0 auto;
}
I hope your code is working correctly, just applied background and observed there is no space after the right most div
Refer this bootply http://www.bootply.com/T0TTJD1kTO
Instead of flex:1 you can use opacity:0.99 on child items, it will solve your Issue.
Here is an link to fiddle:
.item {
opacity: 0.99;
}
It's because all the child has same name, it's creating some problem.
Other Way to solve this is simply remove flex:1 or remove .item in css, it will automatically resolve it.
Here is working example of that in my Fiddle, you can check it.
https://jsfiddle.net/ABhimsaria/z7a4b7jo/
It's important to remember the initial settings of a flex container.
Some of these settings include:
flex-direction: row - flex items will align horizontally.
justify-content: flex-start - flex items will stack at the start of the line on the main axis.
align-items: stretch - flex items will expand to cover the cross-size of the container.
flex-wrap: nowrap - flex items are forced to stay in a single line.
flex-shrink: 1 - a flex item is allowed to shrink
Note the last setting.
Because flex items are allowed to shrink by default (which prevents them from overflowing the container), the specified flex-basis / width / height may be overridden.
For example, flex-basis: 100px or width: 100px, coupled with flex-shrink: 1, will not necessarily be 100px.
To render the specified width – and keep it fixed – you will need to disable shrinking:
div {
width: 100px;
flex-shrink: 0;
}
OR
div {
flex-basis: 100px;
flex-shrink: 0;
}
OR, as recommended by the spec:
flex: 0 0 100px; /* don't grow, don't shrink, stay fixed at 100px */
Some Cool Useful Links to know in-depth about flex and to play with them are:
http://flexboxfroggy.com/
https://scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties
Center and bottom-align flex items
https://philipwalton.com/articles/what-no-one-told-you-about-z-index/
I have flexbox that I want to place two more flexboxes in.
.Summary_Row{
display: -webkit-flex;
display: flex;
-webkit-align-items: stretch;
align-items: stretch;
-webkit-justify-content: center;
justify-content: center;
-webkit-flex-flow: row;
flex-flow: row;
width: 100%;
margin-top: 10px;
border-bottom: 2px solid #d3d3d3;
}
.col_left{ order:1; width: 33%; display:flex; justify-content: center; text-align: center;}
.col_center{order:2; width: 33%; display:flex; justify-content: center; border-right: 2px solid #d3d3d3; border-left: 2px solid #d3d3d3; text-align: center;}
.col_right{ order:3; width: 33%; display:flex; justify-content: center; text-align: center;}
.int_row{
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-flex-flow: row;
flex-flow: row;
width: 100%;
}
#inside_left{order:1; display:flex; justify-content: center; align-items: center; width: 25%;}
#inside_right{order:2; display:flex; flex-flow: column; justify-content: center; width: 75%; text-align:left;}
In my CSS above, I have a flexbox (summary_row) that is split into three equal columns. For col_right, I want to further split that into two more boxes side by side, one taking up 25% and the other 75% of col_right. I have int_row which I thought should contain inside_left and inside_right, but don't know if that's superfluous. Even though I have int_row set to 100%, the width actually doesn't extend the even close to the full width of col_right.
Blue in the image above is int_row and green is inside_right. Notice that the blue doesn't come close to being 100% of the width. I basically don't want the image and green to overlap. I'm thinking if the width is extended more, the overlap wouldn't occur.
Any suggestions on how I can achieve this or if I'm even thinking about this correctly?
I've made a working example for you on CodePen.
html:
<div class="row">
<div class="row__left">.row__left</div>
<div class="row__center">.row__center</div>
<div class="row__right">
<div class="row__right-a">.row__right-a</div>
<div class="row__right-b">.row__right-b</div>
</div>
</div>
css:
.row {
display: flex;
border: 1px solid #000;
padding: .5em;
}
.row__left,
.row__center,
.row__right {
flex: 0 0 33.3333333%;
border:1px solid red;
padding: .5em;
box-sizing: border-box;
}
.row__right {
display: flex;
}
.row__right-a {
flex: 0 0 25%;
background-color: blue;
}
.row__right-b {
flex: 0 0 75%;
background-color: green;
}
You did not need the extra .int_row element. Because a flex item (child) can also be a flex container.
You should also use flex-basis and flex-grow instead of width when trying to make grids with flexbox. I used the shorthand flex property. It's always a good idea to use the flex shorthand property because it forces you to set the flex-grow, shrink and basis value. Some browsers (IE) don't have the right default values so that will save you some trouble.
Also, this is the go to article to get started with Flexbox.