【CSS3】布局

时间:2023-03-09 01:34:28
【CSS3】布局

浮动布局:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>header</header>
<aside>aside</aside>
<section>section</section>
<footer>footer</footer>
</body>
</html>
*{
margin:;
padding:;
}
header{
height: 100px;
background:rgba(10,180,10,0.5);
}
aside{
width: 30%;
height:450px;
background:rgba(180,10,10,0.5);
float: left;
border:1px solid blue;/*aside的width加上section的width等于100%,此句会多出2px宽,影响正常布局。*/
box-sizing: border-box;/*content-box总宽不包括外边框。border-box总宽包括外边框,可以解决上面那个问题。*/
}
section{
width: 70%;
height: 450px;
background:rgba(10,10,180,0.5);
float: left;
}
footer{
height: 100px;
background:pink;
clear: left;
}

定位布局:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>header</header>
<aside>aside</aside>
<section>section</section>
<footer>footer</footer> <div id="div1">
<div id="div2"></div>
</div>
</body>
</html>
 *{
margin:;
padding:;
}
body{
/*border:1px solid red;*/
width: 800px;
margin:0 auto;
position: relative;
}
header{
width: 800px;
height: 100px;
background:red;
}
aside{
width: 200px;
height: 300px;
background:green;
}
section{
width: 600px;
height: 300px;
background:blue;
position: absolute;
left: 200px;
top: 100px;
}
footer{
width: 800px;
height: 100px;
background:orange;
} #div1{
width: 500px;
height: 400px;
background:rgba(200,20,20,0.2);
margin:50px;
position: relative;/*此属性设置后其子元素则相对父元素定位*/
}
#div2{
width: 100px;
height: 80px;
background:rgba(20,20,200,0.5);
position: absolute;/*以body左上角坐标为基准点*/
left: 10px;/*左上角距body左边距离*/
top: 10px;/*左上角距body上边距离*/
}

多列布局:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<section>
<h1>标题h1</h1>
<p>多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局</p>
<p>多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局多列布局</p>
</section>
</body>
</html>
 p{
text-indent: 2rem;/*首先缩进*/
line-height: 2rem;/*行间距*/
}
section{
padding: 1em;
background:rgba(20,200,20,0.3);
column-width: 300px;
/*-webkit-column-width:300px;*/
-webkit-column-count:;
-webkit-column-rule-style: solid;
-webkit-column-rule-width: 2px;
-webkit-column-rule-color: red;
-webkit-column-gap: 40px;
}
h1{
background:rgba(180,18,18,0.7);
text-align: center;
-webkit-column-span:;/*规定元素可横跨的列数,默认为1,不横跨。all为横跨所有列。*/
}

弹性布局:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<section>
<h1>标题h1</h1>
<p id="p1">多列布局多列布局多列布局多列</p>
<p id="p2">多列布局多列布局多列布局多列布局多列布局</p>
<p id="p3">多列布局多列布局多列布局多列布局多</p>
<p id="p4">多列布局多列布局多列布局多列布局多</p>
<p id="p5">多列布局多列布局多列布局多列布局多</p>
<p id="p6">多列布局多列布局多列布局多列布局多</p>
<p id="p7">多列布局多列布局多列布局多列布局多</p>
<p id="p8">多列布局多列布局多列布局多列布局多</p>
<p id="p9">多列布局多列布局多列布局多列布局多</p>
</section>
</body>
</html>
 p{
text-indent: 2rem;/*首先缩进*/
line-height: 2rem;/*行间距*/
width: 200px;
margin:20px;
}
p:nth-child(2){
background:rgba(190,19,19,0.5);
}
p:nth-child(3){
background:rgba(19,190,19,0.5);
}
p:nth-child(4){
background:rgba(19,19,190,0.5);
}
section{
width: 1000px;
height: 500px;
background: rgba(180,18,18,0.1);
display: flex;/*弹性盒子属性开启后浮动float、clear、vertical-align、CSS多列(CSS columns)将失效。*/
flex-direction:;/*row默认横向布局。row-reverse从右到左布局,且内容反转。column竖向布局。column-reverse纵向反转布局。*/
flex-wrap:;/*nowrap不换行。wrap换行。wrap-reverse换行且反转排列*/
justify-content: ;/*flex-start默认排列方式。flex-end从结束处开始对齐。center剧中排列。space-between两端分散等距排列对齐,两端无空。space-around类似space-between,但两端有空。*/
/*横向排列则x为主轴主y为侧轴,竖向排列则y为主轴x为侧轴。*/
align-items: ;/*flex-start默认顶端对齐。flex-end底端对齐。center剧中对齐。baseline基线对齐,基线可理解为小写字母a下划线,底线可理解为小写字母g下划线,顶线可理解为上写字母b上划线,中线可理解为小写字母a中划线。stretch拉伸。*/
align-content:;/*flex-start、flex-end、center、baseline、stretch*/
}
h1{
background: red;
height: 90px;
order:;/*值越小越靠前*/
}
#p1{
height: 200px;
order:;
flex-grow:;/*所有子元素设为1后则所有子元素会平均分配空间使得全部充满父元素。子元素扩展充满父元素时的比例因子*/
flex-shrink: ;/*父元素收缩时子元素所占空间的比例因子,值越大父元素收缩时子元素分担的越大即子元素收缩越多*/
flex-basis: 30px;/*设置固定值,不受其他元素收缩影响*/
}
#p2{
height: 180px;
}
#p3{
height: 160px;
/*align-self: flex-end;*//*作用同align-items,只中align-items设置在父元素上作用于子元素,align-self是设置在子元素上作用于子元素本身。*/
}
#p4{
background: pink;
height: 150px;
}
#p5{
background: orange;
height: 140px;
}
#p6{
background: pink;
height: 130px;
}
#p7{
background:green;
height: 120px;
}
#p8{
background: blue;
height: 110px;
}
#p9{
background: yellow;
height: 170px;
}

给HTML设置边框:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
border:2px solid green;
margin:2px;/*给所有的加边框,包括html*/
}
body,html{
height: 100%;/*此设置会使div的高度百分比设置生效*/
}
div{
width: 100%;
height: 50%;
background:red;
}
</style>
</head>
<body>
<div>div</div>
</body>
</html>

弹性盒子布局简单示例:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>header头部</header>
<section>
<nav>nav导航</nav>
<article>article正文</article>
<aside>aside右侧栏</aside>
</section>
<footer>footer底部</footer>
</body>
</html>
 *{
margin:;
padding:;
}
body,html{
height: 100%;/*若不设置此属性则flex-grow不会生效*/
font-weight: bold;
font:2em 黑体;
}
header,footer{
background:rgba(110,11,11,0.5);
padding: 10px;
text-align: center;
line-height: 2rem;
color: #fff;
/*flex-grow: 1;*//*头底各占一份*/
}
section{
background:rgba(17,170,17,0.5);
/*flex-grow: 8;*//*中间占8份*/
flex-grow:; display: flex;
flex-flow: row;
}
nav{
background:rgba(18,18,180,0.5);
width: 150px;
}
article{
min-width: 400px;
background:rgba(110,110,20,0.5);
flex-grow:;
}
aside{
background:rgba(30,120,120,0.5);
}
body{
display: flex;/*开启弹性盒子布局模式*/
flex-flow: column;
}