The position attribute in CSS: fixed implement div center _ javascript skills

Source: Internet
Author: User
This article mainly introduces the position attribute fixed in CSS to implement div Center-related information. if you need it, you can refer to the position attribute to specify the positioning type of the element. This attribute defines the positioning mechanism used to establish the element layout. Any element can be located, but an absolute or fixed element will generate a block-level box regardless of the element type. The relative positioning element is offset from its default position in the normal stream.

Center between top, bottom, and left

p{  position:fixed;  margin:auto;  left:0;  right:0;  top:0;  bottom:0;  width:200px;  height:150px;}

If you only need to center in the left-right corner, delete bottom: 0 or top: 0.

If you only need to center up and down, set left: 0; or right: 0 ;.

The following is a DIV element that is centered in the browser window.

In fact, it is not complicated to achieve this effect. you can easily locate the position in CSS. Let's look at the code:

I am in the center of the window!

The setting tips are as follows:

.dialog{ position: fixed; _position:absolute; /* hack for IE6 */ z-index:1; top: 50%; left: 50%; margin: -141px 0 0 -201px; width: 400px; height:280px; border:1px solid #CCC; line-height: 280px; text-align:center; font-size: 14px; background-color:#F4F4F4; overflow:hidden;}

Set position: fixed; _ position: absolute;
Set left: 50% and top: 50%;
Set margin:-(DIV offsetWidth/2) 0 0-(DIV offsetHeight/2)

The above content is a full description of the position attribute fixed in CSS to realize p center. I hope you will like it.

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.