您的位置:首页 > Web前端 > CSS

css相对定位绝对定位和内容居中详解

2015-11-13 23:18 701 查看
1,css定位知识

对于定位而言,只有父类采取了相对定位的设置,子类才能很好地使用绝对定位的设置
另外关于背景的定位 ,使用元素background-position:top left;
相对定位
定位元素是相对于原来没设置过的普通流的元素时来定位的,是相对于排在他之前的子元素而言的
绝对定位的子元素的位置是相对于父级元素的初始位置而言的,跟其他的子元素没有关系

标准模式是相对于html父级元素来说的

另外,绝对定位时,如果父级元素中添加一个相对定位position:relative那么在设置子元素的绝对定位时,子元素就已此为开始设置的参照位置(参照物)

另外对于position定位元素,绝对定位方式有两种absolute

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。
元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

fixed 生成绝对定位的元素,相对于浏览器窗口进行定位。
元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。


2,css定位内容居中实例

  <style type="text/css">

       .container {

           width: 200px;

           height: 200px;

           background-color: red;

           position: relative;

           margin: 0 auto;

       }

       .div1 {

           width: 100px;

           height: 120px;

           background-color: darkgreen;

           float: left;

           position: absolute;

           bottom: 0;

           text-align: center;

            line-height: 120px;

       }

       .div2 {

           width: 100px;

           height: 100px;

           background-color: darkgrey;

           position: absolute;

           margin-left: 100px;

           bottom: 0px;

           float: left;

       }

        .span1{

            position: relative;

            text-align: center;

            background-color: greenyellow;

      }

   </style>

</head>

<body>

<div class="container">

    <div class="div1"><span class="span1">13</span></div>

    <div class="div2">23</div>

</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: