Two row navigation, second row elements equal-width nav-justified - html

So trying to achieve responsive collapsible navigation with two rows, where first includes logo and language select and second row main links:
| |
| LOGO lang |
| link1 link2 link3 link4 |
jsfiddle snippet of progress so far
Cant get equal width for the main links, so that are distributed horizontally evenly on lg viewport and above. With the structure and classes am using links are stacked very tight.
Second row navigation:
<ul class="navbar-nav nav nav-pills nav-justified">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">
Categories
</a>
<div class="dropdown-menu">
<a class="dropdown-item">Cat 1</a>
<a class="dropdown-item">Cat 2</a>
<a class="dropdown-item">Cat 3</a>
<a class="dropdown-item">Cat 4</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Live Auction</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact us</a>
</li>
</ul>
Any ideas how to override classes or other solution? Thanks!

Specify the initial size of your flex items (li) to match content by changing the flex-basis from 0 to auto -- you can enable in Bootstrap 4 by using the flex-fill class. Add it to your HTML:
<li class="nav-item flex-fill">
<a class="nav-link" href="#">Live Auction</a>
</li>
Or add the property in your CSS:
.nav-justified li.nav-item {
/* ... */
flex-basis: auto;
}
Fiddle example: https://jsfiddle.net/wcj1gzr5/1/
To distribute flex items with an effect like justify-content: space-between the width of the div and ul which contain the flex items should expand their width (giving available space to distribute flex items). To do this, set min-width: 100%; on both elements to have them fill their containing element and then use justify-content on the ul to control spacing & positioning.
Fiddle example: https://jsfiddle.net/wcj1gzr5/2/

You can do this by adding width:100% to .navbar-collapse, .nav-pills. Below is the snippet for the same
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
.navbar-collapse,
.nav-pills {
width: 100% !important
}
header {
width: 100%;
background: #282828;
background-position-x: 0%;
background-position-y: 0%;
background-image: url("https://cdn.wallpapersafari.com/65/40/cJtjUm.jpg");
background-size: cover;
background-position: center;
}
/* center the logo */
.navbar {
justify-content: center;
}
/* in order to center the logo */
.navbar .navbar-toggler {
position: absolute;
right: 1rem;
top: 0.5rem;
}
/* center all navbar items */
.navbar-nav {
align-items: center;
}
/* since it's expanding at lg */
#media (min-width: 992px) {
/* in order to display in 2 rows */
.navbar-expand-lg {
flex-flow: column nowrap;
}
/* same logic as the navbar-toggler above */
.navbar-nav.upper-controls {
position: absolute;
right: 1rem;
top: 0.5rem;
font-size: 85%;
}
}
</style>
</head>
<body>
<header>
<div class="container">
<div class="row">
<div class="col">
<nav class="navbar navbar-expand-lg navbar-light">
<a href="#" class="navbar-brand">
CompanyLogo
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".collapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse">
<ul class="navbar-nav upper-controls">
<li class="nav-item">
<a class="nav-link" href="#">Language</a>
</li>
</ul>
<ul class="navbar-nav nav nav-pills nav-justified">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">
Categories
</a>
<div class="dropdown-menu">
<a class="dropdown-item">Cat 1</a>
<a class="dropdown-item">Cat 2</a>
<a class="dropdown-item">Cat 3</a>
<a class="dropdown-item">Cat 4</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Live Auction</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact us</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
</div>
</header>
</body>
</html>

Related

Nav Bar Dropdown moves to the left side of screen when parent is clicked

I have a nav bar with dropdowns that appear when hovered over. That part works perfectly. However if you click on the parent or element the dropdown moves to the far left of the screen. I want it to just stay where it is even if this is clicked on. I'll include my html and css code below. Any help getting this element to stay where it is when the screen is expanded would be helpful. I am using bootstrap 5 by the way.
navbar.html
<nav class="navbar navbar-expand-md">
<a class="navbar-brand" href="/">
<img class="navbar-logo" src="{% static 'site_base/images/image.png' %}" alt="Logo">
</a>
<button class="navbar-toggler navbar-dark" type="button" data-bs-toggle="collapse" data-bs-target="#main-navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse text-end" id="main-navigation">
<ul class="navbar-nav ms-auto d-flex justify-content-end">
<li class="nav-item">
<p class="nav-link dropdown-toggle" data-bs-toggle="dropdown">Dropdown 1</p>
<ul class="dropdown-menu text-end dropdown-menu">
<li class="dropdown-item">Sub-item-1</li>
<li class="dropdown-item">Sub-item-2</li>
<li class="dropdown-item">Sub-item-3</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Products</a>
<ul class="dropdown-menu text-end">
<li class="dropdown-item">Product 1</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Resources</a>
<ul class="dropdown-menu text-end">
<li class="dropdown-item">User Guides</li>
<li class="dropdown-item">Videos</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/company">Company</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/contact-us">Contact Us</a>
</li>
</ul>
</div>
</nav>
style.css
#media all and (min-width: 992px) {
.navbar .nav-item .dropdown-menu {
display: none;
}
.navbar .nav-item:hover .nav-link {
}
.navbar .nav-item:hover .dropdown-menu {
display: block;
}
}
/* ============ desktop view .end// ============ */
/* ============ small devices ============ */
#media (max-width: 991px) {
.dropdown-menu .dropdown-menu {
margin-left: 0.7rem;
margin-right: 0.7rem;
margin-bottom: 0.5rem;
}
}

Hiding Sub Menus In A Navigation Bootstrap Explanation [duplicate]

This question already has answers here:
Bootstrap dropdown sub menu missing
(11 answers)
Closed 1 year ago.
I am looking for an explanation on how the CSS selectors work to make it so that the Sub Menu is only shown when the parent element for that Sub Menu is hovered over. To my understanding there is a pure CSS solution for this with the use of :hover and the CSS display property however I do not understand how to use the two together to make the sub menu do what I want it to do. I have a hunch that the .dropdown-menu class is overwriting the display:hidden within the CSS for the submenu.
I have tried adding in some CSS in the selector .navbar .nav-item .submenu { display: hidden; position: absolute; left:100%; top:35px;} to hide the Sub Menu in the navbar. Once the Sub Menu is hidden I should be able to just show it again by using :hover on the parent element but I am unsure which element that would be as I have tried a mix of CSS selectors with no avail.
Here is the code for the page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
</head>
<body style="">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark" style="background-color: black;">
<div class="container-fluid">
<a class="navbar-brand" href="#">Home</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main_nav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="main_nav">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> People </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href="#">Professional Institutions</a></li>
<li class="dropdown-submenu">
<li><a class="dropdown-item text-white" href="#">My New Drop Down</a>
<ul class="submenu dropdown-menu">
<li><a class="dropdown-item text-white" href="#">Sub Area 1</a></li>
<li><a class="dropdown-item text-white" href="#">Sub Area 2</a></li>
<li><a class="dropdown-item text-white" href="#">Sub Area 3</a></li>
</ul>
</li>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> Products </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href=""> Actuation Systems</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Knowledge </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href=""> A S Knowledge</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> Policy Deployment </a>
<ul class="dropdown-menu">
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> Processes And Procedures </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href="#"> Quality Management System</a></li>
<li><a class="dropdown-item text-white" href="#"> Buissness Management System </a></li>
</ul>
</li>
</ul>
</div> <!-- navbar-collapse.// -->
</div> <!-- container-fluid.// -->
</nav>
<style>
#media all and (min-width: 1200px) {
.nav-link {
font-size: 25px
}
.navbar .nav-item .dropdown-menu {
display: hidden;
font-size: 20px
}
.navbar .nav-item:hover .dropdown-menu {
display: block;
background: rgba(66, 66, 66, 0.4)
}
.dropdown-menu>li:hover {
background-color: black
}
.dropdown-menu .dropdown-item:hover {
background-color: black;
}
.navbar .nav-item .submenu {
display: hidden;
position: absolute;
left: 100%;
top: 35px;
}
}
</style>
</body>
</html>
Here is the code again: https://jsfiddle.net/p0etz8jg/
How it works:
The collapse JavaScript plugin is used to show and hide content.
Buttons or anchors are used as triggers that are mapped to specific
elements you toggle. Collapsing an element will animate the height
from its current value to 0. Given how CSS handles animations, you
cannot use padding on a .collapse element. Instead, use the class as
an independent wrapping element.
Bootstrap Collapse

Why is the text in my navbar not centering? [duplicate]

This question already has answers here:
How to center nav-items in Bootstrap? [duplicate]
(3 answers)
Bootstrap NavBar with left, center or right aligned items
(14 answers)
Closed 2 years ago.
I have two navbars, one of which I want the text centered, but I'm having trouble understanding why I am unsuccessful in doing so. I am new to navbars in general as well as bootstrap, so I apologize If my mistake is obvious. Is it something I should be implementing in the bootstrap end, or is it something that can be addressed within the <style> tags? Thanks.
<nav class="navbar navbar-expand-md navbar-dark" style="background-color: #CBB677">
<ul class="nav navbar-nav navbar-center">
<li class="nav-item active">
<a class="nav-link" href="CoordinatorHomePage.aspx" >COORDINATORS</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="TeacherHomePage.aspx">TEACHERS</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="ParentsHome.aspx">PARENTS</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="StudentHome.aspx">STUDENTS</a>
</li>
</ul>
</nav>
IMO, Bootstrap is unnecessarily hard. I use raw CSS JS and HTML, but I'll try to help.
Bootstrap has this example on their site:
<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
That should be what you're looking for.
You could also implement a navbar in raw HTML/CSS/JS:
<!--index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" src="index.css">
<title>Navbar</title>
</head>
<body>
<navbar>
<button onclick="window.location.href = '#'" class="active">Something</button>
<button onclick="window.location.href = '#'">Something else</button>
<button onclick="window.location.href = '#'">A third thing</button>
</navbar>
</body>
</html>
/* index.css */
navbar {
width: 100%;
height: 10%;
background-color: #d4d4d4;
position: fixed;
top: 0px;
left: 0px;
text-align: center;
}
navbar>button {
color: #00ccff;
}
navbar>button.active {
color: #00ccff;
}

How do I stack one navigation menu on top other the other using z-index?

I am a bit stuck trying to place or stack two navigation menus on top of each other, I am using Bootstrap, HTML and CSS.
I think I might need to use the z-index, but not sure exactly how.
I have added the code below and included picture of what I would like to achieve.
Code:
<!<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
<nav class="navbar navbar-expand-md bg-white navbar-white">
<a class="navbar-brand" href="#">
<img src="assets/logo2.png"height="84px">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="accessability">
<li class="nav-item">
<a class="contrast">Contrast</a>
</li>
<li class="Font-size">Font size</a>
</li>
<li class="download">Download a brochure</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">New Vehicles</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Nearly New/ Used Vehicles</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Motability</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Aftercare</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</nav>
<br>
<nav class="navbar navbar-inverse bg-primary">
<ul>
<li class="sec-menu">Versa Connect</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="sec-menu">Overview</li>
<li class="sec-menu">Offers</li>
<li class="sec-menu">Models</li>
<li class="sec-menu">Optional Extras</li>
</ul>
</nav>
<section id="cover_image">
<div class="container">
<img src="assets/cover_image.jpg" >
<div class="text-block">
<h1>Versa Connect</h1>
<p>The Ford Torneo Connect is a relaxing and comfortable place to be.
Great to drive, great to sit in, it boasts premium finish, high quality materials
and fuel efficiency without compromising everyday practicality. </p>
<button type="button" class="btn btn-lg btn-primary">Make an enquiry</button>
</div>
</div>
</body>
</html>
CSS
h1{
font-family: roboto;
}
p{
font-family: roboto;
font-size: 18px;
}
li{
font-family: roboto;
font-size: 18px;
display:inline;
padding: 30px;
}
li a{
color: #000;
}
.navbar-right{
float: right !important;
margin-right: -15px;
display: inline;
}
ul.nav navbar-nav navbar-right li a{
display: flex;
}
li.sec-menu a{
color: #fff;
font-family: roboto;
}
.container{
margin: 0;
padding: 0;
}
.text-block {
text-align: center;
position: absolute;
bottom: 10px;
left: 100px;
background-color:rgba(255,255,255,0.66);
color:#000;
width: 380px;
padding:50px;
}
.btn btn-lg btn-primary{
padding-left: 10px;
width: 360px;
}
I would appreciate it if somebody could help me with my coding and show me how or where I should code the z-index in my code.
The z-index property is used for stacking items on top of each other via the z plane (think of a 3-dimensional plane), so that property would not help you here. All you need to do is create each navbar using a separate "nav" or "div" tag, which will put them on separate lines.
You are on the right track, but you have multiple errors in your code that are causing this issue. Your navbar consisting of "Contrast, Font Size, Download a Brochure" have closing anchor tags but not beginning anchor tags. This navbar is also using a class "accessability" that is not defined in your CSS, which is why it's appearing as an unstyled unordered list.
Here is a simple version of what you are looking for:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<nav>
<ul>
<li>Contrast</li>
<li>Font Size</li>
<li>Download a Brochure</li>
</ul>
</nav>
<nav>
<ul>
<li>New Vehicles</li>
<li>Nearly New/Used Vehicles</li>
<li>Motability</li>
<li>Aftercare</li>
<li>Contact Us</li>
</ul>
</nav>
</body>
</html>
This works, you do not need the z-index. You can view it here on codepen https://codepen.io/anon/pen/Rvdgaq
<nav class="navbar navbar-expand-md bg-white navbar-white">
<a class="navbar-brand" href="#">
<img src="assets/logo2.png"height="84px">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="accessability">
<li class="nav-item">
<a class="contrast">Contrast</a>
</li>
<li class="Font-size">Font size</a>
</li>
<li class="download">Download a brochure</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">New Vehicles</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Nearly New/ Used Vehicles</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Motability</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Aftercare</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</nav>
<nav class="navbar navbar-inverse bg-primary">
<ul>
<li class="nav-item sec-menu">Versa Connect</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item sec-menu">Overview</li>
<li class="nav-item sec-menu">Offers</li>
<li class="nav-item sec-menu">Models</li>
<li class="nav-item sec-menu">Optional Extras</li>
</ul>
</nav>
</div>
</div>
<section id="cover_image">
<div class="container">
<div class="row">
<img src="assets/cover_image.jpg" >
<div class="text-block">
<h1>Versa Connect</h1>
<p>The Ford Torneo Connect is a relaxing and comfortable place to be.
Great to drive, great to sit in, it boasts premium finish, high quality materials
and fuel efficiency without compromising everyday practicality. </p>
<button type="button" class="btn btn-lg btn-primary">Make an enquiry</button>
</div>
</div>
</div>
</section>
A Few things I noticed, 1. You did not close off your section tag so that could be causing issues. And you do not need postion: absolute; on .text-block, take off bottom: 10px;
Make it position relative

Centering navbar links in bootstrap

I want to center my links on mobile view (md and down view) of my navbar but I can't seem to find a solution for it. I am using bootstrap v4-alpha
<div class="container-fluid p-b-3">
<nav class="navbar navbar-full navbar-fixed-top navbar-light bg-faded">
<ul class="nav navbar-nav">
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" data-toggle="modal" data-target="#myModal" style="cursor:pointer;">LINK 3</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#kontakti">LINK 2</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#produktet">LINK 1</a>
</li>
</ul>
</nav>
</div>
Here's the codepen link
You can achieve it using flexbox.
Here's a working pen I created: http://codepen.io/anon/pen/bwbpNx
CSS
.navbar-nav {
display: flex;
align-items: center;
justify-content: center;
}
I think this is one of the proper way to do it. So that, if you add another link. It will remain at center. Hope it helps. Cheers!
Use media query and override styles to make your links center aligned.
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css');
#media (max-width: 767px) {
.navbar ul {
text-align: center;
}
.navbar ul li {
display: inline-block;
vertical-align: top;
padding: 0 10px;
}
.navbar ul li a {
margin-left: 0 !important;
}
}
<div class="container-fluid p-b-3">
<nav class="navbar navbar-full navbar-fixed-top navbar-light bg-faded">
<ul class="nav navbar-nav">
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" data-toggle="modal" data-target="#myModal" style="cursor:pointer;">LINK 3</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#kontakti">LINK 2</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#produktet">LINK 1</a>
</li>
</ul>
</nav>
</div>