css中怎么实现绝对定位-创新互联

css中怎么实现绝对定位,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联服务项目包括松阳网站建设、松阳网站制作、松阳网页制作以及松阳网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,松阳网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到松阳省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

一.基本概念:

如果说相对定位没有脱离文档流,相对于对象本身进行偏移有点拖泥带水的话,那么绝对定位绝对是快刀斩乱麻,因为绝对定位可以使一个对象脱离正常的文档流,好像是漂浮在正常文档流的上空,并相对于包含此对象的元素进行定位,当然这个定位相对元素在不同的情况下也有所不同。

二.如何将一个元素设置为绝对定位:

为对象添加如下属性即可将对象设置为绝对定位:

代码如下:


position:absolute;



或者



代码如下:


position:fixed


三.定位参考对象:

可以使用top属性和left属性设置绝对定位对象的偏移量。
绝对定位虽然脱离了文档流,但是也需要有定位的参考对象,不过在不同的情况下参考对象也是不同。

1.如果没有设置top或者left属性值,那么相应方位的定位参考对象就是此对象的一级父元素,代码实例如下:

代码如下:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>CSS的绝对定位-蚂蚁部落</title>  
<style type="text/css">
body
{
margin:20px;
}
#grandfather
{
width:330px;
height:300px;
background-color:#F90;
}
#father
{
width:200px;
height:200px;
background-color:green;
margin-left:50px;
}
#children
{
width:100px;
height:100px;
background-color:red;
float:right;
}
#inner
{
width:50px;
height:50px;
background-color:blue;
position:absolute;
top:10px;
}
</style>
</head>
<body>
<div id="grandfather">
<div id="father">
 <div id="children">
  <div id="inner"></div>
 </div>
</div>
</div>
</body>
</html>


以上代码中,由于inner元素采用绝对定位,并且没有设置left属性值,所以在水平方位的定位参考对象就是inner元素的一级父元素children。当然如果没有设置top属性值,那么垂直方位的定位参考对象也是children。
2.如果设置了left或者top属性值情况:
1).如果祖先元素中有采用定位的,那么此对象的相应方位的定位参考对象就是此祖先元素,如果祖先元素没有采用定位的,那么相应方位的上的定位参考对象就是浏览器客户区,代码实例如下:

实例一:

代码如下:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>CSS的绝对定位-蚂蚁部落</title>  
<style type="text/css">
body
{
margin:20px;
}
#grandfather
{
width:330px;
height:300px;
background-color:#F90;
}
#father
{
width:200px;
height:200px;
background-color:green;
margin-left:50px;
position:relative;
}
#children
{
width:100px;
height:100px;
background-color:red;
float:right;
}
#inner
{
width:50px;
height:50px;
background-color:blue;
position:absolute;
left:10px;
top:10px;
}
</style>
</head>
<body>
<div id="grandfather">
<div id="father">
 <div id="children">
  <div id="inner"></div>
 </div>
</div>
</div>
</body>
</html>



以上代码,inner元素采用绝对定位,并且它的祖先元素father采用相对定位,那么它的定位参考对象就是father。

实例二:

代码如下:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>CSS的绝对定位-蚂蚁部落</title>  
<style type="text/css">
body
{
margin:20px;
}
#grandfather
{
width:330px;
height:300px;
background-color:#F90;
}
#father
{
width:200px;
height:200px;
background-color:green;
margin-left:50px;
}
#children
{
width:100px;
height:100px;
background-color:red;
float:right;
}
#inner
{
width:50px;
height:50px;
background-color:blue;
position:absolute;
top:10px;
}
</style>
</head>
<body>
<div id="grandfather">
<div id="father">
 <div id="children">
  <div id="inner"></div>
 </div>
</div>
</div>
</body>
</html>


以上代码中,inner元素采用绝对定位,并且它的祖先元素没有采用定位的,那么垂直方位的定位参考对象就是窗口,由于没有设置left属性值,那么水平方位的定位参考对象就是它的一级父元素children。

四.绝对定位元素脱离文档流:
在开头已经提到过,绝对定位能够使元素脱离文档流,那么它周边文档流中的元素就能够占据此元素没有脱离文档流时的位置。
代码实例如下:



代码如下:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>CSS的绝对定位-蚂蚁部落</title>  
<style type="text/css">
div
{
text-align:center;
line-height:100px;
}
.father
{
width:400px;
height:400px;
background-color:green;
margin:50px;
}
.first
{
width:100px;
height:100px;
background-color:red;
position:absolute;
}
.second
{
width:120px;
height:120px;
background-color:blue;
}
</style>
</head>
<body>
<div class="father">
<div class="first">first</div>
<div class="second">second</div>
</div>
</body>
</html>


看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


网页标题:css中怎么实现绝对定位-创新互联
网站地址:https://www.cdcxhl.com/article44/ceeihe.html

成都网站建设公司_创新互联,为您提供自适应网站电子商务网站排名网站建设网站内链网站设计公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发