Flexbox row pushing columns down - html

I've been using flex layouts for a while now.
mainly the holy grail type layout and it works well.
Today decided to try create the same thing to help me learn more about flexbox.
but using columns on the right side for sidebar area, and rows as the logo/heading and page div's.
I've put this in a codepen so you can see the markup code yourself.
my issue is the logo/heading and main page when set to row pushes the columns downwards.
<div class="wrapper">
<div class="nav">logo content and site banner goes here
<div class="spacer">
</div>
<div class="content">main page content goes here</div>
</div>
<div class="one">page index goes here</div>
<div class="two">twitter logo goes here</div>
<div class="three">chatbox goes here</div>
</div>
div {
box-sizing: border-box;
}
.wrapper {
border: 2px solid blue;
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: flex-end;
position: relative;
}
.nav {
flex-direction: row;
border: 2px solid green;
width: 100%;
height: 100px;
align-self: flex-start;
padding-left: 8px;
margin-bottom: 4px;
}
.spacer {
height: 140px;
width: 100%;
}
.content {
flex-direction: row;
border: 2px solid green;
height: 40px;
padding-left: 8px;
align-self: flex-start;
}
.one {
width: 200px;
height: 200px;
border: 2px solid red;
flex-grow: 0;
padding: 8px;
}
.two {
width: 200px;
height: 200px;
border: 2px solid red;
flex-grow: 0;
padding: 8px;
}
.three {
width: 200px;
height: 200px;
border: 2px solid red;
flex-grow: 0;
padding: 8px;
}
https://codepen.io/anon/pen/GmZWvp
even if setting the width to something smaller before it even reaches the columns
the block pushes the columns, down, how could i do this where it doesn't effect the column on the right?
I know i could position: absolute the main page div, or create a nested div in logo/heading
and give it height of say 120px; to get it to go below the logo heading.
But there must be a way of putting a row next to a column without it pushing the column down.
since the div/row for the logo heading/main page is a block element.
If you know how to do this please let me know what to change, where and why etc.
Again this isn't for production, it's just to see if I can solve this quirky puzzle.

First, even if you set display: flex on the wrapper, you can't set flex-direction: row (or column) on its children to change their direction. The flex-direction property should be set on the flex container, in this case the wrapper. If you want to change direction on flex items belonging to one flex container, you need to wrap them and make that nested wrapper be both a flex item and a flex container, here done with the side wrapper in my first sample
If you rearrange your markup a little, you can get this
div {
box-sizing: border-box;
}
.wrapper {
border: 2px solid blue;
display: flex;
}
.nav {
border: 2px solid green;
width: 100%;
height: 100px;
padding-left: 8px;
margin-bottom: 4px;
}
.content {
flex: 1;
border: 2px solid green;
height: 40px;
padding-left: 8px;
}
.side {
width: 200px;
}
.one {
height: 200px;
border: 2px solid red;
padding: 8px;
}
.two {
height: 200px;
border: 2px solid red;
padding: 8px;
}
.three {
height: 200px;
border: 2px solid red;
padding: 8px;
}
<div class="nav">logo and site banner goes here</div>
<div class="wrapper">
<div class="content">main page content goes here</div>
<div class="side">
<div class="one">page index goes here</div>
<div class="two">twitter logo goes here</div>
<div class="three">chatbox goes here</div>
</div>
</div>
If you can't, or don't want, to change markup, then you need to combine flexbox with absolute positioning.
div {
box-sizing: border-box;
}
.wrapper {
position: relative;
display: flex;
flex-direction: column;
}
.nav {
border: 2px solid green;
width: 100%;
height: 100px;
padding-left: 8px;
margin-bottom: 4px;
}
.content {
width: calc(100% - 200px);
border: 2px solid green;
height: 40px;
padding-left: 8px;
}
.one, .two, .three {
position: absolute;
right: 0;
width: 200px;
height: 200px;
border: 2px solid red;
padding: 8px;
}
.one {
top: 104px;
}
.two {
top: 304px;
}
.three {
top: 504px;
}
<div class="wrapper">
<div class="nav">logo and site banner goes here</div>
<div class="content">main page content goes here</div>
<div class="one">page index goes here</div>
<div class="two">twitter logo goes here</div>
<div class="three">chatbox goes here</div>
</div>

Related

Overlapping flex boxes or a different approach?

I'm looking for advice on how to approach the layout as shown in this image.
I'm not sure if flex alone can handle the overlapping green dotted boxes (using a transform?) or if the blocks should be flex boxes and the green dotted overlapping boxes should just be relatively positioned divs? The mobile version is fairly straight-forward as there's no overlapping involved but I'm unsure how to 'slice' the design up so the CSS can handle both situations.
Below is an initial attempt using transform: scale.
.flex-box-row {
display: flex;
justify-content: space-between;
}
.flex-box-row-box {
border: 1px dashed red;
width: 30%;
text-align: center;
min-height: 200px;
}
.flex-box-dots {
max-height: 50px;
border: 1px dashed green;
transform: scale(1.5, 1);
}
.flex-box-dots::after {
content: "..................";
letter-spacing: 4px;
font-size: 18px;
color: black;
margin-left: 10px;
}
<div class="flex-box-row">
<div class="flex-box-row-box">
BLOCK 1
</div>
<div class="flex-box-dots"></div>
<div class="flex-box-row-box">
BLOCK 2
</div>
<div class="flex-box-dots"></div>
<div class="flex-box-row-box">
BLOCK 3
</div>
</div>
You can get the overlapping effect by using negative margin! Here's an example:
.container {
display: flex;
}
.red {
width: 100px;
height: 100px;
border: 2px dotted red;
}
.green {
width: 150px;
height: 20px;
border: 2px dotted green;
margin: 0 -30px;
background-color: white;
z-index: 1;
}
#media (max-width: 500px) {
.container {
flex-direction: column;
align-items: center;
}
.green {
width: 20px;
height: 150px;
margin: 0;
}
}
<div class="container">
<div class="red"></div>
<div class="green"></div>
<div class="red"></div>
<div class="green"></div>
<div class="red"></div>
</div>

Organize DIV content with Bootstrap-4

I am not able to find solution to aligning / organizing my div content with Bootstrap. I don't quite understand how it works
aling-items- *
text-*
aling-self- *
justify-items- *
justify-self- *
I am trying to organize my website in the following way:
but I am not able to align the contents so that it is completely joined to the left or right edges or so that the content is left, whether it be img, text or another div in the center of it. I would like to avoid using padding or margins since I am trying to make a responsive content and then they can give me other problems. Thank you very much for your help, greetings.
This may help you to get started:
fiddle to play-around
.MainDiv {
border: 1px solid red;
height: 210px;
width: 100%;
display: flex;
margin: 5px;
}
.First {
border: 1px solid blue;
width: 33%;
height: 200px;
display: flex;
justify-content: flex-start;
margin: 5px;
}
.FirstSubDiv {
border: 1px solid green;
height: 200px;
width: 50%;
}
.Second {
border: 1px solid blue;
width: 33%;
height: 200px;
display: flex;
justify-content: center;
margin: 5px;
}
.SecondSubDiv {
border: 1px solid green;
height: 200px;
width: 50%;
text-align: center;
}
.Third {
border: 1px solid blue;
width: 33%;
height: 200px;
display: flex;
justify-content: flex-end;
margin: 5px;
}
.ThirdSubDiv {
border: 1px solid green;
height: 200px;
width: 50%;
text-align: right;
}
<div class="MainDiv">
<div class="First">
<div class="FirstSubDiv">
contents are in left;
</div>
</div>
<div class="Second">
<div class="SecondSubDiv">
contents are in center;
</div>
</div>
<div class="Third">
<div class="ThirdSubDiv">
contents are in right;
</div>
</div>
</div>
All the information you require is provided here: https://getbootstrap.com/docs/4.5/layout/grid/
In the beginning, you can just copy and paste the boostrap html and tweak it. Gradually you'll get the gist of it (after test and trial) and you'll be able to work on it on your own.
EDIT
https://getbootstrap.com/docs/4.0/utilities/flex/
View the image attached. If that is what you're looking to do, visit this link

Container not fitting to screen and flex-direction: row not working properly

I'm starting out an application in angular and really struggling getting a basic layout setup; I have experience with angular but actual html/css design is completely new to me
Nothing I've tried seems to allow this container to take up the entirety of the screen. I have tried using multiple different settings on the html and container css classes and nothing will actually fit the container to the screen with width; but the height always seems to fit properly.
Aside from this flex-direction: row does not seem to consistently work. For example, I am trying to get the div "side" and the div "main" inside of the header div to fit next to each-other. Instead of this, those div's act like columns; despite the fact I have nowrap on; I have also tried display: inline-block and that also does not work. I have decreased the width of side and main in hopes that they would then fit next to eachother and that also does not work.
Screenshot:
Full View
html {
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
}
.container {
position: relative;
top: 0;
left: 0;
bottom: 0;
right: 0;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.header {
margin-top: 15px;
border: 1px solid red;
height: 15vh;
flex-direction: row;
flex-wrap: nowrap;
}
.body {
border;
1px solid green;
height: 80vh;
}
.side {
width: 15vw;
border: 1px solid yellow;
}
.main {
width: 50vw;
border: 1px solid blue;
}
<div class="container">
<div class="header">
<div class="side">
<p>HI</p>
<div class="main">
<p>HI2</p>
</div>
</div>
<div class="body">
<p>I am the body</p>
</div>
</div>
</div>
Firstly - there are quite a few typos in your code. You haven't closed the side class or the main class and there is no closing div for the side div.
Secondly - After I'd tidied up your code, I noticed that you were making the .container display: flex; when in fact you needed to make the .header display: flex; as this is the parent of the side and main divs.
This is a great guide for flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
This should work for you:
html {
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
}
.container {
position: relative;
top: 0;
left: 0;
bottom: 0;
right: 0;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.header {
margin-top: 15px;
border: 1px solid red;
height: 15vh;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
}
.side {
width: 15vw;
border: 1px solid yellow;
}
.main {
width: 50vw;
border: 1px solid blue;
}
.body {
border: 1px solid green;
height: 80vh;
}
<div class="container">
<div class="header">
<div class="side">
<p>HI</p>
</div>
<div class="main">
<p>HI2</p>
</div>
</div>
<div class="body">
<p>I am the body</p>
</div>
</div>
main is inside side. if you want them to be beside each other, you will need to arrange them as siblings within the flexbox.
you also forgot to add display: flex on the header css.
try this
html {
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
}
.container {
position: relative;
top: 0;
left: 0;
bottom: 0;
right: 0;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.header {
margin-top: 15px;
border: 1px solid red;
height: 15vh;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.body {
border: 1px solid green;
height: 80vh;
}
.side {
width: 15vw;
border: 1px solid yellow;
}
.main {
width: 50vw;
border: 1px solid blue;
}
<div class="container">
<div class="header">
<div class="side">
<p>HI</p>
</div>
<div class="main">
<p>HI2</p>
</div>
</div>
<div class="body">
<p>I am the body</p>
</div>
</div>

Stretch columns in two columns layout with shared header using flexbox

I'm using flexbox to create a two-columns layout with a header row.
* {
box-sizing: border-box;
position: relative;
}
.container {
border: 2px solid gray;
display: flex;
flex-wrap: wrap;
height: 300px;
}
.header {
flex-basis: 100%;
border: 2px solid magenta;
height: 50px;
line-height: 50px;
text-align: center;
}
.column1 {
flex-basis: 150px;
/* height: calc(100% - 50px); */
border: 2px solid green;
}
.column2 {
/* height: calc(100% - 70px); */
flex: 1;
border: 2px solid orange;
}
<div class='container'>
<div class='header'>it's a header</div>
<div class='column1'>column 1</div>
<div class='column2'>column 2</div>
</div>
Feel free to see the full example here.
As you can see in the example there is a gap between columns and header. My aim is to stretch columns vertically to fill whole empty space in the container.
I can achieve it by setting height property like calc(100% - <header-height>). Is it the correct way?
I just tried to use "flex" style and set align-items: stretch to the container and align-self: stretch to columns but without success. Did I probably miss something trying to implement it this way?
I think specifying flex-direction as column is appropriate in this case.
The second row is itself a flex element with the flex-direction: row. You can fill the rest of the remaining space using flex: 1, which is equivalent to flex-grow: 1.
* {
box-sizing: border-box;
}
.container {
border: 2px solid gray;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
}
.header {
border: 2px solid magenta;
height: 50px;
line-height: 50px;
text-align: center;
}
.subcontainer {
display: flex;
flex-direction: row;
flex: 1;
}
.column1 {
flex-basis: 150px;
border: 2px solid green;
}
.column2 {
flex: 1;
border: 2px solid orange;
}
<div class='container'>
<div class='header'>it's a header</div>
<div class="subcontainer">
<div class='column1'>column 1</div>
<div class='column2'>column 2</div>
</div>
</div>
Do it like shown below
* {
box-sizing: border-box;
}
body,
html {
height: 100%;
}
.container {
border: 2px solid gray;
display: flex;
flex-direction: column;
height: 100%;
}
.header {
width: 100%;
border: 2px solid magenta;
height: 50px;
line-height: 50px;
text-align: center;
}
.body-container {
display: flex;
width: 100%;
flex: 1;
}
.column1 {
width: 50%;
border: 2px solid green;
}
.column2 {
width: 50%;
border: 2px solid orange;
}
<div class='container'>
<div class='header'>it's a header</div>
<div class="body-container">
<div class='column1'>column 1</div>
<div class='column2'>column 2</div>
</div>
</div>

Max Height not working on flexing child with overflow

I have an element with 2 children.
I'm trying to have:
div grow as much as it needs based on 1 of its children
the other always fit the parents height
Thus, I want to avoid setting a height on the parent.
The problem arises when trying to handle overflow of the second child.
Here's the code:
.banner {
display: flex;
background-color: lightblue;
overflow: auto;
border: 4px solid black;
//max-height: 120px; // 1) IF I'M NOT SET THE SCROLL WON'T WORK
}
.constant {
color: white;
flex: 0 0 auto;
width: 200px;
// height: 150px; 2) DISABLED FOR NOW
border: 4px solid yellow;
background-color: olive;
border: 2px solid red;
}
.container {
display: flex;
text-align: center;
}
.main {
max-height: 100%; // 3) I SHOULD STOP MYSELF FROM GROWING MORE THAN MY PARENT
flex: 1;
overflow-y: scroll;
border: 2px solid white;
display: flex;
flex-direction: row-reverse;
align-items: flex-end;
flex-wrap: wrap;
}
.main div {
text-align: center;
width: 200px;
height: 80px;
}
.main-side {
flex: 0 0 auto;
color: white;
background-color: grey;
border: 2px solid yellow;
}
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
background-repeat: no-repeat !important;
min-width: 0px;
min-height: 0px;
}
<div class="banner">
<div class="container">
<div class="main">
<div style="background-color:coral;">A</div>
<div style="background-color:lightgoldenrodyellow;">B</div>
<div style="background-color:khaki;">C</div>
<div style="background-color:pink;">D</div>
<div style="background-color:lightgrey;">E</div>
<div style="background-color:lightgreen;">F</div>
</div>
<div class="main-side">I've a fixed size</div>
</div>
<div class="constant">I can grow...and my parent should grow if I grow</div>
</div>
If I set a fixed height on .banner everything works out, but I would like to avoid doing so if possible.
jsfiddle
Thank you.