1

I am trying to align <a> tags inside <li> but i can do it verticaly. But the <a> is allways in the top and i want to center verticaly and horizontaly the <a> tag

Image that describe the situation

How you can see at the image. I need A. but Getting B.

This is the HTML

<ul> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <ul>​ 

And this the CSS

ul { line-height: 85px; } li { float: left; height: 85px; line-height: 85px; border: 1px solid red; } a { height: 40px; line-height: 40px; width: 40px; display: block; text-align: center; border: 1px solid blue; } 

​You can see the jsfiddle

http://jsfiddle.net/Mum5e/

4 Answers 4

3

You can try using display:table-cell and vertical-align:middle. As I recall, this is a cross browser solution.

See this Fiddle

Edit: with fixed width anchors: Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. but your approch do not allow me to set a width and height for the "a" tag
It's cross-browser in the sense that it is supported by IE8 and above, plus all the other major vendors. See quirksmode.org/css/display.html
Yes this is not compatible with IE8 and below but in my case is not necesary , for a cross-browser approach would user @Chris solutions that works very well.
3

You can try an approach from this site: http://phrogz.net/css/vertical-align/index.html

I've put together a quick fiddle showing this: http://jsfiddle.net/Mum5e/1/

Basically, by using absolute positioning with a top pointing to the half-way mark (50%) inside a relatively positioned element, we can get it to center. However, since we want the center of the link, not the top of the link, at the 50% mark, we change the link's top margin to offset it by half the height.

Note: by changing the link to absolute, we have to give the li a width, since the a will no longer force the li to take its width.

css:

ul { line-height: 85px; } li { float: left; height: 85px; width: 40px; line-height: 85px; border: 1px solid red; position: relative; } a { position: absolute; top: 50%; margin-top: -20px; height: 40px; line-height: 40px; width: 40px; display: block; text-align: center; border: 1px solid blue; } 

6 Comments

Was just typing the same answer :p anyways +1
What if the anchor needs to render on more than one line?
@rdiazv OP doesn't state any such thing, if it is like that than your solution goes better..
@rdiazv - it should work for more than one line, if the line-height of the a is reduced, see: jsfiddle.net/Mum5e/6
Simplicity is good - I was merely honoring the css provided in the OP (fixed height, fixed line-height); I believe that between our two answers, we provide both available approaches well
|
0

Well, iv'e played with it a bit and this worked for me :

a { height: 40px; width: 40px; margin-top:22.5px; border: 1px solid blue; text-decoration:none; } 

Comments

0

One solution is to give it a top margin of 21px (height of container, minus box height of element (border+height in this instance) divided by two).

Another solution, is to give it a height and line-height of 85px, effectively centering the text, but making the element taller.

At the end of the day, I'd probably go for rdiazv's solution, mainly because it's the simplest and easiest to maintain.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.