Assign css to specific element - css

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!

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.

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;}

Or use an inline style, STYLE= on the LI itself:
<li style="font: Helvetica; color: #a9a9a9;">...</li>

On a side note you should probably think about adding additional fallback sans-serif fonts to that rule aside from Helvetica. I'm a big fan of that typeface, but most people don't have it on their computers.
EDIT: Someone posted my same answer while I was writing this one. I'll keep the sidenote though because its useful.

As previously said, either a class or id will do it - depending whether you're going to be using this particular style uniquelly for this list item, or plan to use it elsewhere (though you can only use an id once on a page, you can reuse an id on another page).
You can mix and match classes and ids within one list, so that even if you want different styles for each list item in your list you can do it using different classes and ids.
For example, you can have your style that styles your list by default, so that items with no class or id inherit the default style, and then any number of classes and ids that can control the individual items...
<code><pre>
<ul>
<li>blah blah blah</li>
<li class="special">blah blah blah</li>
<li class="special">blah blah blah</li>
<li id "extraspecial">blah blah blah</li>
<li>blah blah blah</li>
</ul>
</pre></code>

Related

CSS ::selection custom color on list numbers & bullets in webkit browsers?

I'm using a custom highlight color on my site with the CSS ::selection rule, but noticed that in Webkit browsers the selection color doesn't exactly work on everything.
Here's a fiddle showing what I mean, using a numbered list as an example: http://jsfiddle.net/ufGmP/
If you highlight the text in Chrome or Safari, the default blue highlight color is seen around the bullets. I've noticed this issue on every site I've found using ::selection to override the default color; for instance, on http://www.smashingmagazine.com/ the default selection color can be seen if the entire headline of a story is highlighted.
Does anybody know a way around this? Any help would be much apprecaited!
It's a bug that's been hanging around from 2010, https://bugs.webkit.org/show_bug.cgi?id=38943.
A number of elements fail to highlight, here's a fiddle, http://jsfiddle.net/AULsp.
HTML
<input type="text" value="This doesn't get highlighed." />
<textarea>This doesn't get highlighed either.</textarea>
<p>This does get highlighted.</p>
<ul>
<li>The bullets however, don't.</li>
<li>This bullet concurs.</li>
</ul>
<ol>
<li>And so does this one.</li>
<li>And finally, this one.</li>
</ol>
​
CSS
body {
padding:40px;
}
::-moz-selection{
background: #900;
color: #fff;
}
::selection {
background: #900;
color: #fff;
text-shadow: none;
}
input, textarea, ul, ol, p {
display:block;
width:200px;
margin: 0 0 15px;
}
ul {
list-style:disc;
}
ol {
list-style:decimal;
}
WebKit also seems to highlight element padding and margin on the elements that do allow ::selection, which can look pretty off depending on your design.
Why don't you use an image for the bullet, instead of the default bullet? Something like...
list-style:none;
padding-left:20px;
background:url(something.gif) 0 50% no-repeat

Use CSS class in span to set current menu state?

I'm learning CSS and html and am stuck on retaining the look of the hover/active state after an item has been clicked. I've looked at several posts on this site and haven't been able to apply the lesson to my application. I also found a solution here http://www.456bereastreet.com/archive/200503/setting_the_current_menu_state_with_css/ but it didn't work for me (I'll assume it's my fault).
Another source suggested using a span class which is what I'm currently trying. I want to have the same hover color (#fff), weight (bold), and background image in use when a menu item is selected to show the user exactly where they are (this is in the secondary sidebar nav and comes in to use on those pages where the main nav has a dropdown with multiple otions). The only characteristic that's working for me is the bold text. You can see the work in progress here:
http://www.mentalwarddesign.net/dynamec/About/index.html
I'm assuming the class I've created in the span is being overridden, but I'm at a loss as to the remedy. Any and all help would be greatly appreciated.
Following is the code for the li and then the corresponding CSS. Thanks in advance!
<ul class="nav">
<span class="chosen"><li>What We Do</li></span>
<li>How It Started</li>
<li>Who We Are</li>
<li>What We Know</li>
</ul>
.chosen {
font-weight: bold;
color: #ffffff;
background-image: url(../imgGlobal/bulletRight.jpg);
background-repeat: no-repeat;
display: block;
padding-left: -12px;
background-position: 168px;
}
.content ul, .content ol {
padding: 0 15px 15px 40px;
background-color: #fff;
}
ul.nav {
list-style: none;
}
ul.nav li {
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #464646;
height: 50px;
background-color: #000;
}
ul.nav a, ul.nav a:visited {
display: block;
width: 160px;
text-decoration: none;
padding-top: 12px;
padding-right: 5px;
padding-left: 15px;
}
ul.nav a:hover, ul.nav a:active, ul.nav a:focus {
color: #ffffff;
font-weight: bold;
height: 38px;
background-image: url(../imgGlobal/bulletRight.jpg);
background-repeat: no-repeat;
background-position: 168px;
}
Ed, the CSS selector :active means "Being activated (e.g. by being clicked on)", not "Having an href attribute that resolves to the URL of the current page". You can use server-side logic to insert a class=”chosen” or similar. E.g:
<li class="chosen">What We Do</li>
And, CSS style: ul.nav li.chosen a { }
There is another way to do it as mentioned on the tutorial link you gave, however it is not a good example.
Well first of all, you cannot wrap an li inside of a span. The only direct descendent of a ul is a li. You can put the class chosen directly on to the li and it works just fine.
<ul class="nav">
<li class="chosen">What We Do</li>
<li>How It Started</li>
<li>Who We Are</li>
<li>What We Know</li>
</ul>
Put the chosen class in the li element itself. Drop the span altogether.
EDIT:
Sorry, in the a element, i meant to say.
A span is a tag, a class is just an identifier. They don't really have anything to do with one another except a class can be used to apply a style to a span but that's true of any tag.
In your case you're trying to put a span (an inline element) around an li (a block level element). In HTML inline elements should not contain block elements.
You should be able to just do it like this: EDIT fixed based on the actual CSS
<li>What We Do</li>

how to apply css on only one class?

css
#inchannel li{
width:179px;
height:40px;
margin:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
font-size:17px;
color:#999999;
background:url(img/green.png) no-repeat 8% 50%,url(img/back_line.png);
}
.ichn0{
width:179px;
height:123px;
margin:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
font-size:17px;
color:#999999;
background:url(img/green.png) no-repeat 8% 18%,url(img/log_in_body.png),url(img/invite_button.png) no-repeat 60% 60%;
}
html
<ul id="accordion">
<li>
<ul id="inchannel">
<li class="ichn0"><img src="facebook/41403_1434825607_37944358_q.jpg"></li>
<li><img src="facebook/48983_615523712_8495_q.jpg"></li>
<li><img src="facebook/41621_717814907_4472_q.jpg"></li>
</ul>
</li>
</ul>
I want to apply css on only one class but it isn't applied to one class it seems like inchannel css covers all.
can any body tell me how to apply css on only one class?
Any thought?
Thank you.
use higher specificity
#inchannel li.ichn0{
width:179px;
height:123px;
margin:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
font-size:17px;
color:#999999;
background:url(img/green.png) no-repeat 8% 18%,url(img/log_in_body.png),url(img/invite_button.png) no-repeat 60% 60%;
}
#inchannel li has much more specificity tham the class .ichno.
There are several ways to combat this.
Add !important to the properties of .ichn0
This is bad practice because you can overwrite user stylesheets like this, which are used for things like screen readers, etc.
Add an ID to the first li.
This is OK, but not the way I'd go about it, personally.
#inchannel:first-child
This is how I would go about it. In my eyes this is easiest to maintain across multiple pages.
Add a class per Damen's answer.
There's absolutely nothing wrong with this approach, either. I'm just a sucker for :psuedo selectors.
#inchannel li — Applies to all li elements inside the #inchannel element (all of them, in your case.
.ichn0 — Applies to li elements with ichn0 class, regardless of what element they are inside. There is only one of these.
The reason why the first is taking precedent over the .ichn0 CSS is because of CSS specificity. The #inchannel li selector is more specific and therefore overrides the less-specific .ichn0 settings.
Changein .ichn0 to #inchannel li.ichn0 should fix your problem.

How can i overwrite the parent Style to an element

<div class="jqueryslidemenu">
<ul>
<li class="menuitem">TEST1</li>
<li class="navOFFTDDisabled" id="TEST2">TEST2</li>
<li class="navOFFTDDisabled" id="TEST3">TEST3</li>
<li class="navOFFTDDisabled" id="TEST4">TEST4</li>
</ul>
</div>
CSS FILE
.jqueryslidemenu ul li {
display: block;
background: #FFCC00;
color: white;
padding: 4px 12px 6px 5px;
border-right: 1px solid #778;
color: #2d2b2b;
text-decoration: none;
font-weight: bold;
cursor: hand;
}
.navOFFTDDisabled{
//Aplly Style
}
I cannot Apply class="navOFFTDDisabled" to each (li) Items because the "jqueryslidemenu" is overwriting the navOFFTDDisabled style .How can i apply both styles
Make it a better match,
.jqueryslidemenu ul li.navOFFTDDisabled{
//I'm more important neener neener.
}
Just to be more useful, you can actually calculate which selector with take precedence as described in the specification
You have three possibilities to override a selector:
order of the selector: a selector further down in your stylesheet overrides a selector that is further to the top
selector specifity: http://www.w3.org/TR/CSS2/cascade.html and http://www.molly.com/2005/10/06/css2-and-css21-specificity-clarified/
basically it depends on how many tags, classes and ids you have in your selector. classes add more weight than tags and ids add more weight than classes
the last thing you can do is to add an !importantto your style rule, which overrides any other selector. To be correct you can still have more !importantrules, than the selector specifity rule comes into play again. E.g. .klass{color:red !important}

having trouble wrapping <a> tag around <li> in firefox 3.6

<li id="vantage_btn">Vantage</li>
css:
#model_nav li{float:left; display:block; position:relative; z-index:500; cursor: pointer; width:100px; height:37px; text-align: center;}
a.model_link{font-family: Helvetica; color: #333333; font-size: 18px; font-weight: bold; text-decoration: none; border:1px solid;}
a.model_link:hover{color: #aaaaaa;}
above is an example of one of my buttons in a list. what I want is a 100px by 37px li button that would invariably increase the size of the tag to 100px by 37px. It works in safari and chrome, but in firefox the tag only wrap around the text inside the li and not the li width and height.
HTML doesn't allow <a> to be wrapped around <li> (None of the possible parent elements for <li> may have <a> as a child element, and <a> may not have <li> as a child element).
Put the links inside the list items, then style them as desired. It sounds like you want to start with display: block. Listamatic may provide further inspiration.

Resources