Dropdown in Flex Navbar pushes content - html

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>

Related

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

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;
}

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>

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>

Header align center irrespective of device width in css

I am stuck with a css problem where i need to align my header to center irrespective to the device width, i.e for any device the header should appear in center.
Here i am providing the css that is already applied.
ul.nav.nav-tabs{
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row nowrap;
-ms-flex-flow: row nowrap;
flex-flow: row nowrap;
margin: 0;
padding: 0;
padding-left: 10px;
}
ul.nav.nav-tabs li{
list-style: none;
margin: 0;
padding: 0;
position: relative;
white-space: wrap;
}
ul.nav.nav-tabs li:last-child:after{
content: "";
background: url("");
}
ul.nav.nav-tabs li.active:after {
background: url(../images/right-chevron-orange.png) center right -3px no-repeat;
background-size: 16px;
}
ul.nav.nav-tabs li:after{
content: "";
background: url(../images/right-chevron.png) center right -3px no-repeat;
width: 20px;
background-size: 16px;
position: absolute;
height: 20px;
right: 6px;
top: 18px;
}
.nav-tabs>li{
width: auto;
height: auto;
margin-bottom: 5px;
}
<ul class="nav nav-tabs ng-scope" id="navigation-tabs" ng-controller="NavCtrl">
<li class="increase_min_width active" ng-class="{active : navPath == 'basic'}">
<div class="wrap">
<span class="circle">1</span>
<a style="text-decoration:none;">Basic Information</a>
</div>
</li>
<li class="increase_min_width disabled" ng-class="{active : navPath == 'company',disabled:moveToCompanyFlg ==''}">
<div class="wrap">
<span class="circle">2</span>
<a style="text-decoration:none;" class="company_information">Company Information</a>
</div>
</li>
<li ng-class="{active : navPath == 'owner',disabled:moveToOwnerFlg ==''}" class="disabled">
<div class="wrap">
<span class="circle">3</span>
<a style="text-decoration:none;">Owners Info</a>
</div>
</li>
</ul>
I have tried setting margin:0 auto; and width:100% for ul.nav.nav-tabs but it doesn't work. I have not much knowledge in css.
Please help me with this... Thank you in advance.
Your flexbox css is incomplete
Add a justify-content: center; to your .nav-tabs
Add justify-content:center; to ul class .nav-tabs
Check this pen
`https://codepen.io/anon/pen/odRMWV`
Add justify-content: center; in the ul.nav.nav-tabs CSS
ul.nav.nav-tabs{
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row nowrap;
-ms-flex-flow: row nowrap;
flex-flow: row nowrap;
margin: 0;
padding: 0;
padding-left: 10px;
width: 100%;
justify-content: center;
}
ul.nav.nav-tabs li{
list-style: none;
margin: 0;
padding: 0;
position: relative;
white-space: wrap;
}
ul.nav.nav-tabs li:last-child:after{
content: "";
background: url("");
}
ul.nav.nav-tabs li.active:after {
background: url(../images/right-chevron-orange.png) center right -3px no-repeat;
background-size: 16px;
}
ul.nav.nav-tabs li:after{
content: "";
background: url(../images/right-chevron.png) center right -3px no-repeat;
width: 20px;
background-size: 16px;
position: absolute;
height: 20px;
right: 6px;
top: 18px;
}
.nav-tabs>li{
width: auto;
height: auto;
margin-bottom: 5px;
}
<ul class="nav nav-tabs ng-scope" id="navigation-tabs" ng-controller="NavCtrl">
<li class="increase_min_width active" ng-class="{active : navPath == 'basic'}">
<div class="wrap">
<span class="circle">1</span>
<a style="text-decoration:none;">Basic Information</a>
</div>
</li><br>
<li class="increase_min_width disabled" ng-class="{active : navPath == 'company',disabled:moveToCompanyFlg ==''}">
<div class="wrap">
<span class="circle">2</span>
<a style="text-decoration:none;" class="company_information">Company Information</a>
</div>
</li>
<li ng-class="{active : navPath == 'owner',disabled:moveToOwnerFlg ==''}" class="disabled">
<div class="wrap">
<span class="circle">3</span>
<a style="text-decoration:none;">Owners Info</a>
</div>
</li>
</ul>

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>