Method One
Set the container's float mode to absolute positioning
Then determine the width of the container, such as the width of 500 to 300 layer
Then set the outer margin of the layer
CSS code to copy content to clipboard
- div{
- width:500px;
- height:300px;
- margin:-150px 0 0-250px;
- Position:absolute;
- left:50%;
- top:50%;
- Background-color: #000;
- }
Method Two
The parent element and child element float at the same time, then the parent element moves 50% relative to the left, then the child element moves 50% relative to the right, or the child element moves relative to the left-50%.
CSS Code copy content to clipboard
- <! DOCTYPE html>
- <title>demo</title>
- <meta charset= "Utf-8 "/>
- <style type=" Text/css "
- . p{
- position:relative;
- left:50%;
- Float:left;
- }
- . c{
- position:relative
- Float:left;
- rightright:50%;
- }
- </style>
- <div class= "P"
-
- </div>
- </body>
-