The general idea is to have 2 rows of equal heights and the first row contain 2 columns of equal width for a full page layout. The problem I'm running into is that when one of the cells fill up with children elements, the parent row's height expands overtaking the sibling row when the heights should be equal.
body {
min-height: 100vh;
height: auto;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
display: flex;
flex-direction: column;
}
.row {
border: dashed 1px;
flex: 1;
}
.row1 {
display: flex;
}
.cell {
flex: 1;
padding: 8px;
border: dashed 1px black;
margin: 4px;
display: flex;
flex-direction: column;
}
.title {
border: solid 1px;
padding: 8px;
font-size: 24px;
}
.things {
flex: 1;
margin: 8px 0 0;
padding: 0;
overflow-y: scroll;
}
.things li {
list-style-type: none;
padding: 4px;
border: solid 1px;
margin: 8px 8px 8px;
}
<div class="content">
<div class="row row1">
<div class="cell">
<div class="title">Cell 1</div>
<ul class="things">
<li>thing 1</li>
<li>thing 2</li>
<li>thing 3</li>
<li>thing 4</li>
<li>thign 5</li>
<li>thing 1</li>
<li>thing 2</li>
<li>thing 3</li>
<li>thing 4</li>
<li>thign 5</li>
</ul>
</div>
<div class="cell">
<div class="title">Cell 2</div>
</div>
</div>
<div class="row row2">Row 2</div>
</div>
Since flex is a shorthand property, flex: 1 means
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;
But for some reason, the 0% seems to confuse Chrome. So add flex-basis: 0 manually:
.row { flex-basis: 0; }
And since Firefox implements the new auto as the initial value of min-height, it needs
.row { min-height: 0; }
So the final code is
.row {
flex: 1;
flex-basis: 0;
min-height: 0;
}
/* Styles go here */
body {
min-height: 100vh;
height: auto;
display: flex;
flex-direction: column;
}
header {
background: #000;
color: #fff;
display: flex;
justify-content: space-between;
}
a {
color: #fff;
padding: 4px;
text-decoration: none;
}
a:hover {
background: #fff;
color: #000;
}
.content {
flex: 1;
display: flex;
flex-direction: column;
}
.row {
border: dashed 1px;
flex: 1;
flex-basis: 0;
min-height: 0;
}
.row1 {
display: flex;
}
.cell {
flex: 1;
padding: 8px;
border: dashed 1px black;
margin: 4px;
display: flex;
flex-direction: column;
}
.title {
border: solid 1px;
padding: 8px;
font-size: 24px;
}
.things {
flex: 1;
margin: 8px 0 0;
padding: 0;
overflow-y: scroll;
}
.things li {
list-style-type: none;
padding: 4px;
border: solid 1px;
margin: 8px 8px 8px;
}
<header>
Home
Acct
</header>
<div class="content">
<div class="row row1">
<div class="cell">
<div class="title">Cell 1</div>
<ul class="things">
<li>thing 1</li>
<li>thing 2</li>
<li>thing 3</li>
<li>thing 4</li>
<li>thign 5</li>
</ul>
</div>
<div class="cell">
<div class="title">Cell 2</div>
</div>
</div>
<div class="row row2">Row 2</div>
</div>
Related
In my header, I'm trying to put my name in the middle item below the first item "brush up". I've tried justify-content and align-items and just can't get it to move. I also want the navigation to stay on the right side. New to flexbox thank you for any help.
.container {
display: flex;
margin: 0 auto;
max-width: 960px;
flex-direction: column;
}
/* HEADER SECTION***************************************/
header {
width: 100%;
height: 8vh;
display: flex;
flex-direction: row;
outline: 1px solid red;
}
header * {
margin: 0;
padding: 0;
display: block;
}
header h1 {
outline: 1px solid red;
height: 50px;
flex: 3;
}
header p {
flex: 2;
outline: 1px solid red;
height: 25px;
align-items: baseline;
}
header ul {
flex: 1;
justify-content: flex-end;
outline: 1px solid red;
list-style-type: none;
}
header ul li {
margin: 0 0.5em;
}
<header>
<h1>Brushing up</h1>
<p>by Keller Johnson</p>
<ul>
<li>home</li>
<li>project 1</li>
<li>project 2</li>
</ul>
</header>
Are you looking for this?
header {
width: 100%;
display: flex;
}
header, header > * {
outline: 1px solid red;
}
header > div {
flex-grow: 1;
}
header > ul {
flex: 0 0 200px;
display: flex;
flex-direction: column;
justify-content: space-evenly;
list-style-type: none;
}
header * {
margin: 0;
padding: 0;
}
header h1 {
height: 50px;
}
header p {
outline: 1px solid red;
height: 25px;
}
header ul li {
margin: 0 0.5em;
}
<header>
<div>
<h1>Brushing up</h1>
<p>by Keller Johnson</p>
</div>
<ul>
<li>home</li>
<li>project 1</li>
<li>project 2</li>
</ul>
</header>
If this is not the result you want, consider explaining more clearly exactly what you're trying to achieve. Since we're talking about styling, a picture would help.
I'm using CSS flexbox to create a vertical centered navigation:
#main-nav {
display: flex;
height: 400px;
width: 50%;
background: #ccc;
align-items: center;
padding: 50px;
ul {
list-style: none;
padding: 0;
margin: 0;
}
}
The child elements should stay among themselves. For now the elements are vertical centered but they stay next to each other. Is there a fancy solution or some workaround for doing that? Notice that the flex-direction is set to row.
The result should look like this:
RESULT
Check it out:
#main-nav {
display: flex;
height: 400px;
width: 50%;
background: #ccc;
align-items: center;
padding: 50px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
<nav id="main-nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Products</li>
<li>Contact</li>
</ul>
<div class="contact-info">
Lorem Ipsum
</div>
</nav>
https://jsfiddle.net/4d58xrz8/
If I understood, flex-direction is what you need, and replace align-items by justify-content, because the direction changes.
Edit you css like this to obtain your result :
#main-nav {
display: flex;
height: 400px;
width: 50%;
background: #ccc;
justify-content: center;
flex-direction:column;
padding: 50px;
ul {
list-style: none;
padding: 0;
margin: 0;
}
}
Add flex-direction:column; to your #main-nav
#main-nav {
display: flex;
height: 400px;
width: 50%;
background: #ccc;
flex-direction:column;
align-items: left;
padding: 50px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
<nav id="main-nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Products</li>
<li>Contact</li>
</ul>
<div class="contact-info">
Lorem Ipsum
</div>
</nav>
I want to have a menu that has a logo at left and the menu items at right, Im using flexbox for this but its not working. The menu items are stick to the logo. Do you know where is the issue?
This is the html:
<div class="container">
<header class="Header content">
<h1 class="title">LOGO</h1>
<ul class="main_nav">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<div class="clear"></div>
</header>
</div>
example: https://jsfiddle.net/adwkkvt6/
css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
list-style: none;
margin:0;
padding: 0;
}
.container {
float: left;
width: 100%;
}
.content {
width: 94%;
margin: 0 auto;
}
.Header, .main_nav{
display: flex;
}
.title a{
color:green;
font-size: 0.85em;
}
.main_nav li{
padding: 0 15px;
}
.main_nav{
background-color: red;
justify-content: space-between;
align-items: center;
}
.main_nav a{
font-weight: 700;
font-size: 0.85em;
color:gray;
}
Add this
.title{
flex:1 0 0;
}
title is a child so you have to give space to how mach occupy the parent div so use flex:1 0 0 to get space for logo.
flex:1 0 0 is -
flex-grow:1;
flex-shrink:0;
flex-basis:0;
More info about flex visit.
Updated fiddle link
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
.container {
float: left;
width: 100%;
}
.content {
width: 94%;
margin: 0 auto;
}
.Header,
.main_nav {
display: flex;
}
.title {
flex: 1 0 0;
}
.title a {
color: green;
font-size: 0.85em;
}
.main_nav li {
padding: 0 15px;
}
.main_nav {
background-color: red;
justify-content: space-between;
align-items: center;
}
.main_nav a {
font-weight: 700;
font-size: 0.85em;
color: gray;
}
<div class="container">
<header class="Header content">
<h1 class="title">LOGO</h1>
<ul class="main_nav">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<div class="clear"></div>
</header>
</div>
If you remove the <div class="clear"></div>, you can use justify-content: space-between like that
.Header, .main_nav{
display: flex;
justify-content: space-between;
}
You have to specify the positioning of the flexbox items:
.Header, .main_nav{
display: flex;
justify-content: space-between;
}
the default value of justify-content is flex-start which pushes each item to the left of the flexbox.
I am trying to center the outer 'div' container using Flexbox. I have an unordered list with 3 li's. The li's width is: width: calc(100%/3). The ul's width is 70%. The problem is that when I try centering the ul (justify-content: center), it doesn't get centered.
I finally figured out the source of the problem. When I remove the line: width: calc(100%/3), it centers properly. My question is: How can I get it to center properly?
I tried margin: auto, but that didn't work.
Here's the JSFiddle, and here's the code snippet:
#flex-container {
width: 70%;
list-style-type: none;
padding: 0;
margin: 0;
display: -webkit-inline-flex;
display: inline-flex;
-webkit-justify-content: center;
justify-content: center;
}
li {
box-sizing: border-box;
background-color: tomato;
width: calc(100%/3);
}
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
When I remove the line: width: calc(100%/3), it centers properly
You should not calculate the width when you are using flex layout, because that is what flex is itself supposed to do.
If you are looking to align the text inside of the lis then text-align is what you need. You should also remove the width from the lis and use the flex property instead.
Snippet:
* { box-sizing: border-box; padding: 0; margin: 0; }
#flex-container {
list-style-type: none;
width: 70%; display: flex;
}
li {
flex: 1 1 auto;
background-color: tomato; border: 1px solid #fff;
text-align: center;
}
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
If you are looking to have variable width lis then justify-content is what you need. You should control the width via the width property and use flex property as required to expand or shrink.
Snippet:
* { box-sizing: border-box; padding: 0; margin: 0; }
#flex-container {
list-style-type: none; width: 70%;
display: flex; justify-content: center;
background-color: #eee;
}
li {
flex: 0 0 auto; width: 15%;
background-color: tomato; border: 1px solid #fff;
}
li:first-child { width: 20%; }
li:last-child { width: 30%; }
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
I modified your code:
http://jsfiddle.net/hrr65ajr/2/
#flex-container {
width: 70%;
list-style-type: none;
padding: 0;
margin: 0;
display: -webkit-inline-flex;
display: inline-flex;
-webkit-justify-content: center;
justify-content: center;
}
li {
box-sizing: border-box;
background-color: tomato;
margin: auto;
width: calc(100%/3);
text-align: center;
}
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
Try this:
HTML
<div class="center">
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
</div>
CSS
.center {
display:flex;
-webkit-justify-content: center;
justify-content: center;
}
#flex-container {
width: 70%;
list-style-type: none;
padding: 0;
margin: auto 0;
display: -webkit-inline-flex;
display: inline-flex;
-webkit-justify-content: center;
justify-content: center;
}
li {
box-sizing: border-box;
background-color: tomato;
width: calc(100%/3);
}
I have a page with nested flex boxes here: http://jsfiddle.net/fr0/6dqLh30d/
<div class="flex-group">
<ul class="flex-container red">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
</ul>
<ul class="flex-container gold">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>
<ul class="flex-container blue">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
<li class="flex-item">7</li>
<li class="flex-item">8</li>
</ul>
<div>
And the (relevant) CSS:
.flex-group {
display: flex;
flex-direction: row;
height: 500px;
}
.flex-container {
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
display: flex;
flex-direction: column;
flex-wrap: wrap;
flex: 0 0 auto;
}
.flex-item {
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
The outer flexbox (.flex-group) is meant to lay out from left-to-right. The inner flexboxes (.flex-container) are meant to layout from top-to-bottom, and should wrap if there isn't enough space (which there isn't in my jsfiddle). What I want to happen, is that the .flex-containers will grow in the X direction when the wrapping happens. But that's not occurring. Instead, the containers are overflowing.
What I want it to look like (in Chrome): https://dl.dropboxusercontent.com/u/57880242/flex-good.png
Is there a way to get the .flex-containers to size appropriately in the X direction? I don't want to hard-code their widths, since the items that appear in these lists will be dynamic.
I've been playing with this for a few minutes now, and I think I've got what you're looking for.
Here's the CSS
.flex-group {
margin: auto;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.flex-container {
flex: 0 1 auto;
display: flex;
flex-flow: row wrap;
align-items: space-around;
align-content: flex-start;
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
}
.red li {
background: red;
}
.gold li {
background: gold;
}
.blue li {
background: deepskyblue;
}
.flex-item {
flex: 0 1 auto;
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
And here's an updated fiddle to see it in action.
Interesting, I also fiddle around with it. I have given the flex-container a flex:1 100%; This evenly space the containers to 100%; The blocks will flow in their own container space and the containers keep equal height and weight no matter how you size the window.
.flex-container {
flex: 0 100%;
display: flex;
flex-flow: row wrap;
align-items: space-around;
align-content: flex-start;
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
}
See fiddle here