Active class in CSS - html

For some reason the active class doesn't have any effect on my page. Would be very grateful for someone to take a peek and explain why it doesn't.
.nav-item {
display: inline;
list-style: none;
margin-left: 40px;
}
.nav-item a {
font-size: 30px;
text-decoration: none;
color: white;
}
.nav-item a:hover,
.active {
color: #f3ef36;
}
<header id="header">
<nav>
<ul class="nav">
<li class="nav-item"><a class="active" href="#">Hem</a></li>
<li class="nav-item">Meny</li>
<li class="nav-item">Kontakt</li>
</ul>
</nav>
</header>
From the examples I've checked on w3schools and others it should work fine, but for some reason the active class doesn't seem to bite in this context. The hover pseudoclass works but not the active class I've manually typed in the different list elements.

Please read on CSS specificity.
Rules on selector .nav-item a will always have precedence on .active.
You need to increase the specificity e.g. like this a.active
See the snippet below:
header {background:#000}
.nav-item {
display: inline;
list-style: none;
margin-left: 40px;
}
.nav-item a {
font-size: 30px;
text-decoration: none;
color: white;
}
.nav-item a:hover,
a.active {
color: #f3ef36;
}
<header id="header">
<nav>
<ul class="nav">
<li class="nav-item"><a class="active" href="#">Hem</a></li>
<li class="nav-item">Meny</li>
<li class="nav-item">Kontakt</li>
</ul>
</nav>
</header>

.nav-item {
display: inline;
list-style: none;
margin-left: 40px;
}
.nav-item a {
font-size: 30px;
text-decoration: none;
color: white;
}
.nav-item a:hover,
.active {
color: #f3ef36;
}
<header id="header">
<nav>
<ul class="nav">
<li class="nav-item"><a class="active" href="#">Hem</a></li>
<li class="nav-item">Meny</li>
<li class="nav-item">Kontakt</li>
</ul>
</nav>
</header>

Related

How to get rid of right margin in last item of navigation bar using last child

I have created a navigation bar and have added margin-right to all the items.
However, when I try to get rid of the margin right in the last child of the navigation item "contact", it won't disappear.
.navbar {
text-decoration: none;
margin-bottom: 150px;
}
.nav-item {
display: inline-block;
text-decoration: none;
}
.nav-list {
text-align: center;
}
.nav-list li a {
text-decoration: none;
margin-right: 100px;
}
.nav-list ul li a:last-child {
margin-right: 0;
}
.nav-list li a:active {
color: #000;
}
.nav-list li a:visited {
color: #000;
}
<div>
<nav class="navbar">
<ul class="nav-list">
<li class="nav-item">Home</li>
<li class="nav-item">About</li>
<li class="nav-item">Work</li>
<li class="nav-item">Services</li>
<li class="nav-item">Contact</li>
</ul>
</nav>
</div>
Here is my code pen: https://codepen.io/derrickogole/pen/rNxbaaK
Is there a way to do this without adding an additional class to contact?
Use the :last-child psuedo-class.
.nav-item:last-child a {
margin: 0;
}
See it here:
.navbar {
text-decoration: none;
margin-bottom: 150px;
}
.nav-item {
display: inline-block;
text-decoration: none;
}
.nav-list {
text-align: center;
}
.nav-list li a {
text-decoration: none;
margin-right: 100px;
}
.nav-list ul li a:last-child {
margin-right: 0;
}
.nav-list li a:active {
color: #000;
}
.nav-list li a:visited {
color: #000;
}
.nav-item:last-child a {
margin: 0;
}
<div>
<nav class="navbar">
<ul class="nav-list">
<li class="nav-item">Home</li>
<li class="nav-item">About</li>
<li class="nav-item">Work</li>
<li class="nav-item">Services</li>
<li class="nav-item">Contact</li>
</ul>
</nav>
</div>
Additionally, your markup was invalid due to closing </li> before </a>. a is nested in li so it needs to be closed first.

Is there any way to align the logo so that they all lie on the same baseline?

Final HTML output that I have a problem with:
.navbar {
font-family: Cookie, cursive;
font-size: 40px;
text-align: center;
display: block;
}
a:link {
text-decoration: none;
}
.navbar li {
display: inline-block;
padding: 10px;
}
.navbar .cur {
color: black;
}
<nav>
<ul class="navbar">
<li><a href=index.html>Home</a></li>
<li><a href=photo.html>Photography</a></li>
<li><a href=blog.html>Blog</a></li>
<li><a href=work.html>Work With Me</a></li>
<li><a class="cur" href=about.html>About</a></li>
<li><img class="log" src="assets/link_logo.png" alt="logo" height="40px"></li>
</ul>
</nav>
Any suggestions to make the logo appear on the same baseline will be much appreciated.
I think this code can help you!
.navbar {
font-family: Cookie, cursive;
font-size: 20px;
text-align: center;
display: block;
}
a:link {
text-decoration: none;
}
.navbar li {
display: inline-block;
padding: 10px;
}
.navbar .cur {
color: black;
}
.mainDiv{
display: flex;
}
<div class='mainDiv'>
<div class='menuDiv'>
<nav>
<ul class="navbar">
<li><a href=index.html>Home</a></li>
<li><a href=photo.html>Photography</a></li>
<li><a href=blog.html>Blog</a></li>
<li><a href=work.html>Work With Me</a></li>
<li><a class="cur" href=about.html>About</a></li>
</ul>
</nav>
</div>
<div class='logoDiv'>
<span> <img src="https://cdn4.iconfinder.com/data/icons/social-media-icons-the-circle-set/48/linkedin_circle-512.png" width="100px" height="100px"></span>
</div>
<div>
Make the .navbar a flex container with no-wrap attribute set. This way all the items will be on the same line. If you want to vertically position them in the center - set the align-items: center attr on the flex container

Add padding without moving the element

When I hover on an padded element it moves a bit how can I make it stable
<nav>
<ul id="nav-ul">
<li >Profile</li>
<li >Home</li>
<li >Friends</li>
<li >Notifications</li>
</ul>
</nav>
And this is the css code
nav ul li {
float: right;
list-style: none;
padding: 15px;
padding-left: 40px;
}
.nav-item{
color: #fff;
text-decoration: none;
}
.nav-item:hover {
background-color: #2c3e50;
padding: 5px;
}
Just remove the padding: 5px; from your css:
.nav-item {
padding: 5px;
}
.nav-item:hover {
background-color: #2c3e50;
}
<nav>
<ul id="nav-ul">
<li >Profile</li>
<li >Home</li>
<li >Friends</li>
<li >Notifications</li>
</ul>
</nav>
You can add padding to it before without hover
.nav-item {
padding: 5px;
}
.nav-item:hover {
background-color: #2c3e50;
}
<nav>
<ul id="nav-ul">
<li >Profile</li>
<li >Home</li>
<li >Friends</li>
<li >Notifications</li>
</ul>
</nav>
thanks guys i figure it out you just need to add padding to both of them
.nav-item{
color: #fff;
text-decoration: none;
padding: 5px;
}
.nav-item:hover {
background-color: #2c3e50;
padding: 5px;
}

i have <a> links that were working but for some reason have stopped

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.

Getting a custom navbar dropdown to align properly

i am trying to create a nav bar for my website. the drop down is giving me trouble. how do i align the submenu so that it shows as a proper drop down?
<div id="mainhead">
<div id="title">
<h1>StudiousEnough </h1>
</div>
<li>Home</li>
<li><a href="" >class</a>
<ul>
<li> Class XI </li>
<li> CLass XII </li>
</ul>
</li>
<li><a href="#" >jee</a></li>
<li><a href="aboutus.html" >about us</a></li>
</ul>
and here is the css:
#mainhead #mainnav ul {
list-style-type: none;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-left: 0px;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
#mainhead #mainnav li {
display: block;
width: 25%;
background-color: #4D4D4D;
color: #FFFFFF;
text-align: center;
text-transform: uppercase;
float: left;
padding-bottom: 4px;
padding-top: 4px;
font-family: averia-libre;
font-style: normal;
font-weight: 700;
}
#mainhead #mainnav {
position: absolute;
width: 100%;
left: 0%;
right: 0%;
}
#mainhead #mainnav a{
display:block;
color: #ffffff;
text-decoration: none;
}
#mainhead #mainnav li:hover,#mainhead #mainnav li:focus,#mainhead #mainnav li:active {
background-color: #2535F3;
text-decoration: none;
}
#mainhead #mainnav ul li ul{
display: none;
}
#mainhead #mainnav ul li:hover ul{
display:block;
}
See the fiddle
I've added the following CSS to your CSS
CSS
#mainhead #mainnav ul li ul li{
width:100%;
clear:both;
}
First things first - check your html; you have some problems here. The list item tags need to be enclosed inside the unordered list parent tags. Also I don't see some of the ids and classes existing in the html that is used in your css.
If you always format your code, indenting enclosed blocks, that will help you spot all errors more quickly.
I went ahead and added a parent div with id = 'mainnav' and cleaned up your HTML, and it mostly renders as you'd expect. To get the submenu list items to 'stack' just make width 100%.
Clear is not necessary.
Check out the example on jsfiddle here:
https://jsfiddle.net/pianopaul/r3bb8k24/5/
<div id="mainhead">
<div id="title">
<h1>StudiousEnough </h1>
<div id="mainnav">
<ul class="menu">
<li>Home</li>
<li><a href="" >class</a>
<ul class="submenu">
<li> Class XI </li>
<li> CLass XII </li>
</ul>
</li>
<li><a href="#" >jee</a></li>
<li><a href="aboutus.html" >about us</a></li>
</ul>
</div>
</div>
</div>