CSS中怎么实现定位布局,哪些知识点要掌握
Admin 2022-05-21 群英技术资讯 1227 次浏览
这篇文章给大家分享的是“CSS中怎么实现定位布局,哪些知识点要掌握”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。
相对定位:盒子可以根据自己原来的位置进行位置调整(通过位置描述词实现)。
位置描述词:
left: 向右移动; right 向左移动;top 向下移动;bottom 向上移动
(当里面值为负数的时候,往相反方向移动)
举个例子:
原来:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>相对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
p {
width: 500px;
height: 500px;
border: 1px solid #000;
margin: 50px auto;
}
p {
width: 100px;
height: 100px;
background-color: lightblue;
position: relative;
top: 50px;
left: 50px;
}
</style></head><body>
<p>
<p></p>
</p></body></html>
将 p 设置成相对定位:
p {
width: 100px;
height: 100px;
background-color: lightblue;
position: relative;
top: 50px;
left: 50px;}
性质
用途
举个例子:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>相对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
nav {
width: 780px;
height: 50px;
margin: 40px auto;
}
nav ul {
list-style: none;
}
nav ul li {
float: left;
width: 156px;
height: 50px;
line-height: 50px;
text-align: center;
}
nav ul li a {
display: block;
width: 156px;
height: 50px;
background-color: lightcyan;
color: #000;
text-decoration: none;
}
nav ul li a:hover {
border-top: 3px solid red;
}
</style></head><body>
<nav>
<ul>
<li>
<a href="#">导航一</a>
</li>
<li>
<a href="#">导航二</a>
</li>
<li>
<a href="#">导航三</a>
</li>
<li>
<a href="#">导航四</a>
</li>
<li>
<a href="#">导航五</a>
</li>
</ul>
</nav></body></html>这个时候效果是这样:
会发现鼠标悬浮在上面的时候,导航那一块区域都会下沉
我们给它设置了相对定位并微调之后:
nav ul li a:hover {
border-top: 3px solid red;
position: relative;
top: -3px;}
这样就可以解决刚刚的问题了
绝对定位:盒子以坐标进行位置描述,拥有自己绝对位置。
绝对定位的参考盒子:
绝对定位的盒子会以自己的祖先元素中,离自己最近的拥有定位属性的盒子,当做基准点。
这个盒子通常是相对定位的,所以也被称作 “子绝父相”。
位置描述词:
left:到左边的距离;right:到右边的距离;top:到上边的距离;bottom:到下边的距离
举个例子:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>绝对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: absolute;
width: 500px;
height: 300px;
left: 200px;
top: 100px;
background-color: antiquewhite;
}
</style></head><body>
<p class="box">
</p></body></html>绝对定位的盒子垂直居中:
.box {
position: absolute;
top: 50%;
margin-top: -自己高度一半;}绝对定位的盒子水平居中:
.box {
position: absolute;
left: 50%;
margin-left: -自己宽度一半;}
设置绝对定位元素的压叠顺序.
是一个没有单位的正整数,数值大的能够压住数值小的(即数值大的显示在上层)
举个例子:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>绝对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.box1 {
width: 300px;
height: 300px;
position: absolute;
left: 100px;
top: 100px;
background-color: antiquewhite;
}
.box2 {
width: 300px;
height: 300px;
position: absolute;
left: 200px;
top: 200px;
background-color: lightblue;
}
</style></head><body>
<p class="box1"></p>
<p class="box2"></p></body></html>此时效果如下:
这个时候我们想让box1显示在上层,就设置一个z-index 属性。
.box1 {
width: 300px;
height: 300px;
position: absolute;
left: 100px;
top: 100px;
background-color: antiquewhite;
z-index: 100;}.box2 {
width: 300px;
height: 300px;
position: absolute;
left: 200px;
top: 200px;
background-color: lightblue;
z-index: 1;}看看效果:
绝对定位用来“压盖”,“遮罩”的效果
可以结合 CSS 精灵使用
可以结合 JS 实现一些动画
固定定位:不管页面如何滚动,它永远以页面为参考点,固定在那里。
位置描述词:
left:到左边的距离;right:到右边的距离;top:到上边的距离;bottom:到下边的距离
.box {
position: fixed;
top: 100px;
left: 100px;}可以用来实现一些元素要一直浮现在当前窗口前,比如浏览一个页面时的返回顶部按钮,会一直出现在当前页面的某个位置
举个例子:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>固定定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: fixed;
bottom: 20px;
right: 20px;
width: 40px;
height: 40px;
text-align: center;
line-height: 40px;
border-radius: 50%;
background-color: rgba(78, 209, 226, 0.5);
cursor: pointer;
font-size: 24px;
}
</style></head><body>
<a class="box">^</a>
<p>
<img src="https://dummyimage.com/600x400/00bcd4/fff" alt="">
</p>
<p>
<img src="https://dummyimage.com/600x400/00bcd4/fff" alt="">
</p>
<p>
<img src="https://dummyimage.com/600x400/00bcd4/fff" alt="">
</p></body></html>效果如下:
当页面到下方时,右下角返回顶部的按钮位置不变。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了纯CSS实现单一div的正多边形变换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在css中,可以利用“background-repeat”属性让背景图片不平铺,该属性用于设置背景图片的平铺模式,只需要给元素添加“background-repeat:no-repeat;”样式,即可让背景图片不平铺显示。
三栏布局在实际的开发十分常见,比如淘宝网的首页,即左边商品导航和右边导航固定宽度,中间的主要内容随浏览器宽度自适应,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
css中,可利用transform属性实现垂直翻转效果,该属性可以对元素进行旋转、缩放、移动或倾斜操作,当该属性与rotateX()函数配合使用时,可设置元素垂直翻转样式,语法为“元素{transform:rotateX(翻转角度);}”。
css中,可用keyframes规则、animation和transform属性实现上下运动效果,写法“元素{animation:名称 时间}@keyframes 名称{50%{transform:translateY(移动距离)}}”。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008