stretch items using flex follow this context:
flex-direction: row; & align-items: stretch; ==> vertical stretch
flex-direction: column; & align-items: stretch; ==> horizontal stretch
my problem:
i have direction is row ,but i want vertical and horizontal stretch to appear like columns. symmetrical
.inner {
display: flex;
align-items: stretch;
flex-wrap: wrap;
}
.inner button {
display: flex;
align-items: center;
background-color: #E3F2FD;
color: #304FFE;
border: 1px solid #304FFE;
padding-block: 5px;
padding-inline: 6px;
border-radius: 4px;
margin-block: 2px;
margin-inline: 10px;
}
<div class="inner">
<button>Brightness</button>
<button>Contrast</button>
<button>Grayscale</button>
<button>Saturate</button>
<button>Sepia</button>
<button>Invert</button>
</div>
my output:
my purpose:
add flex: 1; to the <button> html element.
like so:
.inner button {
flex: 1;
/* other code */
}
this means if there is some space left,
CSS flexbox tries to take all the space it can have.
also flex: 1 is a shorthand that can be also:
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;
like he said #G-Cyrillus it can break your code:
so put it inside a #media so only mobile versions have this behaviur
details:
https://developer.mozilla.org/en-US/docs/Web/CSS/flex?retiredLocale=it
Related
This question already has answers here:
Why does percentage padding break my flex item?
(1 answer)
Parent is inline-block and child has % padding = strange behaviour
(3 answers)
Closed last month.
I have this problem where my Flexbox does not expand if i add a margin or padding to the content(in this case the buttons)
I dont want to set a fixed width of the second flex-item because i loose responsiveness if i do so, is there any way i can accomplish it without?
.flex-container {
background-color:blue;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-around;
align-items: center;
align-content: stretch;
}
.flex-items:nth-child(1) {
display: block;
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
align-self: auto;
order: 0;
}
.flex-items:nth-child(2) {
display: block;
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
align-self: auto;
order: 0;
}
button {
background-color: brown;
font-size: 1.3rem;
color: rgb(223, 223, 223);
border: none;
margin: 0 5%;
}
<div class="flex-container">
<h1 class="flex-items">test Header</h1>
<div class="flex-items">
<button>test</button>
<button>test</button>
<button>test</button>
</div>
</div>
I have a webpage where I want to center one of the pages both vertically and horizontally, like that:
<main>
<div>
This has to be centered both vertically and horizontally.
</div>
</main>
I can't change display of main as it's widely used for all the pages, and using flex there breaks way too much stuff.
I assumed this should work but it does not, because div does not grow in height:
main {
min-height: 1000px;
border: solid 1px red;
}
div {
display: flex;
align-items: center;
justify-content: center;
align-content: stretch;
flex-grow: 1;
}
I'd rather not do position: absolute; top: 0; right: 0; bottom: 0; left: 0;.
Any hints how can I stretch my div to fill main? Why wouldn't it work out of the box, can't flex simply grow within elements with block/inline-block displays?
https://jsfiddle.net/wcu6fnz5/
It might helps you.
/* I cannot edit main, there are 100s of pages based on it, cant change its display etc. */
main {
min-height: 200px;
border: solid 1px red;
}
div {
display: flex;
min-height: inherit;
justify-content: center;
align-items: center;
}
<main>
<div>
<span>This has to be centered both vertically and horizontally.</span>
</div>
</main>
The thing about flexbox is that you should focus on instructing the parent, not the child.
In your example, justify-content and display: flex should be in the parent. You could also use flex-direction: column to tell the browser you want the child to grow vertically.
You could also use flex: 1 as a shorthand for flex-grow. This way, the child doesn't even need display: flex.
main {
min-height: 1000px;
border: solid 1px red;
display: flex;
flex-direction: column;
justify-content: stretch;
}
div {
flex: 1;
}
Update
To achieve what you mentioned in the comments, why don't you try this:
main {
min-height: 1000px;
border: solid 1px red;
}
section {
height: 1000px;
width: 100px;
background: gainsboro;
/* important stuff */
display: flex;
flex-direction: column;
justify-content: stretch;
}
div {
flex: 1;
width: 40px;
background: aquamarine;
}
Those sizes and colors are only for you to see the results.
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.
I'm trying to align three div blocks vertically using flexbox.
I can get them horizontally aligned correctly, however not vertically.
What am I doing wrong?
.banner {
padding-top: 10px;
padding-bottom: 10px;
background-color: #01b9d5;
color: white;
height: 55px;
}
.banner-align {
display: flex;
align-items: center;
justify-content: center;
border: 1px solid green;
}
.banner-hero {
flex: 1;
align-self: center;
max-width: 50%;
border: 1px solid red;
text-align: center;
display: inline-block;
}
.banner-left {
align-self: flex-start;
flex: 1;
border: 1px solid green;
display: inline-block;
}
.banner-right {
align-self: flex-end;
flex: 1;
text-align: right;
border: 1px solid yellow;
display: inline-block;
}
<div class="banner">
<div class="container banner-align">
<div class="banner-left">
Left Block
</div>
<div class="banner-hero">
<b>Title</b>
</div>
<div class="banner-right">
Right block
</div>
</div>
</div>
Here is a fiddle: https://jsfiddle.net/zqc1qfk1/1/
You are missing the flex-direction:column attribute of flex.
By default any flex container has a flex-direction: row & that is the reason its moving horizontally & not vertically. You need to specify this explicitly.
Here is more about it
.banner-align {
display: flex;
align-items: center;
justify-content: center;
border:1px solid green;
flex-direction: column;
}
Updated the Fiddle.
Simply enable wrap on the container and give each flex item a width: 100%:
.banner-align {
display: flex;
flex-wrap: wrap;
}
.banner-align > * {
flex: 0 0 100%;
}
Now each flex item consumes all the space in the row, forcing other elements to create new rows.
revised fiddle
align-items: center on your flex parent centers everything vertically. However, using align-self: [anything but center] on the children overrides this.
Edit:
oops, as someone else pointed out, you're not getting the align-self effect in the original fiddle because the parent's height wasn't set and so it was only as tall as it needed to be to contain the children. If the children hadn't all been the same height, you would've seen them staggered.
If you're trying to have them all centered, you can get rid of the align-self properties and let the one align-items: center on the parent do that work. If you wanted them staggered, you don't need the one align-items: center on the parent.