5

I want to center the speaker div, which is within the review div. i cannot seem to get it working. :(

HTML:

<div class="review">
<div class="speaker">
<p>SPEAKER DIV</p>
</div>
</div>

CSS:

.speaker{
align:center;
}

This doesn't work :/

djdanster
  • 51
  • 1
  • 1
  • 2

8 Answers8

9

Give it a width and margin:0 auto;

<div class="review">
    <div class="speaker">
        <p>SPEAKER DIV</p>
    </div>
</div>

div.speaker{
    background-color:red;
    width:100px;
    margin:0 auto;
}

See it in action!

firefox1986
  • 1,602
  • 11
  • 9
8

There’s no such CSS property as align.

When you say you want to “center” the speaker div, what exactly do you mean?

You can center-align its text like this:

.speaker {
    text-align:center;
}

(See http://jsfiddle.net/pauldwaite/X7LN5/)

If, alternatively, you want the speaker div to only be as wide as its text, and be positioned in the center of the review div, then you’d need to use this:

.review {
    text-align:center;
}

.speaker {
    display:inline-block;
}

(See http://jsfiddle.net/wxha4/)

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
1

I'm about a year late to the party, but here goes:

In most browsers, this will work:

.speaker{
    margin: 0 auto;
}

However, in IE8 and below, the margin:auto will not work (IE8 only if there is no !DOCTYPE declaration. See W3Schools Horizontal-Align Tutorial)

In that case, you can use a combination of text-align: center and width to get the desired effect:

.review{
    text-align:center;
}
.speaker{
    text-align:left;
    width:200px;
    margin:0 auto;
}

The downside to this method is that you have to declare a width, or it won't be centered.

Good luck!

Zach Lesperance
  • 347
  • 2
  • 8
1

It work perfectly.

.speaker{ 
margin: 0 auto;
text-align:center;
width:100%;

}

good luck

Ritesh
  • 54
  • 4
1

Give the parent a text-align: center; Then you can move the child anywhere in the parent div

.speaker {
       margin: 0 auto;
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Skykid Felix
  • 177
  • 2
  • 14
0

try

.speaker p{
text-align:center;
}

or

.speaker {
 text-align:center;
 }
Headshota
  • 21,021
  • 11
  • 61
  • 82
0

Thats because that syntax does not exist. You need to center the div via the margin attribute.

.speaker{
    margin:0px auto 0px auto;
}

DEMO http://jsfiddle.net/t4kBj/

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
0

There is no align attribute in CSS. Set the horizontal margins to auto to centre a block. See Centring using CSS for more details (including work arounds for Internet Explorer issues)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335