Why won't my nav bar center? - html

So I've got my navbar set up pretty close to the way I'd like it to look, but for some reason it won't go to the center of my page. I've tried putting text-align: center; on most of the different elements that make up my nav bar, but it won't go no matter what I do. What am I doing wrong?
.navbar {
margin: 0;
padding: 0;
text-align: center;
border-bottom: solid #000;
border-width: 1.5px 0;
list-style: none;
height: 25px;
width: 1000px;
}
.navbar a {
text-decoration: none;
padding: 0;
margin: 10px;
border-bottom: 10px;
color: #000;
text-align: center;
}
li {
display: inline-block;
font-family: 'Muli', sans-serif;
font-size: 19px;
margin: 0;
padding: 0;
text-align: center;
}
<div class="banner">
<h1>Brian Funderburke Photography & Design</h1>
<div class="nav">
<ul class="navbar">
<li>Home
</li>
<li>Photography
</li>
<li>Design
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
</div>

Set the .banner width to 100% and then set .nav to center:
.banner{width: 100%;}
.nav{text-align: center;}
See fiddle: https://jsfiddle.net/c51kr3jo/

How I fix this problem usually is by following 3 steps:
Adding a full-width or a big width to the navbar container (.nav div in your case)
Adding a width to the navbar (usually try to find a width that will fit the most elements)
Adding margin: 0 auto to the navbar (this will center align the .navbar div)
Here is a jsfiddle I've created. Hope it helps.

I added
.nav {
display: flex;
justify-content: center;
}
Which I got from this AMAZING article.
http://jsfiddle.net/abalter/44w7b73f/

navbar center
.navbar {
margin: 0;
padding: 0;
text-align: center;
border-bottom: solid #000;
border-width: 1.5px 0;
list-style: none;
height: 25px;
width: 1000px;
}
.navbar a {
text-decoration: none;
padding: 0;
margin: 10px;
border-bottom: 10px;
color: #000;
text-align: center;
}
li {
display: inline-block;
font-family: 'Muli', sans-serif;
font-size: 19px;
margin: 0;
padding: 0;
text-align: center;
}
/* center */
.nav1 {
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-items: center;
align-items: center;
width: 100%;
height: auto;
text-align: center;
}
.navbar1 {
-webkit-align-self: center;
align-self: center;
}
li a:hover {
color: red;
cursor: pointer;
}
/* || center */
<div class="nav1">
<h1>Brian Funderburke Photography & Design</h1>
</div>
<div class="nav nav1">
<ul class="navbar navbar1">
<li>Home
</li>
<li>Photography
</li>
<li>Design
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
</div>

Your code appears to already be working from what I can tell (though you were missing a closing </div> in your example). Make sure your CSS is being properly imported and applied.
Here is a working example to prove it is looking okay: http://jsfiddle.net/611mbvtu/

Related

How do I make the menu in the center of the block?

My question is how do I put the menu in the middle of the header block because I can't figure it out.
I am trying to make a website for my dad's business by using some templates that I find on the internet, this is one of them and I really like but I can't figure out how to center the navbar. I am not the best when it comes to CSS.
body {
margin: 0;
padding: 0;
}
.logo {
float: left;
}
/* ~~ Top Navigation Bar ~~ */
#navigation-container {
width: 1200px;
margin: 0 auto;
height: 70px;
}
.navigation-bar {
background-color: black;
height: 70px;
width: 100%;
}
.navigation-bar ul {
padding: 0px;
margin: 0px;
text-align: center;
display: inline-block;
vertical-align: top;
}
.navigation-bar li {
list-style-type: none;
padding: 0px;
height: 24px;
margin-top: 4px;
margin-bottom: 4px;
display: inline;
color: white;
text-align: center;
}
.navigation-bar li a {
color: white;
font-size: 16px;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
text-decoration: none;
line-height: 70px;
padding: 5px 15px;
opacity: 0.7;
text-align: center;
}
#menu {
float: right;
color: white;
text-align: center;
}
<html>
<link rel="stylesheet" href="index.css" type="text/css">
<head>
<div class="navigation-bar">
<div id="navigation-container">
<img src="images/logo.png">
<ul>
<li>Home</li>
<li>Services</li>
<li>Rent a Boat</li>
<li>Gallery</li>
<li>Contact</li>
<li>Location</li>
</ul>
</div>
</div>
</head>
<body>
</body>
</html>
I rewrote most parts of your code to fix the invalid markup and to include the correct semantic tags for screenreaders. Also, I changed a bit the structure to be semantically correct. Simplified your CSS.
For the invalid Markup, read my comment below your question.
Use the <header>-tag for your header. It is a semantic tag.
The image is part of the header but not part of the navigation, semantically speaking. AS such place it inside the header but not inside the navigation
Use the <nav>-tag for your navigation bar as it is also a semantic tag.
Learn about Flexbox and use it! float is a property to float an element within a text block (like in a newspaper). It is not for aligning purposes. Since 2013 we have Flexbox which is well supported since 2015!
Side Note:
I know this sounds hard but it must be said. If the coding is done for a business Homepage then no unexperienced or beginner without proper skill/knowledge-base should attempt to do it. At least not without the supervision of an experienced coder. A Business Homepage is one of the most important marketing aspects. If you screw this up, then the business will be hurt from it and your dad would lose money!
Then even for experienced coders, it is a NO-GO to just copy code from the internet and include it without correction or knowing what they include. The coder is responsible for the code and can not just state that he didn't write those code pieces originally. It is a coder's job to correct errors such as invalid markups.
body {
margin: 0;
padding: 0;
}
/* ~~ Top Navigation Bar ~~ */
header {
display: flex;
width: auto;
align-items: center; /* vertically center the links */
}
nav {
margin: 0 auto; /* horizontally center the links */
}
nav > ul {
display: flex;
gap: 10px;
list-style: none;
padding: 0;
}
<html>
<head>
<link rel="stylesheet" href="index.css" type="text/css">
</head>
<body>
<header>
<img src="https://via.placeholder.com/50.jpg">
<nav>
<ul>
<li>Home</li>
<li>Services</li>
<li>Rent a Boat</li>
<li>Gallery</li>
<li>Contact</li>
<li>Location</li>
</ul>
</nav>
</header>
</body>
</html>
#navigation-container set max-width instread of width.
margin:0 auto will push from left and right side to #navigation-container so it will be center.
I have replace you css with better approach by using display:flex and setting justify-content:center sets it center to horizontally.
Ans it's good practice to use <header></header> tag for header and include it inside of <body> rather than in <head>
body {
margin: 0;
padding: 0;
overflow-x: hidden;
}
/* ~~ Top Navigation Bar ~~ */
#navigation-container {
max-width: 1200px;
/*you just need to set this*/
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
.navigation-bar {
background-color: black;
}
.navigation-bar ul {
padding: 0px;
margin: 0px;
text-align: center;
display: flex;
}
.navigation-bar li {
list-style-type: none;
padding: 0px;
color: white;
text-align: center;
}
.navigation-bar li a {
color: white;
font-size: 16px;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
text-decoration: none;
padding: 5px 15px;
opacity: 0.7;
text-align: center;
line-height: 70px;
}
<body>
<header>
<div class="navigation-bar">
<div id="navigation-container">
<img src="images/logo.png">
<ul>
<li>Home</li>
<li>Services</li>
<li>Rent a Boat</li>
<li>Gallery</li>
<li>Contact</li>
<li>Location</li>
</ul>
</div>
</div>
</header>
</body>
I suppose you are trying to center the navbar at the top of the page. To the (.navigation-bar li) class, add the following property: display: inline-block;
body {
margin: 0;
padding: 0;
}
.logo {
float: left;
}
/* ~~ Top Navigation Bar ~~ */
#navigation-container {
width: 1200px;
margin: 0 auto;
height: 70px;
}
.navigation-bar {
background-color: black;
height: 70px;
width: 100%;
}
.navigation-bar ul {
padding: 0px;
margin: 0px;
text-align: center;
display: inline-block;
vertical-align: top;
}
.navigation-bar li {
display: inline-block;
list-style-type: none;
padding: 0px;
height: 24px;
margin-top: 4px;
margin-bottom: 4px;
display: inline;
color: white;
text-align: center;
}
.navigation-bar li a {
color: white;
font-size: 16px;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
text-decoration: none;
line-height: 70px;
padding: 5px 15px;
opacity: 0.7;
text-align: center;
}
#menu {
float: right;
color: white;
text-align: center;
}

How to position in parallel navigation bar and logo

I have this HTML code:
<body>
<header id="masthead">
<div id="container">
<!-- logo -->
<img src="logo.png" alt="" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>
And this CSS code:
.container {
width: 80%;
margin: 0 auto;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
header::after {
content : '';
display: table;
clear: both;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 2px;
position: relative;
padding-right: 0.1rem;
}
nav a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
However I want to make my nav bar to the left from the logo, but not down below. How can I do it using the given initial code that i hav pointed ? As you can see, align: right and align: left has been used, but had not helped me
Like on photo (Used arrows to point it )
Create two columns. In one column, place your logo, and in the second column, place your navigation bar.
<div class="row">
<div class="column" style="background-color:#aaa; width:20%;">
<!--pLACE Logo here--->
</div>
<div class="column" style="background-color:#bbb; width:80%">
<!--Place Navbar code here-->
</div>
</div>
Remember Adjust your css accordingly
Give your div with id container a display property of flex, direction property of row and then align or justify items as per your liking
#container{
display:flex;
flex-direction:row;
justify-content:space-between;
}
Also in your HTML code you've given tags ids but you're using class selectors in your CSS
Some resources that'll help you:
A Complete Guide to Flexbox
Basic Concepts of Flexbox
Flexbox Cheatsheet
You will have to change your CSS as shown below:
/*add this line to adjust paddings on the columns and remove default margin padding for all the html elements*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/*change class to # it's ID and not class*/
#container {
width: 80%;
margin: 0 auto;
}
/*recommended to add width in percentage in css and remove fix width from <img width='200px' /> tag*/
.logo {
float: left;
width:20%;
padding: 10px 0;
}
/*add width 80% for <nav> tag*/
nav {
float: right;
width: 80%;
margin-top: 10%;
}
nav li {
display: inline-block;
/* margin-left: 70px; */ /*remove*/
/* padding-top: 2px; */ /*remove*/
position: relative;
/* padding-right: 0.1rem; */ /*remove*/
padding: 0 5px; /*instead add this for space between li content*/
}
I would recommend you to use CSS FLEXBOX.
I used flexbox to do this. your id="container" was not the same as the CSS so I changed it to class="container"
I added some simple 1px borders just to illustrate what is where on the page.
This is likely not a finished solution and only a starting point.
.container {
width: 90%;
margin: 0 auto;
display: flex;
flex-direction: row;
flex: space-between font-size: 16px;
justify-content: center;
align-items: center;
}
.logo {
padding: 10px 0;
height: 3em;
border: 1px solid lime;
}
header::after {
content: '';
display: table;
clear: both;
}
nav {
border: 1px solid cyan;
justify-content: center;
align-items: center;
}
nav ul li::before {
content: "\200B";
}
nav ul {
display: flex;
flex-direction: row;
border: 1px solid blue;
list-style-type: none;
justify-content: center;
list-style-position: inside;
margin-left: 0;
padding-left: 0;
}
nav ul li {
padding-top: 0.2em;
padding-right: 0.1rem;
border: 1px solid pink;
margin-left: 0;
padding-left: 0;
}
nav li a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 0.875em;
margin-left: 1em;
margin-right: 1em;
}
<header id="masthead">
<div class="container">
<img src="logo.png" alt="logo png" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>

HTML Navigation bar styling

I'd like to color my menu points:
like this
But only the text's background is colored. How can I change my code to fill the whole menu to match the color in the picture above?
nav{
margin: 0px;
padding: 0px;
box-sizing: border-box;
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: #F39C12 ;
width: 100%;
}
.nav_links{
display: flex;
justify-content: space-around;
width: 35%;
list-style: none;
margin: 0px;
padding: 0px;
}
.nav_links li{
background-color: #DAF7A6;
}
.nav_links a{
text-decoration: none;
letter-spacing: 2px;
color: whitesmoke;
}
<!-- navigation bar -->
<nav>
<ul class="nav_links">
<li>
Kezdőlap
</li>
<li>
Képek
</li>
<li>
Videók
</li>
<li>
Játék
</li>
</ul>
</nav>
Have a nice day! Thank you!
You must add padding to your li tag.
nav {
margin: 0px;
padding: 0px;
box-sizing: border-box;
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: #F39C12;
width: 100%;
}
.nav_links {
display: flex;
justify-content: space-around;
width: 35%;
list-style: none;
margin: 0px;
padding: 0px;
}
.nav_links li {
background-color: #DAF7A6;
/* EDITED HERE */
padding: 20px;
}
.nav_links a {
text-decoration: none;
letter-spacing: 2px;
color: whitesmoke;
}
<!-- navigation bar -->
<nav>
<ul class="nav_links">
<li>
Kezdőlap
</li>
<li>
Képek
</li>
<li>
Videók
</li>
<li>
Játék
</li>
</ul>
</nav>
You have to add border styling in order to achieve the attached image.
border: 1px solid ;
You can give any color you want in place of solid to design a colorful border

Need assistance centralising an image in a list in my header?

I was just wondering how I would go about horizontally aligning all of the contents of the ul (unordered list) to the centre if that makes sense. As in instead of having their bottoms aligned, having their centres aligned.
Here is a photo of what it looks like currently: https://postimg.cc/S2YrpR24
Here is what I would like it to look like (Edited in photoshop): https://postimg.cc/N9YQT96G
My HTML for my header looks like this:
<header>
<nav>
<ul>
<li><a>Contact Us</a></li>
<li><a>Sign Up</a></li>
<li><img id="logo" src="img/schule logo light.png" alt="mainlogo"></li>
<li><a>Our Resources</a></li>
<li><a>The Teachers</a></li>
</ul>
</nav>
</header>
and my css looks like this:
header{
background-color: rgba(0,0,0,.5);
backdrop-filter: blur(10px);
color: #000000;
padding-top: 10px;
min-height: 110px;
position: fixed;
width: 100%;
}
header h1{
font-weight: 600;
margin: 30px 0px 0px 0px;
color: #000000;
}
header a{
text-decoration: none;
font-size: 20px;
color: #FFFFFF;
}
header img{
padding-top: 0px;
width: 40px;
}
header ul{
text-align: center;
padding: 15px;
margin: 0px;
}
header li{
display: inline-block;
list-style: none;
padding: 0px 30px 0px 30px;
}
Welcome to SO!
This looks like a very good use for flexbox, a decent demo site:
HEADER UL {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row nowrap;
flex-flow: row nowrap;
-webkit-justify-content: space-between;
justify-content: space-between;
-webkit-align-items: center;
align-items: center;
-webkit-align-content: stretch;
align-content: stretch
}
Maybe change
header img {
padding-top: 0px;
}
to
header img {
padding-top: 30px;
}
?
Hard to say with that snippets. Or post an URL?
Here is a working example using flex and vertically aligned items:
<header>
<nav>
<ul>
<li><a>Contact Us</a></li>
<li><a>Sign Up</a></li>
<li><img src="https://via.placeholder.com/200x100" alt="test image"></li>
<li><a>Our Resources</a></li>
<li><a>The Teachers</a></li>
</ul>
</nav>
</header>
<style>
ul {
display: flex;
justify-content: space-between;
width: 100%;
margin: 0;
padding: 0;
align-items: center;
}
li {
display: block;
flex: 0 1 auto;
list-style-type: none;
}
</style>

Need a fixed navbar and landing page VH that incorporates navbar height

I am working on my first portfolio and running into some trouble doing two things.
Creating a fixed navbar. When I use position: fixed; it clears my float on the right "Contact Me", and everything collapses. I need this to stay spaced how it is in my example. Searched for quite some hours and I cannot find a fix as of yet, although I am a novice so I am sure that plays into this some as well.
For my landing page photo, I am trying to make a responsive design that allows it to resize to 100% of the available page size. I had implemented this with height: 100vh; however quickly noticed that it is taking the 100vh, and implementing this after my navbar, which leaves excess below. I tried to compensate by reducing the vh to account for the navbar, but of course I realized that this would not be a good fixed as it would only work for that viewport height, and not scale accordingly.
This leaves me either needing a fix to scale the content appropriately, or allow the photo to slide under the navbar, and occupy that space as well so that it is touching the top of the page.
Relevant HTML:
<header>
<div class="navbar">
<ul>
<li>Home</li>
<li>About Me</li>
<li>My Work</li>
<li style="float:right">Contact Me</li>
</ul>
</div>
</header>
<main>
<section class="homeLanding">
<h1>Hi, I'm Michael.</h1>
<p>A Front-End Web Dev</p>
<a href="#aboutMe" class="myBtn">Start here to learn more about me,
<br>and how I can help you</a>
</section>
Relevant CSS:
body {
margin: 0;
}
/** style navbar **/
.navbar ul {
background-color: #333;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden
}
.navbar li {
float: left;
}
.navbar li a {
display: block;
text-align: center;
text-decoration: none;
color: white;
padding: 14px 16px;
}
.navbar li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
/** style landing page **/
.homeLanding {
height: 100vh;
width: 100%;
margin: auto;
background: url(/**Insert Image**/);
display: flex;
background-size: cover;
background-position: center;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
top: 0;
}
.myBtn {
color: white;
text-decoration: none;
border: solid 3px white;
border-radius: 6px;
padding: 7px 7px 0px 7px;
}
p, h1 {
color: white;
}
Background image: http://res.cloudinary.com/dtgbwo6mf/image/upload/v1502053498/bg_b0vucn.jpg
Point 1
In order to have position: fixed on your navbar without losing its layout, all you need to do is make sure to apply width: 100% as well.
Point 2
What you're looking for is to make use of CSS's calculation-driven values.
This way, you can tell you body to occupy 100% of the vertical height minus the height of your navbar, with height: calc(100v - 46px).
Here's a complete sample:
body {
margin: 0;
}
/** style navbar **/
.navbar {
position: fixed;
width: 100%;
}
.navbar ul {
background-color: #333;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden
}
.navbar li {
float: left;
}
.navbar li a {
display: block;
text-align: center;
text-decoration: none;
color: white;
padding: 14px 16px;
}
.navbar li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
/** style landing page **/
.homeLanding {
padding-top: 46px;
height: calc(100vh - 46px);
width: 100%;
margin: auto;
background: url('http://res.cloudinary.com/dtgbwo6mf/image/upload/v1502053498/bg_b0vucn.jpg');
display: flex;
background-size: cover;
background-position: center;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
top: 0;
}
.myBtn {
color: white;
text-decoration: none;
border: solid 3px white;
border-radius: 6px;
padding: 7px 7px 0px 7px;
}
p,
h1 {
color: white;
}
<header>
<div class="navbar">
<ul>
<li>Home</li>
<li>About Me</li>
<li>My Work</li>
<li style="float:right">Contact Me</li>
</ul>
</div>
</header>
<main>
<section class="homeLanding">
<h1>Hi, I'm Michael.</h1>
<p>A Front-End Web Dev</p>
<a href="#aboutMe" class="myBtn">Start here to learn more about me,
<br>and how I can help you</a>
</section>
<section class="homeLanding">
<h1>SAMPLE EXTRA PADDING</h1>
<p>SAMPLE EXTRA PADDING</p>
<a href="#aboutMe" class="myBtn">SAMPLE EXTRA PADDING
<br>SAMPLE EXTRA PADDING</a>
</section>
Note that I added a second <section> to the HTML to demonstrate the scrolling effect with the fixed navbar.
Hope this helps! :)