Forum Moderators: not2easy
css:
.lhc #box a:link, #box a:visited {
display: block;
border: none;
width: 185px;
height: 185px;
margin:0 0 1em 0;
padding: 0;
font-size: 20px;
line-height: 30px;
color: #555;
text-decoration: none;
background: #ff6600 url(./img/box-background.gif) no-repeat left top;
text-align: left;
}
.lhc #box a:hover {
background-color: #ff9933;
background-position: right top;
}
html:
<div id="box">
<a href="#">Some anchor text</a>
</div>
The background image creates a mouseover effect.
I can't get the text to sit in the middle of the box. I've tried padding, margin and line height, but they all have unwanted side effects.
Any ideas?
/Steve
.lhc #box
{
width: 185px;
height: 185px;
line-height: 185px;
text-align: center;
}
.lhc #box a:link, .lhc #box a:visited {
display: block;
border: none;
margin:0 0 1em 0;
padding: 0;
font-size: 20px;
color: #555;
text-decoration: none;
background: #ff6600 url(./img/box-background.gif) no-repeat left top;
text-align: left;
}
.lhc #box a:hover {
background-color: #ff9933;
background-position: right top;
}
Unfortunately it hasn't helped! The anchor text still sits in the top right hand corner of the div.
Applying margins or borders to the anchor moves the background, I assume because I've declared the link as display:block
For instance:
.lhc #box a:link, .lhc #box a:visited {
padding: 30px 0 0 5px;
display: block;
font-size: 20px;
color: #555;
text-decoration: none;
background: #ff6600 url(./img/box-background.gif) no-repeat left top;
text-align: left;
Moves the text to where I want it, but it also moves the background image.
I thought applying a span to the anchor with padding might work, but this only affects the first line of text.
css:
.lhc #box {
width: 185px;
height: 185px;
padding-left: 2px;
padding-bottom: 18px;
}
.lhc #box a:link, .lhc #box a:visited {
display: block;
font-size: 20px;
color: #444;
text-decoration: none;
background: #ff6600 url(./img/box-background.gif) no-repeat left top;
text-align: left;
}
.lhc #box a:hover {
background-color: #ff9933;
background-position: right top;
}
.lhc #box .try {
display: block;
line-height: 1.75em;
padding: 40px 5px 40px 5px;
margin: 0;
}
html:
<div id="box">
<a href="#"><span class="try">This is some anchor text</span></a>
</div>
Now I'd like to eliminate the span if possible, is there any way to do this?
I tried the following changes and it seemed to do what you want. (Changed lines only shown)
.lhc #box a:link, #box a:visited {
margin:auto;
line-height: 900%;
}
If you are giving a size to the anchor then the size and padding on the container (#box)will control it's position
But the padding and margin will also apply to the background image, causing it to spill out of the div. It would be a lot easier without the background image!
Have you tried any of the vertical align options
Yes, but the background image was cut off, only showing through where there was text.
By using:-
.lhc #box .try {
display: block;
line-height: 1.75em;
padding: 40px 5px 40px 5px; <-- extra vertical padding
margin: 0;
}
with extra padding (40px) top and bottom all the background image is displayed.
This solution works in IE7, FF2 and Opera9 but I would still like to loose the non semantic <span> if possible.
you have the box set at 185x185 so I assume that is the image dimensions too?
setting the link to block and setting its line-height to 185px will vertically center 1 x line of text
display: table-cell; would do it, but for IE.. vertical centering is possible and there are various methods summarised here [webmasterworld.com]
but, I don't think any of those methods are perfect for text on a specific (non-tiling) background image, so to get the vertical centered text, block link (by which I mean being able to hover over any part of the 185x185 box) and image rollover together I would suggest using an "image replacement technique" and building the text into the image.
You can use the text in a hidden span if you want the link value, and it's an arguable accessibility feature to do so, but even if you don't, from an SEO POV with most menus "home", "about us" etc.. it's debatable whether it's worth it, and if that's the case maybe just use images with "alt" text for accessibility?
This is probably not what you want to hear, but there is always going to be a problem using background images in links
it's debatable whether it's worth it, and if that's the case maybe just use images with "alt" text for accessibility?
After going for a walk and thinking about it, I'm ready to let go! It's so easy to get drawn into a complex solution when there is a simpler, more robust one staring you in the face.
Thank you both for the perspective, I'll add text to the image.
Steve
One of the problems with image links is that they do not have link text which certain Search Engines may use. Text in an Alt or Title tag may not carry the same weight. Also if you put the text in an Alt or Title tag, and you have a big index, then you end up with a lot of Alt text. If this 'hidden text' is a large proportion of total text on the page, then it may trip a spam filter. Naturally, I have no proof of this. :)