I'm having an issue with flexbox not filling spaces as I intend, I'm trying to see what I've been missing but couldn't wrap my head around it.
Below is a reproduction of my issue with a link to a codepen.
I need the div in the middle with class="artcl-description" to fill the remaining available space, taking into consideration the header and the footer and their content, I tried giving the parent (class="artcl-content-container") properties align-items: stretch; justify-content: stretch; but no bueno, I don't want to resort to hard coded height values and percentages if that's possible :/
html:
<div class="artcl-container">
<div class="artcl-content-container">
<div class="artcl-title-container">
<h1>Some Title</h1>
</div>
<div class="artcl-description">
<p>Why is this not taking the remaining height?</p>
</div>
<div class="artcl-footer">
<div>Some guy</div>
<div>
X/X/XXXX XX:XX:XX
</div>
</div>
</div>
<img class="artcl-thumbnail" src="https://picsum.photos/200" />
</div>
scss:
$article-height: 240px;
$pad: 16px;
body {
padding: 16px;
}
* {
direction: ltr;
margin: 0px;
}
.artcl-container {
height: $article-height;
background-color: black;
color: white;
font-family: sans-serif;
border-radius: 4px;
overflow: hidden;
display: flex;
flex-direction: row;
.artcl-thumbnail {
height: $article-height;
width: $article-height;
object-fit: cover;
}
.artcl-content-container {
flex: 1;
flex-direction: column;
align-items: stretch;
justify-content: stretch;
.artcl-title-container {
padding: $pad;
border: dashed 1px wheat;
}
.artcl-description {
flex: 1;
padding: $pad;
border: dashed 1px wheat;
}
.artcl-footer {
padding: $pad;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
border: dashed 1px wheat;
}
}
}
Link to Pen
You forgot to add display: flex; to your class .artcl-content-container. I added it to your pen and it just works fine.
Related
How can make child-6 is stretch to half the height of parent?
avoiding the way of splitting 6 items into 2 groups.
I want to stretch child-6 but not working and this makes sense to me because to make a specific item stretch will be stretched the rest space, not half.
.parent {
width: 600px;
margin: 0 auto;
padding: 10px;
height: 300px;
background-color: #eee;
display: flex;
justify-content: space-between;
align-items: flex-start;
flex-wrap: wrap;
align-content: space-between;
}
.parent div {
color: #eee;
background-color: rgb(29, 28, 27);
padding: 10px 0;
width: calc(94%/3);
display: flex;
justify-content: center;
}
.parent div.ch6 {
align-self: stretch;
}
<div class="parent">
<div class="ch1">1</div>
<div class="ch2">2</div>
<div class="ch3">3</div>
<div class="ch4">4</div>
<div class="ch5">5</div>
<div class="ch6">6</div>
</div>
Like below:
.parent {
width: 600px;
margin: 0 auto;
padding: 10px;
height: 300px;
background-color: #eee;
display: flex;
justify-content: space-between;
align-items: flex-start;
flex-wrap: wrap;
/*align-content: space-between; removed */
}
.parent div {
color: #eee;
background-color: rgb(29, 28, 27);
padding: 10px 0;
width: calc(94%/3);
display: flex;
justify-content: center;
}
.parent div.ch6 {
align-self: stretch;
}
.parent div.ch5,
.parent div.ch4{
align-self: end; /* added */
}
<div class="parent">
<div class="ch1">1</div>
<div class="ch2">2</div>
<div class="ch3">3</div>
<div class="ch4">4</div>
<div class="ch5">5</div>
<div class="ch6">6</div>
</div>
I'm trying to create a layout for this website using flexbox but the justify-content property doesn't seem to be working. Ultimately, I want it to look like this:
I have the grid laid out correctly but can't get space between elements at this point. This is what I've got right now:
How do I align my content correctly? Here is my codepen: https://codepen.io/caseycling/pen/poNXRXZ
.service-container {
display: flex;
justify-content: center;
}
.img {
margin: .5rem;
border: 1px solid grey;
min-width: 100px;
}
.container>div {
border: 1px solid grey;
min-height: 100px;
min-width: 100px;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around; //Not working
}
.service {
flex: 40%;
margin: .5rem;
}
<div class='service-container'>
<div class='img'>IMG</div>
<div class='container'>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
</div>
</div>
I think it's because you attach space-around in wrong place. Now you have it on container which is only the left rectangle. This property will affect everything inside but not outside. You should add this prop to the highest wrapper which in this case is service-container
.service-container {
display: flex;
justify-content: space-around; // working here
}
Your primary flex container has justify-content: center applied.
You don't want to center the items, it seems. You want them separated like in your image.
So use justify-content: space-between.
But if you still want the layout centered, set a width to the container and use the margin property.
.service-container {
display: flex;
justify-content: space-between;
width: 800px;
margin: 0 auto;
}
.img {
margin: .5rem;
border: 1px solid grey;
min-width: 100px;
}
.container>div {
border: 1px solid grey;
min-height: 100px;
min-width: 100px;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around; //Not working
}
.service {
flex: 40%;
margin: .5rem;
}
<div class='service-container'>
<div class='img'>IMG</div>
<div class='container'>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
</div>
</div>
Add margin-right to .img
.img {
margin: .5rem;
border: 1px solid grey;
min-width: 100px;
margin-right: 100px; // Increase space between image and servies
}
Image showing the center line where the "vs" should be on
I will try to explain my problem as clear as I can. Here I am trying to make the "vs" in the center line and left and right elements flow beside it.
I tried using flex-box but it centers the whole thing as you can see in the photo above but not what I am trying to achieve.
Thanks!
Here is a snippet of what I have tried:
<div class='flex page-container'>
<div class='flex team-name'>
THIS TEAM NAME IS LONG
<img class='team-logo' src={TEAM_1_LOGO} />
</div>
<div class='vs'>VS</div>
<div class='flex team-name'>
<img class='team-logo' src={TEAM_2_LOGO} />
SHORT NAME
</div>
</div>
.flex {
display: flex;
}
.page-container {
align-items: center;
justify-content: center;
margin: 1.5rem;
max-height: 9rem;
overflow: hidden;
.team-name {
align-items: center;
}
.team-logo {
height: 6rem;
max-width: 7rem;
margin: 0 5rem;
overflow: hidden;
}
.vs {
align-self: center;
}
}
flex-basis:0 to ensure the elements truly share the available space, instead of calculating content of each flex item first, then distribute the left over space.
The rest is just simple alignment.
flex: 1 0 0; is short for
flex-grow:1; // make element grow
flex-shrink:0; // prevent element from shrinking (preference really)
flex-basis:0; // ignore content width
/* Just for illustrating, To be removed */
body * {
border: 1px solid;
padding: 10px;
}
[center] {
display: flex;
flex-direction: column;
align-items: center;
margin: 1.5rem;
}
/* ============ */
.flex {
display: flex;
}
.page-container {
margin: 1.5rem;
max-height: 9rem;
overflow: hidden;
align-items: center;
}
.team-name {
flex: 1 0 0;
align-items: center;
justify-content: flex-end;
}
.team-logo {
max-height: 6rem;
max-width: 7rem;
overflow: hidden;
margin: 0 10px;
}
.vs {
margin: 0 10px;
}
.team-name~.team-name {
justify-content: flex-start;
}
True center example
<div center="">
<div>VS</div>
</div>
<div class="flex page-container">
<div class="flex team-name">
THIS TEAM NAME IS LONG
<img class="team-logo" src="https://i.picsum.photos/id/163/200/300.jpg">
</div>
<div class="vs">VS</div>
<div class="flex team-name">
<img class="team-logo" src="https://i.picsum.photos/id/163/200/300.jpg"> SHORT NAME
</div>
</div>
Try
.page-container {
justify-content: space-between;
}
Or
.page-container {
justify-content: space-evenly;
}
if you don't want the team names to be aligned to the edges
HTML:
<div id="content">
<div class="card">
<span class="card-title">Album</span>
<div class="card-content">
<img class="album-thumbnail" src="a.jpg">
<img class="album-thumbnail" src="b.jpg">
<img class="album-thumbnail" src="c.jpg">
<img class="album-thumbnail" src="d.jpg">
</div>
</div>
</div>
CSS:
.card {
background-color: white;
margin: 0 2em 2em 0;
display: inline-flex;
flex-direction: column;
padding: 1.5em;
border: 1px solid black;
}
.card-content {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-content: flex-start;
max-height: 300px;
}
.album-thumbnail {
width: 150px;
margin: 1px;
}
In the browser I can see two columns of images, but the width of card-content ends where the first column ends. What causes this and how can I solve it?
This is how it looks currently:
Cause you have a duplicate for display in this code section:
.card {
background-color: white;
margin: 0 2em 2em 0;
display: flex;
flex-direction: column;
padding: 1.5em;
border: 1px solid black;
display: inline-block;
}
Remove this line display: inline-block; and it should work just fine. Here's a replica jsfiddle of the problem with the applied solution
Here's a screenshot with your element inspected:
Another thing you should remove if you don't want space around the columns is align-items: center; inside the class .card-content.
Open the updated fiddle to see the changes.
Bit confused here. I am quite tired, so I'm sure I'm making a silly mistake but here's my HTML/CSS:
.CAContainer {
display: flex;
flex-flow: row nowrap;
justify-content: flex-start;
align-items: center;
margin: 0;
padding: 0;
}
.CASideBorder {
height: 100%;
background: #000;
width: 8px;
}
.CAMiddleContainer {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
}
.CATopBorder {
height: 8px;
background: #000;
width: 100%;
}
.CAMiddleTextContainer {
padding: 40px 80px 40px 80px;
font-size: 4em;
color: #000;
}
.CABottomBorderContainer {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
width: 100%;
height: 8px;
}
.CABottomBorderLeft,
.CABottomBorderRight {
flex: 1;
height: 100%;
background: #000;
}
<div class="CAContainer">
<div class="CASideBorder" id="CALeftBorder"></div>
<div class="CAMiddleContainer">
<div class="CATopBorder"></div>
<div class="CAMiddleTextContainer">
text
</div>
<div class="CABottomBorderContainer">
<div class="CABottomBorderLeft"></div>
<div class="CABottomBorderRight"></div>
</div>
</div>
<div class="CASideBorder" id="CARightBorder"></div>
</div>
And the fiddle.
Shouldn't CASideBorder be taking up 100% of its parent container, which has its height calculated by the text size and padding of CAMiddleTextContainer? Am I missing something really obvious here? Any help is appreciated.
That is because you have align-items: center for the flexbox in which case it will default to the content height.
What you need to do is to remove that (let align-items: stretch come into play as it is the default) and also remove the height:100% from CASideBorder.
See demo below:
.CAContainer {
display: flex;
flex-flow: row nowrap;
justify-content: flex-start;
margin: 0;
padding: 0;
}
.CASideBorder {
background: #000;
width: 8px;
}
.CAMiddleContainer {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
}
.CATopBorder {
height: 8px;
background: #000;
width: 100%;
}
.CAMiddleTextContainer {
padding: 40px 80px 40px 80px;
font-size: 4em;
color: #000;
}
.CABottomBorderContainer {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
width: 100%;
height: 8px;
}
.CABottomBorderLeft, .CABottomBorderRight {
flex: 1;
height: 100%;
background: #000;
}
<div class="CAContainer">
<div class="CASideBorder" id="CALeftBorder"></div>
<div class="CAMiddleContainer">
<div class="CATopBorder"></div>
<div class="CAMiddleTextContainer">
text
</div>
<div class="CABottomBorderContainer">
<div class="CABottomBorderLeft"></div>
<div class="CABottomBorderRight"></div>
</div>
</div>
<div class="CASideBorder" id="CARightBorder"></div>
</div>