Centering navbar not working - html

I'm very new to HTML and CSS. I'm trying to center the navbar at the top of a site. Here is my HTML code:
<!-- Navigation -->
<div class="nav_container">
<nav class="navbar navbar-default navbar-fixed-top top-nav-collapse" role="navigation">
<div class="container">
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav page-scroll" style="text-align:center">
<li>
<a class="page-scroll" href="#page-top">Start</a>
</li>
<li>
<a class="page-scroll" href="#packages">Packages</a>
</li>
<li>
<a class="page-scroll" href="#about">About</a>
</li>
<li>
<a class="page-scroll" href="#showcase">Showcase</a>
</li>
<li>
<a class="page-scroll" href="#portfolio">Portfolio</a>
</li>
<li>
<a class="page-scroll" href="#contact">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
And here is (some) relevant CSS code:
.navbar {
position: relative;
min-height: 50px;
margin-bottom: 20px;
border: 1px solid transparent;
}
#media (min-width: 768px) {
.navbar {
border-radius: 4px;
}
}
.nav_container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
position: relative;
opacity: 1;
text-align: center;
}
.nav_container nav {
display: inline-block;
text-align: center;
}
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
I must apologise in advance if this question looks like a duplicate of other questions. I have tried out different permutations of how the CSS code should be set according to other answered questions, but to be honest how and which class in the CSS code takes precedence is still not clear to me and my navbar is just not centering. I took the code from a bootstrap site, and am simply reverse engineering. It could be that what is already written contains some redundant code (for example, there may be too many divs) and so may need to be cleared up.

change :
<div class="nav_container">
<nav class="navbar navbar-default navbar-fixed-top top-nav-collapse" role="navigation">
<div class="container">
To :
<div class="container">
<div class="nav_container">
<nav class="navbar navbar-default navbar-fixed-top top-nav-collapse" role="navigation">
Demo
Note : For See Result increment Result Page.

Related

issue with CSS minus margin

i have use minus margin to place the navbar inside a div which has a class main..in small screen when i click on the navbar toggler icon it does not look good..because i need to change the margin again...how can i solve this problem without using minus margin?
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css">
<div class="top"></div>
<div class="wrapper">
<nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
<button class="navbar-toggler ml-auto" data-toggle="collapse" data-target="#navDrop">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navDrop">
<ul class="navbar-nav">
<li class="nav-item">
HOME
</li>
<li class="nav-item">
ABOUT
</li>
<li class="nav-item">
SKILL
</li>
<li class="nav-item">
PORTFOLIO
</li>
<li class="nav-item">
TEAM
</li>
<li class="nav-item">
BLOG
</li>
<li class="nav-item">
CONTACT
</li>
</ul>
</div>
</nav>
<div class="main"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter- bootstrap/4.1.1/js/bootstrap.min.js"></script>
here is output of my code:
https://codepen.io/Sakhawat5/pen/NzwQJz
--Update--
As I think your question is how to append the navbar class to the main div if the user has scrolled more then 50px. This is what could help you out.
$(document).scroll(function() {
if ($(document).scrollTop() >= 50) {
// user scrolled 50 pixels or more;
// do stuff for example:
$('.navbar').appendTo('.main');
// this will append the navbar to the main div, if you want other styling on the navbar aswell we can do the following:
$('.navbar').addClass('.UserHasScrolled')
// now in you CSS set styling to .UserHasScrolled
} else {
// do things when user has NOT scrolled more then 50px
}
});
Also make sure to include jQuery, this is possible, paste this beneath all other scripts:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
To your .UserHasScrolled class you can set something like this:
.UserHasScrolled {
position: absolute;
width: 100%;
top: 0;
}
--First answer--
What you ask for is: #media screen and (max-width 767px) while the screen width has a lower size than 767px you can define an overwriting css style like this:
#media screen and (min-width 767px) {
.navbar {
margin-bottom: -336px;
}
}
But this causes another problem, while the navbar is closed the main class will still have a margin of -336px and that's not what you want I guess.
To keep your navbar on top just set position to fixed:
.navbar {
position: fixed;
width: 90%;
margin-left: 5%;
margin-right: 5%;
}
This is your solution:
.top{
height:50px;
background:red;
}
.navbar{
margin-left:20px;
margin-right:20px;
}
.main{
height:1500px;
background:orange;
}
<div class="top"></div>
<div class="wrapper">
<div class="main">
<nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
<button class="navbar-toggler ml-auto" data-toggle="collapse" data-target="#navDrop">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navDrop">
<ul class="navbar-nav">
<li class="nav-item">
HOME
</li>
<li class="nav-item">
ABOUT
</li>
<li class="nav-item">
SKILL
</li>
<li class="nav-item">
PORTFOLIO
</li>
<li class="nav-item">
TEAM
</li>
<li class="nav-item">
BLOG
</li>
<li class="nav-item">
CONTACT
</li>
</ul>
</div>
</nav>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter- bootstrap/4.1.1/js/bootstrap.min.js"></script>
The changes to your CSS as follows should fix the issue:
.navbar{
margin-left: 5%;
margin-right: 5%;
width: 90%;
position: fixed;
}
and if you don't want a fixed nav:
.navbar{
margin-left: 5%;
margin-right: 5%;
width: 90%;
position: absolute;
}
Let me know if you have any questions.

Can't make Navigation Bar horizontal HTML/CSS

float: left;
and
display: inline;
are both failing to work.
What am I doing wrong? How can I make this navigation bar horizontal?
.navbar li {
margin: 5px;
padding: 0;
width: 80px;
float: left;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Simmo Simpson</a>
</div>
<ul class="nav navbar-nav">
<li id="navbar" class="active">Home</li>
<li id="navbar" class="active">About</li>
<li id="navbar" class="active">Portfolio</li>
<li id="navbar" class="active">Contact</li>
</ul>
</div>
</nav>
put your css inside style tags or an external css file and include it in the header section of your html
<style>
ul li{ display: inline;}
</style>
try the following code snippet, it might help you
.navbar li {
margin: 5px;
padding: 0;
width: 80px;
float: left;
list-style-type : none;
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Simmo Simpson</a>
</div>
<ul class="nav navbar-nav">
<li id="navbar" class="active">Home</li>
<li id="navbar" class="active">About </li>
<li id="navbar" class="active">Portfolio </li>
<li id="navbar" class="active">Contact </li>
</ul>
</div>
</nav>
<!DOCTYPE html>
<html>
<head>
<style>
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;
}
</style>
</head>
<body>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</body>
</html>
Changed
.navbar li {
to
li.navbar {
li.navbar {
margin: 5px;
padding: 0;
width: 80px;
float: left;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Simmo Simpson</a>
</div>
<ul class="nav navbar-nav">
<li id="navbar" class="active">Home</li>
<li id="navbar" class="active">About</li>
<li id="navbar" class="active">Portfolio</li>
<li id="navbar" class="active">Contact</li>
</ul>
</div>
</nav>
Is this what you are looking for?
Solved:
The
<li id="navbar" class="active">Home</li>
elements need the corresponding CSS to be
#navbar
and NOT
.navbar li
.navbar li should be used in the CSS for the html class attribute class="navbar li" whereas #navbar should be used the ID attribute
When I changed that, the CSS styling was applied to the html with the corresponding id attribute.
It seems you're doing things the right way, but your code sample misses some tags :
<html>
<head>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Simmo Simpson</a>
</div>
<ul class="nav navbar-nav">
<li class="navbar-link" class="active">Home</li>
<li class="navbar-link" class="active">About</li>
<li class="navbar-link" class="active">Portfolio</li>
<li class="navbar-link" class="active">Contact</li>
</ul>
</div>
</nav>
<style>
.navbar li {
margin: 5;
padding: 0;
width: 80;
float: left;
}
</style>
</body>
</html>
will do the trick. You have to say to the browser, what it have to do with those lines : use Html <html> and apply that style using <style> . If your style directives have to be more important, I think that it could be a better way to create a second file that will contains all your css and to link it with your html document.
EDIT :
The css rule : .navbar li will find the navbar class and find all its li childs, so you can remove ids on each li and replace them by a class name
I hope it may help

Alignment of elements of navigation bar

I was building a website where I encountered a problem in navigation bar that the elements of navigation bar is not coming center aligned
HTML and CSS code:
<nav class="navbar navbar-inverse">
<div class="container">
<ul id="navigation" class="nav navbar-nav">
<li class="active"> Home </li>
<li> Marketing Services </li>
<li> IT Management Services </li>
<li> Molex Portfolio </li>
<li> Contact Us </li>
<li> Employement Opportunities </li>
</ul>
</div>
</nav>
My css Code is
body{
background-repeat:no-repeat;
background-position: top right;
background-attachment: fixed;
}
h1{
margin:1px ;
color:red !important;
text-align:center;
}
hr{
border-color:red !important;
width:50%;
}
a {
padding: 0px 10px;
}
#navigation{
text-align:center!important;
display: inline-block!important;
margin: auto;
}
You are trying to center the ul with inline-block style, try to change the it to be block instead
body{
background-repeat:no-repeat;
background-position: top right;
background-attachment: fixed;
}
h1{
margin:1px ;
color:red !important;
text-align:center;
}
hr{
border-color:red !important;
width:50%;
}
a {
padding: 0px 10px;
}
#navigation {
text-align:center!important;
display: block!important;
margin: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-inverse">
<div class="container">
<ul id="navigation" class="nav navbar-nav">
<li class="active"> Home </li>
<li> Marketing Services </li>
<li> IT Management Services </li>
<li> Molex Portfolio </li>
<li> Contact Us </li>
<li> Employement Opportunities </li>
</ul>
</div>
</nav>
Try the below code, Hope this work...!
#media (min-width: 768px){
.navbar-nav {
float: none!important;
margin: 0 auto!important;
display: table!important;
table-layout: fixed!important;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-inverse">
<div class="container">
<ul id="navigation" class="nav navbar-nav">
<li class="active"> Home </li>
<li> Marketing Services </li>
<li> IT Management Services </li>
<li> Molex Portfolio </li>
<li> Contact Us </li>
<li> Employement Opportunities </li>
</ul>
</div>
</nav>
If you want to center align the text in the "li" just do
.navbar navbar-inverse{
ul>li{
text-align: center;
}
}

Bootstrap navbar-toggle is not at the top

So I have this issue, I'm trying to get the navbar-toggle to get up into the corner but it's over lapping.
I also have a icon in pc view that over lapping which I really like.
Is there a way to fix the navbar-toggle??
My code is below.
<!-- Navigation -->
<nav class="navbar navbar-default navbar-fixed-top">
<img class="visible-xs img-rounded pull-right" src="smallicon2.png" alt="" >
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-brand page-scroll">
<button type="button" class="navbar-toggle pull-left" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<img src="icon2.png" class="img-rounded hidden-xs" />
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="hidden">
</li>
<li>
<a class="page-scroll" href="#">Home</a>
</li>
<li>
<a class="page-scroll" href="#services">Services</a>
</li>
<li>
<a class="page-scroll" href="#portfolio">Portfolio</a>
</li>
<li>
<a class="page-scroll" href="#about">About</a>
</li>
<li>
<a class="page-scroll" href="#contact">Contact</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
Thanks for looking.
In bootstrap CSS, Following causing the issue padding: 15px;
.navbar-brand {
float: left;
height: 50px;
padding: 15px;
font-size: 18px;
line-height: 20px;
}
This may fix the issue padding: 0px 15px; but it will effect the logo too
.navbar-brand {
float: left;
height: 50px;
padding: 0px 15px;
font-size: 18px;
line-height: 20px;
}
Fiddle
Proper Fix without effecting the logo, change margin-top: 8px; to margin-top: -5px; to following CSS in bootstrap css file
.navbar-toggle {
position: relative;
float: right;
padding: 9px 10px;
margin-top: -5px; <<<< Here
margin-right: 15px;
margin-bottom: 8px;
background-color: transparent;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
}
Fiddle
Note: Better approach do not edit bootstrap css, make customstyle.css and add above css with !important rule in custom css.
Don't waste your time with fixing. Copy and paste the complete navbar from Bootstrap navbar. After that, delete HTML you don't need, correctly!
The problem often begins if you want to have a navbar with a bigger logo.
Here is a precise instruction how to create a Bootstrap navbar with a bigger logo.

Bootstrap 3 Navbar menu item alignment

Site : http://www.Spotlightdd.co.uk
I have a navbar that has an increased size due to the logo in the middle. The logo has a -margin value to line it up with the bottom border (I know it's out by a few px). What i'm trying to do is move the text down without just increasing the size of the navbar, so it looks more centered in the navbar. Currently, i've tried using margin/padding.line-height, anything i can think of that would only move the text to no avail. If someone could try a hand at sorting it, that would be brilliant.
Thanks
(Navbar html)
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img alt="CompanyLogo" src="img/spotlightLogo.png" style = "height: 80px" >
</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav nav-center">
<li> HOME </li>
<li> WEB DESIGN</li>
<li> GRAPHIC DESIGN </li>
<li> SOCIAL MEDIA </li>
<li class = "logo"> <img src = "img/spotlightLogo.png" class = "headerLogo img-responsive" alt = "spotlight" style = "height: 150px;"></li>
<li> PRINTING </li>
<li class="active"> BRANDING </li>
<li> VIDEO PRODUCTION </li>
<li> CONTACT </li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
(Navbar CSS)
.navbar-default {
border-bottom: 3px solid #CE1344;
background-image: none;
background-color: white;
}
.navbar .navbar-nav {
text-align: center;
display: inline-block;
float: none;
vertical-align: top;
}
li > a {
margin-top: 0px;
}
.navbar .navbar-nav {
display: inline-block;
float: none;
vertical-align: top;
}
.navbar .navbar-collapse {
text-align: center;
}
.headerLogo {
margin-bottom: -50px;
}
I tried to put in every bit of code relevant. Apologies because the css file is a mess at the moment.
Try adding this to your css:
.navbar-nav>li{
padding: 20px 0;
}
.navbar-nav>li.logo{
padding: 0
};
This should do the trick.
Try this:
.nav-center{
position: relative;
top: 20px;
}
li.logo {
position: relative;
bottom: 22px;
}