I know this is a silly question but for some reason I'm always having a problem with changing the background colour of a selected navigation item, I looked this up so many times and I tried doing the selected classes for a button but for some reason it doesn't work for me, can someone point out what am I doing wrong?
html:
<div id="nav">
<ul>
<li><a class="selected" href="?page=home">Home</a></li>
<li>Gallery</li>
<li>About</li>
<li>Contact</li>
<li>Shop</li>
</ul>
</div>
css:
#nav {
list-style: none;
width: 100%;
text-align: center;
background-color: #000;
height:52px;
background-color: #000; opacity: 0.7; filter: alpha(opacity=50);
}
#nav ul {
list-style-type: none;
margin: 0px;
padding: 10px;
padding-left: 650px;
}
#nav li {
margin: 0px;
display: inline-block;
}
#nav li a {
padding: 10px;
text-decoration: none;
font-family: 'Quicksand';
font-size: 25px;
color: #fff;
background-color: #000;
}
#nav li a:hover {
color: black;
background-color: #999;
background-color: #666;
color: white;
}
li.selected a { background-color: blue;
}
You've two issues here.
You are using li.selected a but you are assigning your class to a tag so the selector should be li a.selected
Specificity. You have #nav in all the selectors, since ID selectors are more specific, you need to add it to your .active selector as well. So it should be #nav li a.selected
Demo
Suggestion :
I would recommend you to use class instead of id. Keep ID's for JavaScript selectors, as it can access your DOM faster, but for CSS, stick to classes as much as you can, else you will end up with long specific selectors and even !important.
So you should have something like <div id="nav" class="nav"> and use .nav in CSS instead of #nav
Related
I am coding a website for a class that I am taking and I am having a strange issue. I want the menu items in the top to be grayed out when they are the current page. This is working with all of them except for one. Here is the HTML and CSS for this section along with screenshots of what is happening.
HTML for the menu:
<nav>
<ul>
<li>Home</li>
<li>About Me</li>
<li>Hobbies</li>
<li>Extra Curriculars</li>
<li>Projects</li>
<li class="selected">Self Reflections</li>
</ul>
</nav>
CSS for the menu:
nav ul {
list-style-type: none;
background-color: DarkRed;
border: 4px solid Black;
border-radius: 10px;
font-family: sans-serif;
font-weight: bold;
padding: 16px;
}
nav ul li {
display: inline;
border-right: 2px solid Black;
padding-right: 8px;
padding-left: 8px;
}
nav ul li:last-child {
border-right: none;
color: White;
}
nav ul li a {
color: White;
}
nav ul li a {
text-decoration: none;
}
nav li.selected {
color: Gray;
}
nav li a:hover {
text-decoration: underline;
}
Screenshot of what I want:
Screenshot of what is happening on the one page:
As you can see, the Self Reflections page is the current page. The problem is that it is not turning gray. The link is not active, I just want it to be gray like the rest of the pages. I am still fairly new to HTML and CSS so any help is greatly appreciated. Thank you!
It is because of this line
nav ul li:last-child {
border-right: none;
color: White;
}
Remove color: White from here
I'm trying to turn off the hover for the current page in a navigation menu.
div.nav {
width: 100%;
padding: 6px;
height: 40px;
}
.nav li {
color: #FFFFFF;
display: inline;
list-style-type: none;
text-align: center;
text-transform: uppercase;
padding: 20px;
margin: 0px;
height: 40px;
}
li.current {
background-color: #424242
}
li.current:hover {
background-color: inherit;
}
.nav li:hover {
background-color: #737373;
<div class="nav">
<ul>
<li class="current">Home</li>
<li>About
</li>
<li>Contact
</li>
<li>Gallery
</li>
</ul>
</div>
Here is the jsfiddle:
https://jsfiddle.net/swordams/jk6z5aqj/
I want the li for home to stay dark and not change on hover. I've tried setting the hover background color to "inherit", but that doesn't work.
Thanks!
You can use the CSS :not() pseudo-class:
.nav li:hover:not(.current) {
background-color: #737373;
}
jsFiddle example
You can use the solution by j08691 but ultimately, the problem with your css is that .nav li:hover is more specific than li.current:hover. Tacking a .nav will do the trick.
.nav li.current:hover {
background-color: inherit;
}
just make the active/current li background color important
li.current {
background-color: #424242 !important;
}
This is probably a very simple question for many of you, but I can't seem to figure it out and I've been at it for about an hour now.
I'm trying to create a navigation bar with buttons. I'd like the buttons to have the following features, but I can't figure out how to implement them:
Clicking anywhere on the button will activate the button redirection.
The font is not underlined or recolored like a regular text hyperlink.
The font is much more "fat" (I don't know how else to describe it.
In the picture, I put small red cross to signal that I'd like to be able to click in those locations for it to work.
I've included a jsfiddle with what I've been able to accomplish thusfar.
My html:
<ul>
<li><a id="nav" href="#">Page1</a></li>
<li><a id="nav" href="#">Page2</a></li>
<li><a id="nav" href="#">Page3</a></li>
<li><a id="nav" href="#">Page4</a></li>
</ul>
My CSS:
body {
margin: 0px
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
ul li {
float:left;
overflow: auto;
padding: 6px 12px 6px 12px;
background-color: grey;
border: 1px solid #000;
text-decoration: none;
margin: 0;
border: 0;
margin-right: 3px;
font-weight: bolder;
color: yellow;
}
ul li:hover{
background-color: black;
}
}
replace ul li { with ul li a{ and ul li:hover{ with ul li a:hover{
http://jsfiddle.net/vxre4x2k/2/
Simply replace ul li with ul li a and ul li:hover with ul li a:hover.
To make the font thicker or thinner change your font-weight
You need to make you anchors work like block elements. The LI's will contain the anchor.
ul li a {
display: block;
text-decoration: none;
padding: 6px 12px 6px 12px;
}
ul li a:hover {
background-color: black;
}
You can use display: inline-block to a(anchor element) and give it some padding for your first request(Clicking anywhere on the button will activate the button redirection.). Also you can remove underline with text-decoration: none; and reduce "fat" from font with font-weight: 100;:
body {
margin: 0px
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
ul li {
float: left;
padding: 6px 12px 6px 12px;
background-color: grey;
border: 1px solid #000;
text-decoration: none;
margin: 0;
border: 0;
margin-right: 3px;
font-weight: bolder;
color: yellow;
}
ul li:hover {
background-color: black;
}
ul li {
padding: 0;
}
ul li a {
display: inline-block;
padding: 5px;
text-decoration: none;
color: white;
font-weight: 100;
}
<ul>
<li><a id="nav" href="#">Page1</a>
</li>
<li><a id="nav" href="#">Page2</a>
</li>
<li><a id="nav" href="#">Page3</a>
</li>
<li><a id="nav" href="#">Page4</a>
</li>
</ul>
References:
font-weight
text-decoration
Clicking anywhere on the button will activate the button redirection.
to do this, wrap your list elements with the anchor elements instead.
ex.
<a href='#'><li>foo</li></a>
The font is not underlined or recolored like a regular text hyperlink.
for the links you gave them each an id of "nav". This isn't recommended as its commonplace to set id's to individual elements. I would look up the difference between CSS id's and classes. In short, classes are used for multiple elements that share the same styles. In this case you would use a class so it should be:
<a class="nav" href="#"><li>Page1</li></a>
The styles for the class to remove the underline would be:
.nav{
text-decoration:none;
color: black;
}
The font is much more "fat" (I don't know how else to describe it.
are you saying you want it to be fat?
if so then try this:
<a class="nav" href="#"><strong><li>Page1</li></strong></a>
or you could also set the css style font-weight property
the font-weight property can be used to increase or decrease the "fattness" of the font.
if that doesn't do it for you i'd suggest using a different font.
Doing some basic html/css. I was making a rudimentary navbar with floated links. After getting it working I was stuck with this problem, and so far have not come to a solution.
My links have these dots in them. As the picture shows.
My code is simple:
HTML
<div id="nav-wrapper">
<div id="navbar">
<ul id="nav">
<li>Home</li>
<li>About</li>
<li>Blog</li>
<li>Contact Us</li>
</ul>
</div>
</div>
and the CSS
#nav-wrapper {
background-color: black;
height: 40px;
width: 100%;
border-top: 2px solid gray;
border-radius: 0 0 5px 5px;
}
#navbar {
}
ul#nav li {
float: left;
font-family: sans-serif;
font-size: 18px;
text-decoration: none;
}
ul#nav * a {
width: 25px;
margin: 0 10px 10px 0;
}
My question is what is causing these dots? And why don't they appear if I add more words/links to the list or I erase all but one item? It's odd. I must be missing something extremely embarrassing because this just seems odd.
You want to use the code - list-style: none;
so your code will look like
ul#nav li {
float: left;
font-family: sans-serif;
font-size: 18px;
text-decoration: none;
list-style: none;
}
Add this style:
list-style-type: none;
To this selector:
ul#nav li
Modify your declaration for ul#nav li to include this property
list-style:none;
http://jsfiddle.net/bcDDk/
I know this question has been asked so many times before. But I just can't find the right trick for my code. I want a different color for my active list item in the navigation bar. Obviously. Silly little thing. I know. But please try to help.
Here's my HTML code:
<div id="container">
<ul id="nav">
<li class="active">Home</li>
<li>Teaching Assistants</li>
<li>Course Info</li>
<li>Time Table</li>
</ul>
</div>
and Here's the CSS file:
#container {
position: relative;
top: -2em;
z-index: 2;
width: 1200px;
margin: auto auto;
}
#nav {
position: relative;
float: left;
margin-left: 400px;
}
#nav li {
list-style: none;
float: left;
border-right: 1px solid #afc4cc;
}
#nav li a {
position: relative;
z-index: 2;
float: left;
padding: 5px 45px;
font-size: 15px;
font-weight: bold;
font-family: helvetica, arial, sans-serif;
text-decoration: none;
color: #39aea8;
}
ul, li {
margin: 0;
padding: 0;
}
ul#nav li a:link,ul#nav li a:visited {
color: #39aea8;
text-decoration: none;
}
ul#nav li a:hover,ul#nav li a:active {
color: #f4ba51;
text-decoration: none;
}
There's something wrong with your CSS code. Just replace this:
ul#nav li a:hover,ul#nav li a:active{
}
with this:
ul#nav li a:hover,ul#nav li.active a{
// here styling
}
and you are good to go. You just made a mistake while calling the active class in CSS.
ul#nav li.active a { color: #f4ba51 ; }