To center the div layer horizontally with CSS

Source: Internet
Author: User
Tags split

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 Element Vertical 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:expre ssion ((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="; 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>
<td height=1 width=178> material network </td>
<td Align=middle onclick= "l1.style.visibility= ' hidden '"
style= "Cursor:hand" WIDTH=21>Х</TD></TR>
<TR Valign=top>
<td colspan=2
height=91> Welcome to this site!<br><br> site domain name: <br><a href= "http://www. 3lian . com "target=" _blank ">http://www.3lian .com</a>
</td></tr></table></div>
<script LANGUAGE= The "JavaScript"
Function Moveit ()//is used to place 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//Moveit ()
Window.onresize=moveit;////When resizing the browser executes Moveit ()
Window.onscroll=moveit; Executes Moveit ()
</script>

When pulling the scroll bar

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! There is also a drawback to this approach is that you

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.