Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center align anchor within a `li` element?

Tags:

css

alignment

So I have a simple list thats set out like below

<ul class='my-videos'>
   <li>
       <a class='show-more' href='' title=''>Show More</a>
   </li>
<ul>

I am trying to get the .show-more to be center aligned. I have got this far

ul.my-videos li .show-more
{
   display:inline-block;
   margin:0 auto;
}

Now this doesn't work. I have setup a JSFiddle here http://jsfiddle.net/6HHKf/

Any ideas on this?

PS I want to keep the anchor as inline or inline-block so that the width isn't 100%

UPDATE There are other elements in the li, so text-align is out of the answer

like image 513
Sean H Jenkins Avatar asked Dec 30 '11 13:12

Sean H Jenkins


2 Answers

ul.my-videos li .show-more {
    margin:0 auto;
    border:#aaa 1px solid;
    width: 100px;
    display: block;
}

if you want center an element width margin: 0 auto you need to set the width

and also, you need display:block

check jsfiddle here: http://jsfiddle.net/6HHKf/5/

like image 88
Ya Zhuang Avatar answered Oct 30 '22 03:10

Ya Zhuang


Simply set the CSS for the list item to center align the text.

.my-videos li { text-align: center; }
like image 4
Scott Avatar answered Oct 30 '22 01:10

Scott