Consider the following HTML code:
<div class="ungrouped-ordered-item">
<div class="information-container">
<div class="originating-order-id"> #00019405 </div>
<div class="placed-by"> Placed by: 18175 </div>
</div>
<div class="indicator-container">
<div class="indicator"> Unpaid </div>
<div class="indicator" >Pending </div>
</div>
</div>
with the following css:
.ungrouped-ordered-item {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.information-container {
display: flex;
flex-direction: column;
}
.indicator-container {
display: flex;
flex-direction: row;
align-items: center;
}
.indicator-container .indicator {
display: flex;
justify-content: space-between;
align-items: center;
flex: 1;
padding: 0 10px;
border: 1px solid #2e2240;
}
The design I am aiming for, is:
to let the outermost flex containers (.ungrouped-ordered-item) children, grow as they need, and leave space between them, so that they are aligned to the left and right respectively of their containers
to make the children of the inner flex container (.indicator-container .indicator) be equal-width, by taking up the width of the wider element (in this case, it being the element with the text "Pending")
My first goal is achieved, but it seems, that even if adding flex: 1 to the .indicator containers, the browser will not correctly calculate the width of the two elements, and they will have uneven widths. I am presuming that this is because that their container, .indicator-container, has a fluid width. Am I right in this? How can I achieve my desired effect with all fluid width containers? (preferably without javascript).
Here's a fiddle also!
What is the argument against a width:50% for the .indicatorbox?
.ungrouped-ordered-item {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.information-container {
display: flex;
flex-direction: column;
}
.indicator-container {
display: flex;
flex-direction: row;
align-items: center;
gap: 10px;
}
.indicator-container > * {
border: 1px solid #2e2240;
padding: 0 10px;
flex-wrap: wrap;
}
.indicator-container .indicator {
width: 50%;
text-align: center;
}
<div class="ungrouped-ordered-item">
<div class="information-container">
<div class="originating-order-id"> #00019405 </div>
<div class="placed-by"> Placed by: 18175 </div>
</div>
<div class="indicator-container">
<div class="indicator"> Unpaid </div>
<div class="indicator" >Pending long long</div>
</div>
</div>
Changing the .indicator-container class from flex to grid and using automatic columns solves my problem. Although, it doesn't have as much coverage as flex, for my needs, this works:
.indicator-container {
display: grid;
grid-auto-columns: minmax(0, 1fr);
grid-auto-flow: column;
}
Here's the updated fiddle also.
Related
I currently have a problem with nesting a flex box layout. I have a parent flex container and four separate containers within it, so far the other 3 column containers work correctly. However when I try to place another container underneath the main one it simply moves to the right side.
.container-wrap {
display: flex;
justify-content: center;
}
.container-leftbar {
display: flex;
height: 100%;
gap: 10px;
padding: 10px;
flex: 1;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: fit-content;
}
.container-main {
display: flex;
height: 100%;
gap: 10px;
padding: 10px;
flex: 1;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: fit-content;
}
.container-main2 {
display: flex;
height: 100%;
gap: 10px;
padding: 10px;
justify-content: center;
flex-direction: row;
flex-wrap: wrap;
}
.container-rightbar {
display: flex;
height: 100%;
gap: 10px;
padding: 10px;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: fit-content;
}
<!--flex wrap-->
<div class="container-wrap">
<!--left column-->
<div class="container-leftbar">
<div class="1a">123</div>
<div class="1b">123</div>
<div class="1c">123</div>
</div>
<!--1 middle body-->
<div class="container-main">
<div class="2a">123</div>
<div class="2b">123</div>
</div>
<!--2 middle body-->
<div class="container-main2">
<div class="3a">123</div>
<div class="3b">123</div>
</div>
<!--right column-->
<div class="container-rightbar">
<div class="3a">123</div>
<div class="3b">123</div>
<div class="3c">123</div>
</div>
</div>
I tried to manually place the the top and left margin of the main box however I only really was able to achieve the top margin placing. I'm still somewhat new to using flex so any sort of help is appreciated.
I can't manage to place in a row horizontal 2 images by using flex. I made sure there was enough space in the parent element.
They will just remain in column.
.card {
width: 50%;
height: 90%;
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
}
<div class="card">
<img src={htmlLogo} alt="html" width="72px" height="72px">
<img src={cssLogo} alt="css" width="53px" height="74px">
</div>
It aligns horizontally.
.card{
width: 50%;
height: 90%;
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
}
<div className="card">
<img src="https://via.placeholder.com/72" alt="html" width="72px" height="72px"></img>
<img src="https://via.placeholder.com/53x74.png" alt="css" width="53px" height="74px"></img>
</div>
Are you trying to get the images to stack on top of eachother?
If that's the case, you can try flex-direction: column;
.card {
width: 50%;
height: 90%;
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
}
<div class="card">
<img src={htmlLogo} alt="html" width="72px" height="72px">
<img src={cssLogo} alt="css" width="53px" height="74px">
</div>
I figured it out, react was actually not happy with the className "card".
I don't know why because i'm not using this class anywhere else.
Maybe created a conflict with Bootstrap?
Anyway, thanks guys for the help!
I want to have a centered flex column layout with some items inside in a row with space between them.
html:
<div class="flex">
<h1>Welcome</h1>
<p>
Before you start there's a short introduction
</p>
<p>
<b>Do you want to see it?</b>
</p>
<div class="flex-row">
<span><b>Skip</b></span>
<button>Start</button>
</div>
</div>
css:
.flex {
display: flex;
flex-direction: column;
align-items: center;
}
.flex-row {
display: flex;
flex-direction: row;
justify-content: space-between;
}
The problem is the flex-row space-between doesn't work while the parent container has align-items: center and I can't figure out to how ignore it for the flex-row.
Try switching
align-items: center;
with
text-align: center;
but since
justify-content: space-between;
gives you the max space you can have between two items try adjusting the width of the site.
I'm attempting to center the flex items, however I must be doing something wrong because it isn't working. The basic outline is that Timeline-Container holds 3 smaller containers (TL-1,TL-2, etc). Everything is working fine between the parent container and the children container (meaning that all the TL-# containers are centered correctly with flex), however none of the items within the TL-# containers are centering.
I've tried "justify-content: center;" and it isn't affecting anything.
<div id="Timeline-Container">
<div id="TL-1">
<img src="Photos/2018 SB Photos/Lighted_Stadium_1.JPG">
<p>Caption 1</p>
</div>
<div id="TL-2">
<img src="Photos/2018 SB Photos/Lighted_Stadium_2.JPG">
<p>Caption 2</p>
</div>
<div id="TL-3">
<img src="Photos/2018 SB Photos/Lighted_Stadium_3.JPG">
<p>Caption 3</p>
</div>
</div>
#Timeline-Container {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: nowrap;
}
#Timeline-Container img {
height: 35%;
width: 35%;
border: 5px solid #cccccc;
}
#TL-1 {
/*Parent Flex Code*/
display: flex;
flex-direction: column;
justify-content: center;
align-items; center;
/*Child Flex Code*/
order: 1;
}
#TL-2 {
/*Parent Flex Code*/
display: flex;
flex-direction: column;
justify-content: center;
align-items; center;
/*Child Flex Code*/
order: 2;
}
#TL-3 {
/*Parent Flex Code*/
display: flex;
flex-direction: column;
justify-content: center;
align-items; center;
/*Child Flex Code*/
order: 3;
}
You have a typo align-items; center; which has a semi-colon instead of align-items: center; which has a colon. Here is a code pen: https://codepen.io/the_legitTDM/pen/NQXPWv
FYI: There is added margin for better viewing
The justify-content rule only works on actual flex items. Flex items are elements whose immediate parent has display: flex; set. So if you want to center the content in your #TL-? items usinf flexbox rules, you will need to add display: flex to them as well.
My favorite flexbox resource, helps the concepts just make sense: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
When I set flex-container height larger than what flex-items would occupy, items that wrap, have space between them. Mind you - justify-content and align-items are both set to flex-start. Here is snippet (click on full page after run)
.flex-container {
min-height: 100vh;
position: relative;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
flex-wrap: wrap;
}
header,
main,
aside,
footer {
padding: 1rem;
background-color: tomato;
}
header,
footer {
width: 100%;
}
main {
width: 75%;
}
aside {
flex-grow: 1;
}
<div class="flex-container">
<header>Header Content</header>
<main>Main content here.</main>
<aside>Sidebar content</aside>
<footer>Footer Content</footer>
</div>
Here's the pen
This can be reproduced with flex-direction: column; if you reversed all the properties. Is this expected behavior? If so, why? Is there a way I came around this and get something like that:
with the flex-container height set to 100vh ?
The correct answer without adding extra markup, is align-content: flex-start; - default is stretch, that's why wrapping elements have extra space between them, when the flex-container's size exceeds the size of the elements in it.
If I good understand your question - you can add following div .wrappper inside flex-container
.wrapper {
position: relative;
display: flex;
justify-content: flex-start;
align-items: flex-start;
flex-wrap: wrap;
flex-basis: 100%;
}
body {margin: 0}
.wrapper {
position: relative;
display: flex;
justify-content: flex-start;
align-items: flex-start;
flex-wrap: wrap;
flex-basis: 100%;
}
.flex-container {
min-height: 100vh;
position: relative;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
flex-wrap: wrap;
}
header,
main,
aside,
footer {
padding: 1rem;
background-color: tomato;
}
header,
footer {
width: 100%;
background-color: blue;
}
main {
flex-basis: 75%;
flex-grow: 1;
background-color: yellow;
}
aside {
flex-grow: 1;
}
<div class="flex-container">
<div class="wrapper">
<header>Header Content</header>
<main>Main content here.</main>
<aside>Sidebar content</aside>
<footer>Footer Content</footer>
</div>
</div>
Explanation what previous solution doesn't wokrs: because the height of your flex items was set to 1rem+font size, and the align-items: flex-start; was set so flex not change items height but put them on proper place (flex-start). But if you would use align-items: streth; then flex will stretch elements. Because you want to have 100vh for .flex-container, we need to use wrapper which was not stretched to full height of container because container has still align-items: flex-start;. And that wrapper height is sum of his chidren height without extra space.