网站首页 > 知识剖析 正文
用position属性实现绝对定位
<html>
<head>
<title>position属性</title>
<style type="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1px dashed #000000;
width:100%;
height:100%;
}
#block{
background-color:#fff0ac;
border:1px dashed #000000;
padding:10px;
position:absolute; /* absolute绝对定位 */
left:20px; /* 块的左边框离页面左边界20px */
top:40px; /* 块的上边框离页面上边界40px */
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block">absolute</div>
</div>
</body>
</html>
以上的代码我们可以看出父块#father没有设置position属性,而子块#block采用的是绝对定位,经过测试发现子块#block参照浏览窗口左上角
为原点,子块左边框相对页面<body>左边的距离为20px,子块的上边框相对页面<body>上面的距离为40px
为父块这是position属性
#father{
background-color:#a0c8ff;
border:1px dashed #000000;
position:relative;
width:100%;
height:100%;
}
我们发现子块的参照物为父块的#father,距左侧20px,距上端40px
注意top、right、bottom、left这4个CSS属性,它们都是配合position属性使用的,表示的是块的各个边界离页面边框(position设置为absolute时)
或者原来的位置(position设置为relative)的距离。只有当position属性设置为absolute或者relative时才能生效;
用position属性实现相对定位
<html>
<head>
<title>position属性</title>
<style type="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1px dashed #000000;
width:100%; height:100%;
padding:5px;
}
#block1{
background-color:#fff0ac;
border:1px dashed #000000;
padding:10px;
position:relative; /* relative相对定位 */
left:15px; /* 子块的左边框距离它原来的位置15px */
top:10%;
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block1">relative</div>
</div>
</body>
</html>
我们可以看到字块的左边框相对于其父块的左边框(它原来所在的位置)距离为15px,上边框也是一样的道理,为10%;
理解"它原来的位置":子块和父块原先的位置的是一致的(因为父块包含字块,视觉上看是几乎重叠的)
此时子块的宽度依然是未移动前的宽度,撑满未移动前父块的内容。只是由于向右移动了,因此右边框超出了父块。
如果希望子块的宽度仅仅为其内容加上自己的padding值,可以将它的float属性设置为left,或者指定其宽度width;
案例: 文本在图片上显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 200px;
margin: 100px auto;
border: 1px solid red;
position: relative;
}
img{
vertical-align: top; /* 解决图文间歇问题 */
}
p{
color: #fff;
width: 200px;
height: 40px;
font-size: 14px;
line-height: 40px;
background-color: rgba(0, 0, 0, 0.5);
text-align: center;
position: absolute;
left: 0;
/* top: 160px; */
bottom: 0;
}
</style>
</head>
<body>
<div>
<img src="img/tb.webp" alt="">
<p>箱子箱子箱子箱子箱子</p>
</div>
</body>
</html>
- 上一篇: 菜鸟学习记:第二十五天 菜鸟学习记
- 下一篇: 谈谈工作中常用的设计模式 工作设计模型
猜你喜欢
- 2024-11-12 原来隐藏一个DOM元素可以有这么多种方式,最后一种你肯定不知道
- 2024-11-12 你知道什么是BFC么 你知道什么是bfc么英语
- 2024-11-12 为什么我写的z-index不生效? z index无效
- 2024-11-12 开发人员在编写 HTML 和 CSS 时最常犯的六大错误
- 2024-11-12 css中如何让div水平居中(上) 怎么让div水平居中
- 2024-11-12 CSS 12个趣味小技巧大公开 | 原力计划
- 2024-11-12 谈谈工作中常用的设计模式 工作设计模型
- 2024-11-12 菜鸟学习记:第二十五天 菜鸟学习记
- 2024-11-12 响应式网页中的高度设计,你认真的吗?
- 2024-11-12 前端基础CSS为什么要用定位?定位的4中分类是什么?
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)