1

I have a div containing a span and I want the span to vertically and horizontally align to the center of my div.

Here's the fiddle : http://jsfiddle.net/RhNc2/1/

I've try margin:auto on the span and the vertical-align on the div, but it's not working

EDIT : My div and my span don't have a fixed height, it depends of the content, i've put it fixed on the fiddle just to show you

GregM
  • 2,634
  • 3
  • 22
  • 37
  • possible duplicate of [How to vertically center a inside a div?](http://stackoverflow.com/questions/4357315/how-to-vertically-center-a-span-inside-a-div) – mercator Nov 10 '11 at 20:58
  • Amazing. I have one of the only two working solutions, yet I have 3 downvotes? Go figure :) I'm tempted to just delete it and get my "peer pressure" badge. – James Johnson Nov 10 '11 at 21:37
  • @mercator not sure for the duplicate, because the solution in the link propose a top:50% and here they tell in the comment it's not a great technique. And I made a little edit. – GregM Nov 10 '11 at 21:50
  • @GregM: There are a lot of these questions on here, but there is not accepted way as far as I know. I don't consider it a duplicate. – James Johnson Nov 10 '11 at 21:55
  • possible duplicate of [CSS: Vertically align div when no fixed size of the div is known](http://stackoverflow.com/questions/7206640/css-vertically-align-div-when-no-fixed-size-of-the-div-is-known) – NGLN Nov 10 '11 at 22:12
  • @NGLN It's not a duplicate the solution that they are giving it's not working ... the line-height solution is a dirty trick that is not working everywhere – GregM Nov 10 '11 at 22:48
  • @GregM That's only the accepted answer you're referring to, but there are ten more! Especially the answer of [kizu](http://stackoverflow.com/questions/7206640/css-vertically-align-div-when-no-fixed-size-of-the-div-is-known/7256012#7256012) promisses to be of help to you. – NGLN Nov 10 '11 at 22:53
  • @NGLN Not sure the inline-block working in older browser – GregM Nov 10 '11 at 22:55

5 Answers5

-1

Add this to the div CSS:

display:table-cell; text-align:center

working fiddle is here: http://jsfiddle.net/sdoking/DCT85/

CSS:

#myDiv {
border:1px solid black;
height:50px;
width:200px;
vertical-align:middle;
display:table-cell; 
text-align:center
}

#mySpan {
width:100%;
border:thin blue solid
}

Borders are for clarity :)

sdo
  • 652
  • 3
  • 13
  • This is fine but not compatible with some browsers (some popular versions of IE, for example). You can see more about vertical centering here: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html – David Brainer Nov 10 '11 at 20:58
  • This is in addition to his current CSS which includes the vertical-align:middle already - see the fiddle – sdo Nov 10 '11 at 21:23
  • You've lucked out with the downvotes, but this latest edit treads dangerously close to my first solution. – James Johnson Nov 10 '11 at 21:31
  • Well.. except I posted my solution 3 minutes before yours? All I did in my edit was move the code out of the fiddle and into the post. – sdo Nov 10 '11 at 21:34
  • "**but this latest edit** treads dangerously close to my first solution." – James Johnson Nov 10 '11 at 21:36
-2

Vertical alignment is a tricky business, and I don't know if there's one tried-and-true way. The most reliable technique available in the last couple of years is to use a CSS table layout. The only downside to this approach is that it may not work on outdated browsers. Still, in my experience this is probably the best overall solution. See my example below:

<style type="text/css">
    #container {    
        display:table;    
        border-collapse:collapse;  
        height:200px;  
        width:100%;
        border:1px solid #000;
    }         
    #layout {    
        display:table-row;   
    }           
    #content {    
        display:table-cell;  
        text-align:center; 
        vertical-align:middle;    
    }           
</style>

<div id="container">    
    <div id="layout">    
        <div id="content"> 
            Hello world! 
        </div>     
    </div>   
</div> 

Here's a jsFiddle: http://jsfiddle.net/aGKfd/2/

There's another technique, but it's not as foolproof as the above technique. It involves two containers, with the outer container's position set to relative and the inner set to absolute. Using absolute positioning on the inner container you can get close, but it requires some tweaking to get it just right:

<style type="text/css">
    #vertical{ 
        position:absolute; 
        top:50%;     
        left:0; 
        width:100%; 
        text-align:center;
    } 
    #container {
        position:relative;
        height:200px;
        border:1px solid #000;
    }
</style>         
<div id="container"> 
    <div id="vertical"> 
        Hello world! 
    </div>               
</div> 

Here's a jsFiddle: http://jsfiddle.net/6SWPe/

James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • I didn't downvote you, but your second example only vertically centers the top of the `#vertical` box, not the box as a whole. – Ben Nov 10 '11 at 21:07
  • -1. top 50% - not center. you need to - margin-top = height /2 – korywka Nov 10 '11 at 21:09
  • 1
    so dont downvote my solution if YOUR solution ALSO not perfect ;) – korywka Nov 10 '11 at 21:20
  • There are no perfect solutions for this particular problem, but my top solution is about as good as it gets. – James Johnson Nov 10 '11 at 21:21
  • @TarasNeporozhniy: You have to remember that this is not about you or me, it's about the asker. I didn't downvote your answer because I don't like you. I downvoted it because `line-height` is not a good solution. When you're talking about content like "Hello world" and what not it's easy to be fooled, but as soon as real content enters the equation, `line-height` drops a huge bomb. – James Johnson Nov 10 '11 at 21:22
  • @TarasNeporozhniy: The difference between our solutions is that mine will not break the whole design, whereas yours will. That has to be priority #1 when answering a CSS question. – James Johnson Nov 10 '11 at 21:25
-2

use line-height = height: http://jsfiddle.net/RhNc2/8/

korywka
  • 7,537
  • 2
  • 26
  • 48
-3

Here it is

#myDiv{
    border:1px solid black;
    height:50px;
    width:200px;
}

#mySpan{
    display:block;
    text-align:center;
    line-height:50px;
}

And the jsFiddle: http://jsfiddle.net/Simo990/RhNc2/9/

Edit: since your div and span height depends of the content, my solution will not work, because it needs fixed height and only one row of text. Just look for a solution with position:absolute.

Simone
  • 20,302
  • 14
  • 79
  • 103
-3

You can also just apply these styles to the containing <div>. The line-height solution assumes you only need one line of text to be centered though.

#myDiv{
    border:1px solid black;
    height:50px;
    width:200px;
    text-align:center;
    line-height:50px;
}
Ben
  • 10,056
  • 5
  • 41
  • 42
  • One line may be all that's needed – Ben Nov 10 '11 at 21:10
  • That's a dangerous assumption to make. There are many variables that could effect this solution: changes in resolution, shrinking the window size, etc. – James Johnson Nov 10 '11 at 21:15
  • Not when you're hard-coding your dimensions, as the OP did in his fiddle. – Ben Nov 10 '11 at 21:17
  • 1
    In general, `line-height` is not a good solution for vertical alignment. I know that the options are limited, but when `line-height` breaks it *really* breaks. – James Johnson Nov 10 '11 at 21:20