CSS/HTML Weird LI spacing - html

I have a HTML5 header setup. It uses <ul> and <li> elements for the links.
This is the HTML:
<header>
<nav>
<ul>
<li id="logoli"><img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li class="5px">Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
And the CSS:
header {
height: 100%;
width: 100%;
}
nav {
width: 100%;
height: 65px;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
padding-left: 10px;
background-color: rgba(0, 0, 0);
color: #0077C5;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float: left;
}
li {
width: 100px;
line-height: 65px;
vertical-align: middle;
text-align: center;
margin: 0;
text-align: center;
display: inline;
list-style: none;
float: left;
}
#logo {
height: 50px;
}
#logoli {
padding-top: 7px;
width: 250px;
}
For some reason this happens: https://image.prntscr.com/image/w0LAMn5qRMq0OqIAfDTsHw.png
If you look at the nav bar you can easily see that the first two elements have a normal amount of spacing in-between. But the seccond and third have weirdly small spacing. I used inspect element on the <li>'s but they are all the same height. Can someone help me?

You need some HTML and CSS Fixes, Wrap the anchors in li elements and the issue was you have mentioned a width to the li, which causes this wierd spacing.
HTML
<header>
<nav>
<ul>
<li id="logoli">
<img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li class="5px">Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
CSS
header {
height: 100%;
width: 100%;
}
nav {
width: 100%;
height: 65px;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
padding-left: 10px;
background-color: rgba(0, 0, 0);
color: #0077C5;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float: left;
}
li {
line-height: 65px;
vertical-align: middle;
text-align: center;
margin:0px 10px;
text-align: center;
display: inline;
list-style: none;
float: left;
}
#logo {
height: 50px;
}
#logoli {
padding-top: 7px;
width: 250px;
}

You should put the <a href>'s inside the <li> tags like this:
<header>
<nav>
<ul>
<li id="logoli"><img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li>Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
Also - you have a class="5px" on one of the <li>'s, but that class isn't in your CSS. You should make your <li>'s a fixed width and center the text, or use a standard padding on the left and right of the <li> text to maintain a uniform spacing rather than injecting classes to push single elements around.

This is happening because you're explicitly defining a width on your list items. Remove the width and separate the list items with padding instead. Optionally, remove any padding on the last list item, since you won't need it.
li {
/* width: 100px; */
padding-right: 1em;
line-height: 65px;
vertical-align: middle;
text-align: center;
margin: 0;
text-align: center;
display: inline;
list-style: none;
float: left;
}
li:last-child {
padding-right: 0;
}
https://jsfiddle.net/eulloa/yx7vkt79/
Also, restructure your list items so that they are the parent element of your anchors, not the other way around.
<li>Home</li> <!-- structure your list items this way -->
<li>Home</li>

Related

I can't change the height or text-align in navbar items

I'm really new to this whole CSS and HTML and i'm trying to make a simple navbar with what I have learned.
The problem is, I have the navbar items centered and I was happy with it. Then I decided to add this hover option so the background color of each item would change when hovering on it.
The issue is I cannot change the height or alignment of the boxes.
weird box
#header {
background: #9842f5;
position: fixed;
width: 100%;
padding: 20px 0;
text-align: center;
}
nav ul {
font-family: Arial;
display: inline;
text-align: center;
margin: 0 20px;
}
nav ul:hover {
background: blue;
height: 20px important;
}
nav li {
display: inline;
opacity: 0.78;
text-align: center;
}
<nav id="header">
<ul>
<li>Home</li>
</ul>
<ul>
<li>Contact Info</li>
</ul>
<ul>
<li>NUKE</li>
</ul>
</nav>
The hover effect should be added to the list-items (<li>). To get more space, you can add padding instead of changing the height.
Also, only add one <ul> instead of 3.
#header {
background: #9842f5;
position: fixed;
width: 100%;
padding: 20px 0;
text-align: center;
}
nav ul {
font-family: Arial;
display: inline;
text-align: center;
margin: 0 20px;
}
nav li:hover{
background: blue;
}
nav li {
display: inline;
opacity: 0.78;
text-align: center;
padding: 20px;
}
<nav id="header">
<ul>
<li>Home</li>
<li>Contact Info</li>
<li>NUKE</li>
</ul>
</nav>

I am having trouble positioning my list element in my nav element

I am having troubles making my list go to the left of my nav element. I have tried to make the nav element relative and list absolute, but that just makes the words overlap each other.
nav {
height: 50px;
width: 400px;
background-color: red;
}
li {
display: inline;
justify-content: space-evenly;
margin: 10px;
font-size: 20px;
}
<nav>
<ul style="list-style: none">
<li>#stayhome</li>
<li>anime</li>
<li>queue</li>
<li>discord</li>
</ul>
</nav>
You can achieve the same result by setting display property of ul to flex
nav {
height: 50px;
width: 400px;
background-color: red;
line-height: 48px;
}
ul{
margin: 0;
padding: 0;
display:flex;
justify-content: space-around;
}
<nav>
<ul style="list-style: none">
<li>#stayhome</li>
<li>anime</li>
<li>queue</li>
<li>discord</li>
</ul>
</nav>
The justify-content property is irrelevant here - it only applies to elements in a Flex or Grid layout.
The issue here is the default margin/padding on the <ul> and <li> elements.
You might also want to set the line-height on the <nav> so that the list items are more centred.
nav {
height: 50px;
width: 400px;
background-color: red;
line-height: 48px;
}
nav > ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: inline;
margin: 10px;
font-size: 20px;
}
<nav>
<ul style="list-style: none">
<li>#stayhome</li>
<li>anime</li>
<li>queue</li>
<li>discord</li>
</ul>
</nav>

How to change the color of only one elemet and move only one element to right side?

This is my HTML:
<nav class="navbar">
<ul>
<li><a class="active home_button" href="#home">Home</a></li>
<li>Profile</li>
<li>Results</li>
<li>About Us</li>
</ul>
</nav>
This is the CSS:
.navbar {
height: 73px;
width: 100%;
}
.navbar ul {
list-style-type: none;
margin: 0;
height: 70px;
padding: 0;
overflow: hidden;
background-color: #362890;
}
.navbar li {
float: left;
}
.navbar li a {
display: block;
color: #fff;
text-align: center;
padding: 22px 20px;
text-decoration: none;
font-size: 26px;
I want to do two things. Firstly I want to make the color of Home orange. Secondly, I want to move About Us on the right side while keeping others on the left side. How can I do that?
Use float:right for the About Us item, and color:orange for the Home item.
.navbar {
height: 73px;
width: 100%;
}
.navbar ul {
list-style-type: none;
margin: 0;
height: 70px;
padding: 0;
overflow: hidden;
background-color: #362890;
}
.navbar li {
float: left;
}
.navbar li a {
display: block;
color: #fff;
text-align: center;
padding: 22px 20px;
text-decoration: none;
font-size: 26px;
}
.home_button{
color:orange !important;
}
#about-us{
float:right;
}
<nav class="navbar">
<ul>
<li><a class="active home_button" href="#home">Home</a></li>
<li>Profile</li>
<li>Results</li>
<li id="about-us">About Us</li>
</ul>
</nav>
I think adding a
!important to .home_button should do the trick, give it a try...
Example
.home_button {
color: orange !important;
}
The !important property in CSS is used to provide more weight (importance) than normal property. In CSS, the !important means that “this is important”, ignore all the subsequent rules, and apply !important rule
For more info on how !important works check here
give home and about a class, then style it

<nav> element is overflowing the <header>

The<nav> element is not rendered inside the <header> element even though it is nested inside.
I tried adding the over-flow:hidden property to the <header> element, using the index-head class. I also tried adding both position:relative and position:absolute.
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
}
a{
text-decoration: none;
}
.index-head{
height: 90px;
width: 100%;
background-color: #000;
overflow: hidden;
}
.logo{
width: 50px;
float: left;
margin: 20px;
margin-right: 0;
}
.brand-name{
color: #ffc107;
line-height: 90px;
font-family: 'Catamaran', sans-serif;
}
.index-head nav{
float: right;
margin-top: 0;
width: 50%;
}
.index-head nav ul li{
list-style: none;
display: inline-block;
font-size: 25px;
padding-left: 50px;
}
<body>
<header class="index-head">
<img class="logo" src="images/logo.png">
<h1 class="brand-name">Eeat</h1>
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Signup</li>
<li>Login</li>
</ul>
</nav>
</header>
</body>
Because you added a "h1" tag inside the header, which by default has
display: block
property that stretches the element to the entire width of the "header" element.
to solve this problem, you must add a css rule to the "h1" element
display: inline-block;
JSFiddle link: https://jsfiddle.net/nzf1egcr/1/
The simplest way to get the <nav> inside the <header> is to set the <h1.brand-name> element to display:inline-block. By default browser agents set <hX> tags to display:block, which spans those elements 100% of the available space and in this case is was pushing your <nav> down below it. Since the <header> has a fixed height this forced the <nav> outside.
I also added...
display: flex;
align-items: center;
justify-content: space-between;
To <header.index-head> to space the child elements evenly vertically and horizontally.
I then added flex-grow: 1; to the <nav> element, which makes sure it takes 'priority' when flex-box determines its width relative to its siblings.
Learn more about Flex Box
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
}
a{
text-decoration: none;
}
.index-head{
height: 90px;
width: 100%;
background-color: #000;
overflow: hidden;
display: flex;
align-items: center;
justify-content: space-between;
}
.logo{
width: 50px;
float: left;
margin: 20px;
margin-right: 0;
}
.brand-name{
color: #ffc107;
line-height: 90px;
font-family: 'Catamaran', sans-serif;
display: inline-block;
}
.index-head nav{
float: right;
margin-top: 0;
width: 50%;
flex-grow: 1;
}
.index-head nav ul li{
list-style: none;
display: inline-block;
font-size: 25px;
padding-left: 50px;
}
<body>
<header class="index-head">
<img class="logo" src="images/logo.png">
<h1 class="brand-name">Eeat</h1>
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Signup</li>
<li>Login</li>
</ul>
</nav>
</header>
</body>

What is the correct way of aligning this menu?

http://jsfiddle.net/rctdzdsm/
I'm trying to align the menu to the logo.
Do I have to manually margin it till I align correctly?
Or is there a better/proper way of doing this?
Because manually margining till I align it correctly, feels kinda kinda dumb.
* {
margin: 0;
padding: 0;
}
header {
background-color: #e58b1a;
}
header .logo {
float: left;
width: 400px;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
position: relative;
top: 20px;
left: 50px;
}
.menu {
float: right;
font-family: arial;
}
.menu li {
/** Float to the left, helps it become horizontal */
float: left;
/** Marigin-right allows you to space your links */
margin-right: 30px;
}
<header class="show-when-loaded" style="height: 108px;">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<div class="menu">
<nav>
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</div>
</header>
This is not a very flexible way of doing this. What if the height of your header changes?
Than you would manually have to align your padding and margins. If you are looking for a more accurate way of doing this, take a look at the different possibilities of displaying elements and aligning them. Even if your menu lives inside an unordert -list. Think of your list being a table and the list items being the table cells. If your elements inherit the height from your header you could easily align them vertically in the middle. Here is a snippet to give you an idea of what i mean.
html, body {
margin: 0;
padding: 0;
}
header {
height: 108px;
background-color: orange;
}
header:after {
clear: both;
}
nav {
height: 100%;
}
nav ul {
margin: 0;
padding: 0;
float: left;
display: table;
height: inherit;
}
nav li {
display: table-cell;
height: inherit;
vertical-align: middle;
}
nav a {
text-decoration: none;
color: white;
font-family: 'Arial';
}
.logo a {
font-size: 50px;
padding: 0 0 0 20px;
}
nav .nav-bar {
float: right;
}
nav .nav-bar li {
padding-right: 20px;
}
<header>
<nav role='navigation'>
<ul class="logo">
<li>
Daz Me Lulz
</li>
</ul>
<ul class="nav-bar">
<li>Home</li>
<li>About</li>
<li>Clients</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
Have fun.
I removed the position: relative. because its not needed.
Added the some margins for being aligned better.
Tried to make the li elements wrap, ended up with a none wrapping solution instead.
Made the links text direction from right to left. So they stick to the right side of the screen, without breaking the flow, as float: right does.
html, body {
margin: 0;
padding: 0;
}
header {
white-space: nowrap;
background-color: #e58b1a;
min-width: 650px;
}
header .logo {
display: inline-block;
font-family: 'Arial';
color: white;
font-size: 50px;
padding: 20px 0px 20px 20px;
}
.menu {
display: inline-block;
direction: rtl;
font-family: arial;
min-width: 350px;
width: calc(100% - 350px);
margin-top: 40px;
}
.menu ul {
display: inline;
direction: rtl;
padding: 0;
}
.menu ul li {
display: inline;
list-style: none;
margin-right: 10px;
}
<header class="show-when-loaded">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<nav class="menu">
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</header>
Looks like CSS tables will help here.
header {
background-color: #e58b1a;
display: table;
vertical-align: middle;
width: 100%;
}
header .logo {
display: table-cell;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
vertical-align: middle;
}
.menu {
display: table-cell;
font-family: arial;
text-align: right;
vertical-align: middle;
}
* {
margin: 0;
padding: 0;
}
header {
background-color: #e58b1a;
display: table;
vertical-align: middle;
width: 100%;
}
header .logo {
display: table-cell;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
vertical-align: middle;
}
.menu {
display: table-cell;
font-family: arial;
text-align: right;
vertical-align: middle;
}
.menu li {
display: inline-block;
}
<link rel="stylesheet" type="text/css" href="style.css">
<title>Daz Me Lulz - Providng Renewable Energy to the children of Latin America</title>
<body>
<header class="show-when-loaded" style="height: 108px;">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<div class="menu">
<nav>
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</div>
</header>
</body>