Pseudo class for navbar not working - html

So I have a simple navbar which is not working for some reason. All the other links and pages work except for this one and I was wondering if someone would be able to spot an error in the following code. Notice how 'glob' is not yellow. I thought I had a more specific rule somewhere else which was overriding that rule but I don't think I have such a rule, I only have less specific.
#subnav {
height: 10%;
text-align: center;
background-color: green;
width: 100%;
}
#subnav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
text-align: center;
width: 100%;
font-weight: bold;
}
#subnav li {
display: inline-block;
}
#subnav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#subnav li a:hover {
color: yellow;
}
#subnav li a:active {
color: yellow;
}
<div id="subnav">
<ul>
<li> Sam </li>
<li> Sam </li>
<li> Sam </li>
<li> Sam </li>
<li> Sam </li>
<li> Po </li>
<li> <a class="active" href="glob.html">Glob </a></li>
<li> Donors </li>
</ul>
</div>

.active in your case is a class, not a state which would be adressable via a pseudo-selector. So your selector for it has to be
#subnav li a.active {
color: yellow;
}
(note the . instead of the :)
#subnav {
height: 10%;
text-align: center;
background-color: green;
width: 100%;
}
#subnav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
text-align: center;
width: 100%;
font-weight: bold;
}
#subnav li {
display: inline-block;
}
#subnav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#subnav li a:hover {
color: yellow;
}
#subnav li a.active {
color: yellow;
}
<div id="subnav">
<ul>
<li> Sam </li>
<li> Sam </li>
<li> Sam </li>
<li> Sam </li>
<li> Sam </li>
<li> Po </li>
<li> <a class="active" href="glob.html">Glob </a></li>
<li> Donors </li>
</ul>
</div>

If you want to target the active class, you must use .active, not :active
so the rule will be:
#subnav li a.active {
color: yellow;
}
The :active pseudo selector works a little different, here is a good explanation https://css-tricks.com/almanac/selectors/a/active/
But in your code you are adding the active class, and not using it on the css later.
Hope this help you.

Related

How can I make the background color here to not move whan I hover the links?

This is A navigation bar with links that I made. I want that the black background will not move when hovering...
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
width: 40%;
}
li {float: left;}
li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}
li a:hover {font-size: large;}
<body>
<nav>
<ul>
<li> Home </li>
<li> Articles </li>
<li> About us </li>
</ul>
</nav>
</body>
You can use transform:scale(X), it won't disturb the flow
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
width: 40%;
}
li {float: left;}
li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}
li a:hover {transform:scale(1.15);}
<body>
<nav>
<ul>
<li> Home </li>
<li> Articles </li>
<li> About us </li>
</ul>
</nav>
</body>
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transforms/Using_CSS_transforms
By modifying the coordinate space, CSS transforms change the shape and position of the affected content without disrupting the normal document flow. This guide provides an introduction to using transforms.
I suppose you wanted to do something like this. Though I would suggest as #g-cyrelius mentioned, in other answer -- You should make use of transform: scale(2) or something, to avoid break of flow, in your code.
I tried to use your code font-size: large, but this is not the better option.
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333333;
width: 80%;
}
li {display: inline-block;text-align:center;position:relative;height: 50px;width: 30%;box-sizing: border-box;}
li a {
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
position:absolute;
top: 0;
left: 30%;
}
li a:hover {
font-size: large;
}
<nav>
<ul>
<li> Home </li>
<li> Articles </li>
<li> About us </li>
</ul>
</nav>

Centering text on a navigation bar

I need to center these links while leaving Santos Fire Department where it is
BTW the only reason that Santos Fire Department has two different tags is to avoid the hover animation.
ul.nav{
display: block;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
height: 3.3em;
}
ul.nav li {
float:left;
}
ul.nav li a{
display: block;
color: white;
padding: 14px 16px;
text-decoration: none;
text-align: center;
}
ul.nav li .navlogo{
overflow: hidden;
background-color: #333;
height: 3.3em;
}
ul.nav li a.navlogo{
display: inline-block;
padding-top: 10px;
margin-left: 10px;
font-size: 150%;
z-index: -1;
}
<nav class="navbar">
<ul class="nav">
<li id="logo"><a class="navlogo">Santos Fire Department</a></li>
<li id="nav">TeamSpeak (Temp Server)</li>
<li id="nav">US Server</li>
<li id="nav">EU Server</li>
<li id="nav">SantosRP</li>
</ul>
</nav>
Santos Fire Department in original position, navbar centered :)
.navbar {
background-color: #333;
}
.nav {
display: table;
margin: 0 auto;
}
ul.nav li {
display: inline-block;
}
ul.nav li a {
display: block;
color: white;
padding: 14px 16px;
text-decoration: none;
text-align: center;
}
.navlogo a {
color: white;
font-size: 150%;
margin: 8px;
position:absolute;
}
<nav class="navbar">
<span class="navlogo"><a>Santos Fire Department</a></span>
<ul class="nav">
<li id="nav1">TeamSpeak (Temp Server)
</li>
<li id="nav2">US Server
</li>
<li id="nav3">EU Server
</li>
<li id="nav4">SantosRP
</li>
</ul>
</nav>
Update
position:absolute; on .navlogo a class, might not be the best way but gives you the results desired.
First of all you should eliminate multiple ids like id="nav" this is important for styling.
Now the parent needs to be text-align: center; and for your #logo you can float: left; like now. Otherwise, never avoid flexbox, but remember the browser support http://pleeease.io/play/

Can anyone tell me is there any method of giving seprate styles (li, ul, a) in a same CSS

I want to change (ul, li, a) of my navigation. But it also apply to my social media links. Can anyone tell me how can I give separate style to both of them.
Here are Social media links:-
<ul>
<li> <img src="GRProvider/Facebook.jpg"> </li>
<li> <img src="GRProvider/LinkedIn.jpg"> </li>
<li> <img src="GRProvider/Google.jpg"> </li>
<li> <img src="GRProvider/Twitter.jpg"> </li>
</ul>
Here are Navigation links:-
<div class="navigation">
<ul>
<li> HOME </li>
<li> ABOUT US </li>
<li> CAREER </li>
<li> CONTACT US </li>
</ul>
I'm applying this CSS which is working on both but I want give them separate styles.
ul {
float: left;
width: 100%;
}
li {
float: left;
list-style: outside none none;
text-decoration: none;
width: 25%;
}
a {
color: black;
float: left;
text-align: center;
text-decoration: none;
width: 100%;
border: 1px solid #0e8393;
}
</div>
Just do this for the navigation links only:
.navigation > ul {
float: left;
width: 100%;
}
.navigation > ul li {
float: left;
list-style: outside none none;
text-decoration: none;
width: 25%;
}
.navigation > ul li a {
color: black;
float: left;
text-align: center;
text-decoration: none;
width: 100%;
border: 1px solid #ccc;
}
.navigation ul {
float: left;
width: 100%;
}
.navigation li {
float: left;
list-style: outside none none;
text-decoration: none;
width: 25%;
}
.navigation a {
color: black;
float: left;
text-align: center;
text-decoration: none;
width: 100%;
border: 1px solid #0e8393;
}

Why is my text not in middle of the blocks on nav bar? What am I doing wrong?

I want the text in the center but they all seem to be sticking on top of the nav bar.
see my image attached. Please tell me what I am doing wrong.
edit:I am unable to attach image as I don't have 10 reputations.
body {
background-color: white;
}
ul {
list-style-type: none;
}
li {
float: left;
}
a:link,
a:visited {
display: block;
padding: 0;
width: 200px;
height: 30px;
background-color: blue;
text-decoration: none;
font-family: arial;
color: white;
font-weight: bold;
text-align: center;
}
a:hover,
a:active {
background-color: green;
}
<body>
<ul>
<li> Home
</li>
<li> Products
</li>
<li> Services
</li>
<li> Contact us
</li>
<li> About us
</li>
<li> Blog
</li>
</ul>
</body>
use line-height same has height to align text of 'a' vertically to center.
set the style on a or a:link, a:visited
a {
line-height: 30px;
}
jsfiddle demo
Take padding from top padding-top:12px of a:link, a:visited in your css
and it's done
Because of that you gave padding:0 the text is sticking to the top.
so i changed two thing and it works for me.
padding:5px 0px;
height:20px;//To make your `li height 30px`
Fiddle: Text at center
I think this is what you want...Just add a padding-top to the anchor tag.
body {
background-color: white;
}
ul {
list-style-type: none;
}
li {
float: left;
}
a:link,
a:visited {
display: block;
width: 200px;
height: 30px;
background-color: blue;
text-decoration: none;
font-family: arial;
color: white;
font-weight: bold;
text-align: center;
padding-top:12px;
}
a:hover,
a:active {
background-color: green;
}
<body>
<ul>
<li> Home
</li>
<li> Products
</li>
<li> Services
</li>
<li> Contact us
</li>
<li> About us
</li>
<li> Blog
</li>
</ul>
</body>
add line height to a tag
<style>
body {
background-color: white;
}
ul {
list-style-type: none;
}
li {
float: left;
}
a:link,
a:visited {
display: block;
padding: 0;
width: 200px;
height: 30px;
line-height: 30px;
background-color: blue;
text-decoration: none;
font-family: arial;
color: white;
font-weight: bold;
text-align: center;
}
a:hover,
a:active {
background-color: green;
}
</style>
Add padding-top:10px; in a:link, a:visited
Here is working fiddle :Demo

Navbar with css with different background colours

I have the following navbar :
nav ul {
display: block;
list-style-type: none;
height: 52px;
}
nav ul li {
display: inline-block;
width: 90px;
line-height: 52px;
text-align: center;
font-size: 15px;
}
nav ul li a {
display: block;
text-decoration: none;
color: #000;
}
nav ul li a:hover {
transition: 0.4s ease;
background-color: pink;
}
<nav>
<ul>
<li>One
</li>
<li>
Two
</li>
<li>
Three
</li>
<li>
Four
</li>
<li>
Five
</li>
<li>
Six
</li>
<li>
Seven
</li>
</ul>
</nav>
I want the background color of rainbow. I mean tab 1 will have violet, tab 2 will have indigo... and so on. I do this by adding class to each li. I can also do this by adding id to each li. Isn't there a faster way? CSS only!
You can use nth-of-type() selector
nav ul {
display: block;
list-style-type: none;
height: 52px;
}
nav li {
display: inline-block;
width: 90px;
line-height: 52px;
text-align: center;
font-size: 15px;
}
nav a {
display: block;
text-decoration: none;
color: #000;
}
li:hover {
transition: 0.4s ease;
}
li:nth-of-type(1):hover {
background-color: violet;
}
li:nth-of-type(2):hover {
background-color: indigo;
}
li:nth-of-type(3):hover {
background-color: blue;
}
li:nth-of-type(4):hover {
background-color: green;
}
li:nth-of-type(5):hover {
background-color: yellow;
}
li:nth-of-type(6):hover {
background-color: orange;
}
li:nth-of-type(7):hover {
background-color: red;
}
<nav>
<ul>
<li>One
</li>
<li>
Two
</li>
<li>
Three
</li>
<li>
Four
</li>
<li>
Five
</li>
<li>
Six
</li>
<li>
Seven
</li>
</ul>
</nav>
BONUS
Just a suggestion, don't use long selectors like nav ul li a. You know that you have one navbar element, in that only one a tag. So just cut it short and use nav a and so forth ...
Yes you can use:
nav > ul > li:nth-child(1){
background: yellow;
}
nav > ul > li:nth-child(2){
background: red;
}
//etc...