How to center the div horizontally in CSS

Source: Internet
Author: User

Today, using CSS to meet a very difficult problem, the div itself does not define its own center of the property, many online methods are introduced with the superior text-align:center and then nested a layer of div to solve the problem. Text-align:center; This property is not the center of the Div, is to let the content in the div is centered, when we want to let a container of div horizontal Center In fact the method is simple simply define the margin, that is margin:0 auto; then your div is centered horizontally. The following code example: Box2 horizontally centered in Box1: <! DOCTYPE html>
<meta charset= "Utf-8"/>
<title>div Center Problem </title>
<style type= "Text/css" >
. box1{
width:1000px;
height:700px;
Background-color:blue;
}
. box2{
width:666px;
height:500px;
Background-color: #ccc;
margin:0 Auto;
}
</style>
<body>
<div class= "Box1" >
<div class= "Box2" ></div>
</div>
</body>

How to center the div horizontally in CSS

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.