Flex Container and child container with justify end and start [duplicate] - html

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
I'm having an issue with my header, on the .container element when I apply flex the child flex-justify doesn't work, but when I remove it I can't align them center.
* {
margin: 0;
padding: 0;
text-decoration: none;
color: white;
}
header {
background: inherit;
border-bottom: 1px solid var(--border);
padding: 1rem 0rem;
background: pink;
}
/*
Remove the flex to preview
*/
header .container{
display: flex;
align-items: center;
}
.right {
display: flex;
align-items: center;
justify-content: flex-end;
background: green;
}
.left {
display: flex;
align-items: center;
justify-content: flex-start;
background: red;
}
<header>
<div class="container">
<div class="logo left">
<a href="#">
MYLOGO(IMG)
</a>
</div>
<nav class="right">
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
</nav>
</div>
</header>
So basically I want a container with flex to align center and its child one justify end and one right.

Simply do this:
header .container {
display: flex;
align-items: center;
justify-content: space-between;
/* this will put the logo.left at the left and .right at the right */
}
* {
margin: 0;
padding: 0;
text-decoration: none;
color: white;
}
header {
background: inherit;
border-bottom: 1px solid var(--border);
padding: 1rem 0rem;
background: pink;
}
/*
Remove the flex to preview
*/
header .container{
display: flex;
align-items: center;
justify-content: space-between;
}
.right {
display: flex;
align-items: center;
justify-content: flex-end;
background: green;
}
.left {
display: flex;
align-items: center;
justify-content: flex-start;
background: red;
}
<header>
<div class="container">
<div class="logo left">
<a href="#">
MYLOGO(IMG)
</a>
</div>
<nav class="right">
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
</nav>
</div>
</header>

#Its Fragilis recommended that I add display flex and justify space around, but because I was using bootstrap it showed some margin on the bottom and left. to fix that you need to add this code:
.clearfix:before, .clearfix:after, .container:before, .container:after, .container-fluid:before, .container-fluid:after, .row:before, .row:after{
content: none !important;
}

Related

list is not aligned vertically center in navbar in the flex

I am not new to flex but I am not able to vertically align the content of the navbar to the center in my code.
<nav className="nav navbar">
<h3> Logo </h3>
<ul className= "nav-link1">
<li className="nav-item1 ">
<Link to="/">Home</Link>
</li>
<li className="nav-item1 ">
<Link to="/button">Button</Link>
</li>
<li className="nav-item1 ">
<Link to="/text">Text</Link>
</li>
</ul>
</nav>
and my CSS is:
.nav{
display: flex;
justify-content: center;
align-items: center;
min-height: 10vh;
background: rgb(250, 172, 3);
color: aliceblue;
}
.nav-link1{
width: 50%;
display: flex;
justify-content:space-between;
align-items: center;
list-style: none;
}
and I also have bootstrap imported into the project.
Any help would be appreciated !!! Thank you.
Replace class with className if using react.
.nav{
display: flex;
flex:1;
justify-content: center;
align-items: center;
min-height: 10vh;
background: rgb(250, 172, 3);
color: aliceblue;
}
.nav-link1{
width: 50%;
display: flex;
flex-direction:row;
justify-content:space-between;
align-items: center;
list-style: none;
}
<nav class="nav">
<h3> Logo </h3>
<ul class= "nav-link1">
<li class="nav-item1 ">
<Link to="/">Home</Link>
</li>
<li class="nav-item1 ">
<Link to="/button">Button</Link>
</li>
<li class="nav-item1 ">
<Link to="/text">Text</Link>
</li>
</ul>
</nav>
In bootstrap, there is a padding-bottom given to the ul element.
Add margin-bottom: 0 to your .nav-link1 class and it will be aligned vertically.

Align a-tags and spans in a list item on the same hight

I try to align sub elements in a list element.
At the moment it looks like this:
If one of the tags, for e.g. the headline tag gets a row more, all other flowing elements are not aligned anymore.
My goal is to get the aligment like following example:
My code looks like this:
#posts {
display: inline-block;
}
#posts li {
list-style: none;
background-color: #ccc;
width: 32%;
float: left;
}
#posts ul {
display: flex;
justify-content: space-around;
}
#posts li a {
padding: 1.5rem;
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.excerpt {
display: flex;
flex-direction: column;
align-items: center;
}
<div id="posts">
<ul>
<li>
<a class="title" href="#">Matcha-Tee?</a>
<span class="excerpt">Teezubereitung
<a class="excerpt-more" href="">Mehr</a>
</span>
</li>
<li>
<a class="title" href="#">Zitronengras?</a>
<span class="excerpt">Zitronengras ist eine besondere Pflanze
<a class="excerpt-more" href="">Mehr</a>
</span>
</li>
<li>
<a class="title" href="#">Wie man heißen Apfel-Zimt-Cider macht</a>
<span class="excerpt">Apfel-Zimt-Cider
<a class="excerpt-more" href="#">Mehr</a>
</span>
</li>
</ul>
</div>
How can I achieve this desired alignment?
Move the flex settings from excerpt to #posts li and apply margin-top: auto to excerpt in order to move everything from there on to the bottom.
(Note: I applied a min-height to your li elements in this snippet to make them higher in order to clearly see the effect.)
#posts {
display: inline-block;
}
#posts li {
display: flex;
flex-direction: column;
align-items: center;
list-style: none;
background-color: #ccc;
width: 32%;
min-height: 300px;
}
#posts ul {
display: flex;
justify-content: space-around;
}
#posts li a {
padding: 1.5rem;
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.excerpt {
margin-top: auto;
}
<div id="posts">
<ul>
<li>
<a class="title" href="#">Matcha-Tee?</a>
<span class="excerpt">Teezubereitung
<a class="excerpt-more" href="">Mehr</a>
</span>
</li>
<li>
<a class="title" href="#">Zitronengras?</a>
<span class="excerpt">Zitronengras ist eine besondere Pflanze
<a class="excerpt-more" href="">Mehr</a>
</span>
</li>
<li>
<a class="title" href="#">Wie man heißen Apfel-Zimt-Cider macht</a>
<span class="excerpt">Apfel-Zimt-Cider
<a class="excerpt-more" href="#">Mehr</a>
</span>
</li>
</ul>
</div>

Dropdown in Flex Navbar pushes content

I'm trying to add a dropdown to a Navbar, however, when hovering on the dropdown button (Kieli, in my case), center alignment pushes the rest of the items out of the way. I have attached a snippet with an example of my problem.
.Navbar {
display: flex;
height: 10vh;
background-color: lightgray;
justify-content: space-between;
align-items: center;
}
.Navbar-menu {
display: flex;
}
.Navbar-dropdown {
display: inline-flex;
flex-wrap: wrap;
flex-direction: column;
}
.Navbar-dropdownMenu {
display: none;
}
.Navbar-dropdown:hover .Navbar-dropdownMenu {
display: flex;
flex-direction: column;
}
<nav class="Navbar">
<a class="Navbar-brand Navbar-link" href="./">Brand</a>
<div class="Navbar-menu">
<a class="Navbar-link Text-uppercase" href="./">Link 1</a>
<a class="Navbar-link Text-uppercase" href="./">Link 2</a>
<div class="Navbar-dropdown">
<button class="Navbar-dropdownBtn">Kieli</button>
<div class="Navbar-dropdownMenu">
<a class="Navbar-link" href="../en-fi/">Suomi</a>
<a class="Navbar-link" href="../en-gb/">Englanti</a>
</div>
</div>
</div>
</nav>
The content is pushed because your dropdown menu is in your Navbar-dropdown. On hover you're displaying the dropdown content and as it takes vertical space it pushes the content.
To avoid that, you have to set the dropdown content position to absolute.
Don't forget to set the position of the Navbar-dropdown to relative.
.Navbar {
display: flex;
height: 10vh;
background-color: lightgray;
justify-content: space-between;
align-items: center;
}
.Navbar-menu {
display: flex;
}
.Navbar-dropdown {
display: inline-flex;
flex-wrap: wrap;
flex-direction: column;
position: relative;
border: 1px solid red;
}
.Navbar-dropdownMenu {
background-color: lightgray;
display: none;
position: absolute;
top: 20px;
right: 0;
}
.Navbar-dropdown:hover .Navbar-dropdownMenu {
display: flex;
flex-direction: column;
}
<nav class="Navbar">
<a class="Navbar-brand Navbar-link" href="./">Brand</a>
<div class="Navbar-menu">
<a class="Navbar-link Text-uppercase" href="./">Link 1</a>
<a class="Navbar-link Text-uppercase" href="./">Link 2</a>
<div class="Navbar-dropdown">
<button class="Navbar-dropdownBtn">Kieli</button>
<div class="Navbar-dropdownMenu">
<a class="Navbar-link" href="../en-fi/">Suomi</a>
<a class="Navbar-link" href="../en-gb/">Englanti</a>
</div>
</div>
</div>
</nav>

Sidebar using Flexbox: contained links not spreading out properly

I´m trying to make a side navigation bar using flexbox. The sidebar looks good, but I want the contained links to be spread out and take over the entire left of the screen. I try to do this using padding, but it ends up leaving a white space at the end. Any ideas?
body {
margin: auto 0;
}
#navbar {
display: flex;
flex-direction: column;
border: solid;
background: grey;
height: 900px;
width: 200px;
text-align: center;
}
.nav-link {
text-decoration: none;
border: solid;
padding: 80px;
}
<nav id="navbar">
<header>"A dissertation on fast food"</header>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
</nav>
Since you're using a flex container, instead of using padding for spacing, take advantage of flex features. The flex-grow property can distribute free space in the container evenly across all items.
body {
margin: auto 0;
}
#navbar {
display: flex;
flex-direction: column;
border: solid;
background: grey;
height: 900px;
width: 200px;
text-align: center;
}
.nav-link {
text-decoration: none;
border: solid;
/* padding: 80px; */
flex-grow: 1; /* equal distribution of free space */
/* for centering the text */
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
<nav id="navbar">
<header>"A dissertation on fast food"</header>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
</nav>
You are having an overflow. You defined the height to be 900px and for each box you have 160px of padding-top/bottom so 800px of padding and if we consider the border plus the content plus the header we will have more than 900px.
Instead of padding you can use flex:1 to stretch the elements then rely on flexbox inside them to center the text:
body {
margin: auto 0;
}
#navbar {
display: flex;
flex-direction: column;
border: solid;
background: grey;
height: 900px;
width: 200px;
text-align: center;
}
.nav-link {
text-decoration: none;
border: solid;
flex:1;
/*to center the text*/
display:flex;
align-items:center;
justify-content:center;
}
<nav id="navbar">
<header>"A dissertation on fast food"</header>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
</nav>

Vertical center with flexbox not working

I'm trying to use flexbox within a bootstrap grid to make a thin menu with items vertically centered but it isn't working. I've attempted to use the styles presented in this demo, but apparently i've still got something incorrect.
Markup
<section class="container secondary-header">
<div class="row">
<div class="col-xs-6">
<nav class="secondary-header__nav">
<ul class="secondary-nav__list-items">
<li class="secondary-nav__list-item">
<a class="secondary-nav__link">Option</a>
</li>
<li class="secondary-nav__list-item">
<a class="secondary-nav__link">Option</a>
</li>
<li class="secondary-nav__list-item">
<a class="secondary-nav__link">Option</a>
</li>
</ul>
</nav>
</div>
<div class="col-xs-6"> </div>
</div>
</section>
SCSS
.secondary-header {
height: 60px;
background-color: #2bf;
}
.secondary-nav {
&__list-items {
display: flex;
align-items: center;
justify-content: space-between;
}
&__nav {
height: 60px;
display: flex;
}
&__list-items {
list-style-type: none;
}
&__list-item {
border: 1px solid;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
// height: 60px;
}
&__list-item {
display: inline;
color: #fff;
}
}
The problem is .secondary-nav__list-items is your flex-parent, but it's height doesn't consume the height of .secondary-header which has the blue background, and is where you want the items within .secondary-nav__list-items to be centered.
You could either make sure everything between .secondary-header and .secondary-nav__list-items has height 100%, so that .secondary-nav__list-items will be as tall as .secondary-header, or just move the height/background to .secondary-nav__list-items instead.
.secondary-nav__list-items {
display: flex;
align-items: center;
justify-content: space-between;
}
.secondary-nav__nav {
height: 60px;
display: flex;
}
.secondary-nav__list-items {
list-style-type: none;
height: 60px;
background-color: #2bf;
}
.secondary-nav__list-item {
border: 1px solid;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
}
.secondary-nav__list-item {
display: inline;
color: #fff;
}
<section class="container secondary-header">
<div class="row">
<div class="col-xs-6">
<nav class="secondary-header__nav">
<ul class="secondary-nav__list-items">
<li class="secondary-nav__list-item">
<a class="secondary-nav__link">Option</a>
</li>
<li class="secondary-nav__list-item">
<a class="secondary-nav__link">Option</a>
</li>
<li class="secondary-nav__list-item">
<a class="secondary-nav__link">Option</a>
</li>
</ul>
</nav>
</div>
<div class="col-xs-6"> </div>
</div>
</section>