177

I am trying to center the two links 'view website' and 'view project' inside the surrounding div. Can someone point out what I need to do to make this work?

JS Fiddle: http://jsfiddle.net/F6R9C/

HTML

<div>
  <span>
    <a href="#" target="_blank">Visit website</a>
    <a href="#">View project</a>
  </span>
</div>

CSS

div { background:red;overflow:hidden }

span a {
    background:#222;
    color:#fff;
    display:block;
    float:left;
    margin:10px 10px 0 0;
    padding:5px 10px
}
J82
  • 8,267
  • 23
  • 58
  • 87

7 Answers7

200

Another option would be to give the span display: table; and center it via margin: 0 auto;

span {
    display: table;
    margin: 0 auto;
}
Jesper
  • 1,007
  • 7
  • 24
user1721135
  • 6,864
  • 8
  • 34
  • 63
167

One option is to give the <a> a display of inline-block and then apply text-align: center; on the containing block (remove the float as well):

div { 
    background: red;
    overflow: hidden; 
    text-align: center;
}

span a {
    background: #222;
    color: #fff;
    display: inline-block;
    /* float:left;  remove */
    margin: 10px 10px 0 0;
    padding: 5px 10px
}

http://jsfiddle.net/Adrift/cePe3/

Jase
  • 1,369
  • 2
  • 12
  • 24
Adrift
  • 58,167
  • 12
  • 92
  • 90
  • 1
    This did the trick for me in a moderately complex theme i needed to hack a bit. The link to JSFiddle further up was awesome to allow me to test. I edited this entry to include to change "overflow:hidden " to "overflow:hidden;" – drew.. Nov 10 '14 at 16:03
  • 1
    Looks like a very clean solution. – Jim Aho Sep 23 '15 at 07:08
  • See Ahmed Bahtity answer below, style="text-align:center", it's a lot more concise. – Wallace Howery May 10 '18 at 15:46
19
<div style="text-align:center">
    <span>Short text</span><br />
    <span>This is long text</span>
</div>
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
Ahmed Bahtity
  • 536
  • 4
  • 9
11

Applying inline-block to the element that is to be centered and applying text-align:center to the parent block did the trick for me.

Works even on <span> tags.

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
Talha Imam
  • 1,046
  • 1
  • 20
  • 22
3

Spans can get a bit tricky to deal with. if you set the width of teach span you can use

margin: 0 auto;

to center them, but they then end up on different lines. I would suggest trying a different approach to your structure.

Here is the jsfiddle I cam e up with off the top of my head: jsFiddle

EDIT:

Adrift's answer is the easiest solution :)

David Ziemann
  • 960
  • 1
  • 8
  • 22
1

only css div you can center content

div{
       display:table;
       margin:0 auto;
    }

http://jsfiddle.net/4q2r69te/1/

Đọc truyện hay
  • 1,913
  • 21
  • 17
0

I assume you want to center them on one line and not on two separate lines based on your fiddle. If that is the case, try the following css:

 div { background:red;
      overflow:hidden;
}
span { display:block;
       margin:0 auto;
       width:200px;
}
span a { padding:5px 10px;
         color:#fff;
         background:#222;
}

I removed the float since you want to center it, and then made the span surrounding the links centered by adding margin:0 auto to them. Finally, I added a static width to the span. This centers the links on one line within the red div.

Duffmaster33
  • 1,160
  • 9
  • 16