CSS instances: multiple solutions for horizontal and vertical centering

Source: Internet
Author: User

1, single-line vertical center

Text is centered vertically in the Layer Vertical-align property is not available. Here's a neat way to do this: set height to the same height as Line-height!

Example Source Code [Www.52css.com]<div style= "line-height:500px;height:500;" ></div>

2. Horizontal center of Layer

Set the width of the div to be less than the width of the parent div, set margin:0 auto, and let the div center.

Example Source Code [www.52css.com] #parentdiv {width: 500px;}  {width: 200px;  Margin:0 auto;}

3. The text in the layer is centered horizontally

In the childdiv CSS plus text-align:center;

Example Source Code [www.52css.com] #parentdiv {width: 500px;}  {width: 200px;  Margin:0 Auto; text-align:center;}

4, DIV layer Vertical Center

Example Source Code [www.52css.com]<div style= "Width:275px;height:375px;border:solid red;" > <div style= "background:green;height:375px; width:275px; position:relative; Display:table-cell; Vertical-align:middle; " > <div style= "background:red;position:static;position:absolute\9; top:50%; " >        <div style= "background:blue;position:relative; top:-50%;" >                     www.52css.com         </div>      </div>   </div></div>

5, div layer Vertical Horizontal center, English super long line-wrapping

Example Source Code [www.52css.com]<div style= "Float:left;width:275px;height:375px;border:solid red;" >    <div style= "height:375px; width:275px; position:relative; display:table-cell; vertical-align:middle;" >          <div style= "position:static;position:absolute\9; top:50%;" >              <div style= "position:relative; top: -50%; text-align:center;" >                  <div style= "width:85px; Word-wrap:break-word; table-layout:fixed;word-break:break-all;margin:0 auto; " >            www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com               </ div>              </div>          </div>    </div></div>

6, Div vertical scrolling

Example Source Code [Www.52css.com]<div style= "width:160px; height:260px; OVERFLOW-Y: scroll; BORDER:1PX solid; " ></div>

7. Center vertically and horizontally using text-align

Example Source Code [www.52css.com]<div style= "Float:left;width:275px;height:375px;border:solid red;" > <div style= "height:375px; width:275px; position:relative; Display:table-cell; Vertical-align:middle; " > <div style= "position:static;position:absolute\9;top:50%;" >           <div style= "position:relative; top: -50%; text-align:center;" > <div style= "width:275px;" >                      <div style= "width:160px; Word-wrap:break-word; Table-layout:fixed;word-break:break-all;text-align:left; " >            www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com             &l t;/div>              </div>           </div> </div> </div></div>

8. Center vertically and use margin horizontally

Example Source Code [www.52css.com]<div style= "Float:left;width:275px;height:375px;border:solid red;" > <div style= "height:375px; width:275px; position:relative; Display:table-cell; Vertical-align:middle; " > <div style= "position:static;position:absolute\9; top:50%; " >           <div style= "position:relative; top:-50%; ">           <div style=" margin:0 auto;width:160px; Word-wrap:break-word; Table-layout:fixed;word-break:break-all; "     >           Www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com              </div>           </div>       </div>   </div></div>

CSS instances: multiple solutions for horizontal and vertical centering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.