Code Page 1/2 for vertical text center using CSS

Source: Internet
Author: User

How to set the CSS to vertically center the text in the object on the object is also a problem that binds many friends. There are many Code Are not compatible with browsers. I sorted out the relevant methods on the Internet and made some changes to be compatible with mainstream browsers.
The following describes the vertical center Method in different cases.
1. Vertical center of a line of text
Check out the following code: Copy code The Code is as follows: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> script home-vertical center of CSS </title>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function sel (ID) {Switch (ID) {Case "1": document. getelementbyid ("sub "). style. lineheight = "normal"; break; Case "2": document. getelementbyid ("sub "). style. lineheight = "20px"; break; Case "3": document. getelementbyid ("sub "). style. lineheight = "28px"; break ;}}
</SCRIPT>
<Style type = "text/CSS">
# All {
Width: 240px;
Padding: 10px;
Font-size: 12px;
Color: # FFF;
Background-color: # CCC;
}
# Sub {
Width: 230px;
Padding: 0 5px;
Height: 20px;
Overflow: hidden;
Background-color: # f90;
}
# Sel {
Margin-top: 5px;
}
Select {
Width: 260px;
}
</Style>
</Head>

<Body>
<Div id = "all">
<Div id = "sub"> a line of text is vertically centered. The object height is 20px </div>
</Div>
<Div id = "Sel"> <select onchange = "sel (this. value) "> <option value =" 1 "> default value, set the Row Height to normal </option> <option value =" 2 "> set the same row height and object height: 20px </option> <option value = "3"> set the height of the row height to a large object: 28px </option> </SELECT> </div>
</Body>
</Html>

Method: Line-Height: 20px. Set the same row height and object height.

Note: This setting is simple and compatible with various browsers and supports inline objects. However, for objects with a fixed height, only one row can be displayed. When there are two lines of text, if "overflow: hidden" is not set, the object will be opened.
2. Multi-line text and highly adaptive
See the following code:Xmlns = "http://www.w3.org/1999/xhtml">


The three lines of text are vertically centered. You can set the same top-padding-top and bottom-padding-bottom.Vertical center of three lines of textVertical center of two lines of textVertical center of a line of text

Related Article

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.