Web page Production Summary: Use CSS to make div layer horizontally centered

Source: Internet
Author: User
Tags expression split window domain name
css| Web page

One, with CSS to make the div layer horizontally centered

Add the following properties to the DIV layer that needs to be centered horizontally:

Margin-left:auto;
Margin-right:auto;

This is already centered in FF, but in IE is not centered!

The problem is not in CSS but in the XHTML page itself.
You need to add such a code to make the above settings effective:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

How do I make a div centered

The main style definitions are as follows:

body {text-align:center;}
#center {Margin-right:auto; Margin-left:auto; }

Description
First, the parent element defines text-align:center, which means that the content within the parent element is centered, and for IE this setting is OK. But you can't center in Mozilla. The solution is to add "Margin-right:auto" when the child element definition is set; Margin-left:auto; ”
What needs to be explained is that if you want to use this method to make the entire page centered, it is recommended not to be nested in a div, you can split out a number of Div, as long as in each split div defined margin-right:auto; Margin-left:auto; It's OK.
How do I get a picture centered vertically in a div

Use the background method. Example:

body{Background:url (background image path) #FFF No-repeat Center;

The key is the final center, which defines the location of the picture. It can also be written as "top left" or "bottom right", or you can write the value "50 30" directly.

How do I align text vertically in a div

The result is the text, you can not use the background method, the way to increase the line spacing to achieve vertical center, complete code as follows:

<style>
body{Text-align:center;}
#center {Margin-right:auto;
Margin-left:auto;
height:200px;
Background: #F00;
width:400px;
Vertical-align:middle;
line-height:200px;
}
</style>
<body>
<div id= "center" ><p>test content</p></div>
</body>

Description
Vertical-align:middle indicates that the line is centered vertically, we increase the spacing to be as high as the entire Div line-height:200px, and then insert the text and center it vertically.
Css+div Control page Elements Vertically center code global and area vertically

<style type= "Text/css" media=screen>
Body
{
Text-align:center;
}
#a
{
width:200px;
height:400px;
Background: #000;
}
#b
{
Margin-top:expression ((A.CLIENTHEIGHT-50)/2);
width:50px;
height:50px;
Background: #FFF;
}
#c
{
Position:absolute;
Left:expression ((BODY.CLIENTWIDTH-50)/2);
Top:expression ((BODY.CLIENTHEIGHT-50)/2);
width:50px;
height:50px;
Background: #F00;
}
</style>
<div id= "a" >
<div id= "B" ></div>
</div>
<div id= "C" ></div>

Another method:

<div style= "Background:blue;position:absolute;left:expression ((BODY.CLIENTWIDTH-50)/2); Top:expression ( BODY.CLIENTHEIGHT-50)/2); width:50;height:50 "></div>

***************************************

Second, use JS to center the layer

Add the following code to the <body> area

<div id=l1 style= "HEIGHT:107PX; left:341px; Position:absolute; top:585px; width:205px; Z-index:1 ">
<table bgcolor= #ffffff border=1 bordercolordark= #ffffff bordercolorlight= #000000 cellpadding=0 cellspacing=0 width = "100%" >
<TR>
&LT;TD height=1 width=178> Web Teaching Network </TD>
&LT;TD Align=middle
Style= "Cursor:hand" width=21>х</td></tr>
<tr valign=top>
&LT;TD colspan=2
Height=91> Welcome to this site!<br><br> site domain name: <br><a href= "http://www.webjx.com" target= "_blank" >http ://www.webjx.com</a>
</TD></TR></TABLE></DIV>
<script language= "JavaScript" >
function Moveit ()//is used to put the layer in the middle of the browser
{
L1.style.left= ((Document.body.offsetwidth-parsefloat (L1.style.width))/2 +document.body.scrollleft;
L1.style.top= ((Document.body.offsetheight-parsefloat (L1.style.height))/2 +document.body.scrolltop;
}
Window.onload=moveit; Perform Moveit () when the network surface is open
Window.onresize=moveit; Perform Moveit when resizing browser ()
Window.onscroll=moveit; Executes Moveit when pulling scroll bars ()
</SCRIPT>

Third, centering with positioning method

<div style= "background: #f00; width:740px; height:340px; left:50%; margin:0px 0 0-370px; Position:absolute; " >

Explanation: This method was only known before, but once a friend asked me the principle of the method, then I did not answer, a few days ago read an article suddenly understand its principle! Before my understanding is first left boundary -370px, then is wants position question, therefore always thinks impassability. In fact, we can go back and try to locate the back border, so it's easy to understand. Position:absolute, it is positioned on the browser, left:50%, right in the middle of the position, that is, the left edge of this div block in the middle of the viewer, in the join style margin-left:-370px, this 370 is calculated, It happens to be half of div740, we let Div go back 370, so the Div line and the center of the browser line coincide so see the div is always in the middle! The disadvantage of this approach is that if your browser is less than width, it is not a pleasant thing for you!
Process please look at the following figure:

Add the difference between left and Margin-left:
Left this property is only available when the object's positioning (position) property is set. Otherwise, this property setting is ignored.



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.