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.
Related
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
I am facing a problem with flex-direction: column on responsive mode. When I write flex-direction: column to .container its not wrapping to responsive like flex-direction: row, it's going outside of the layout. How to fix that?
<div class="content">
<div class="container">
<div class="box">First Name Man</div>
<div class="box">First Name Man</div>
<div class="box">First Name Man</div>
<div class="box">First Name Man</div>
</div>
</div>
.content {
width: 100%;
min-height: 800px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
flex-wrap: wrap;
background-color: whitesmoke;
}
.container {
width: 100%;
min-height: 500px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
flex-wrap: wrap;
}
.box {
width:700px;
margin: 10px;
color:white;
min-height: 70px;
display: flex;
align-items: center;
box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.06);
justify-content: space-around;
background-color: indigo;
flex-direction: column;
flex-wrap: wrap;
}
You have a fixed width setting for .box (700px). Change that to width: calc(100% - 20px) (i.e. full width minus 20px for the margin left and right) in a media query, then it will work as desired.
I need a layout using flexbox, where 2 flex-items, item-1 should be aligned at top-center, while item-2 should be at bottom-center. I could not figure out how to do that.
See the below code:
.container{
padding:10px;
background: #fff;
border-radius:5px;
margin: 45px auto;
box-shadow: 0 1.5px 0 0 rgba(0,0,0,0,1.1);
}
.item{
color: #fff;
padding: 15px;
margin: 5px;
background: #3db5da;
border-radius: 3px;
}
.container{
display: flex;
min-height: 50vh;
justify-content: center;
align-items: center;
flex-direction: column;
}
.item{
/*margin: auto;*/
/*align-self: flex-start;*/
}
.item-4{
align-self: flex-start;
}
<div class="container">
<div class="item item-4">
Flex Item
<p>Vertical and horizontal align easy!</p>
</div>
<div class="item item-5"> bottom-center</div>
</div>
Here is fiddle link : https://jsfiddle.net/q5cw4xvy/
What you are looking for is justify-content: space-between; to align the items until the corners.
Add that to the .container and there you go.
.container{
/*justify-content: center;*/
justify-content: space-between;
}
On .item-4 you have align-self: flex-start; but you don't need that. just remove it.
https://jsfiddle.net/q5cw4xvy/2/
To better help you understand flexbox, there is a really nice css-tricks article.
Do you mean something like this?
https://jsfiddle.net/da4jdff7/1/
.container{
display: flex;
min-height: 50vh;
align-items: center;
flex-direction: column;
}
.item-5 {
margin-top: auto
}
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.
I have seen a few questions like mine, but none of them seemed to be sucessfully answered. My problem might be simple, but I dont get my mistake.
I use several flexboxes in each other. When i change the browsers size it should resize the same way. But at a point the childrens are overflowing the parent flexbox - Ugly
Because I've not found my mistake, i started a new HTML doc - I still have the mistake.
The "gamebox"s children dont do as I want them to.
Here's a fiddle: Live-Demo
Thank you for your help
- RoetzerBub
html , body, main {
margin: 0;
padding: 0;
min-height: 100%;
min-width: 100%;
}
body{
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
}
header, footer{
display: flex;
min-width: 100%;
justify-content: center;
}
header{
flex-shrink: 0;
background-color: #cc0000;
color: white;
z-index: 10;
margin-bottom: 10px;
}
main{
display: flex;
justify-content: center;
align-items: center;
}
footer{
background-color: #444444;
color: white;
flex-shrink: 0;
z-index: 10;
margin-top: 10px;
}
/*as*/
.gamebox{
display: flex;
flex-direction: row;
min-width: 30%;
max-width: 45%;
border: 2px solid #b30000;
justify-content: space-between;
align-items: center;
}
.gb_left, .gb_center, .gb_right{
margin: 2%;
justify-content: space-around;
background-color: yellow;
}
.gb_left{
display: flex;
justify-content: space-between;
align-items: center;
flex-grow: 1;
}
.gb_right{
display: flex;
justify-content: space-between;
align-items: center;
flex-grow: 1;
}
.gb_center{
display: flex;
flex-direction: column;
justify-content: space-between;
}
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="flexstyle.css">
</head>
<body>
<header>HEADER</header>
<main>
<div class="gamebox">
<div class="gb_left">
FLAG
TEAM-A
</div>
<div class="gb_center">
type<br/>
SCORE<br/>
date
</div>
<div class="gb_right">
TEAM-B
FLAG
</div>
</div>
</main>
<footer>FOOTER</footer>
</body>
</html>
It doesn't really make sense here to define the children as flex containers again - there are no elements in them (divs or spans), only text (i.e. nothing that could function as a flex item).
In the following fiddle I removed all this and used the following CSS settings:
.gamebox{
display: flex;
flex-direction: row;
min-width: 30%;
max-width: 45%;
border: 2px solid #b30000;
justify-content: space-between;
align-items: center;
}
.gb_left, .gb_center, .gb_right{
margin: 2%;
background-color: yellow;
min-width: 29%;
word-wrap: break-word;
}
The very last one makes sure that when a word is longer than the width of its container, it's broken.
Here is the fork of your fiddle: https://jsfiddle.net/o6wxy5z7/
To prevent elements from overflowing their containers you need to allow them to shrink as well as grow, for example:
.gb_left{
display: flex;
justify-content: space-between;
align-items: center;
flex-grow: 1;
}
can be changed to:
.gb_left{
display: flex;
justify-content: space-between;
align-items: center;
flex: 1 1 auto;
}
using the shorthand to assign flex-grow, flex-shrink, and flex-basis.
#media (max-width:400px) {
.gamebox {
flex-direction: column;
}
}
Which prevents overflow and keeping in touch with responsive design element
First off, there are four styles you are using for the flex's children that don't belong to them.
Remove these from the flex children (.gb-left, .gb-center, and .gb-right.):
display
justify-content
align-items
flex-direction
Here is a good article on what should be assigned to who: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Now for your problem with the children not shrinking past a certain point -- the text wraps as much as it can and then when the text is as small as it can be it holds the width of the box open.
There is an easy way around this, although it will mean cutting off the text.
Just assign overflow: hidden; to the children.
.gb_left, .gb_center, .gb_right {
overflow: hidden;
margin: 2%;
background-color: yellow;
}