Stretch items elements height in a Flexbox nav with fixed height - html

I have a navigation, with fixed height, that use the flexbox layout module. It have two items (ul elements) where in one I want that its li tags take all the height of the nav and the other one I want that its li tags behave normally. I tought that assigning display:block to the li tags that I want they take all the height of the nav would work, but it behaves as a display:inline-block.
nav {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #000;
height: 65px;
}
nav ul {
list-style-type: none;
padding-left: 0;
}
nav ul li {
float: left;
}
nav ul.item1 li {
display: block;
background-color: red;
}
nav ul.item2 li {
display: inline-block;
background-color: green;
}
<nav>
<ul class="item1">
<li>#1</li>
<li>#2</li>
<li>#3</li>
</ul>
<ul class="item2">
<li>#4</li>
<li>#5</li>
<li>#6</li>
</ul>
</nav>

Short answer is to use nested flexbox.
Remove align-items: center; from nav, so that flex items can stretch (default behavior) to the same height as the container.
Reset the margin by adding ul { margin: 0; }, so that no top and bottom margins.
Remove all the float and inline-block, you don't need it in flexbox layout.
Apply display: flex; to all - nav, ul, and li.
Finally add align-items: center; to the li to center the text vertically.
nav {
display: flex;
justify-content: space-between;
background-color: black;
height: 65px;
}
nav ul {
list-style: none;
padding: 0;
margin: 0;
display: flex;
}
nav li {
display: flex;
align-items: center;
}
nav ul.item1 li {
background-color: red;
}
nav ul.item2 li {
background-color: green;
}
<nav>
<ul class="item1">
<li>#1</li>
<li>#2</li>
<li>#3</li>
</ul>
<ul class="item2">
<li>#4</li>
<li>#5</li>
<li>#6</li>
</ul>
</nav>
Update
Modified version for just the left side fully stretches to height, and the right side behaves as inline elements.
nav {
display: flex;
justify-content: space-between;
background-color: black;
height: 65px;
}
nav ul {
list-style: none;
padding: 0;
margin: 0;
display: flex;
}
nav li {
display: flex;
}
nav ul.item1 li {
align-items: center;
background-color: red;
}
nav ul.item2 {
align-items: center;
}
nav ul.item2 li {
background-color: green;
}
<nav>
<ul class="item1">
<li>#1</li>
<li>#2</li>
<li>#3</li>
</ul>
<ul class="item2">
<li>#4</li>
<li>#5</li>
<li>#6</li>
</ul>
</nav>

Just add height: 100% to both the ul and li.
nav {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #000;
height: 65px;
}
nav ul {
list-style-type: none;
padding-left: 0;
height: 100%;
display: flex;
align-items: center;
}
nav ul li {
float: left;
display: flex;
align-items: center;
}
nav ul.item1 li {
background-color: red;
height: 100%;
}
nav ul.item2 li {
background-color: green;
}
<nav>
<ul class="item1">
<li>#1</li>
<li>#2</li>
<li>#3</li>
</ul>
<ul class="item2">
<li>#4</li>
<li>#5</li>
<li>#6</li>
</ul>
</nav>

Related

Why isn't ul using full nav's height?

I'm having some problems creating a header.
Here, my ul is not using the full height of the nav, but the nav IS using the full height of the header.
I want to fix this in order to center vertically the text.
Thank you.
(edit) When I use height: 100%; on the ul, this happens.
This is my HTML:
<header>
<div class="logo">
<img src="img/logo.png"/>
</div>
<nav>
<ul>
<li>HOME</li>
<li>ABOUT</li>
<li>WORKS</li>
<li>CONTACT</li>
</ul>
</nav>
</header>
This is my SASS:
$BLACK:#000;
$WHITE:#fff;
* {
box-sizing: border-box;
}
header {
width: 100%;
height: 75px;
background-color: $BLACK;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
.logo {
width: 50%;
display: flex;
align-items: center;
padding-left: 50px;
}
nav {
width: 50%;
padding-right: 50px;
ul {
list-style-type: none;
display: flex;
flex-flow: row wrap;
justify-content: right;
li {
display: inline-block;
margin-left:50px;
a {
text-decoration: none;
color: $WHITE;
}
}
}
}
}
Here is a solution for this. Just add line-height: 75px; to the li elements and set the ul element's margin to zero by adding margin: 0px;.
* {
box-sizing: border-box;
}
header {
width: 100%;
height: 75px;
background-color: #000;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
}
header .logo {
width: 50%;
display: flex;
align-items: center;
padding-left: 50px;
}
header nav {
width: 50%;
padding-right: 50px;
}
header nav ul {
list-style-type: none;
display: flex;
flex-flow: row wrap;
justify-content: right;
margin: 0px;
}
header nav ul li {
display: inline-block;
margin-left: 50px;
line-height: 75px;
}
header nav ul li a {
text-decoration: none;
color: #fff;
}
<header>
<div class="logo">
<img src="img/logo.png"/>
</div>
<nav>
<ul>
<li>HOME</li>
<li>ABOUT</li>
<li>WORKS</li>
<li>CONTACT</li>
</ul>
</nav>
</header>
However I converted your SASS code into CSS to run it on the stack overflow code snippet runner. You can change this to SASS again if you want.
Thanks and best regards!
Its probably because you did not reset the margin for the ul tag and also apply a 100% percent height to it also.
$BLACK: #000;
$WHITE: #fff;
* {
box-sizing: border-box;
// margin: 0;
// padding: 0;
}
header {
width: 100%;
height: 75px;
background-color: $BLACK;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
.logo {
width: 50%;
display: flex;
align-items: center;
padding-left: 50px;
}
nav {
width: 50%;
padding-right: 50px;
background: red;
ul {
list-style-type: none;
display: flex;
flex-flow: row wrap;
justify-content: right;
background: orange;
height: 100%;
li {
display: inline-block;
margin-left: 50px;
a {
text-decoration: none;
color: $WHITE;
}
}
}
}
}
<header>
<div class="logo">
<img src="https://via.placeholder.com/20x20.png/09f/fff" />
</div>
<nav>
<ul>
<li>HOME</li>
<li>ABOUT</li>
<li>WORKS</li>
<li>CONTACT</li>
</ul>
</nav>
</header>
Please update your css with following code:
$BLACK:#000;
$WHITE:#fff;
* {
box-sizing: border-box;
}
header {
width: 100%;
height: 75px;
background-color: $BLACK;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
.logo {
width: 50%;
display: flex;
align-items: center;
padding-left: 50px;
}
nav {
width: 50%;
padding-right: 50px;
height: 100%;
display: flex;
align-items: center;
ul {
list-style-type: none;
display: flex;
flex-flow: row wrap;
justify-content: right;
flex: 1 0 auto;
align-self: stretch;
align-items: center;
li {
display: inline-block;
margin-left:50px;
a {
text-decoration: none;
color: $WHITE;
}
}
}
}
}

How to align side by side 2 differents component in angular with Flexbox

How can I align 2 different Component side by side using flexbox ?
I have my 1st component "nav-menu" and the 2nd "homepage" I would like it to be side by side.
homepage.component.html
<app-nav-menu></app-nav-menu>
<div class="container">
<div>
<img src="https://colorlib.com/preview/theme/amado/img/product-img/pro-big-1.jpg">
</div>
</div>
I gave a width to my container
homepage.component.css
.container {
display: flex;
width: 50%;
}
div.container>div {
flex-grow: 1;
}
img {
width: 20%;
height: auto;
}
nav-menu.component.html
<h1>Furniture</h1>
<nav class="nav" role="navigation">
<ul clas="menu">
<li>Home</li>
<li>Shop</li>
<li>Product</li>
<li>Cart</li>
</ul>
</nav>
I gave a width to my nav
nav-menu.component.css
* {
box-sizing: border-box;
}
h1 {
display: flex;
justify-content: space-between;
margin-left: 30px;
margin-bottom: 100px;
}
nav {
width: 40%;
}
.nav ul {
margin: 0px;
padding: 0px;
background: white;
list-style: none;
}
.nav a {
display: flex;
flex-direction: column;
align-items: flex-start;
padding: 0.5em;
text-decoration: none;
color: black;
font-size: 2em;
width: 10px;
}
.nav a:hover {
color: #FFA500;
transition: 0.1s;
}
whats the next step ?
thanks.
you have to wrap your both components in a flex
<div style="display: flex; flex-grow: grow">
<app-component-1></app-component-1>
<app-component-2></app-component-2>
</div>

Why is the ul with class "subnav" not bigger?

The ul with class "subnav" should be bigger. I noticed it because the background color is only shown in a small area. If i hover nav ul li the "subnav" ul is not going out of the nav. Strangely the li a inside "subnav" are shown correct and are going outside the nav just fine.
Only when I give the "subnav" a specific height it will go outside the nav.
header {
background-image: url("../images/header.jpg");
background-size: cover;
background-position: 0 -15vh;
background-repeat: no-repeat;
height: 40vh;
display: flex;
align-items: flex-end;
}
nav {
width: 100%;
height: 10vh;
background-color: rgba(255, 255, 255, 0.75);
}
nav ul {
height: 100%;
display: grid;
grid-template-columns: 3fr 1fr 1fr 1fr;
align-items: center;
}
nav ul li {
position: relative;
}
nav ul li a {
color: #1b1a5c;
}
nav ul li .far {
margin-right: 0.5em;
color: #1b1a5c;
}
nav ul li .fas {
margin-right: 0.5em;
color: #1b1a5c;
}
#logo {
height: 100%;
}
#logo img {
max-height: 100%;
}
.subnav {
display: none;
position: absolute;
background-color: #b18e4a;
width: 80%;
transform: translateX(10%);
padding: 0.8em;
z-index: 1;
}
.subnav li {
margin: 0.4em;
}
.subnav li a {
color: white;
}
.subnav li a:hover {
color: #1b1a5c;
}
nav ul li:hover .subnav {
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
}
<header>
<nav>
<ul>
<li id="logo">
<img src="images/salonorchesterlogo.png" alt="Logo des Salonorchester Zürich Oberland">
</li>
<li><i class="far fa-calendar-alt"></i>Konzerte
<ul class="subnav">
<li>Kalender</li>
<li>Billette</li>
</ul>
</li>
<li><i class="far fa-newspaper"></i>News</li>
<li><i class="fas fa-info"></i>Über uns
<ul class="subnav">
<li>Orchester</li>
<li>Vorstand</li>
<li>Dirigent</li>
<li>Kontakt</li>
</ul>
</li>
</ul>
</nav>
</header>
The height: 100% property you are setting in the nav ul selector is targeting the top-level ul inside nav as well as your ul.subnav. You could either change the nav ul to nav > ul if you intend it to be specific only to the top-level ul or you could override that height in your .subnav block:
.subnav {
display: none;
position: absolute;
background-color: #b18e4a;
width: 80%;
transform: translateX(10%);
padding: 0.8em;
z-index: 1;
height: auto; /* ADD THIS TO OVERRIDE HEIGHT: 100% SET IN `nav ul` block */
}
You can see this in action in the snippet below:
header {
background-image: url("../images/header.jpg");
background-size: cover;
background-position: 0 -15vh;
background-repeat: no-repeat;
height: 40vh;
display: flex;
align-items: flex-end;
}
nav {
width: 100%;
height: 10vh;
background-color: rgba(255, 255, 255, 0.75);
}
nav ul {
height: 100%;
display: grid;
grid-template-columns: 3fr 1fr 1fr 1fr;
align-items: center;
}
nav ul li {
position: relative;
}
nav ul li a {
color: #1b1a5c;
}
nav ul li .far {
margin-right: 0.5em;
color: #1b1a5c;
}
nav ul li .fas {
margin-right: 0.5em;
color: #1b1a5c;
}
#logo {
height: 100%;
}
#logo img {
max-height: 100%;
}
.subnav {
display: none;
position: absolute;
background-color: #b18e4a;
width: 80%;
transform: translateX(10%);
padding: 0.8em;
z-index: 1;
height: auto; /* ADD THIS TO OVERRIDE HEIGHT: 100% SET IN `nav ul` block */
}
.subnav li {
margin: 0.4em;
}
.subnav li a {
color: white;
}
.subnav li a:hover {
color: #1b1a5c;
}
nav ul li:hover .subnav {
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
}
<header>
<nav>
<ul>
<li id="logo">
<img src="images/salonorchesterlogo.png" alt="Logo des Salonorchester Zürich Oberland">
</li>
<li><i class="far fa-calendar-alt"></i>Konzerte
<ul class="subnav">
<li>Kalender</li>
<li>Billette</li>
</ul>
</li>
<li><i class="far fa-newspaper"></i>News</li>
<li><i class="fas fa-info"></i>Über uns
<ul class="subnav">
<li>Orchester</li>
<li>Vorstand</li>
<li>Dirigent</li>
<li>Kontakt</li>
</ul>
</li>
</ul>
</nav>
</header>
For what it is worth, I would also revisit your decision to set the positioning to absolute and then use translateX to position the element, as opposed to just using some combination of top, left, right, and bottom. I don't know that there is anything explicitly wrong with that, but I wonder if you might get some unexpected behavior by mixing your intentions there.
Here try this. I gave your .subnav class the style height: auto;. Also I removed your padding and width styles.
.subnav {
height: auto;
display: none;
position: absolute;
background-color: #b18e4a;
/*transform: translateX(10%);*/
z-index: 1;
}
add the height to nav ul li:hover .subnav
nav ul li:hover .subnav {
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
height: 150px
}

Vertically center UL and horizontally align LI children

I want my UL to do the following things:
I want to have my UL vertically centered in Nav. I tried using a
wrapper with display: table; but that didn't work.
I want my LI's to be evenly spread horizontally across the width of
the UL, which I did with display: flex; just not perfectly.
And I need it to be independent from the height because that might change later on.
I have the following code:
nav {
width: 780px;
height: 50px;
text-align: center;
background-color: pink;
}
nav ul {
list-style-type: none;
display: flex;
}
nav ul li {
display: inline-block;
margin: 0 auto;
padding: 10px;
background-color: lightgreen;
}
<nav>
<ul>
<li>sample</li>
<li>sample</li>
<li>sample</li>
</ul>
</nav>
fiddle
I hope you can help me, Thanks in advance.
Just a bit of tweaking to your flexbox layout:
nav {
width: 780px;
height: 50px;
text-align: center;
background-color: pink;
}
nav ul {
height: 100%; /* take height of nav parent */
list-style-type: none;
padding-left: 0; /* remove default UL padding */
display: flex;
justify-content: space-around; /* horizontal alignment (applies to child elements) */
align-items: center; /* vertical alignment (applied to child elements) */
}
nav ul li {
/* display: inline-block; <-- not necessary */
/* margin: 0 auto; <-- not necessary */
padding: 10px;
background-color: lightgreen;
}
<nav>
<ul>
<li>sample</li>
<li>sample</li>
<li>sample</li>
</ul>
</nav>
Revised Fiddle
It can be done by using display:table & display:table-cell. Please check this updated Fiddle
nav {
width: 780px;
height: 50px;
text-align: center;
background-color: pink;
display: table;
}
nav ul {
list-style-type: none;
display: table-cell;
vertical-align: middle;
padding: 0;
}
nav ul li {
padding: 10px;
background-color: lightgreen;
width: 33.33%;
box-sizing: border-box;
float: left;
}

On hover, li items should take full height of the container

I'd like to make the li elements tall as the height of the header.
CodePen
*,
::before,
::after {
border-box: box-sizing;
margin: 0; padding: 0;
}
header {
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-items: center;
background: yellow;
height: 7.5vh;
}
.navigation li {
display: inline-block;
padding: 0 1em;
}
.navigation li:hover {
text-align: center;
background-color: white;
}
<header>
<h1>Logo</h1>
<nav>
<ul class="navigation">
<li>Home</li>
<li>About</li>
<li>Products</li>
<li>Contact</li>
</ul>
</nav>
</header>
One way to achieve this is to utilize the line-height property and make it the same as the height you defined for your <header>:
.navigation li:hover {
text-align: center;
background-color: white;
line-height: 7.5vh;
}
Here's an updated CodePen. Hope this helps! Let me know if you have any questions.
Add display:block in .navigation li:hover.