CSS:盒模型和position定位

盒模型

页面上显示的每个元素(包括内联元素)都可以看作一个盒子,即盒模型( box model )。请看Chrome DevTools 里的截图:

可以显而易见的看出盒模型由 4 部分组成。从内到外分别是:

content 内容 -> padding 内边距 -> border 边框 -> margin 外边距
例如规定一个元素:
<style>
    .example {
        width: 200px;
        padding: 10px;
        border: 5px solid #000;
        margin: 20px;
    }
</style>

这里还有两种特殊情况:

  • 无宽度 —— 绝对定位(position: absolute;) 元素
  • 无宽度 —— 浮动(float) 元素

它们在页面上的表现均不占据空间(脱离普通流,感觉像浮在页面上层一样,移动它们不影响其他元素的定位)。这就涉及到另外两个核心概念 position 和 float。

我的练习代码:

<style>

.box{
            width: 200px;
            height: 200px;
            /* border: 1px solid #999; */
            /* background-color: blue; */
            float: left;
            /* 外边距 */
            /* margin-top: 10px;
            margin-right: 10px;
            margin-bottom: 10px;
            margin-left: 10px; */
            margin: 100px 0px 0px 10px;

            /* 内边距 */
         /*padding-top: 10px;
            padding-right: 10px;
            padding-bottom: 10px;
            padding-left: 10px; */
            padding: 100px 50px 30px 10px;

            /* 边框 */
            border-top- 5px;
            border-top-style: solid;
            border-top-color: #999; 
            /* border-top: 5px solid #999; */

            /* border-right- 5px;
            border-right-style: solid;
            border-right-color: #999; */
            /* border-right: 5px solid #999; */
           /* 
            border-bottom- 5px;
            border-bottom-style: solid;
            border-bottom-color: #999; */
            /* border-bottom: 5px solid #999; */
            /* 
            border-left- 5px;
            border-left-style: solid;
            border-left-color: #999; */
            /* border-left: 5px solid #999; */

            border : 5px solid #999;
</style>

Position

css里经常用到它,使html元素在网页中精确定位

三种形式

  • 绝对定位(position: absolute)
  • 相对定位(position: relative)
  • 固定定位(position: fixed)

绝对定位

position:absolute;   (表示绝对定位)

这条语句的作用将元素从文档流中拖出来,然后使用left、right、top、bottom属性相对于其最接近的一个具有定位属性的父包含块进行绝对定位。如果不存在这样的包含块,则相对于body元素,即相对于浏览器窗口

/* 如下面代码可以实现div元素(以一张200x200图片为例)相对于浏览器窗口向右移动100px,向下移动50px */
<style>
div{
    width:200px;
    height:200px;
    position:absolute;
    left:100px;   /*移动到距离最左100px的地方*/
    top:50px;     /*移动到距离最顶20px的地方*/
}
</style>
...
<div><img src="img/right2.png"></div>

相对定位

position:relative;     (表示相对定位)

相对定位完成的过程是首先按static(float)方式生成一个元素(并且元素像层一样浮动了起来),然后相对于以前的位置移动

移动的方向和幅度由left、right、top、bottom属性确定,偏移前的位置保留不动。(站着茅坑不拉屎)

/*相对于以前位置向下移动50px,向右移动100px*/

<style>
#div1{
    width:200px;
    height:200px;
    border:2px red solid;

    position:relative;
    left:100px;
    top:50px;
}
</style>
...
<div id="div1"></div>

固定定位

fixed:表示固定定位,与absolute定位类型类似,但它的相对移动的坐标是视图(屏幕内的网页窗口)本身。由于视图本身是固定的,它不会随浏览器窗口的滚动条滚动而变化。

就像那些培训网站一打开右下角那个聊天窗口就是固定定位,害怕

position:fixed;     (表示固定定位)

但我们经常会把相对定位和绝对定位搭配使用:

Relative与Absolute组合使用

使用 position:absolute 可以实现被设置元素相对于浏览器(body)设置定位以后,

然后元素相对于其它元素(如他的上一层)进行定位,使用 position:relative 来帮忙搞定

<style>
#box1{
    width:200px;
    height:200px;
    position:relative;        
}

/*定位元素加入position:absolute,便可以使用top、bottom、left、right来进行偏移定位了。*/

#box2{
    position:absolute;
    top:20px;
    left:30px;     

</style>
...

<!-- 参照定位的元素必须是相对定位元素的前辈元素 -->

<div id="box1">爸爸
    <div id="box2">儿子
    </div>
</div>    
原文地址:https://www.cnblogs.com/kumata/p/9484691.html