H5基础知识第七课时(CSS负边距的使用)

DIV居中:利用绝对定位加负边距

html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DIV居中title>
    <style>
        #div1{
            width: 300px;
            height: 300px;
            background-color: yellow;
            position: relative;
        }
        #div2{
            width: 200px;
            height: 200px;
            background-color: antiquewhite;
            position: absolute;
            left: 50%;
            margin-left: -100px;
            top:50%;
            margin-top: -100px;

        }
    style>
head>
<body>

<div id="div1">
    <div id="div2">

    div>
div>

body>
html>

你可能感兴趣的:(H5基础知识第七课时(CSS负边距的使用))