Welcome to WebmasterWorld Guest from 54.198.15.59

Forum Moderators: not2easy

Message Too Old, No Replies

adding css to hyperlinks

     
5:25 pm on Jun 8, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1541
votes: 0


I am having great trouble setting hyperlink style to a specific class. I cant work out why it wont take, here is my code:

.class:link,.class:visited {
font-weight:bold;
color:#CCD9EA;
background-color:#445469;
text-align:center;
padding:6px;
text-decoration:none;
}
5:32 pm on June 8, 2012 (gmt 0)

Full Member

10+ Year Member

joined:Jan 2, 2005
posts:333
votes: 0


Try this:

.class a {
font-weight:bold;
color:#CCD9EA;
background-color:#445469;
text-align:center;
padding:6px;
text-decoration:none;
}
8:01 pm on June 8, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:926
votes: 21


Post a sample of the HTML for one of your links. That will make it easy to suggest the correct CSS.
10:05 pm on June 8, 2012 (gmt 0)

Junior Member

joined:June 5, 2012
posts:49
votes: 0


Should be........

.class a:visited {
font-weight:bold;
color:#CCD9EA;
background-color:#445469;
text-align:center;
padding:6px;
text-decoration:none;
}
10:48 pm on June 8, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Aug 9, 2008
posts: 961
votes: 0


Hi whatson, the provided code snippet should work as desired for simething like <a class="myclass">My link</a> so as rainborick says, we need a sample of the html to see what you are trying to select.

jinxed and legaleagle - nice suggestions, just note they select a link that is the descendent of an element with the specified class. Something like:
<li class="myclass"><a>My link</a>
1:11 am on June 9, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


If there's a conflict between

.class (generic)

and

a.class (specific to a)

the browser will go with the <a class...> version. This is much safer than messing about with !important flags.

:link shouldn't be necessary, as it's the default behavior.

Here as elsewhere, it really helps to explain in English what the problem is that you're trying to solve. Are you dealing with an anchor class as such

<a class = "blahblah" ...

or do you want the rule to apply to any anchors that come inside of some other element, like

<div class = "blahblah" ...
<a href ...

?
1:12 am on June 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1541
votes: 0


Here is my html
<li class="class"><a href="/">Home</a></li>
1:22 am on June 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:926
votes: 21


Then your style definitions should read as jinxed showed above:

.class a { ... }
2:18 am on June 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1541
votes: 0


Ok, I got that working, but now the hover effects are not working.
Here is the full CSS code:

.class {
display:inline;
}
.class a{
font-weight:bold;
color:#CCD9EA;
background-color:#445469;
text-align:center;
padding:6px;
text-decoration:none;
}
.class:hover,.class:active {
background-color:#667992;
text-decoration:underline
}

And here is the html:

<li class="topnavlinks"><a href="/">Home</a></li>

Sorry, I am just really new to CSS.
2:40 am on June 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1541
votes: 0


Maybe I should say what I am trying to achieve.
I want a text background for hyperlinks, I want to be able to adjust the width and height of the background, and I want the background color to change on hover.

This is for the top nav bar. I hope that makes sense, and would love to hear the best suggestion to achieve this.
3:14 am on June 9, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


.class:hover,.class:active


Shouldn't that be

.class a:hover
and
.class a:active

?
3:45 am on June 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1541
votes: 0


Thanks Lucy, I think you nailed it. Thanks everyone else too.
3:41 pm on June 10, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 6, 2004
posts: 2239
votes: 0


:hover is only going to work reliably when associated with an anchor (See [w3schools.com ].) So you need...

.class a:hover,.class a:active {...}


... to get the hover effect you're after.

As for this:
I want to be able to adjust the width and height of the background


You can only directly control the width and height of block level elements. Anchors are inline level elements. You can add
display:block;
to your anchor CSS...

.class a {display: block;}


... and then add width and height settings to that as the baseline and to the :hover settings for the change. Just remember that this may cause items to jump around on your page as the anchors change in size.

You can also gain some control over the height of an inline anchor tag by using
line-height
(sometimes) or
padding
changes in the
:hover
code.

Best,
cEM
12:50 pm on June 11, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:926
votes: 21


tutsplus has a great tutorial on selectors. Search on "30 css selectors you must memorize".