Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 03:42 - Mar 07, 2007  

sbjc23

How do I put a margin around a link in CSS?

I've tried a number of things but none of them seem to be working effectively.

Categories

Answer Discussion
Tutorials

 

jgivoni

Date:: Mar 07, 2007

Time:: 08:20

The short answer is that you cannot put a top or bottom margin on an inline element.
The <a> tag is an inline element, and should appear inside a block element like <p>.

The correct solution would have been to cast the <a> element as an inline-block:
[In CSS]
a.withMargin
{
display: inline-block;
margin: 50px 10px;
}

But the "inline-block" unfortunately is not supported by all browsers. For me, Firefox 2 didn't respond to it.

Instead I believe you should put the margin on the containing block element.
I don't know if there is a better solution in your specific case, but if you post some of your code and explains how it should look, we might find out.

For instance - if your link is not inside a <p> tag and really is not part of a line of text, you can just put "display: block" on it and it will behave like a normal block element and have whatever margins you set.

Jakob

sbjc23

Date:: Mar 12, 2007

Time:: 18:40

Thanks Jakob

Question Answered

This question has been closed, and points have been rewarded to the following experts:


jgivoni: 75

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

jgivoni

Date:: Jan 06, 2009

Time:: 02:40

The answer depends on whether your link is part of a text paragraph or living by itself.

First some background:
A normal hyperlink in HTML is created with the <a> tag.
<a> tags are per default 'inline' elements, and as such should always appear inside a 'block' level element, usually <p>.

Inline elements cannot have top or bottom margins:
http://www.w3.org/TR/CSS21/box.html#marg...
(quote) "[...] vertical margins will not have any effect on non-replaced inline elements."
Setting vertical margins on these elements will simply be ignored.
(A 'replaced inline element', in case you were wondering, is for instance an inline image, which CAN have vertical margins.)

You can set horizontal (left and right) margins, though:
a.myLink
{
margin-left: 10px;
}

If your link is not part of a paragraph, you can consider turning it into a block level element and then you will be free to give it margins as you wish:

a.myLink
{
display: block;
margin: 5px 10px;
}

Best regards,
Jakob


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

tecbrat

Date:: Aug 17, 2011

Time:: 10:32

I know this is an old thread, but it might stil be helpful. Try line-height for vertical space on links.
a.myLink
{
line-height:200%;
}


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10279

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es