Center of Div in HTML

Source: Internet
Author: User

Background: The div in HTML can be said to be a layout artifact, but sometimes there is no such one step attribute to define a div to be centered. So you need to manually write CSS to debug

Code:

<style type= "Text/css" >
<!--
Div {
Position:absolute;
top:50%;
left:50%;
margin:-150px 0 0-200px;
width:400px;
height:300px;
border:1px solid #008800;
}
-
</style>
<div> make a layer vertically centered in the browser window </div>

In fact, the idea of the solution is this: the first need position:absolute; absolute positioning. Instead of the layer's anchor point, use the external patch margin negative method. The negative size is the height of the layer itself divided by two.

such as: A layer width is 400, the height is 300. Use absolute positioning distance between upper and left is set to 50%. And the value of Margin-top is-150. The value of Margin-left is-200. This allows us to write a style that is vertically centered on the browser.

Note: Above is generally used for vertical center with the browser, want to solve the div vertical Center and the outer Div, only need to add position:relative to the outer div; remember, the outer div sets the height and width .
The code section is posted from the network. The idea is very good, worth studying and drawing lessons from.

Center of Div in HTML

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.