Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Noob alert! - selecting a certain <a> tag in a list

Featured Replies

<div id="nav">
  <ul>
  	<li id="selected"><a href="#"><dfn title="nav1">button 1</dfn></a></li>
  	<li><a href="#"><dfn title="nav2">button 2</dfn></a></li>
  	<li><a href="#"><dfn title="nav3">button 3</dfn></a></li>
    <li><a href="#"><dfn title="nav4">button 4</dfn></a></li>
  </ul>
</div>

 

I want to change the text color of the <a> tag that is inside the "selected" <li> tag which is inside the "nav" <div> tag

 

the default color of the text of the <a> tag is

 

#nav li a{
    color: #EDDBAC;
}

I tried changing the "selected" color with

 

#selected li a{
    color: #394042;
}
and
#nav #selected li a{
    color: #394042;
}
 

but the color didn't change :(

 

can someone help me out here?

Take out li, just use #selected a

 

Your code looks for <li id="selected"> <li> <a....

 

You're already referencing the <li> item using #selected so you don't need to reference it again.

 

Hope I made sense.

What Lyndsey said.

Your code...

#nav #selected li a{
    color: #394042;
}

 

Is trying to select an <a> tag that's within a <li> tag that's within an element with the ID #selected that's within an element with the ID #nav.

The problem here is that the <li> tag IS the element with the ID #selected so in reality what your code actually says is this...

#nav li li a{
    color: #394042;
}

 

Remove the second li and you'll be fine - like this...

#nav #selected a{
    color: #394042;
}

 


Also, what is it that you're trying to achieve here? You may be able to just use pseudo selectors rather than having to define separate IDs.

What Alluziion said, after what Lyndsey said... :p

 

Just to round everything off...

 

Your code here:

#nav #selected li a{
    color: #394042;
}
Is indeed looking for another <li> inside <li id="selected"...> (as the others pointed out).

 

If you wanted to use the li syntax within your CSS for your own semantics, it would come before #selected and without a space:

#nav li#selected a{
    color: #394042;
}
Edit: Check my post count...2222 - must mean something?

Edited by andy9l

What those three said ^

 

I'd also like to point out some other things, not related to your question but your code.

 

Firstly, it is better practice to use html classes for styling with css. You can read a post a made about this with some more links here.

 

Secondly, I don't think this is the correct/semantic place to use a <dfn>. The definition tag is used to represent the 'defining instance of a term'.

 

The spec says:

 

If the dfn element is specified, nearest parent of the dfn element(paragraph, description list group, or section) must also contain the definitions for the defining term.

 

W3C spec

 

e.g.

<p>A <dfn id="definition">definition</dfn> gives the meaning of a word or phrase.</p>

 

Which gives you the ability to link back to definitions of words in your writing. It seems to me to be tag for more technical writing that you might need to refer to definitions of words or abbreviations.

Edited by heydanreeves

Firstly, it is better practice to use html classes for styling with css. You can read a post a made about this with some more links here

 

I don't necessarily agree with that statement.

 

Classes should be used to style elements that you may have several of on a page such as a small container with the same border/background/dimensions etc.

 

IDs should be used to style elements that are unique to the page and are not repeated elsewhere.

 

These are their semantic uses.

But if you follow the semantics correctly then you'll often not need to use IDs anyway such as using <nav> instead of <div id="nav">. I mainly use IDs for elements with Javascript functionality and I only use classed for elements that will be repeated with same styles. CSS Specificity isn't an issue if your code is clean and you work down through each nest.

  • Author

thank you all for all the wonderful responses. I wish I could mark the question answered by TWO different respondents because 2 are right

Alluziion

 

#nav #selected a{
    color: #394042;
}

Andy9I

#nav li#selected a{
    color: #394042;
}

<EDIT>

If one or the other is the (best or most accepted) then I will mark that comment the solver

</EDIT>

@Alluzions -> the affect I am after is that the tabs themselves (<li> backgrounds) change from beige ( selected ) to caramel ( not selected ) and the text( <a> inside the <li> ) needs to adjust so that it isn't the same color as the selected <li> background

Edited by drt_t1gg3r

Hey.

As you've noticed, both mine and Andy's solutions both do exactly the same thing - this is because they are.

What Andy has done is select the element like this... <a> tag that is a child of an <li> with the ID of #selected that is the child of an element with an ID of #nav.
So basically, it specifies the element with the ID #selected belongs to an <li>; this is to improve it's selection power as the browser weighs which style to apply to it if another selector is also applying styles to it - the most specific selection will be used. There is an article about it here if you're interested: CSS Specificity.

Another use for this technique would be better demonstrated on classes.
Say for example you have a <p> tag, and an <a> tag both with the class .red - let's assume that you've named this class red because you want both of these elements to be have red colour text so in your CSS you have this...

.red {
color: red;
}


This works by all means but say you wanted the <a> tag to be bold and not the <p> tag, if you did this...

.red {
color: red;
font-weight: bold;
}


Then the <p> tag would also be bolded so you can specifically state that you only want the <a> tag that has a class of .red to be selected and styled like this...

a.red {
font-weight: bold;
}

This would make the <a> tag bold whilst the <p> tag would remain at its default weight.

 

This method is much more commonly used for classes for the reasons that I have demonstrated but it's not as common for IDs because they are used for unique selections/elements so the previous example of use (CSS Specificity) would be the most useful reason.

 

--------------

 

To the second part of your reply...

 

Do you mean that you want the <li> to change it's background colour when the mouse is hovered over it or do you want it to stay a different colour if you are on that page? e.g. if you are on "home" the "home" <li> would be different until you move to a different page?

 

If the former then you may want something like this...

li:hover {
background: #000; /* Change this to the HEX of your beige colour */
}

 

Or if the latter...

a:active {
background: #000; /* Change this to the HEX of your beige colour */
}

 

To use this method you would have to give the <a> tag the same width and height as your <li> tag so that it covers it as I don't think that there's a way of using :active on a non <a> tag or selecting a parent of a child in reverse order.

I don't necessarily agree with that statement.

 

Classes should be used to style elements that you may have several of on a page such as a small container with the same border/background/dimensions etc.

 

IDs should be used to style elements that are unique to the page and are not repeated elsewhere.

 

These are their semantic uses.

But if you follow the semantics correctly then you'll often not need to use IDs anyway such as using <nav> instead of <div id="nav">. I mainly use IDs for elements with Javascript functionality and I only use classed for elements that will be repeated with same styles. CSS Specificity isn't an issue if your code is clean and you work down through each nest.

Just to provide a difference of opinion here, I almost never use IDs any more (even for JavaScript). I prefer using classes because it makes the CSS more modular and easier to work with owing to the smaller specificity. Having read up on SMACSS, I live the idea of sub-classing my modules and that's much harder with IDs.

 

I'm not saying that IDs are bad things, just that there are those of us out there who chose not to use them.

Just to provide a difference of opinion here, I almost never use IDs any more (even for JavaScript). I prefer using classes because it makes the CSS more modular and easier to work with owing to the smaller specificity. Having read up on SMACSS, I live the idea of sub-classing my modules and that's much harder with IDs.

 

I'm not saying that IDs are bad things, just that there are those of us out there who chose not to use them.

 

Exactly my point of view. I would definitely suggest giving SMACSS a read to anyone that makes websites.

I also use it as a nice separation of concerns. Classes handle styling, IDs handle behaviour(whether hash values or JS).

  • Author

Upon reading all the comments that relate to the use of ID's and Classes I did a lot of research. Very little of the research concentrated on whether one is used for styling or behavior. On the other hand the most basic of use for ID versus Class is that an ID is a unique tag while a Class is a "blueprint" tag. Only one ID with a given value can exist on a page at any given time, while a class can exist on ALL elements if you wanted. This was a huge chunk of information that htmldog dot com DIDN'T go over that would have been useful knowledge. So to sum up what I've learned....

 

if you want to target a certain element inside a group of them ( like one <li> out of a bunch of them ) you would use and ID, but if you want to target a bunch of elements to apply the same style to, then a Class is used.

 

EDIT: well there is a special use for ID in browsers.

 

http://yourdomain.com#comments, 
 

the browser will attempt to locate the element with an ID of "comments" and will automatically scroll the page to show that element.

Edited by drt_t1gg3r

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.