I've got a nav bar and I'm trying to have the logo and text on the same line as each other and the links. Also, my links are in the wrong order so I want to know why that is and how to fix it?
This is what it looks like:
Here is my html code:
#nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #D4D4D4;
}
#nav li {
float: right;
}
#nav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#nav
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
<div id="nav" >
<ul >
<p style="display:inline-block;vertical-align:middle; margin-top:2px"><img src='images/DBA
LOGO.png 'width="30" height="30" style="float:left">DIGITAL BUCKET COMPANY</p>
<li style="text-align:right;"> <a> Home </a></li>
<li style="text-align:right;"> <a> Courses </a></li>
<li style="text-align:right;"> <a> Log in </a></li>
<li style="text-align:right;"> <a> Trainers </a></li>
<li style="text-align:right;"> <a> Enterprise </a></li>
</ul>
</div>
I simplify your snippet and rewrite the css. Please note it is not valid to put <p> under <ul>. Introducing <nav>, ideal for navigation. You don't have to float everything. float: right put the first element to to the most right, that's why the menu order is reverse. You can shift the whole navigation to right using text-align: right. Finally, to align the logo with the company name, I set its line-height to 30px, same as the <img> height.
#logo {
display: inline-block;
vertical-align: middle;
margin-top: 2px;
float: left;
}
#company-name {
line-height: 30px;
}
#nav {
background-color: #D4D4D4;
}
nav {
text-align: right;
}
nav a {
display: inline-block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
color: white;
}
nav a:hover {
background-color: #111;
}
<div id="nav">
<p id="logo" style="">
<img src='images/DBA LOGO.png ' width="30" height="30" style="float: left"> <label id="company-name">DIGITAL
BUCKET COMPANY</label>
</p>
<nav>
<a> Home </a> <a> Courses </a> <a> Log in </a> <a> Trainers </a> <a> Enterprise </a>
</nav>
</div>
You could remove all float properties and use flexbox.
Add flexbox properties to ul, have it take the whole space and align items to the center; clear the inherited margins of your p and let it grow; remove float: right from the li item.
Also note that you should not have a <p> inside <ul>
#nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #d4d4d4;
display: flex;
align-items: center;
justify-content: space-between;
}
#nav p {
flex-grow: 1;
margin: auto;
}
#nav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#nav
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
<div id="nav">
<ul>
<p><img src='images/DBA
LOGO.png ' width="30" height="30" style="float:left">DIGITAL BUCKET COMPANY</p>
<li style="text-align:right;"> <a> Home </a></li>
<li style="text-align:right;"> <a> Courses </a></li>
<li style="text-align:right;"> <a> Log in </a></li>
<li style="text-align:right;"> <a> Trainers </a></li>
<li style="text-align:right;"> <a> Enterprise </a></li>
</ul>
</div>
Added new CSS, Hope this helps.
p {
margin: 0;
display: flex;
align-items: center;
white-space: nowrap;
}
#nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #D4D4D4;
display: flex;
align-items: center;
justify-content: space-between;
}
#nav li {
float: right;
}
#nav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
white-space: nowrap;
}
#nav
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
<div id="nav" >
<ul >
<p style="vertical-align:middle; margin-top:2px"><img src='images/DBA
LOGO.png 'width="30" height="30" style="">DIGITAL BUCKET COMPANY</p>
<li style="text-align:right;"> <a> Home </a></li>
<li style="text-align:right;"> <a> Courses </a></li>
<li style="text-align:right;"> <a> Log in </a></li>
<li style="text-align:right;"> <a> Trainers </a></li>
<li style="text-align:right;"> <a> Enterprise </a></li>
</ul>
</div>
p is valid children of ul, it's bad html so move p tag in nav id
second you can set layout using flexbox and remove float:right from li
#nav {
display: flex;
justify-content: space-between;
align-items: center;
overflow: hidden;
background-color: #D4D4D4;
}
#nav ul {
list-style-type: none;
margin: 0;
padding: 0;
}
#nav li {
/*float: right;*/
display:inline-block;
}
#nav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#nav li a:hover {
background-color: #111;
}
<div id="nav" >
<p style="display:inline-block;vertical-align:middle; margin:0"><img src='images/DBA
LOGO.png 'width="30" height="30" style="float:left">DIGITAL BUCKET COMPANY</p>
<ul>
<li style="text-align:right;"> <a> Home </a></li>
<li style="text-align:right;"> <a> Courses </a></li>
<li style="text-align:right;"> <a> Log in </a></li>
<li style="text-align:right;"> <a> Trainers </a></li>
<li style="text-align:right;"> <a> Enterprise </a></li>
</ul>
</div>
its better practice to put only <li> inside <ul>
check flexbox
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
#nav{
display: flex;
justify-content: space-between;
align-items: center;
background-color: #D4D4D4;
}
#nav p{
height: 100%;
display: flex;
justify-content: flex-start;
align-items: center;
}
#nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
#nav li {
float: left;
}
#nav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
<div id="nav" >
<p><img src='images/DBA
LOGO.png 'width="30" height="30">DIGITAL BUCKET COMPANY</p>
<ul>
<li style="text-align:right;"> <a> Home </a></li>
<li style="text-align:right;"> <a> Courses </a></li>
<li style="text-align:right;"> <a> Log in </a></li>
<li style="text-align:right;"> <a> Trainers </a></li>
<li style="text-align:right;"> <a> Enterprise </a></li>
</ul>
</div>
Related
This question already has answers here:
How do I center floated elements?
(12 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I have a problem with my Nav Bar. I need it to be centered on the screen, so it is in the middle of everything. But my Nav Bar will only be on the left side of my screen.
I tried to use Margin to fix the issue, but then it will not be responsive to the rest, so that does not work.
Here is my code for the Nav Bar:
HTML:
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>
Ignore the "href", they will be sorted afterwards
the CSS:
*{
margin: 0%;
padding: 0%;
background-color: rgb(53, 53, 53);
text-decoration: none;
}
nav{
width: 100%;
height: 100px;
text-align: center;
display: inline-block;
}
ul{
text-align: center;
}
ul li{
list-style: none;
display: inline-block;
float: left;
line-height: 100px;
}
ul li a{
font-family: 'PT Sans', sans-serif;
color: white;
font-size: 200%;
padding: 0px 20px;
display: block;
display: inline-block;}
ul li a:hover {
color: red;
}
I did read some of the others answered questions but without any luck from them, hope you can help me once again!
Just add display: inline-block; to the CSS rule for ul to make it only as wide as its contents and therefore also to allow the text-align: center for nav to have an effect:
* {
margin: 0%;
padding: 0%;
background-color: rgb(53, 53, 53);
text-decoration: none;
}
nav {
width: 100%;
height: 100px;
text-align: center;
display: inline-block;
}
ul {
display: inline-block;
text-align: center;
}
ul li {
list-style: none;
display: inline-block;
float: left;
line-height: 100px;
}
ul li a {
font-family: 'PT Sans', sans-serif;
color: white;
font-size: 200%;
padding: 0px 20px;
display: block;
display: inline-block;
}
ul li a:hover {
color: red;
}
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>
Try putting the nav in center tags.
Like this:
<center>
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>
</center>
Sorry if this doesn't help. I couldn't fully understand your problem.
Try this. Don't use <center> tag, it's obsolete.
html:
<header>
<nav>
<ul>
<li>Menu</li>
</ul>
</nav>
</header>
css:
header { width: 100%; }
nav { width: 980px; margin: 0 auto; display: block; }
margin: 0 auto; will center the element. just make sure that it has a width, otherwise this will not work.
try this =)
* {
margin: 0;
padding: 0;
background-color: rgb(53, 53, 53);
text-decoration: none;
}
nav {
position: absolute;
width: 100%;
height: 100px;
text-align: center;
display: block;
margin: auto;
border: 2px solid white;
}
ul {
display: inline-block;
text-align: center;
width: max-content;
}
ul li {
list-style: none;
display: inline-block;
float: left;
line-height: 100px;
margin: auto;
}
ul li a {
font-family: 'PT Sans', sans-serif;
color: white;
font-size: 100%;
padding: 0px 10px;
display: inline-block;
}
ul li a:hover {
color: red;
}
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>
Here's my HTML:
<body>
<nav class="navbar">
<span class="navbar-toggle" id="js-navbar-toggle">
<i class="fas fa-bars"></i>
</span>
logo
<ul class="main-nav" id="js-menu">
<li>
Home
</li>
<li>
Products
</li>
<li>
About Us
</li>
<li>
Contact Us
</li>
<li>
Blog
</li>
</ul>
</nav>
</body>
Here's my CSS:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-family: "Josefin Sans", sans-serif;
}
.navbar {
font-size: 18px;
padding-bottom: 10px;
}
.main-nav {
list-style-type: none;
display: none;
}
.nav-links,
.logo {
text-decoration: none;
color: black;
}
.main-nav li {
text-align: center;
margin: 15px auto;
}
.logo {
display: inline-block;
font-size: 22px;
margin-top: 10px;
margin-left: 20px;
}
.navbar-toggle {
position: absolute;
top: 10px;
right: 20px;
cursor: pointer;
color: rgba(255, 255, 255, 0.8);
font-size: 24px;
}
.active {
display: block;
}
And this is the output I'm getting:
I'd like my output to be like this instead (ignore the spacing difference, just my screenshot, but have them split in the center like so):
What's the best way of doing this?
I'm thinking of creating another unorder list and moving it, but I don't know how to do that.
Make use of justify-content: space-between;.
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
ul {
list-style-type: none;
}
.nav-links, .logo {
text-decoration: none;
color: #000;
}
.navbar {padding: 20px;}
.navbar, ul {display: flex; flex-direction:row;}
ul li {padding: 0 5px;}
.wrapper {display:flex; justify-content: space-between; width:100%; }
<nav class="navbar">
logo
<div class="wrapper">
<ul class="main-nav" id="js-menu">
<li>
Home
</li>
<li>
Products
</li>
<li>
About Us
</li>
</ul>
<ul class="ul2">
<li>
Contact Us
</li>
<li>
Blog
</li>
</ul>
</div>
</nav>
I have a unordered list in my HTML with 5 lists that all had with links on them that were working fine. It was working fine until I added another unordered list, but i have no links inside it. I even gave it a separate class.
Here is my markup/CSS:
ul {
margin: 0;
list-style-type: none;
color: white;
diplay: block;
padding-top: 20px;
padding-left: 350px;
font-family: "Economica", sans-serif;
font-size: 28px;
text-decoration: none;
}
ul li {
padding-left: 45px;
padding-right:45px;
display: inline;
text-decoration: none;
}
ul li a:hover {
font-weight:bold;
color:grey;
}
a {
text-decoration: none;
color:white;
}
<div id="mainPage" >
<img src="http://xoio.de/al_by_xoio1.jpg" alt="concert stadium" style="width:100%; position:absolute; padding-top:70px; height:930px">
<div id="navBar">
<div id="Logo">
<h1 style="font-weight:bold;"> Ice Arena </h1>
</div>
<ul id="select">
<li>
<a style="color:#ffe700;" href="#">Home</a>
</li>
<li>
Gallery
</li>
<li>
Order Form
</li>
<li>
The Arena
</li>
<li>
Contact Us
</li>
</ul>
</div>
</div>
I've only added the code regarding the list and area of my problem.
Add position: relative; to ul, otherwise other elements are above it and prevent the link behavior.
ul {
margin: 0;
list-style-type: none;
color: white;
diplay: block;
padding-top: 20px;
padding-left: 350px;
font-family: "Economica", sans-serif;
font-size: 28px;
text-decoration: none;
/* added */
position: relative;
}
ul li {
padding-left: 45px;
padding-right: 45px;
display: inline;
text-decoration: none;
}
ul li a:hover {
font-weight: bold;
color: grey;
}
a {
text-decoration: none;
color: white;
}
<div id="mainPage">
<img src="http://xoio.de/al_by_xoio1.jpg" alt="concert stadium" style="width:100%; position:absolute; padding-top:70px; height:930px">
<div id="navBar">
<div id="Logo">
<h1 style="font-weight:bold;">Ice Arena</h1>
</div>
<ul id="select">
<li><a style="color:#ffe700;" href="#">Home</a></li>
<li>Gallery</li>
<li>Order Form</li>
<li>The Arena</li>
<li>Contact Us</li>
</ul>
</div>
</div>
Try deleting the CSS property (position:absolute;) in line 3.
Hello I'm a beginner and got a problem with my navigationbar in HTML. I've tried a lot but they are still unclickable...
Is there a mistake in my HTML?
<header>
<nav display: inline;>
<ul>
<li>
<navi><img src="images/header_logo.png" alt="Logo" href="index.html" width="40%" height="40%" /></navi>
</li>
<li>
<navi>
<z class="active" href="index.html">Startseite</z>
</navi>
</li>
<li>
<navi>
<z href="produkte.html">Produkte</z>
</navi>
</li>
<li>
<navi>
<z href="about.html">Über uns</z>
</navi>
</li>
<li>
<navi>
<z href="agb.html">AGB</z>
</navi>
</li>
</ul>
</nav>
or is the mistake in my CSS? It's probably not the best way to style it but it looks good in my eyes. However I cant click any links...
body {
margin: 0px;
padding: 0px;
}
img {
max-width: 100%;
}
ul {
margin: 0px;
list-style: none;
padding: 0px;
overflow: hidden;
background-color: burlywood;
}
li navi {
float: left;
display: block;
text-align: center;
padding: 14px 16px;
}
li navi z {
float: left;
display: block;
text-align: center;
padding: 14px 30px;
margin-top: 40px;
color: white;
border: 2px solid white;
}
.active {
background-color: #deae6f;
}
li z:hover:not(.active) {
background-color: #deb27a;
}
Thanks for help
Here's the link for Your above Problem https://jsfiddle.net/kj0w9g76/
Reason:- for anchor tag we use 'a' not with 'z'
instead of navi tag we use nav tag as used in code below.
body {
margin: 0;
padding: 0;
}
img {
max-width: 100%;
}
ul {
margin: 0;
list-style: none;
padding: 0;
overflow: hidden;
background-color: burlywood;
position: relative;
z-index: 1;
}
li nav {
float: left;
display: block;
text-align: center;
padding: 14px 16px;
}
li nav z {
float: left;
display: block;
text-align: center;
padding: 14px 30px;
margin-top: 40px;
color: white;
border: 2px solid white;
}
.active {
background-color: #deae6f;
}
li a:hover:not(.active) {
background-color: #deb27a;
}
<header>
<nav style="display: inline;">
<ul>
<li>
<nav><a href="index.html" width="40%" height="40%" ><img src="images/header_logo.png" alt="Logo"/></a></navi>
</li>
<li>
<nav>
<a class="active" href="index.html">Startseite</a>
</nav>
</li>
<li>
<nav>
Produkte
</nav>
</li>
<li>
<nav>
Über uns
</nav>
</li>
<li>
<nav>
AGB
</nav>
</li>
</ul>
</nav>
</header>
You've a problem in your code, styles, anchors are not correct there is the correct code below.
* {
box-sizing: border-box;
}
body {
margin: 0px;
padding: 0px;
}
img {
max-width: 100%;
}
ul {
margin: 0px;
list-style: none;
padding: 0px;
overflow: hidden;
background-color: burlywood;
}
li nav {
float: left;
display: block;
text-align: center;
padding: 14px 16px;
}
li a {
float: left;
display: block;
text-align: center;
padding: 14px 30px;
margin-top: 40px;
color: white;
border: 2px solid white;
}
.active {
background-color: #deae6f;
}
li a:hover:not(.active) {
background-color: #deb27a;
}
<header>
<nav style="display: block;">
<ul>
<li>
<nav><img src="images/header_logo.png" alt="Logo" href="index.html" width="40%" height="40%" /></nav>
</li>
<li>
<nav>
<a class="active" href="index.html">Startseite</a>
</nav>
</li>
<li>
<nav>
Produkte
</nav>
</li>
<li>
<nav>
Über uns
</nav>
</li>
<li>
<nav>
AGB
</nav>
</li>
</ul>
</nav>
</header>
I am building a horizontal menu using unordered lists. 4 items are links and then there's an image in the center. Currently the links are centered on the middle of the image, but the I would rather the text vertically align lower than middle. The image is 140px tall and I would like the text to be at 50px. I've tried playing with vertical-align and line-height, but no joy. Padding doesn't work. I'm sure this is obvious and I'm just missing it. Any help would be appreciated.
Code:
.menu {
text-align: center;
}
.menu ul {
display: inline-table;
}
.menu ul li {
display: inline;
line-height: 0px;
}
.menu .link {
padding: 15px;
}
.menu a {
text-decoration: none;
font-size: 17px;
font-weight: 400;
color: #131313;
}
.menu a:hover {
color: #330000;
}
<div class="container">
<div class="row menu">
<ul>
<li class="link">MENU
</li>
<li class="link">ABOUT
</li>
<li class="link">
<a href="index.html">
<img class="center logo" src="https://placehold.it/140x140" />
</a>
</li>
<li class="link">BLOG
</li>
<li class="link">CONTACT
</li>
</ul>
</div>
</div>
Using bootstrap 3.3.6 as well.
You mean like this fiddle?
.menu { text-align:center; }
.menu ul { display:inline-table;}
.menu ul li {display:inline; line-height:0px;}
.menu .link {
padding: 15px;
}
img{
width: auto;
height: 140px;
vertical-align: -50px;
}
.menu a {
text-decoration: none;
font-size: 17px;
font-weight: 400;
color: #131313;
}
.menu a:hover {
color: #330000;
}
<div class="container">
<div class="row menu">
<ul>
<li class="link">MENU</li>
<li class="link">ABOUT</li>
<li class="link"><img class="center logo" src="https://lh4.ggpht.com/wKrDLLmmxjfRG2-E-k5L5BUuHWpCOe4lWRF7oVs1Gzdn5e5yvr8fj-ORTlBF43U47yI=w300"/></li>
<li class="link">BLOG</li>
<li class="link">CONTACT</li>
</ul>
</div>
Give the text LI's a different class, and then apply vertical-align: 50px.
.menu {
text-align: center;
}
.menu ul {
display: inline-table;
}
.menu ul li {
display: inline;
line-height: 0px;
}
.menu .link {
padding: 15px;
}
.menu .link.align {
vertical-align: 50px;
}
.menu a {
text-decoration: none;
font-size: 17px;
font-weight: 400;
color: #131313;
}
.menu a:hover {
color: #330000;
}
<div class="container">
<div class="row menu">
<ul>
<li class="link align">MENU
</li>
<li class="link align">ABOUT
</li>
<li class="link">
<a href="index.html">
<img class="center logo" src="https://placehold.it/140x140" />
</a>
</li>
<li class="link align">BLOG
</li>
<li class="link align">CONTACT
</li>
</ul>
</div>
</div>
Not sure if you want the text to be vertical-align: top or vertical-align: middle but you can adjust it as you please with this codepen!
I made the li's inline-block which enables them to be adjusted by the vertical-align property and still be horizontal across your navigation.
I gave the background some color so that the stock image wouldn't blend in as much and you'd be able to see the alignment.
HTML:
<div class="container">
<div class="row menu">
<ul>
<li class="link">MENU</li>
<li class="link">ABOUT</li>
<li class="link"><img class="center logo" src="http://lorempixel.com/140/200/"/></li>
<li class="link">BLOG</li>
<li class="link">CONTACT</li>
</ul>
</div>
CSS:
body {
background: #BBB;
}
.menu {
text-align:center;
}
.menu ul {
display:inline-table;
}
.menu ul li {
display: inline-block;
vertical-align: middle;
}
.menu .link {
padding: 1em;
}
.menu a {
text-decoration: none;
font-size: 3.125em;
color: #131313;
}
.menu a:hover {
color: #330000;
}