How to change CSS style of nested list items? - html

I have a style for styling <a> elements in list items in a #navigation container. This is working fine.
#navigation li a {
text-decoration:none;
background:#bfe5ff;
color:#045e9f;
width:125px;
height:35px;
padding-top:11px;
display:block;
float:left;
margin-left:2px;
text-align:center;
font-size:18px;
font-weight:bold;
}
Now in some <li>s I am inserting <div>s. In these I am again using a list again, but it should be different in style or have no style.
When I put in <li>s, their style matches the outer <li> elements, but it should not.
I am trying to use this:
#newnavigation li a {
font-size:12px;
margin-left:20px;
}
but it's not working - it applies the "outer" styles.
This is my markup:
<ul id="navigation">
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li class="browse">
Browse
<div id="browsecontainer">
<h3>Browse By Category</h3>
<li></li>
</div>
</li>
</ul>

It will continue to apply the outer styles - that's the "C" in CSS, cascading. Your new styles are being picked up correctly, but if I am reading the question right you are trying to eliminate the other "inherited" styles like the background colour?
If you want the outer styles to not be applied, then you either need to be using an element that doesn't match the outer pattern (i.e. not an li, not practical here), or to be overriding the styles you don't want applied. If you really only want these styles applied to the outer set of li elements, then consider as an alternative using a CSS class on the outer li elements and applying the formatting you don't want inherited to that class directly.

Your css is targetting the #id newnavigation but your ul #id is navigation
Try the following:
<ul id="navigation">
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li class="browse">
Browse
<div id="browsecontainer">
<h3>Browse By Category</h3>
<ul id="newnavigation">
<li>First category</li>
</ul>
</div>
</li>
</ul>

To select the inner-items, just nest them:
/** Matches outer *AND* inner LIs */
#navigation li {
}
/** Matches inner LIs only (li within li within #navigation) */
#navigation li li {
}
Or, to match the anchors:
#navigation li a {}
#navigation li li a {}
In the inner styles, you will start with a styleset inherited from the outer styles, so you might want to 'undo' some settings by overriding them to fit your needs.

Note that your markup is invalid. To insert new items you should also insert new lists, i.e.:
<ul id="newnavigation>
<li>
<div>
<ul>
<li></li>
</ul>
</div>
</li>
</ul>
It's always a good thing to validate your markup when you have problems with your style, Javascript, etc.
Having said that, to match only inner LIs, the CSS rule you need is:
#newnavigation li ul li{
// stuff here
}

I'm guessing it's something like this?
<ul id="navigation">
<li>link</li>
<li><ul id="newnavigation"><li>link</li></ul></li>
</ul>
I copy and pasted your styles and it's working fine. What is it exactly that is not working?
Update:
My guess wasn't quite right. In the code you show there is no id="newnavigation" to match the #newnavigation css selector.

You can also use a child selector like : #navigation > li
So only the outer li is styled.
Note that IE6 and below does no support child selectors.

Related

Calling li's in css

i am working on a site with this fieldset with ul. It is a joomla template where I am logged in as a publisher. This is the menu where you are able to change your articles.
What I would like to happen is that the first a and the last two a's are not visible. (like with a display:none). But I don't know how to call the lines in css. I know :nth-child() exists but It did not work the way I tried it.
<fieldset>
<ul class="nav nav-tabs">
::before
<li class>
Content
</li>
<li class="active">
Publishing
</li>
<li class>
Language
</li>
<li class>
Metadata
</li>
::after
</ul>
Is there anyone who knows how to call them in css? I am sorry if this question is not really expert level. Thanks
You can do it with simple CSS
.nav li:first-child,
.nav li:nth-child(3),
.nav li:nth-child(4) {
display: none;
}
But this is not a clean way. The edit form should be redesigned by an override. If you haven't the privilege to do that, ask the template developer.
Hide all and show the active one.
.nav-tabs li {display: none;}
.nav-tabs li.active {display: list-item} /* or block, if LIs are floated or you want them block */
Put the same class to the li's you want to hide and call them on your css.
Example:
<li class="hide-me"></li>
li.hide-me { display: none;}
BTW if you plan on changing the display value later on to show them separately id suggest you to use an id instead of a class.

How can I add subtext in list?

http://fiddle.jshell.net/stealthpancakes/jvrqhxdk/5/
I tried to put text in div, but everything after Subtext_1 is displayed in block.
Upper_text_1
Subtext_1
Upper_text_2 Upper_text_3
<nav>
<ul>
<li><div>Upper_text_1</div><div>Subtext_1</div></li>
<li>Upper_text_2</li>
<li>Upper_text_3</li>
</ul>
</nav>
I want something like this:
http://i.imgur.com/rAe2i6H.png
That is because div is a block level tag, you can try sub instead
<nav>
<ul>
<li>Upper_text_1<br /><span>Subtext_1</span></li>
<li>Upper_text_2<br /><span>Subtext_2</span></li>
<li>Upper_text_3<br /><span>Subtext_3</span></li>
</ul>
</nav>
Try this :
<nav>
<ul>
<li><div>Upper_text_1<span>Subtext_1</span></div></li>
<li>Upper_text_2</li>
<li>Upper_text_3</li>
</ul>
</nav>
Div is a block level element. You want an inline element for this case. Try <em> tag (emphasis) or the more generic <span> tag instead. There is a lot of different semantic elements for different purposes - you should find one that fits your content type and use that.
See this page for a full list:
https://developer.mozilla.org/en/docs/Web/Guide/HTML/HTML5/HTML5_element_list#Text-level_semantics
I believe this is what you need, correct me if am wrong or mistaken.
<nav>
<ul>
<li><p>Upper_text_1<br/><sub>Subtext_1</sub></p></li>
<li><p>Upper_text_2</p></li>
<li><p>Upper_text_3</p></li>
</ul>
</nav>
Also add the below CSS code:
nav ul li{
margin: 5px;
display:inline;
}
li p
{
display:inline;
float:left;
margin: 10px;
}
span{
font-size:0.7em;
}
Check the fiddle: http://jsfiddle.net/fu66c6b4/
Hope this helps.
<nav>
<ul>
<li>Upper_text_1</li>
<sub>Subtext_1</sub>
<li>Upper_text_2</li>
<sub>Subtext_2</sub>
<li>Upper_text_3</li>
<sub>Subtext_3</sub>
</ul>
</nav>

Seemly redundant CSS selectors

I'm dissecting Wordpress's default theme TwentyThirteen in attempts to learn HTML&CSS and more importantly, what I believe to be, industry standards for HTML&CSS.
I ran into a part in the CSS that I believe to be redundant but I would like to some insight on (probably) why the Wordpress team used these 2 CSS selectors together.
ul.nav-menu,
div.nav-menu > ul {
First Selector
ul.nav-menu
This first selector relates only to ul elements with a class named nav-menu. For example:
<ul class="nav-menu">
<li></li>
<li></li>
</ul>
Here it relates to the ul element because it is simply a ul with a class of nav-menu.
Second Selector
div.nav-menu > ul
This second selector relates only to ul elements that are direct children (directly below) div elements with a class named nav-menu. For example:
<div class="nav-menu">
<ul>
<li></li>
<li></li>
</ul>
</div>
Here it relates to the ul within the div because it is a ul directly below the div with a class of nav-menu.

Why my a element tag does not effect the links? Right syntax?

Why my a element tag does not effect the links? Right syntax? I have navigation links and for some reason, the css properties does not effect my links in any way, any reason why? I also want the links to effect only inside the id #nav, no other a tags are to be effected as what I'm trying to do.
CSS CODE:
a#nav:link, a#nav:visited, a#nav:active{color:#FFB405;text-decoration:none;font-weight:bold;} /*doesnt work... why?*/
a#nav:hover{background-color:#FFB405;color:#FFFFFF;text-decoration:none;font-weight:bold;}
HTML CODE
<!--NAVIGATION-->
<ul id="nav">
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Projects</li>
<li>Contact</li>
<li>Class List</li> <!--change URL later-->
</ul>
a#nav is incorrect. Try #nav a. This syntax targets an a that is a descendant of #nav. Your syntax targets an a with id="nav"
Your CSS selectors are wrong. You have
a#nav:link, ...
but it should be
#nav a:link, ...
Try change css to this:
#nav a:link, #nav a:visited, #nav a:active{color:#FFB405;text-decoration:none;font-weight:bold;} /*doesnt work... why?*/
#nav a:hover{background-color:#FFB405;color:#FFFFFF;text-decoration:none;font-weight:bold;}
http://jsfiddle.net/WMLx7/
You have set a#nav and the nav id is set to the parent ul.
Please try ul#nav li a instead.
http://jsfiddle.net/Ek2DT/
Change this
a#nav:link to #nav a:link
When you use this: a#nav it means <a id="nav... but you are having something like:
<ul id="nav">
<li>Home</li>
So use:
#nav a { // and add its properties like link, hover, active!
The main problem you have is that you are using a#nav when it should be #nav a.
nav a references the tag with the id 'nav' and selects all the tags.

a:hover does not change my anchor's list properties

Consider my html as follows:
<ul id="menu">
<li class="highlighted" id="first_item">Home</li>
<li class="non_selected_tabs">Join</li>
<li class="non_selected_tabs">Fixtures</li>
<li class="non_selected_tabs">Our Club</li>
<li class="non_selected_tabs">History</li>
<li id="hover" class="non_selected_tabs">Club Gear</li>
</li>
</ul>
My lists are styled as tabs, and I have my anchors as their parents so that when a user hovers over a tab it becomes selectable
My issue is that I was hoping to use a:hover, or the other anchor properties to change the background colour of my list item...is this possible using CSS?
I can't get it to work so I'm thinking I may have to use some JavsScript?
Wrapping the <li>'s in <a> is improper HTML and may not render properly in all browsers. A better solution would be to set the display property of the anchor to display:inline-block. Then you will be able to set the width and height of the anchor to the width and height of the li's. This way you can also use the hover property of the anchors.
<ul id="menu">
<li class="highlighted" id="first_item">Home</li>
<li class="non_selected_tabs">Join</li>
<li class="non_selected_tabs">Fixtures</li>
<li class="non_selected_tabs">Our Club</li>
<li class="non_selected_tabs">History</li>
<li id="hover" class="non_selected_tabs">Club Gear</li>
</ul>
#menu li a
{
display: inline-block;
width: 100%;
height: 100%;
}
#menu li a:hover
{
background-color:red;
}
The direct children of a ul element should only ever be list items elements, not an a.
You could either use :hover on the li element it's self, this works in browsers that aren't IE, maybe even IE8 and up..
Or you could style the a to take up the entire space area of the li, and style the li to be inline, so not to display as a typical list.
You can use :hover on other elements, not just anchors.