dispaly属性,position属性

本文介绍了CSS中不同定位方式的使用方法,包括绝对定位、相对定位和固定定位的特点及应用场景,并提供了自适应布局和居中的实现技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

position:absolute;绝对定位相对于父元素(父元素设为relative)

position:relative;相对定位相对于自己

position:fixed;固定定位相对于浏览器

display:inline;行内元素,不换行,text-align:center不起作用

display:block;块级元素,换行,能设置宽高

display:inline-block;能设置宽高,不换行,text-align:center起作用

注意,如果父元素不设置高度的话,子元素浮动,或者绝对定位的话,撑不开父元素的高度,最终父元素的高度为0

自适应居中

<div class="title">
<span class="message">等待钣金</span>
<span class="num">12</span>
</div>
.content .title span{position: relative;top:12px;height:15px;line-height: 15px;}
.content .title span:last-child{display:inline-block;font-size: 14px;color:white;border-radius: 15px; background-color:#c19b74;width:27px;}

上下左右自适应

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#test{
position: relative;
width:100px;
height:100px;
background: green;
}
img{
position: absolute;
margin-top: auto;
margin-bottom : auto;
top: 0;
bottom: 0;
height:100%;
}
</style>
</head>
<body>
<div id="test">
<img>
</div>
</body>
</html>
只是对img有用

转载于:https://www.cnblogs.com/windseek/p/6008833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值