I'm trying to understand CSS mechanism but tutorials so far haven't been a great source. They only scratch the surface.
I need to understand the fundamental differences between using #navlist li #current and #navlist li .current.
The names are not generic in order to be a very practical example.
What I think the different is:
#navlist li #current
if applied to an li element inside a parent element #navlist will bypass any inherited format to display #navlist li #current format.
On the other hand:
#navlist li .current
will apply its format but also inherit from other format.
In this example:
#navlist li a:hover
{
color: #FFFFFF;
background: #3364BB;
border-color: #0F3974;
}
#navlist li .current
{
color: #000;
background: #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
<ul id="navlist">
<li>Home</li>
<li>Profile</li>
<li>Destinations</li>
<li>Discuss</li>
</ul>
The tab will be white with a black font but hover will be applied.
With this other example:
#navlist li a:hover
{
color: #FFFFFF;
background: #3364BB;
border-color: #0F3974;
}
#navlist li #current
{
color: #000;
background: #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
<ul id="navlist">
<li>Home</li>
<li>Profile</li>
<li>Destinations</li>
<li>Discuss</li>
</ul>
#current is applied and nothing else, leaving the tab white even if the mouse hover over it.
Is that right?
Is that right?
Yup. This is because a:hover is more specific than .current, but less than #current. So your hover styles will override your class styles, but your ID styles are untouched.
a:hover is more specific than .current because it combines a type selector and a pseudo-class selector. That beats out a class selector (although :hover and .current are equally specific), because of the a.
#current is more specific than a:hover because IDs are always the most specific, even if you combine a multitude of non-IDs in the hover style rule's selector.
Yes. (And seems specificity is the reason as BoltClock said)
This page will tell you how the browser read your css selector: http://www.css-101.org/descendant-selector/go_fetch_yourself.php
remember: id is unique, can be used for 1 element only; but class can be used to more than 1 element and tag
note: actually both rule select the same element and applied. If you write more in #navlist li a:hover, those non-overlapping prosperities will appear when mouse over #current (so call 'cascading')
more: Some keyword/concept you need to know: inheritance/cascading, css selector, css specificity, pseudo class
p.s. try jsbin / jsfiddle / cssdesk for your css test - learn from practice :D
I think you've got some syntax errors in your question. This style:
#navlist li .current
will target a child element of your LI, like this:
<ul id="navlist">
<li>
Foo
</li>
</ul>
I'm assuming you intend to put a class or ID of "current" on the LI itself. If so, your rule should look something like:
#navlist li.current // no space in between 'li' and '.current'
Also remember that ID's are "weighted" more than Classes in CSS. So if you have two equally structured rules, but one uses and ID and the other uses a class, then the ID rule will trump the class rule. They BOTH will appy, but the ID rule will apply LAST. This is important to note if you are doing font-sizing and such, where rules are cumulative instead of just overriding each other.
Related
So, I'm having this issue with trying to make code apply to items within a sublist, which I have named "Horizontal"
<nav class="horizontal" id="horizontal">
<ul>
<li>Home</li>
<li>Menu</li>
<li>Locations</li>
<li>Catering</li>
<li>About Us</li>
</ul>
This should in turn translate over to my CSS file as so
#horizontal > li {
background-color: white;
font-size: 16px;
height: 50px;
line-height: 50px;
width: 180px;
display: inline-block;
horizontal-align: left;
I know the file is linked to my HTML doc as it has worked with other style rules.. I'd like to know why. Any help would be appreciated.
Also, side note: for this snippet
display: inline-block;
horizontal-align: left;
}
The effect I'm trying to go for here is to display the item as a block and float it to the left, I'd like to know if I'm on the right track with that code.
Finally, I seem to be having an issue with the font-color and/or text-color properties, as neither seem to work for my lists as such:
#horizontal > ul > li > a:hover {
background-color: rgb(255,101,101);
text-color: black;
The background color changes, but the text remains the same.
#horizontal > li
Where > li look for and LI element with a Parent element that has an ID horizontal
The > (greater than) operator looks for a direct descendant of a parent element. In your example, #horizontal is the parent element and the direct descendant is the UL.
Therefore, the effect you expect on the LI would not be applied.
You can use
#horizontal li, or #horizontal ul li, or #horizontal > ul > li which has a higher specificity weight.
I am trying to highlight the active <a> however, my CSS is being overwritten.
#portfolio-filter li a {
color: black;
text-decoration: none;
padding:3px 8px 3px 8px;
background:#8d8d8d;
}
#portfolio-filter li:hover, a.filter.active {
background: white;
}
<ul id="portfolio-filter">
<li>
All
</li>
<li> etc... </li>
</ul>
The #portfolio-filter li a style is overwritting the #portfolio-filter li:hover, a.filter.active style and not sure what I need to do to fix this.
Link: http://velnikolic.com/ramova3/?page_id=25
The problem is that #portfolio-filter li a is more specific than a.filter.active. Since the background is on that a element, not the li element, your a background won't change even if #portfolio-filter li:hover is more specific.
To fix it, use something like #portfolio-filter li a.filter.active, which is more specific and will correctly take precedence.
As a general rule of thumb, when working with active classes, always use a similar selector as the original (non-active) definition. Otherwise, you may run into specificity issues like this one.
Here's a useful specificity calculator when in doubt.
The comma makes your "second" style two separate styles, remove the command and you should be fine.
ref: http://webdesign.about.com/od/cssselectors/f/comma-in-css-selectors.htm
Here was my solution.
portfolio-filter li a:hover, #portfolio-filter > li a.active{ background: white; }
Sorry with title if it is not looks bad, I don't know how can I write the title for my issue.
So I have a menu bar with some menu-items and submenu-items as:
<div id="mainmenu">
<ul id="menu">
<li><a class="current" href="">Menu1</a></li>
<li>Menu2
<ul>
<li>Submenu1</li>
<li>Submenu2</li>
</ul>
</li>
<li>Menu3</li>
<li>Menu4</li>
</ul>
</div>
Here is the fiddle of what I have done so far.
What I want is to keep the menu-item also in :hover state if its submenu-item is being hover, such as If my mouse is on submenu or submenu2 the Menu2 should also be darkened. How can I do this with CSS?
I hope I am clear with my question.
EDIT:
Wooo thanks a lot every-one.
got it with: #menu li:hover
#menu li:hover,#menu a:hover,#menu > li a.current{
}
In last line of your CSS, add #menu li:hover to target selectors
Updated example here: http://jsfiddle.net/7UaNn/
Add this #menu li:hover > a in the following css
#menu a:hover, #menu > li a.current {
// your Style
}
So it should look like this:
#menu a:hover, #menu > li a.current, #menu li:hover > a {
// your Style
}
See Demo: http://jsfiddle.net/akhurshid/bk2HA/7/
At the moment all you hoverstyles for the listitem are applied to the a- Element.
However, you need to apply them to the li-Element to keep the hover state active.
Sometimes that may become tricky, but in your case it's pretty easy, just add:
#menu > li:hover{
background:#474747;
}
to your styles.
See your modified fiddle
Along with "a" also add hover for "li" :
#menu > li:hover{
background:#474747;
}
You may achieve this by hooking up to the :hover state of the parent li element instead of the anchor element - which is a children of the list-item that is actually hovered - like so:
li a:hover,
li:hover > a {
color: #fff;
}
You might need to use :nth-child() to get around every sublink being in hover state. Did not test this.
/edit: updated the selector, now using > a to only select the direct child anchor element of the hovered list element, no need for :nth-child or the like.
Make the thing that is changed on hover the background of the LI rather than the background of the 'a'.
ul#menu li:hover { background:#000; }
I have a unordered list as so:
<ul id="vertical_menu">
<li>Home</li>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
Now, I need to add borders to the "list item" li elements only if it is a child of the ul with the id vertical_menu.
Any ideas on how to do this?
ul#vertical_menu > li
{
/* apply li styles here as needed */
border: 1px solid #000;
}
ul#vertical_menu describes a ul element with ID 'vertical_menu'. > li describes a direct child element of type li. Any styles you list here will apply directly to the li elements as you wanted.
If you require support for IE6, the child selector > will be an issue. One work around to this is to apply styling to single level li, and "reset" it on further nested li, such as:
ul#vertical_menu li
{
border: 1px solid #000;
}
ul#vertical_menu li li
{
border: none;
}
This will add border to the first level list items within the vertical_menu, and will remove the border from any further nested items. Again not necessary to consider with modern browsers - depends on requirements.
In HTML markup, element IDs should be unique, so realistically you also don't need to select by ul and can simply use #vertical_menu > li or #vertical_menu li. Including ul in the selector is very strict. To each his own..
This selector translates exactly to what you're asking:
ul#vertical_menu > li
If your markup is always going to have one level of li elements under that ul, and/or only the ID is only going to be assigned to ul and not some other type of element (which it should be, for site consistency), there are a number of other ways to select the same elements, for example:
ul#vertical_menu li /* Assumes only one level of <li> */
#vertical_menu > li /* Assumes only this <ul> has the ID */
#vertical_menu li /* Assumes both of the above */
These alternatives are useful if IE6 support is a requirement, as it doesn't support the > combinator, but they all depend one way or another on the structure of your HTML.
Try this:
ul#vertical_menu > li{
border:1px;
}
It's worth noting that using the > selector doesn't work in older versions of Internet Explorer (IE6, I believe), so if you're looking also to support that,
ul#vertical_menu li
will also work (but it will apply the border to every list item under #vertical_menu even if its a submenu.
just make
ul#vertical_menu li{
border:1px solid #CCC;
Remember to use ul as it will make the css specificity more clear and it will style only the li items which are under the ul.
hope it will work for you.
#vertical_menu > li { border: 1px solid black; }
Should work, but IE6 will need
#vertical_menu li { border: 1px solid black; }
Both answers are correct. While ul#vertical_menu li targets all lis from ul#vertical_menu, ul#vertical_menu > li targets only first line li descendents.
For example, using the last selector only the id=one li will be targeted:
<ul id="vertical_menu">
<li id="one"></li>
<another-element>
<li id="two"></li>
</another-element>
</ul>
I've got an ordered list like this
<ol class="tracklist">
<li>
LINK
<span>some text</span>
</li>
</ol>
I want to change the color of the list-numbers, link and span upon hovering on the list element.
Therefore I have this in css:
ol.tracklist li:hover {
background-color: #D21600;
color: #FFFFFF;
}
It only changes the list-numbers and span's color though.
What can I do about this (without using JS).
Anchor tags don't inherit attributes such as color when the href attribute is present.
You can use multiple selectors to apply the same style, separate them with a comma.
ol.tracklist li:hover, ol.tracklist li:hover a {
background-color: #D21600;
color: #FFFFFF;
}
It looks like you have some extra s's in you styles
Change ol.strackslist to ol.tracklist in your css and it works.
You also need to add this for the link to change color:
ol.tracklist li:hover a, ol.tracklist li a:hover {
color:#fff;
}
http://jsfiddle.net/jasongennaro/mje9t/1/
One can restore color inheritance on <a> also:
ol.tracklist li a {
color: inherit;
}
<a> behaves like any other element thereafter. Hovers on <li> will change its color as well.