How to use Flexbox? - html

I have a layout like this:
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner">
<div class="deeper">
<div class="newLineContent"></div>
<div class="newLineContent"></div>
<div class="newLineContent"></div>
</div>
</div>
</div>
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner">
<div class="deeper">
<div class="newLineContent"></div>
<div class="newLineContent"></div>
<div class="newLineContent"></div>
</div>
</div>
</div>
I have to align the divs like: All the divs with "outer" class has to start from new line and all the divs with "inner" class has to be in the same line within "outer" div and the divs with deeper class inside "outer" div should start from new line and the "newLineContent" divs has to be in the same line within "deeper" divs
How can I achieve this using flexbox? or is there any other way to achieve it?

One can do similar without Flexbox, thought Flexbox appears to be the best in this case.
.outer,
.deeper {
display: flex;
flex-wrap: wrap;
}
.inner {
flex-grow: 1;
padding: 20px 10px;
border: 1px solid white;
background: lightgray;
}
.inner:last-child {
flex-basis: 100%;
}
.newLineContent {
flex-grow: 1;
padding: 10px;
border: 1px solid white;
background: lightgray;
}
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner">
<div class="deeper">
<div class="newLineContent"></div>
<div class="newLineContent"></div>
<div class="newLineContent"></div>
</div>
</div>
</div>
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner">
<div class="deeper">
<div class="newLineContent"></div>
<div class="newLineContent"></div>
<div class="newLineContent"></div>
</div>
</div>
</div>
Updated based on a comment
If the deeper can be a child to any of the inner, and to achieve similar result, one would need either a parent selector, which doesn't exist, or give the inner an additional class for those that contain a deeper.
Another possible workaround could be to use viewport units vw.
.outer,
.deeper {
display: flex;
flex-wrap: wrap;
width: calc(100vw - 40px); /* 40px to make up for body's margins/scrollbar */
margin: 0 auto;
}
.inner {
flex-grow: 1;
padding: 20px 10px;
border: 1px solid white;
background: lightgray;
}
.deeper {
width: calc(100vw - 62px); /* 62px is to make up for "inner" padding/border, 22px,
and 40px for body's margins/scrollbar */
}
.newLineContent {
flex-grow: 1;
padding: 10px;
border: 1px solid white;
background: lightgray;
}
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner">
<div class="deeper">
<div class="newLineContent"></div>
<div class="newLineContent"></div>
<div class="newLineContent"></div>
</div>
</div>
</div>
<div class="outer">
<div class="inner"></div>
<div class="inner">
<div class="deeper">
<div class="newLineContent"></div>
<div class="newLineContent"></div>
<div class="newLineContent"></div>
</div>
</div>
<div class="inner"></div>
</div>

.outer{
display: flex;
}
Simple as that! How you configure parent and child options, depends on the effect you want to achieve.

You can do it with Flexbox:
.outer {
display: flex; /* displays flex-items (children) inline */
justify-content: space-between; /* MDN: The items are evenly distributed within the alignment container along the main axis. The spacing between each pair of adjacent items is the same. */
}
.inner:last-child .deeper {
display: flex;
}
<div class="outer">
<div class="inner">1.1</div>
<div class="inner">1.2</div>
<div class="inner">1.3
<div class="deeper">
<div class="newLineContent">1.3.1</div>
<div class="newLineContent">1.3.2</div>
<div class="newLineContent">1.3.3</div>
</div>
</div>
</div>
<div class="outer">
<div class="inner">2.1</div>
<div class="inner">2.2</div>
<div class="inner">2.3
<div class="deeper">
<div class="newLineContent">2.3.1</div>
<div class="newLineContent">2.3.2</div>
<div class="newLineContent">2.3.3</div>
</div>
</div>
</div>
With your current HTML structure this is the result you get. Flex-items of the .inner:last-child .deeper div can't stretch the full width of the browser because the .deeper div represents one third of the parent element, i.e. the .outer div.

Related

Last row in CSS Grid where cells have a footer and are stretched to same height drawing strangely

I have a code pen at https://codepen.io/james-hudson3010/pen/wveJqXd
What I am looking to achieve is the following:
I have an arbitrary number of cells. This example only uses 10, but it could be more or less than 10
Each cell has a footer which needs to be aligned to the bottom of cell so that they are aligned across cells in the same row
The height of a cell can vary, but the height needs to be stretched so every cell in the same row has the same height to support #2
This almost works perfectly using Safari ( Version 14.1.2 (15611.3.10.1.5, 15611) ), but the last cell in the last row is wider than it should be.
The behavior is worse in Chrome ( Version 93.0.4577.63 (Official Build) (x86_64) ). The rows are drawn higher than they should be. The last cell in the last row is to wide.
Is this due to a lack of complete browser support?
If this is due to not specifying my css correctly, what do I need to do?
.example1 {
display: grid;
grid-template-columns: repeat(auto-fill, 300px);
align-items: stretch;
}
div {
border: solid 1px;
padding: 1rem;
}
.contentdiv {
background-color: red;
display: table;
height: 100%;
width: 100%;
}
.foot {
display: table-row;
vertical-align: bottom;
height: 1px;
}
<div class="example1">
<div class="contentdiv">1<br/>a<br/>a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">2
<div class="foot">bottom</div>
</div>
<div class="contentdiv">3
<div class="foot">bottom</div>
</div>
<div class="contentdiv">4
<div class="foot">bottom</div>
</div>
<div class="contentdiv">5a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">6
<div class="foot">bottom</div>
</div>
<div class="contentdiv">7a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">8
<div class="foot">bottom</div>
</div>
<div class="contentdiv">9
<div class="foot">bottom</div>
</div>
<div class="contentdiv">10a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
</div>
This has to do with the setting the display to table for the contentdiv - and setting it's width and height to 100%.
Basically what is happening - if you view your elements in the web inspector - you'll see that all of your elements are wider than 300px because of the padding 1rem, so they are actually overlapping already, you just can't visually see it until the last row.
You can avoid this setting, * { box-sizing: border-box } and the display to flex instead, and using the justify-content property to keep the foot aligned on all rows:
* {
box-sizing: border-box;
}
.example1 {
display: grid;
grid-template-columns: repeat(auto-fill, 300px);
align-items: stretch;
}
div {
border: solid 1px;
padding: 1rem;
}
.contentdiv {
background-color: red;
display: flex;
flex-direction: column;
justify-content: space-between;
/* Avoid the borders stacking */
margin: -1px;
}
<div class="example1">
<div class="contentdiv">1<br/>a<br/>a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">2
<div class="foot">bottom</div>
</div>
<div class="contentdiv">3
<div class="foot">bottom</div>
</div>
<div class="contentdiv">4
<div class="foot">bottom</div>
</div>
<div class="contentdiv">5a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">6
<div class="foot">bottom</div>
</div>
<div class="contentdiv">7a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">8
<div class="foot">bottom</div>
</div>
<div class="contentdiv">9
<div class="foot">bottom</div>
</div>
<div class="contentdiv">10a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
</div>
From my comment,
display:table is not a good idea here (table-layout:fixed would be required for the width) and height : 1px (expanded cause of the table-layout display) is also not a good idea, while flex or grid will do this without sides effects minus the box-sizing:border-box missing to mind height:100% and padding/ border.To set that element at the bottom, flex or grid should be the way to IMHO
here is an example with grid instead table for display ;)
.example1 {
display: grid;
grid-template-columns: repeat(auto-fill, 300px);
align-items: stretch;
}
div {
border: solid 1px;
padding: 1rem;
box-sizing:border-box;
}
.contentdiv {
background-color: red;
display: grid;
height: 100%;
}
.foot {
margin-top:auto;/* push it all the way down */
}
<div class="example1">
<div class="contentdiv">1<br/>a<br/>a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">2
<div class="foot">bottom</div>
</div>
<div class="contentdiv">3
<div class="foot">bottom</div>
</div>
<div class="contentdiv">4
<div class="foot">bottom</div>
</div>
<div class="contentdiv">5a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">6
<div class="foot">bottom</div>
</div>
<div class="contentdiv">7a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
<div class="contentdiv">8
<div class="foot">bottom</div>
</div>
<div class="contentdiv">9
<div class="foot">bottom</div>
</div>
<div class="contentdiv">10a<br/>a<br/>a<br/>
<div class="foot">bottom</div>
</div>
</div>
Not clear about the requirement you have put in ,if you are expecting this ()
Also you can better play around with flex property which is easier
grid image sample
if so I just changed the height in class contentdiv to inherit.

How to reposition HTML element to a different part of hierarchy based on the breakpoint?

This is some html and css that uses flexbox to position an element (colored in yellow). In a large breakpoint it appears in one place but in a small breakpoint it needs to be in a different part of the hierarchy. So it's not as simple as just repositioning its order but actually moving it to a different place.
How can I do this using just CSS and flexbox? If that's not possible then I could use CSS grid. Would prefer not to have to use javascript unless there's no other way.
.one {
background-color: lightgray;
}
.two {
background-color: red;
}
.three {
background-color: blue;
}
.element {
background-color: yellow;
}
<h1>large breakpoint</h1>
<div>
<div style="display:flex">
<div class="one">
<div>one</div>
</div>
<div class="element">
possible position 1
</div>
<div class="two">two</div>
<div class="three">three</div>
</div>
</div>
<h1>small breakpoint</h1>
<div>
<div style="display:flex">
<div class="one">
<div>one</div>
</div>
<div class="two">two</div>
<div class="three">three</div>
</div>
<div class="element">
possible position 2
</div>
</div>
Pretty sure you can achieve what you want with the order property. Here's an example where an element is in a different order than the order in which it was declared.
You can set the order property in each breakpoint to match your needs.
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.item {
border: 0.025em solid black;
padding: 1em;
}
#special-item {
order: 3;
width: 100%;
background-color: yellow;
}
<h1>Large breakpoint</h1>
<div class="container">
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three</div>
<div class="item">Four</div>
</div>
<h1>Small breakpoint</h1>
<div class="container">
<div class="item">One</div>
<div id="special-item" class="item">Two</div>
<div class="item">Three</div>
<div class="item">Four</div>
</div>

Remove redundant space at bottom when using Angular Flex with "row wrap" and grid-gap

I'm using Angular Flex to align cards in a row. The cards should wrap into a new line if there are several of them. The relevant settings of the block are
fxLayout="row wrap" fxLayoutGap="40px grid"
fxLayoutGap uses paddings on the inner elements and a negative margin on the container so that the gap is also applied when the inner elements wrap to a new row. So I do not want to remove the grid setting (or the paddings or negative margins in the
In addition, the cards are grouped into blocks with a header and a line on the left. I've created a sample that mirrors the settings that Angular Flex applies. The image is taken from this sample:
As you can see, there is redundant space at the bottom of each group. I want the block and the line on the left to end where the last row of cards (of the block) ends:
You can find the sample on jsfiddle.
How can I adjust the CSS and/or the Angular Flex settings to remove the redundant space and make the line end at the last row of cards while preserving the space between the blocks?
remove the padding-bottom from the last two elements:
#outer {
border-left: 2px solid red;
padding-left: 0px;
padding-bottom: 40px;
}
#outer:not(:first-child) {
margin-top: 40px;
}
#header {
padding: 10px;
margin: 0px 0px 40px 0px;
background-color: red;
}
#container {
margin: 40px -40px -40px 40px;
display: flex;
flex-flow: row wrap;
box-sizing: border-box;
}
#inner {
padding: 0px 40px 40px 0px;
flex: 0 0 50%;
box-sizing: border-box;
max-width: 50%;
min-width: 50%;
}
/* added */
#inner:last-child,
#inner:nth-last-child(2):nth-child(odd){
padding-bottom:0;
}
/**/
#card {
background-color: green;
}
<div id="outer">
<div id="header">
HEADER
</div>
<div id="container">
<div id="inner">
<div id="card">
CARD
</div>
</div>
<div id="inner">
<div id="card">
CARD
</div>
</div>
<div id="inner">
<div id="card">
CARD
</div>
</div>
<div id="inner">
<div id="card">
CARD
</div>
</div>
</div>
</div>
<div id="outer">
<div id="header">
HEADER
</div>
<div id="container">
<div id="inner">
<div id="card">
CARD
</div>
</div>
<div id="inner">
<div id="card">
CARD
</div>
</div>
</div>
</div>
<div id="outer">
<div id="header">
HEADER
</div>
<div id="container">
<div id="inner">
<div id="card">
CARD
</div>
</div>
</div>
</div>
<div id="outer">
<div id="header">
HEADER
</div>
<div id="container">
<div id="inner">
<div id="card">
CARD
</div>
</div>
<div id="inner">
<div id="card">
CARD
</div>
</div>
<div id="inner">
<div id="card">
CARD
</div>
</div>
</div>
</div>

Columns and rows flex

So, I am having an issue where I can mostly get my flexbox working, except when it comes to two columns side by side with uneven rows beside each other. For the life of me I can't figure out why it isn't working.
Here is the screenshot of how it looks now:
http://prntscr.com/ndig4v (by lightshot)
Here is the HTML:
/* Stats */
#stats-container {
width: 100%;
height: 100%;
border: 1px black solid;
}
#free-stats {
width: 100%;
height: 45%;
border: 1px black solid;
}
#stats-column-one,
#stats-column-two {
display: flex;
flex-direction: column;
}
#stats-column-one {
width: 75%;
}
#stats-column-two {
width: 25%;
}
#stats-flex-one,
#stats-flex-two {
display: flex;
flex-direction: row;
}
#str-row,
#end-row,
#dex-row,
#eva-row,
#int-row,
#res-row,
#has-row {
background: black;
display: flex;
flex-direction: row;
position: relative;
width: 55px;
border: 1px yellow solid;
}
<div id="stats-container" class="yellow-black-shadow">
<div id="free-stats">FS</div>
<div id="stats-column-one">
<div id="stats-flex-one">
<div id="str-row">
<div class="stat-icon"></div>
<div id="str">SR</div>
</div>
<div id="dex-row">
<div class="stat-icon"></div>
<div id="dex">DR</div>
</div>
<div id="int-row">
<div class="stat-icon"></div>
<div id="int">IR</div>
</div>
</div>
<div id="stats-flex-two">
<div id="end-row">
<div class="stat-icon"></div>
<div id="str">ER</div>
</div>
<div id="eva-row">
<div class="stat-icon"></div>
<div id="eva">VR</div>
</div>
<div id="res-row">
<div class="stat-icon"></div>
<div id="res">RR</div>
</div>
</div>
</div>
<div id="stats-column-two">
<div id="has-row">
<div class="stats-icon"></div>
<div id="has">HR</div>
</div>
</div>
</div>
Basically everything is correct, until it reaches the HR block, it SHOULD be on the right side. I want avoid using float: right;
I'm pretty sure I am doing this in the wrong order, but I am not sure which order I am messing up. I've played with it some, but each different thing I try I seem to break a bit more, this is the closest I've gotten.
I got it, I forgot the order in which things are supposed to work, but here is the fix
HTML:
<div id="stats-container" class="yellow-black-shadow">
<div id="free-stats">FS</div>
<div id="stats-column-container">
<div id="stats-column-one">
<div id="stats-flex-one">
<div id="str-row">
<div class="stat-icon"></div>
<div id="str">SR</div>
</div>
<div id="dex-row">
<div class="stat-icon"></div>
<div id="dex">DR</div>
</div>
<div id="int-row">
<div class="stat-icon"></div>
<div id="int">IR</div>
</div>
</div>
<div id="stats-flex-two">
<div id="end-row">
<div class="stat-icon"></div>
<div id="str">ER</div>
</div>
<div id="eva-row">
<div class="stat-icon"></div>
<div id="eva">VR</div>
</div>
<div id="res-row">
<div class="stat-icon"></div>
<div id="res">RR</div>
</div>
</div>
</div>
<div id="stats-column-two">
<div id="has-row">
<div class="stats-icon"></div>
<div id="has">HR</div>
</div>
</div>
</div>
</div>
I just wrapped it all in another contain div, and added:
#stats-column-container {
display: flex;
flex-direction: row;
}
I also removed this completely
#stats-column-one,
#stats-column-two {
display: flex;
flex-direction: column;
}
For reference on how it was supposed to look
http://prntscr.com/ndijk0 (by lightshot)
Hope this may help someone understand the order better in the future cause it confuses me sometimes.

html/css - create self expanding dynamic div

I have sample div structure:
<div id="outer" style="width:600">
<div class="inner"></div> <!--row1-->
<div class="inner"></div>
<div class="inner"></div><br/>
<div class="inner"></div> <!--row2-->
<div class="inner"></div>
</div>
Outer div has fixed width. Inner divs are generated dynamically, so there could be 1,2,3 etc divs per row.
Is it possible to resize (maybe in clear css?) inner divs according to number per row?
So, in example first row divs would have 200px width and in second row 300px width.
You can do this with display: table-cell. Browser support: http://caniuse.com/css-table
See: http://jsfiddle.net/thirtydot/x5ZFg/
HTML: (changed slightly from your question)
<div id="outer">
<div class="innerWrap"> <!--row1-->
<div class="inner"></div>
<div class="inner"></div>
<div class="inner"></div>
</div>
<div class="innerWrap"> <!--row2-->
<div class="inner"></div>
<div class="inner"></div>
</div>
</div>
CSS:
#outer {
width: 600px
}
.innerWrap {
width: 100%;
display: table;
table-layout: fixed
}
.inner {
display: table-cell;
height: 50px;
border: 1px dashed #f0f
}