tags:

views:

916

answers:

3

Hello,

I am trying to to make link inside a div button that when you mouseover in a div then it will detect the link because of the css property

display:block;width:100%;height:100%;

Using a div it works fine, but I am trying to use it as a span but the display gets misaligned. How can I make the display correct?

Here's the code:

<style>
.link-rounded-button {
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border: 1px solid #828282;
    padding:0 0 0 3px;
    /* for test purposes, expand the width */
    width:200px;
}

.link-block {
    display:block;
    width:100%;
    height:100%;
}
</style>

<div class="link-rounded-button">
    <a class="link-block" href="#">this is a link inside a div</a>
</div>
<hr />
<!-- If I make the div to a span, the display is not correct. -->
<span class="link-rounded-button">
    <a class="link-block" href="#">this is a link inside a span</a>
</span>

Thanks in advance :)

Kind Regards, Mark

A: 

Try adding

display: block;

to your .link-rounded-button style.

Kane Wallmann
A: 

A span's default display is inline so placing a block element inside of it won't give you your expected results. Add display: block to .link-rounded-button.

Salaryman
Plus placing block element inside inline element is against standards
Clement Herreman
@salaryman: I added display:block to .link-rounded-button but the display looked like a div tag instead of a span and the link inside the span did not expand to the span's width and height. I am trying to use a span similar to this link: http://archivist.incutio.com/viewlist/css-discuss/49553Thanks
marknt15
**@marknt15:** What do you mean by *"the display looked like a div tag"*? Do you mean that it was on it's own line? Then use `display: inline-block;` as specified in my answer.
Andrew Moore
@andrew: What I mean is it is on its own line but if you mouseover the space inside the span tag then it will not highlight the link. I want to highlight the link by doing that. Do you know how? Thanks again :)
marknt15
+6  A: 

Make the <span> a block level element as well. By doing display: block; on the <a> tag, you are making it a block level element. A <span> is a inline element. You cannot have a block level element inside a inline element. You must therefore make your <span> a block level element.

The following CSS will achieve this:

SPAN.link-rounded-button {
    display: block;
}

If you are using <span> in order to keep all the links on the same line, then use the following:

SPAN.link-rounded-button {
    display: inline-block;
}

WARNING: IE6 and below only supports inline-block on elements which are by default inline. It will not work on <div> for example, but it will work fine on a <span>.


Semantic Solution

You could also drop your extra <div> or <span> to make your code more semantic and still achieve the same effect (with the added benefit of having a CSS :hover effect which will work in IE6):

HTML:

<a class="link-rounded-button" href="#">this is a link with no extra markup</a>

CSS:

a.link-rounded-button {
    display: inline-block; 
    /* or display:block; depending of 
       the effect you are trying to achieve */

    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border: 1px solid #828282;
    padding:0 3px;
}

a.link-rounded-button:hover {
    background-color: #828282;
}

I've setup a demo of this solution.

Semantic Solution Demo


More Information

Here is a list of elements that are considered block level elements or accept block level elements as children.

XHTML 1.0 Block-level Elements

Andrew Moore
Hi Andrew, thanks for your answer. I did what you said but the link did not expand to the whole span tag. I am trying to achieve something similar to this: http://archivist.incutio.com/viewlist/css-discuss/49553Thanks :)
marknt15
**@marknt15:** specify a `width` and `height` to the `<span>` as well, or, specify a `width` and `height` to the `<a>` tag and see the `<span>` expand. *Personally, I would go for the semantic solution I posted instead.*
Andrew Moore
@andrew: I thought about adding a width and height to my span but I want my rounded border span to be dynamic to the link's width. For example, I have a <a>looong link</a> then my span's width will automatically adjust to my link's width. If I have a short link like <a>short</a> then my span's width will also base its width to my link's width.I don't know if what I want is achievable or not. Hhmm...
marknt15
**@marknt15:** Then use my semantic solution. It will automatically expand if you don't specify a `width` to the `<a>` tag. You do not need that extra markup.
Andrew Moore
**@marknt15:** I've setup a demo of my semantic solution: http://alpha.wolfmicrosystems.com/demo/semantic_link_button.html
Andrew Moore
@andrew: Thanks it now works. Thanks also to your demo :) Awhile ago I insisted on doing it with a span tag. Sorry I got a little confused. +1 to you.
marknt15