开发者

css center div not working

开发者 https://www.devze.com 2023-02-25 17:22 出处:网络
I want to center the speaker div, which is within the review div. i cannot seem to get it working. :(

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:cente开发者_如何学运维r;
}

This doesn't work :/


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!


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/)


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!


It work perfectly.

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

}

good luck


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

.speaker {
       margin: 0 auto;


try

.speaker p{
text-align:center;
}

or

.speaker {
 text-align:center;
 }


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)


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消