CSS的相对定位和绝对定位理解

今天我们来说说css的相对定位和绝对定位.

相对定位是依照元素本来的位置的改变,相对原来的位置的改变,position 必须配合left top才能看待效果.来看看代码.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  #test{
    font-size: 30px;
    border:1px solid black;
    text-align: center;
    font-family: fantasy;
    font-style: italic;
    width: 200px;
    height: 200px;
    line-height: 200px;
    color: rgb(222, 179, 50);
    background-color: rgb(255, 00, 00);
    z-index: 1000000;
  
  }
  #hello{
    width: 200px;
    height: 200px;
    font-size: 20px;
    border:1px dotted red;
    line-height: 200px;
    text-align: center;
    background-color: rgb(17, 34, 8);
    position: relative;
    left: 20px;
    top: 30px;
  }
  #world{
    width: 200px;
    height: 200px;
    border:1px solid red;
    background-color: rgb(252, 180, 87);
    text-align: center;
    line-height: 200px;
    font-size: 20px;
    position: absolute;
    top: 60px;
    left: 60px;
    
  }
  </style>
</head>
<body>
  <div id="test">我是Div块的文字1 </div>
  <div id="hello">我是Div块的文字2</div>
  <div id="world">我是Div块的文字3</div>
</body>
</html>

我们来看看程序的截图效果.

假如我们要想让div里面的文字剧中的话,那么父元素采用相对定位,子元素采用绝对定位的方式.表示该元素相对父元素的定位

猜你喜欢

转载自blog.csdn.net/hackerbaseing/article/details/89668224