I have a link inside a DIV. How can I change the color of this link inside this div. This code does not seem to work
<style type="text/css">
.someDiv
{
font-size:14px;
color:#c62e16;
}
</style>
<div id="someDiv">
SOne Text
</div>
Thanks.
ids are accessed by a pound sign (#), and classes are accessed by a period (.)
<style type="text/css">
#someDiv a
{
font-size:14px;
color:#c62e16;
}
</style>
<div id="someDiv">
SOne Text
</div>
use
.someDiv a {
font-size:14px;
color:#c62e16;
}
You are using the wrong selector. You have an id="someLink", and the CSS is looking for the class="someLink". Try with #someLink, it'll work.
div#someDiv a{
color: #hexcode;
}
That will work too, you use the selector to select ALL the elements of the type "a" in a div with the id="someDiv".
While you're using the wrong selector for someDiv you will usually need to set a colours separately:
#someDiv, #someDiv a {
color: red;
}
Related
I would like make all text within div.main gray except for all content within the child div.exception. div.exception should appear as if class main was never added to the parent div.
Is this possible? If so, how? Thanks!
<style type="text/css">
.main{color: gray;}
.hello{color: red;}
</style>
<div class="main">
<div>
<div class="exception"><p class="hello">Hello</p><a>Link</a></div>
</div>
<div><p>Howdy</p></div>
<div><a>Link</a></div>
</div>
for modern browser, just apply the rules to every div but .exception
.main div:not(.exception) p {
/* style for very nested div not exception */
}
otherwise override the rules later (as suggested by #jacktheripper)
This is simply done by:
.main .exception {
your styling here (e.g. color: black)
}
See this jsFiddle example
You cannot use color: inherit as this selects only the immediate parent, when you want to select two parents above. Therefore you have to override the colour 'manually'
#F. Calderan's answer is an alternative, but browser support is variable
No, that's not possible.
You can easily override the style so that it appears not to have been colored gray, but then you have to know what the original color was:
.main .exception { color: black; }
If you would set the style on the inner elements directly intead of on the main element, and set the exception class on the same level, you could override it using inheit:
<style type="text/css">
.main div { color: gray; }
.main div.exception { color: inherit; }
.hello { color: red; }
</style>
<div class="main">
<div class="exception">
<div><p class="hello">Hello</p><a>Link</a></div>
</div>
<div><p>Howdy</p></div>
<div><a>Link</a></div>
</div>
In the below example link 2 comes out white and not black as expected how can I style the color of link two without wrapping it in a container tag?
.text a{
color:#FFF;
}
.black{
color:#000;
}
<div class="text">
Link 1
Link 2
</div>
Your second selector needs to be more specific than the first one to override it:
.text a {
color:#FFF;
}
.text a.black {
color:#000;
}
<div class="text">
Link 1
Link 2
</div>
It comes out white because the previous selector has higher specificity. One solution in this:
.black{
color:#000 !important;
}
This can cause complex problems if you use it too much, however. Generally the best solution is to try and avoid too many selectors. Have one selector that sets a default style for links, then only use classes to change specific links. For example:
a {
color: #fff;
}
.black {
color: #000;
}
It turns out white because the first selector is much more specific, namely: get a link in an element that has a class "text", whereas the last is merely get any element with the class "black".
You can solve this in two ways:
.text a.black {
color:#000;
}
OR
.black{
color:#000 !important;
}
In which 'important' overwrites other rules that are give to elements with the class "black".
here is working solution you just apply the style to black with id rather than class:
.text a{
color:#FFF;
}
#black{
color:#000;
}
<div class="text">
Link 1
Link 2
</div>
As others have mentioned, it comes out white because your previous selector for "a" tags is more specific than your "black" class.
There are two options here:
Be more specific:
.text a{
color:#FFF;
}
.text a.black {
color:#000;
}
<div class="text">
Link 1
Link 2
</div>
Or, you could us the "!important" rule:
.text a{
color:#FFF;
}
.black{
color:#000!important;
}
<div class="text">
Link 1
Link 2
</div>
I would strongly advise the first approach, but in some situations, "!important" can be a quick fix until you figure out where the real problem lies. Don't abuse the "!important" rule because it'll mess you up for the future - trust me on that!
Hope this answers your question. Have a good day.
Michael.
I have a row of divs with :hover and it is working when I hover over the images within the divs. However, it doesn't want to work for the text. I am on the newer side of html and css, so help appreciated. I must be missing something obvious?
The first one with the div.topIconsHover:hover CSS works. The other does not. I have tried applying the topIconsHover class to the div as well and it still doesn't work. So, I must be doing something wrong with the HTML? But I'm just not sure what. Help appreciated! Thanks.
Note: I have the CSS in an external sheet.
div.topIconsHover:hover {
background-color:#555555;
}
<div class="topIcons topIconsHover">
<img src="tools16lg.png" />
</div>
div.topTextHover:hover {
background-color:#555555;
color:#ffffff
}
<div id="topBrowse" class="topTextHover">
Browse
</div>
The color attribute is working only with text elements, not divs. So you should apply the class tag to your href tag like this :
<style type="text/css">
.topTextHover:hover {
background-color:#555555;
color:#ffffff
}
</style>
<div id="topBrowse">
Browse
</div>
EDIT :
If you're looking to define a base class for the link itself, and a HOVER state, do it like this :
<style type="text/css">
.topTextHover {
background-color: transparent;
color: #0000ff;
}
.topTextHover:hover {
background-color: #555555;
color: #ffffff;
}
</style>
<div id="topBrowse">
Browse
</div>
Good luck
You applied style to the ":hover text" but not for links. This should do the trick (not tested):
div.topIconsHover:hover {
background-color:#555555;
}
<div class="topIcons topIconsHover">
<img src="tools16lg.png" />
</div>
div.topTextHover:hover, div.topTextHover:hover a {
background-color:#555555;
color:#ffffff
}
<div id="topBrowse" class="topTextHover">
Browse
</div>
Anchor tags have a default text colour which gets priority (usually blue). What you need is to define this explicitly:
div.topIconsHover:hover {
background-color: #555555;
}
div.topTextHover:hover {
background-color: #555555;
}
div.topTextHover:hover a {
color: #ffffff
}
There are two really simple ways to resolve this issue.
First if you don't have any height/width requirements on the anchor tag (<a href=''></a>) being inside the div do the following:
.topTextHover a:hover{
background-color:#555555;
color:#ffffff
}
<div id="topBrowse" class="topTextHover">
Browse
</div>
If you do have spacial requirements for the text inside the div (i.e. you want the text to be vertically-aligned to the center and horizontally centered) then I would do the following note* this is backwards compatible but is really only compliant with CSS3
#BrowseLink:hover {
background-color:#555555;
color:#ffffff
}
<a id="BrowseLink" href="browse.html">
<div id="topBrowse" class="topTextHover">
Browse
</div>
</a>
Also of note IE6 doesn't like the pseudo-class hover on anything other than an anchor tag and therefor will not work properly. This may be applicable in other browsers as well but the main one that I know that has issues is IE6 of the browsers that are typically seen on a website.
in my navigation bar i have this code.
<div id="aboutnav"><a class="hl" href="#"><h2>about\a</h2></a></div>
all the div does in the case is put the text in position and in the a.hl it's -
a.hl{
background-color:#000;
text-decoraction:none;
color:#fff;
}
the text is the right colour, it is in the correct position but there is no background colour.
This is because in HTML4/XHTML you can't nest hx elements into a! Try using this instead:
<div id="aboutnav"><h2><a class="hl" href="#">about\a</a></h2></div>
I think you would need to update your css in a similar way:
a.hl{
display:block;
background-color:#000;
text-decoraction:none;
color:#fff;
}
Your style is probably being overridden by another style for h2. Try:
a.h1 h2{
background-color: #000;
}
You should write the HTML like this
<div id="aboutnav">
<h2>
<a class="hl" href="#">about</a>
</h2>
</div>
And then style it like so
#aboutnav h2 {
background-color:#000;
}
#aboutnav h2 a {
text-decoration:none;
color:#fff;
}
Example: http://jsfiddle.net/jasongennaro/rbxBL/
Fyi... you had text-decoration misspelled.
Also, you really don't need the class for the a when the HTML is done this way.
Why is the link using the underline?
<html>
<head>
<style type="text/css">
body{
text-decoration: underline;
}
#text{
text-decoration: none;
}
</style>
</head>
<body>
Text text text <br/>
<div id = "text">
link
</div>
</body>
</html>
Because this is the default (user agent css have this rule, to apply underline in every tag a). The default isn't inherit, so even if parent tag has underline, the child won't get it.
EDIT 1:
For example, firefox have this rule:
*|*:-moz-any-link {
text-decoration:underline;
}
Default would be:
*|*:-moz-any-link {
text-decoration:inherit;
}
Then, in your example, the tag a would inherit div text-decoration.
EDIT 2:
You can overwrite default behavior with:
a {
text-decoration: inherit;
}
It's the default behavior of the a tag. It doesn't match the #text style.
I think you want to replace this...
#text{
text-decoration: none;
}
with this...
#text a:link{
text-decoration:none;
}
this tells the rule to apply to all anchors that are children of the tag who's id is 'text'
It's included in the default browser CSS. It's just as if this was included prior to your style tag:
a{
text-decoration: underline;
}
Sure, the link also matches #text, but since a is more specific, it wins. Any attributes not explicitly specified by the browser's a (such as font-size) will be inherited.