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...
Related
I have two nav elements that I'm using one just below the header and another one where I have the nav element nested inside an aside element. I added the code for the HTML for the nav elements and added the code for the CSS for the nav elements at the bottom of the page. I also have a picture of the website that you guys can look at which is also at the bottom of the page. What I am trying to do is add a horizontal navigation bar for one nav element and a vertical navigation bar for another nav element, but the first nav element I created is overriding the CSS for my second nav element. I'm trying to figure out a way to separate the two so the second nav element has its own CSS code. I don't know if I can use a class attribute or id attribute for the second nav element so it's separate from the first nav element I created. I can really use some help in figuring out this issue.
Here is the HTML code for the first nav element:
<nav>
<ul>
<li>
<a class="current" href="index7.html">Home</a>
</li>
<li>
Product List
</li>
<li>
Personal
</li>
<li>
Decoration Ideas
<ul class="submenu">
<li>
Outdoor
</li>
<li>
Indoor
</li>
<li>
Table
</li>
<li>
Treats
</li>
</ul>
</li>
<li>
Join Email
</li>
</ul>
</nav>
<aside>
<nav id="nav_list">
<ul>
<li>
Props
</li>
<li>
Costumes
</li>
<li>
Special Effects
</li>
<li>
Masks
</li>
</ul>
</nav>
</aside>
Here is the CSS code for the first and second nav elements
/* nav styles */
nav {
position: absolute !important;
top: 0px !important;
left: 0px !important;
display: block !important;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
nav ul > li {
float: left;
}
nav a {
display: block;
width: 160px;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #000000;
color: white;
font-weight: bold;
}
.current {
color: orange
}
.submenu {
position: absolute;
display: none;
top: 3em;
}
.submenu li {
float: none;
}
.submenu a:hover {
background-color: gray;
}
nav ul li:hover > ul {
display: block;
}
/* second nav element */
aside {
position: absolute !important;
top: 100px !important;
left: 0px !important;
display: block !important;
}
#nav_list ul {
list-style-type: none;
<!-- margin-left: 1.25em; -->
margin-bottom; 1.5em;
}
#nav_list ul li {
width: 200px;
margin-bottom: .5em;
<!-- border: 2px solid blue; -->
}
#nav_list a:hover {
background-color: gray;
}
Website screenshot below:
Your CSS is targeting all nav elements because you asked it to. You have no specific nav or ul classes (or id's) assigned.
I would suggest you give each nav element different classes (or Id's) so you can then target each one separately in your CSS and apply your desired styling. For example you can give the horizontal nav a class="horizontal-nav" and the vertical nav a class="vertical-nav".
That way, one is separate from the other style-wise.
check out the !important stuff.
<style>
/* nav styles */
nav {
position: absolute !important;
top: 0px !important;
left: 0px !important;
display: block !important;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
nav ul > li {
float: left;
}
nav a {
display: block;
width: 160px;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #000000;
color: white;
font-weight: bold;
}
.current {
color: orange
}
.submenu {
position: absolute;
display: none;
top: 3em;
}
.submenu li {
float: none;
}
.submenu a:hover {
background-color: gray;
}
nav ul li:hover > ul {
display: block;
}
/* second nav element */
aside {
position: absolute !important;
top: 100px !important;
left: 0px !important;
display: block !important;
}
#nav_list ul {
list-style-type: none;
<!-- margin-left: 1.25em; -->
margin-bottom; 1.5em;
}
#nav_list ul li {
width: 200px;
margin-bottom: .5em;
<!-- border: 2px solid blue; -->
}
#nav_list a:hover {
background-color: gray;
}
</style>
<nav>
<ul>
<li>
<a class="current" href="index7.html">Home</a>
</li>
<li>
Product List
</li>
<li>
Personal
</li>
<li>
Decoration Ideas
<ul class="submenu">
<li>
Outdoor
</li>
<li>
Indoor
</li>
<li>
Table
</li>
<li>
Treats
</li>
</ul>
</li>
<li>
Join Email
</li>
</ul>
</nav>
<aside>
<nav id="nav_list">
<ul>
<li>
Props
</li>
<li>
Costumes
</li>
<li>
Special Effects
</li>
<li>
Masks
</li>
</ul>
</nav>
</aside>
i am learning html css, now i want to create nav bar as my task. so when am trying to create horizontal nav bar, my menus are not showing at the center of my nav bar div ? why its attached with top corner ? i want them at center from top and bottom here is my code correct my mistake
* {
margin: 0;
padding: 0;
}
nav {
background-color: red;
}
ul {
list-style: none;
height: 35px;
background-color: purple;
width: 50%;
}
nav ul li {
display: inline;
padding: 10px;
margin-top: 10px;
}
nav ul li a {
text-decoration: none;
color: black;
}
nav ul li:hover {
background-color: blue;
}
<nav>
<ul>
<li> Home</li>
<li> About Us</li>
<li> Contact Us</li>
<li> Privacy Policy</li>
</ul>
</nav>
Here is what you can do, you can use flex and tweak your code if needed.
body {
margin: 0;
padding: 0;
justify-content: center;
text-align: center;
display: flex;
}
nav {
background-color: red;
}
ul {
justify-content: center;
text-align: center;
list-style: none;
height: 35px;
background-color: purple;
width: 50%;
display: flex;
flex-direction: row;
}
ul li {
margin-top: 10px;
}
ul li a {
text-decoration: none;
color: black;
padding: 5px;
}
ul li:hover {
background-color: blue;
}
<ul>
<li> Home</li>
<li> About Us</li>
<li> Contact Us</li>
<li> Privacy Policy</li>
</ul>
I added display:inline-block to both li and ul and text-align:center to nav.
and that will make margin-top works.
* {
margin: 0;
padding: 0;
}
nav {
text-align: center
}
ul {
list-style: none;
height: 35px;
background-color: purple;
width: 50%;
display: inline-block
}
nav ul li {
display: inline-block;
padding: 10px;
}
nav ul li a {
text-decoration: none;
color: black;
}
nav ul li:hover {
background-color: blue;
}
<nav>
<ul>
<li> Home</li>
<li> About Us</li>
<li> Contact Us</li>
<li> Privacy Policy</li>
</ul>
</nav>
check here: https://jsfiddle.net/w3hn1L95/
You need to add a
test-align: center;
to your ul element in css.
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.
I am wanting to align a vertical sub-submenu that is horizontal to a submenu, like this:
I am able to achieve this, as the picture shows, but I have to make the position absolute. The problem with that is I would want the top part of each sub-submenu to line up with the top of the submenu it is attached too. For instance, the artist sub-submenu would be exactly the same as the one shown, but would have A to Z lined up with Artist.
In order to do that the way I am doing it now, I would have to create many different css sections, rather than being able to select multiple submenus with one section (for instance #sortsongmenu, #sortartistmenu { styling }. I would like to find a way to have the sub-submenus in the position shown without having to position each sub-submenu separately, but rather have a styling approach that could apply to all sub-submenus that have relative or some other positioning.
HTML code:
<-- CSS code-->
#topbar {
background-color: #222;
}
#topbar_wrapper {
width: 100%;
margin: 0 auto;
text-align: left;
}
#mainmenu {
list-style-type: none;
padding: 0px;
margin: 0px;
position: relative;
min-width: 200px;
}
#mainmenu li {
display: inline-block;
width: 200px;
}
#mainmenu li:hover {
background-color: #333;
}
#mainmenu li a{
color: #CCC;
display: block;
padding: 15px;
text-decoration: none;
}
#mainmenu li:hover > ul {
display: block;
}
#sortmenu {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-top: 0;
margin-left: -5px;
}
#sortmenu li {
display: block;
}
#sortmenu li a:hover {
color: #699;
}
#sortmenu li: hover ul {
display: inline-block;
}
#sortsongmenu, #sortartistmenu {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-left: 0px;
text-align: right;
left: 100%;
bottom: 65%;
width: 100px;
}
#sortsongmenu li, #sortartistmenu li{
display: inline;
}
#sortsongmenu li a:hover, #sortartistmenu li a:hover {
color: #DB7093;
}
<div id="topbar">
<div id="topbar_wrapper">
<ul id="mainmenu">
<li>Home
</li>
<li>
Search
</li>
<li>
Sort By ▼
<ul id="sortmenu">
<li><a href='#'>Song</a>
<ul id="sortsongmenu">
<li><a href='#'>A to Z</a>
</li>
<li>
<a href='#'>Z to A</a>
</li>
</ul>
</li>
<li>
<a href='#'>Artist</a>
<ul id="sortartistmenu">
<li><a href='#'>A to Z</a>
</li>
<li>
<a href='#'>Z to A</a>
</li>
</ul>
</li>
<li>
<a href='#'>Album</a>
</li>
<li>
<a href='#'>Genre</a>
</li>
<li>
<a href='#'>BPM</a>
</li>
<li>
<a href='#'>Release Date</a>
</li>
</ul>
</li>
<li>
Add Song
</li>
<li>
Contant Us
</li>
</ul>
</div>
</div>
Try:
Change
#sortmenu li {
display: block;
}
#sortsongmenu, #sortartistmenu {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-left: 0px;
text-align: right;
left: 100%;
bottom: 65%;
width: 100px;
}
to
#sortmenu > li {
display: block;
position: relative;
}
#sortsongmenu, #sortartistmenu {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-left: 0px;
text-align: right;
left: 100%;
top: 0;
width: 100px;
}
EDITED:
Change top to -5px, as your sub submenu have a border top of 5px. It will look better that way.
The problem is when you leave the <a> the font color will revert back to its non hover color.
I want the color of the "About" text to be white even the cursor leaves the "About" link.
I've been trying to put all color: on the text on my hover link but no joy.
Here's my fiddle
HTML
<nav>
<ul>
<li class="n1">
Home
</li>
<li class="n2">
About
<ul class="menu">
<li>
List 1
</li>
<li>
List 2
</li>
<li>
List 3
</li>
</ul>
</li>
<li class="n3">
Contact
</li>
</ul>
</nav>
CSS
nav {
width 100%;
}
nav ul {
list-type: none;
}
nav ul li {
maring: 0;
padding: 0;
}
nav ul li a {
display: block;
float: left;
padding: 10px 20px;
line-height: 1;
color: dodgerblue;
background: silver;
text-decoration: none;
text-transform: uppercase;
}
nav ul li a:hover {
color: white;
}
nav ul li.n2:hover > .menu {
display: block;
}
.menu {
position: absolute;
top: 36px;
left: 86px;
background: silver;
width: 93px;
display: none;
}
.menu li a {
color: white;
}
.menu li a:hover {
color: dimgray;
}
You need to add the hover to the li
nav ul li:hover a {
color:white;
}