CSS tutorials A variety of solutions that are centered horizontally and vertically
Vertical-align is a good attribute, but this attribute was initially for the cell of the table. Of course, this property does not work on block-level elements, but it works on inline elements such as spans (this time, the property is set to a negative element content to move down, positive values move up).
The following is a more professional, vertically centered, centralized solution. Recorded here for inspection.
1, single line vertically centered
The text is vertically centered in the layer Vertical-align property is not available. We have a more ingenious way here: the height of the set height is the same height as the line-height!
Example source code [www.jzread.com]
<div style= "line-height:500px;height:500;" >
2, the level of the center
Set div width Less than the width of the parent div, set margin:0 auto;
Example source code [www.jzread.com]
#parentdiv
{
width:500px;
}
#childdiv {
width:200px;
margin:0 Auto;
}
3, the level of text horizontally centered
In Childdiv CSS plus text-align:center;
Example source code [www.jzread.com]
#parentdiv
{
width:500px;
}
#childdiv {
width:200px;
margin:0 Auto;
Text-align:center;
}
4, the DIV layer vertically centered
example source code [www.jzread.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:absolute9; top:50%; "
<div
style= "background:blue;position:relative; Top:-50%;
www.jzread.com
</div>
</div>
</div>
</div>
5, the div layer is centered vertically horizontally, and the English line wraps
example source code [www.jzread.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:ABSOLUTE9; 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.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread. com
</div>
</div>
</div>
</DIV>
</div>
6, div vertical scrolling
Example source code [www.jzread.com]
<div
Style= "width:160px; height:260px; OVERFLOW-Y: scroll; BORDER:1PX solid; " >
Www.jzread.com
</div>
7, vertical center and use text-align Horizontal Center
example source code [www.jzread.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:absolute9;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.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.com
</div>
</div>
</div>
</div>
</div>
</DIV>
8, center vertically and center horizontally with margin
example source code [www.jzread.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:ABSOLUTE9; 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.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.comwww.jzread.com
</div>
</div>
</div>
</div>
</div>