Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign css to specific element

I have an <li> element on the page that I want to look a certain way. Changing the css affects all the <li> elements.

here is the element I want to change
<li>Outside Job<span class="toggle"<input type="checkbox" /></span></li>

here is my css for the <ul><li>

ul li {
    /*font: normal 17px Helvetica;*/
    color: #a9a9a9;
    border-top: 1px solid #333;
    border-bottom: #555858;
    list-style-type: none;
    padding: 10px 10px 10px 10px;
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#4c4d4e), to(#404142));
    overflow: hidden;}

The commented out font portion and the color is what I want to affect this <li> and not any others.

How would I do this? Thank you!

like image 205
shaiss Avatar asked Nov 06 '09 19:11

shaiss


People also ask

Can I apply a CSS style to an element name?

You can apply a CSS style to the element name by using attribute selectors that match elements based on their attributes.

How do I select a specific element in a class CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

Can you make a class selector particular to an element type?

You can create a selector that will target specific elements with the class applied.

How do I apply CSS to child element based on parent?

It's easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinator ( > ), which is placed between two CSS selectors. For example, div > p selects all <p> elements where the parent is a <div> element.


2 Answers

If you can set a id or a class on the LI, then it would be trivial..

ul li {
    color: #a9a9a9;
    border-top: 1px solid #333;
    border-bottom: #555858;
    list-style-type: none;
    padding: 10px 10px 10px 10px;
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#4c4d4e), to(#404142));
    overflow: hidden;
}

ul li.special {
    font: normal 17px Helvetica;
}

<li class="special">Outside Job<span class="toggle"><input type="checkbox" /></span></li>

Aside from that, there is no simple way to do this cross-browser with css alone.

like image 115
Quintin Robinson Avatar answered Oct 31 '22 18:10

Quintin Robinson


Basically, you just need to give it an extra identity/attribute so you can have a CSS selector to style that li tag. Example:

/*using class*/
ul li.different {font: normal 17px Helvetica;}

/*using attribute, title in this case*/
ul li[title=diff] {font: normal 17px Helvetica;}

/*using id if it's one and only*/
#id {font: normal 17px Helvetica;}
like image 26
o.k.w Avatar answered Oct 31 '22 18:10

o.k.w