5. Center in full screen and center in full screen

Source: Internet
Author: User

5. Center in full screen and center in full screen

There are two ways to center block elements in full screen mode:

1. When the width and height of block elements are known:

. Css {position: absolute; height: block height; width: block width; top: 50%; margin-top:-block height/2; left: 50%; margin-left: -Block width/2 ;}

The following example shows that the picture of a dog is centered on the top, bottom, and left of the dog:

<body class="Site">    <div class="Media">            </div></body>

 

 body{         background: rebeccapurple;     }      .Media{          height: 100px;          width: 180px;          position: absolute;          top: 50%;          left: 50%;          margin-top: -50px;          margin-left: -90px;      }      .Media img{        width: 100%;         }

 

 

2. If you do not know the width and height of an element:

You can also use transform to change the Media class to the following.

.Media {    position: absolute;    top: 50%;    left: 50%;    transform: translate3d(-50%,-50%,0);}

 

3. Block center knowledge a). Use margin: 0 auto (position: relative and do not locate) to center. The position cannot be absolute during positioning. Cause: the margin: 0 auto attribute cannot be used because it has been absolutely located. Position: absolute; how to center a block?
  • Width is displayed in %. If the outer layer is 100%, the block is 80%, and the center method is left and right margin is 10%, that is, margin-left: 10%; margin-right: 10%;
  • Width is displayed in pixels. In the same method, the value is subtracted and divided by 2 to the value of the property around margin.
B) when using margin: 0 auto, the premise is that position is not absolute or fixed.

 

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.