This question already has answers here:
Targeting flex items on the last or specific row
(10 answers)
Closed 3 years ago.
I need to center a div and inside the div there are more div's like the picture.
But the div's inside should go to the left site of the outer div.
How can I achive this?
The cards are arranged with Flexbox. Image
//outer div
.cards {
display: flex;
justify-content: flex-start;
flex-direction: row;
flex-wrap: wrap;
}
// inner div's
.card-inner {
background: #cecdcd;
color: #000;
position: relative;
width: 110px;
height: 110px;
margin: 8px auto;
overflow: hidden;
word-wrap: break-word;
border-radius: 10px;
transition: 200ms;
}
<div class="cards" #end="checkMove">
<l-card
class="card-inner"
v-for="card in filterCards"
:key="card.uuid"
:data="card"
#open-card="openCard($event)"
></l-card>
</div>
Below is solution using flex
.flex-wrap {
display: flex;
justify-content: flex-start;
align-content: stretch;
flex-wrap: wrap;
justify-content: space-around;
}
.flex-inner {
display: flex;
height: 100px;
width: 25%;
align-items: center;
justify-content: center;
background-color: #ccc;
margin: 15px;
}
<div class="flex-wrap">
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
<div class="flex-inner">
content
</div>
</div>
Should be something like in flexbox:
.cards {
display: flex;
flex-direction: row;
flex-flow: row wrap;
justify-content: flex-start;
}
.card-inner {
display: flex;
flex: 0 0 110px;
height: 110px;
margin: 8px;
justify-content: center;
align-items: center;
background: #cecdcd;
color: #000;
border-radius: 10px;
}
<div class="cards">
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
<div class="card-inner">
Example
</div>
</div>
Related
I have a list of items, each one in display: flex to position its children elements. The height of the item is given by the image, and the button at the right position should fill the vertical space of the item. But I can't do that.
.Main-item {
display: flex;
border: 1px solid gray;
margin-bottom: 2px;
}
.Main-img {
height: 50px;
width: 50px;
overflow: hidden;
object-fit: cover;
object-position: center;
border-radius: 50%;
}
.Main-name {
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: space-evenly;
align-items: flex-start;
flex: 1 1;
padding: 0 10px;
}
.Main-buttonWrapper {
height: 100%;
width: 146px;
}
.Main-button {
height: 100%;
width: 100%;
}
<div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
</div>
Any idea will be welcome!
When you create a flex container various default flex rules come into play.
Two of these default rules are flex-direction: row and align-items: stretch. This means that flex items will automatically align in a single row, and each item will fill the height of the container.
If you change the value of align-items to for example flex-start it will change the default behavior 'stretch' as well if you set a specific height to the child of a flex container.
So just remove the height: 100% from .Main-buttonWrapper and it will have the default behavior.
For a better understanding look this answer: https://stackoverflow.com/a/33220564/4966662
I've given a height to the Main-item + align-items:stretch;. I hope this is what you need.
.Main-item {
align-items:stretch;
height: 50px;
}
.Main-element {
padding-top: 16px;
}
.Main-item {
display: flex;
border: 1px solid gray;
margin-bottom: 2px;
align-items:stretch;
height: 50px;
}
.Main-img {
width: 50px;
overflow: hidden;
object-fit: cover;
object-position: center;
border-radius: 50%;
}
.Main-name {
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: space-evenly;
align-items: flex-start;
flex: 1 1;
padding: 0 10px;
}
.Main-buttonWrapper {
width: 146px;
}
.Main-button {
width: 100%;
height: 100%;
}
<div class="Main-element">
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
</div>
A flex item stretches along the container cross axis by default, so the only thing you need to make the button stretch visually as well, is to make the button container a flex container by adding display: flex to .Main-buttonWrapper.
I have a div using the display flex method that contains other divs also using display flex to get the proper alignment that I want. I usually develop my web pages using Firefox and then later test them with other browsers to see if they work/display correctly there too.
I want to have a super-container that arranges its items in a normal flow. These items contains two images. These images are placed next to each other, and only their widths are fixed. The heights may differ per item. Below these two images I place a bit of text. All items are aligned such, that the first line of the texts are aligned with each other. The images should be placed in the horizontal middle of the item.
The following image shows how I want it to be displayed:
Firefox correct display. In Chrome and Edge (and Safari on iOS), the image looks like this: Incorrect display.
I have the following HTML and CSS:
.CompleteContainer
{
align-items: baseline;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 300px;
}
.ItemContainer
{
align-items: center;
background-color: aqua;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
margin: 0 0 5px 10px;
width: 130px;
}
.ItemTopContainer
{
display: flex;
justify-content: space-between;
width: 100%;
}
.ItemTopLeftContainer
{
align-items: flex-end;
background-color: #ff8888;
display: flex;
justify-content: flex-end;
width: calc(50% - 2px);
}
.ItemTopLeft
{
background-color: #ff0000;
height: 50px;
width: 50px;
}
.ItemTopRightContainer
{
align-items: flex-end;
background-color: #88ff88;
display: flex;
justify-content: flex-start;
width: calc(50% - 2px);
}
.ItemTopRight
{
background-color: #00aa00;
height: 25px;
width: 50px;
}
.ItemBottomContainer
{
text-align: center;
}
<div class="CompleteContainer">
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar, lorum ipsum
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Lorum ipsum
</div>
</div>
</div>
What am I doing wrong or what am I missing to get the display in Chrome and Edge the same as in Firefox? To be clear: in Firefox the above code snippet gives the results I want, in Chrome and Edge (and Safari on iOS) they are wrong.
UPDATE: I am not yet allowed to comment on the answers directly, but I will do it this way. Both the answers did point me in the correct direction, though they did not produce the correct results I wanted. I have updated the code in this question to reflect the changes in CSS to create the layout I want.
I have adjusted your code below and included documentation in the source. Taken out the floats. Please note floats do not work inside a flexbox.
.CompleteContainer
{
align-items: baseline;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 300px;
}
.ItemContainer
{
align-items: center;
background-color: aqua;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
margin: 0 0 5px 10px;
width: 130px;
}
.ItemTopContainer
{
width: 100%;
display: flex; /* Added */
}
.ItemTopLeftContainer
{
align-items: flex-end;
display: flex;
height: 100%;
/* justify-content: right; "right" does not exist. */
width: calc(50% - 2px);
}
.ItemTopLeft
{
background-color: red;
height: 50px;
width: 50px;
}
.ItemTopRightContainer
{
align-self: flex-end; /* Replacing align-items */
/* Not necessary
display: flex;
height: 100%;
justify-content: left;
*/
width: calc(50% - 2px);
}
.ItemTopRight
{
background-color: green;
height: 25px;
width: 50px;
}
.ItemBottomContainer
{
text-align: center;
}
<div class="CompleteContainer">
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar, lorum ipsum
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Lorum ipsum
</div>
</div>
</div>
.CompleteContainer
{
align-items: baseline;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 300px;
}
.ItemContainer
{
align-items: center;
background-color: aqua;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
margin: 0 0 5px 10px;
width: 130px;
}
.ItemTopContainer
{
width: 100%;
display: flex;
align-items: center;
}
.ItemTopLeftContainer
{
align-items: flex-end;
display: flex;
float: left;
height: 100%;
justify-content: right;
width: calc(50% - 2px);
}
.ItemTopLeft
{
background-color: red;
height: 50px;
width: 50px;
}
.ItemTopRightContainer
{
align-items: flex-end;
display: flex;
float: right;
height: 100%;
justify-content: left;
width: calc(50% - 2px);
}
.ItemTopRight
{
background-color: green;
height: 25px;
width: 50px;
}
.ItemBottomContainer
{
text-align: center;
}
<div class="CompleteContainer">
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Foo bar, lorum ipsum
</div>
</div>
<div class="ItemContainer">
<div class="ItemTopContainer">
<div class="ItemTopLeftContainer">
<div class="ItemTopLeft">
</div>
</div>
<div class="ItemTopRightContainer">
<div class="ItemTopRight">
</div>
</div>
</div>
<div class="ItemBottomContainer">
Lorum ipsum
</div>
</div>
</div>
How do I get flexbox children to line up vertically to the top edge of each row?
HTML:
#container {
align-items: flex-start;
align-content: flex-start;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.child {
margin: auto;
border: 1px dotted #CCC;
}
img, h3 {
width: 160px;
}
<div id="container">
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>Title</h3>
</div>
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>The quick brown fox jumps over the lazy dog</h3>
</div>
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>Title</h3>
</div>
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>Title</h3>
</div>
</div>
Demo: https://codepen.io/anon/pen/GOBzOp
What I see is but I want it to look like
change margin:auto of .child to margin: 0px auto.
Give justify-content: space-around; to #container id instead of justify-content: space-between; and remove margin: auto; to .child class.
#container {
align-items: flex-start;
align-content: flex-start;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.child {
border: 1px dotted #CCC;
}
img, h3 {
width: 160px;
}
<div id="container">
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>Title</h3>
</div>
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>The quick brown fox jumps over the lazy dog</h3>
</div>
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>Title</h3>
</div>
<div class="child">
<img src="http://via.placeholder.com/160x160">
<h3>Title</h3>
</div>
</div>
A simple fix would be changing margin: auto; to margin: 0 auto; this will prevent the box auto centralize vertically but retaining its horizontal alignment, like so:
.child {
border: 1px dotted #CCC;
margin: 0 auto;
}
you can try like
.child {
vertical-align: baseline; //either of them
margin: 0px auto; // any one
}
I need to make a div's width with a class grid the same as content (items).
But when the items wrap to the next line I need to shrink the div accordingly, so I don't have any extra space on the left and of the right of items.
Thanks in advance !
.home {
height: 100vh;
display: flex;
align-items: center;
}
.grid {
flex-basis: content;
display: flex;
background-color: red;
flex-wrap: wrap;
}
.gridItem {
margin: 2px;
width: 90px;
height: 60px;
background: #FFF;
display: flex;
align-items: center;
justify-content: center;
};
<div class='home'>
<div class='grid'>
<div class='gridItem'>Item 1</div>
<div class='gridItem'>Item 1</div>
<div class='gridItem'>Item 1</div>
<div class='gridItem'>Item 1</div>
<div class='gridItem'>Item 1</div>
<div class='gridItem'>Item 1</div>
<div class='gridItem'>Item 1</div>
</div>
</div>
I use a "full design" flexbox.
I have a weird issue : I have a container that takes all the remaining space and I want in this container that the child, which is also flexbox, to have their height adjust to their content.
Here is the issue:
body, html {
width:100%;
height:100%;
display:flex;
}
.container {
display:flex;
flex:1;
flex-wrap:wrap;
}
.icon {
width:10vh;
margin:10px;
display:flex;
flex-direction:column;
}
.img {
width:10vh;
height:10vh;
display:flex;
align-items:center;
justify-content:center;
background-color:red;
}
.text {
text-align:center;
}
<div class="container">
<div class="icon">
<div class="img">
</div>
<div class="text">
action 1
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 2
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 3
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 4
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 5
</div>
</div>
</div>
As you can see, the icon takes the full height of the container : in fact, I don't want to specify a height because I don't know the text length and really want that, if the content is huge, the icon takes the height of its content ( don't want to cut the text). Moreover, if the page is resized, I really want the icon to be aligned (like on smartphone).
Also, I don't understand why the icon takes the height of its parent and not its content because I didn't specify "flex:1" on it. I assume that the default behaviour it's to fit the content size, but this seems not to be working.
image of the issue
.icon's are flex-column which makes .img's stretch by default unless .icon's have align-items. The reason why I didn't apply align-items to .icon's is because other nested flex-containers/flex-items started collapsing. Instead of adjusting down through the hierarchy, I went up and adjusted .container instead.
The relevant CSS:
.container {
display: flex;
flex: 1; /* If you remove this .container will shrink to wrap around .icon's */
flex-wrap: wrap;
justify-content: center; /* This centers .icon's along a horizontal axis. */
align-items: baseline; /* This aligns .icon's along a common baseline vertically. */
outline: 3px dashed blue; /* To show the size of .container */
}
.icon {
width: 10vh;
margin: 10px;
display: flex;
flex-direction: column;
outline: 1px dashed red; /* To show the size of .icon */
}
body,
html {
width: 100%;
height: 100%;
display: flex;
}
.container {
display: flex;
flex: 1;
flex-wrap: wrap;
justify-content: space-between;
align-items: baseline;
align-content: flex-start;
outline: 3px dashed blue;
}
.icon {
width: 10vh;
margin: 10px;
display: flex;
flex-direction: column;
outline: 1px dashed red;
}
.img {
width: 10vh;
height: 10vh;
display: flex;
align-items: center;
justify-content: center;
background-color: red;
}
.text {
text-align: center;
}
<div class="container">
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 1
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 2
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 3
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 4
</div>
</div>
<div class="icon">
<div class="img">
</div>
<div class="text">
Action 5
</div>
</div>
</div>