CSS的布局

目录

1..盒子布局

border:

padding:

写法:

margin:

图片展示

2.元素的分类

dispaly:

介绍

效果

效果

 css的隐藏元素的方式

3.浮动布局

float:

clear:

4.定位布局

position:

代码

(1)效果

 (2)效果

(3)效果


1..盒子布局

border:

dotted定义点状边框
dashed虚线,但是大多浏览器为实现
solid实线
double定义双线

padding:

auto浏览器计算内边距。
length规定以具体单位计的内边距值,比如像素、厘米等。默认值是 0px。
%规定基于父元素的宽度的百分比的内边距。

写法:

(1)padding:1px 5px 12px 3px;

表示:

上内边距是 10px,右内边距是 5px,下内边距是 15px,左内边距是 20px

(2)padding:10px 5px 15px;

表示:

上内边距是 10px,右内边距和左内边距是 5px,下内边距是 15px

(3)padding:12px 15px;

表示:

上内边距和下内边距是 12px,右内边距和左内边距是 15px

(5)padding:6px;

表示:

四边的边距为6px

margin:

margin与padding的属性相同。

写法也相同,效果也相同。

图片展示

2.元素的分类

 元素:总是在新行上开始。

元素:和其他元素在一行。

白话一点的话

块级元素:独占一行,能自动换行,可以设置长和高。例如div、p、和hn(n可以去1、2等等)和nav、footer、address等等。

元素:不能自动换行,设置长和高不起作用.例如span、修饰字体<b>和<i>标签等等

dispaly:

none此元素不被显示
block此元素显示为块级元素,此元素前后会有换行符
inline默认元素显示为内联元素,元素前后无换行符

介绍

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
		<style type="text/css">
  a{
           	width: 200px;
           	height:200px;
            display: block;
            text-decoration: none;
           }        
		</style>
</head>
<body>
	<div class="one">1</div>
	<div class="two">2</div>
	<div class="there">3</div>
	<a href="#">a</a>
	<a href="#">a</a>
	<a href="#">a</a>
</body>
</html>

效果

本来a是内联元素并不会换行,也不能设置长和宽。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
		<style type="text/css">
           .one{
           	width: 200px;
           	height:200px;
           	background-color: yellow;

           }
           .two{
           	width: 200px;
           	height:200px;
           	background-color: red;
           }
           .there{
           	width: 200px;
           	height:200px;
           	background-color: blue;
           	display: none;
           } 
</style>
</head>
<body>
	<div class="one">1</div>
	<div class="two">2</div>
	<div class="there">3</div>
	<a href="#">a</a>
	<a href="#">a</a>
	<a href="#">a</a>
</body>
</html>

效果

明显看出3图标的方块不见了,说明div转变为了内联元素

 css的隐藏元素的方式

width:0;  height:0;

display:0;

3.浮动布局

float:

left向左浮动
right向右浮动
none
不浮动

clear:

left在左侧不允许浮动元素
right在右侧不允许浮动元素
both在两侧不允许浮动元素
none允许浮动元素

4.定位布局

position:

absolute

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

fixed

生成绝对定位的元素,相对于浏览器窗口进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative

生成相对定位的元素,相对于其正常位置进行定位。

因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

static默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。

代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
			<style type="text/css">
			*{ 
				padding: 0;
				margin: 0;
			}
		   .main{
            width: 300px;
            height: 300px;
            border: 1px solid black;
            margin: 200px;
           /*  position: relative; */
        }
         .one{
         	width: 100px;
         	height:100px;
         	background-color: yellow;
         	position: absolute;
         	left: 200px;
         	top: 100px;
         }
         .two{
         	width: 100px;
         	height:100px;
         	background-color: red;
         }
         .there{
         	width: 100px;
         	height:100px;
         	background-color: blue;
         } 
         </style>
 </head>
 <body>
 <div class="main">
 	<div class="one">1</div>
 	<div class="two">2</div>
 	<div class="there">3</div>	
 </div>
 </body>
 </html> 

(1)效果

 (2)效果

当前我们把 /*  position: relative; */打开

得到如下

(3)效果

 如果我们把margin:200px;关掉,那么图像没有办法先上面移动的空间,将会得到如下

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值