views:

610

answers:

4

I have a div with links in it. And I'm lining them up one over the other with <br> tags because I couldn't figure out how to add vertical spacingn with CSS. I tried adding a bottom margin and a bottom padding to the a style rule but it didn't seem to have any effect (why?). I could add another <br> tag to separate them more but I have to assume there's a nicer way to do this with CSS that I just haven't been able to figure out.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body 
{
    height: 100%;
    margin: 0;
    padding: 0;
    font-weight:normal;
    font-size:12pt;
    font-family: Verdana, Arial, Helvetica, serif, sans-serif;
    background:lime;
}

#linksouter
{
    margin: 0;
    padding: 0;
    border-style:solid;
    border-width:0px;
    position:absolute;
    top: 0px;
    left: 0px;
    width: 80px;
    background: blue;
    text-align:left;
}
#linksinner
{
    margin: 80px 0 0 .5em;
    width:100%;
    background:fuchsia;
    display:inline;
    height:100%;
}
#linksinner a
{
    color:red;
    text-decoration: none;
    background:yellow;
}
</style>
</head>

<body>
<div id="linksouter">
    <div id="linksinner">
    <a href="#">1</a><br />
    <a href="#">1</a><br />
    <a href="#">1</a><br />
    <a href="#">1</a><br />
    <a href="#">1</a><br />
    </div>
</div>

</body>
</html>
+1  A: 

To add vertical spacing, you can do this:

#linksinner
{
    line-height: 150%;
}

Margins won't have any effect on <a> elements because they're inline. An alternative solution is to make them:

display: block;

which would mean they respected your margins, and you then wouldn't need your <br>s.

RichieHindle
+3  A: 

Vertical margin and padding only works on block level elements like div and p. a is an inline element so it wont work.

In order to do what you want you need to add the following style to your links:

display:block;

only then will margin and pagging for top and bottom get applied correctly

EDIT: if you do it this way you can also get rid of the <br/> tags

Darko Z
I was just about to say that...
jeroen
then we agree :)
Darko Z
Why would you make a-Tags to block-Elements just to separate them vertically? Enclosing them in a block-Element is more appropriate imho.
stefanw
why add another layer of elements when you dont need to?? that would be adding another layer of elements just for presentational purposes. CSS should be used for that...
Darko Z
@Darko - good point. I think your approach makes sense. I need to read up on block vs inline. It seems like I keep running into this block vs inline problem all the time without ever recognizing it's the cause of my issues. Thanks!
gday
A: 

Links can't have margins defined because by default they are "inline" elements. Inline elements can not have margin or width rules applied. To allow inline elements to have these things get applied, you need to add another property to your rule.

Try this:

#linksinner a {
    display: inline-block;
    /* Add your margin or height rules here */
}

I think for what you're looking to do, you should be using a list though:

<ul id="linksinner">
    <li><a href="#">1</a></li>
    <li><a href="#">1</a></li>
    <li><a href="#">1</a></li>
    <li><a href="#">1</a></li>
    <li><a href="#">1</a></li>
<ul>

You can then apply your margin or padding rules to the <li /> tags. If you don't want bullet points to show up use:

#linksinner li { list-style-type: none}
Dan Herbert
this doesn't work cross browser. to get inline-block to display correctly on all browsers there is a hack, but its not as simple as you have it.
Darko Z
Why won't that work cross browser? It is supported in every major browser: http://www.quirksmode.org/css/display.html
Dan Herbert
the link you provide says the support is incomplete for IE6 and 7 and no support at all in FF2. Im not sure how you can deduce that it works in every major browser from that...
Darko Z
Firefox 2 has fairly low market share (It's a few major versions behind) and IE6/7 work for anchor tags, which is exactly what was used in my example. The "incomplete" for IE6/7 should really be "buggy" however, since it can work provided you trigger hasLayout correctly, although it doesn't matter for anchor tags.
Dan Herbert
A: 

You need to give a padding or margin to the anchor-tags, not the divs. But actually don't, do this instead:

<p><a href="#"></a></p>

and give the p's a padding-bottom or padding-top.

stefanw