The position attribute of CSS in the fixed Implementation div Center _javascript Tips

Source: Internet
Author: User

The Position property sets the positioning type of the element. This property defines the positioning mechanism used to establish the element layout. Any element can be positioned, but an absolute or fixed element generates a block-level box regardless of the type of element itself. The relative positioning element is offset from its default position in the normal stream.

Center up or down

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

If only need to center, then put bottom:0; or top:0; You can delete it.

If you only need to center up and down, then put the left:0; or right:0; Can

The following is a DIV element centered in the browser window

In fact, the implementation of this effect is not complicated, using CSS position positioning can be easily done. Let's take a look at the code:

<style type= "Text/css" >
dialog{
 position:fixed;
 _position:absolute;
 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;
} 
</style>
<div class= "dialog" > I was in the middle of the window, hehe! </div>

The tips for setting are all here:

. 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;
Setting up left:50% and top:50%;
Set margin:-(div offsetwidth/2) 0 0-(Div's OFFSETHEIGHT/2)

Effect chart

The above content is small to share the CSS in the position attribute of the fixed implementation div centered all the narration, I hope you like.

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.