博主信息
博文 15
粉丝 0
评论 0
访问量 12480
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS中常用的绝对路径和相对路径画十字2018年8月16日20时00分
Kenxc2011的博客
原创
1161人浏览过

Css元素位置 position的位置有两种,一种是绝对路径,一种是相对路径。 


1、绝对路径的十字

实例

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>绝对定位</title>
	<style >
		.box {
			width: 600px;
			height: 600px;
			/*background-color: wheat;*/
			/*定位父级必须设置定位属性*/
			position: relative;
		}
		.box1 {
			width: 200px;
			height: 200px;
			background-color: lightblue;
			/*绝对定位会脱离文档流*/
			position: absolute;
			top:0;
			left: 200px;
			
		}
		.box2 {
			width: 200px;
			height: 200px;
			background-color: lightgreen;
			position: absolute;
			top:200px;
			left: 0;  
			
		}
		.box3 {
			width: 200px;
			height: 200px;
			background-color: lightcoral;
			position: absolute;
			top:200px;
			left: 400px;  
		}
		.box4 {
			width: 200px;
			height: 200px;
			background-color: lightgray;
			position: absolute;
			top:400px;
			left: 200px;  
		}

	</style>
</head>
<body>
<div class="box">
	<div class="box1"></div>
	<div class="box2"></div>
	<div class="box3"></div>
	<div class="box4"></div>

</div>



</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


2、相对路径的十字

实例

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>相对定位</title>
	<style >
		.box1 {
			width: 200px;
			height: 200px;
			background-color: lightblue;
			position: relative;
			left: 200px;
		}
		.box2 {
			width: 200px;
			height: 200px;
			background-color: lightgreen;  /*相对定位,相对自身定位*/
			/*position: relative;
			top: 20px;
			left: 50px;*/
		}
		.box3 {
			width: 200px;
			height: 200px;
			background-color: lightcoral;
			position: relative;
			left: 400px;
			top: -200px;
		}
		.box4 {
			width: 200px;
			height: 200px;
			background-color: lightgray;
			position: relative;
			left:200px;
			top:-200px;
		}
	</style>
</head>
<body>

<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结:绝对路径会脱离文件流,如果不设置指定的位置,后面的图形会覆盖前面的图形。

绝对路径的起始位置(0,0)就是左上角。top XX px 意思就是距离左上角向下XX像素,如果要往上则前面加负号。

left XX px 就是新的位置往右移动XX像素。


相对路径的top XX px 就是原来的位置就是(0,0),两者的区别在于原点定义不同,top 和left的对图形的移动方向是一样的。

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学