1.盒子模型
<!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;
/* 取消a标签的下划线 */
text-decoration: none;
}
p{
width: 200px;
/* padding: 上 右 下 左
padding: 上 右/左 下
padding: 上/下 左/右
padding: 上下左右
*/
/* padding-top:单独设置上内边距
padding-right:单独设置右内边距
padding-bottom:单独设置下内边距
padding-left:单独设置左内边距
*/
padding: 10px 20px 30px 40px;
padding: 10px 20px 30px;
padding: 10px 20px;
padding: 10px;
/* padding-left: 10px; */
padding-left: 40px;
/* border:粗细 样式 颜色
*/
/* border-style:上 右 下 左
取值顺序与padding相同
可选值:
solid:实线
dashed:虚线
double:双线
dotted:点线
浏览器默认边框是2像素黑色,没有样式
border-top/right/bottom/left:单独设置上、右、下、左的边框样式
*/
/* border: 3px solid red; */
/* border-style: solid dashed double dotted;
border-color: blue yellow pink gray;
border-width: 1px 3px 5px; */
border-bottom: 3px solid yellowgreen;
/* 单独设置下边框的颜色 */
border-bottom-color: brown;
/* border-style: solid dashed ; */
margin-top: 20px;
/* border-box:边框盒:border+padding+content
*/
box-sizing: border-box;
}
</style>
</head>
<body>
<!-- 盒子模型组成部分:标准盒子
从内向外
1.内容区:content:分内容的
2.内边距:padding:设置内容与边框的距离
3.边框:border:设置区分盒子内外
4.外边距:margin:设置元素与元素之前的距离,设置元素的位置
用法与padding基本一致 -->
<!-- 盒子模型的理解:组成部分及各自的含义
盒子模型的分类:
标准盒子/W3C盒子:元素默认设置的宽度和高度是指内容区域
IE盒子/怪异盒子:元素默认设置的宽度和高度是指内容区域+内边距+边框
box-sizing:设置盒子尺寸,设置width和height作用的对象
默认值:content-box:内容盒
border-box:边框盒:这时设置的宽度和高度=border+padding+content-->
<p>
得仆两郭陈是卡斯望极皇。
</p>
<p id="first">lorendjkfjf</p>
<h3>量鲜弟了。</h3>
<a href="#">判下叹由要。</a>
</body>
</html>
2.边框
<!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>
p {
width: 200px;
height: 40px;
/* 单行文本垂直居中 */
line-height: 40px;
border: 3px solid red;
/* 设置边框弧度 */
border-radius: 30px;
}
/* 设置圆:width=height
border-radius=半径=一半的宽高 */
div.cri {
background-color: pink;
width: 200px;
height: 200px;
/* 50%:是指当前元素的宽高的50% */
border-radius: 50%;
border-radius: 50% 50% 50% 50%;
}
/* 设置半圆:上半圆 */
div.cri1 {
width: 200px;
height: 100px;
background-color: yellowgreen;
/* 左上和右上=》大小应该是半径
border-radius的值=半径*/
border-radius: 100px 100px 0 0;
}
/* 设置四份之一圆 左上*/
div.cri2 {
width: 100px;
height: 100px;
background-color: gold;
border-radius: 100px 0 0 0;
border-radius: 100% 0 0 0;
}
/* 通过边框设置三角形:
注意:1.元素宽高设置为0
2.通过透明色要设置 */
.squ {
/* width: 10px;
height: 10px; */
width: 0;
height: 0;
border: 20px solid red;
border-color: red yellowgreen blue gold;
border-color: transparent transparent blue transparent;
/* border-width: 0 0 20px 0; */
}
</style>
</head>
<body>
<!-- 1.边框弧度
border-radius:左上 右上 右下 左下 -->
<p>让害毒,身变。</p>
<div class="cri"></div>
<div class="cri1"></div>
<div class="cri2"></div>
<br>
<hr>
<!-- 小三角形 -->
<div class="squ"></div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</body>
</html>
3.元素显示和隐藏
<!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;
}
div {
background-color: pink;
width: 200px;
height: 30px;
line-height: 30px;
}
p {
background-color: aqua;
width: 200px;
height: 30px;
line-height: 30px;
/* 设置元素隐藏 */
display: none;
/* 设置元素不可见 */
/* visibility: hidden; */
}
div:hover>p {
/* 设置元素显示 */
display: block;
/* 设置元素可见 */
/* visibility: visible; */
}
</style>
</head>
<body>
<!-- 显示隐藏
display:
可选值:none(影藏)block(显示)
visibility:
可选值:hidden(元素不可见)visible(元素可见)
区别是display设置隐藏时,不占用原来的位置
visibility设置元素不可见时,依旧占有原来的位置 -->
<div>Lorem, ipsum dolor.
<p>人也反成,主。</p>
<h2>别人尹未。</h2>
</div>
</body>
</html>
4.外边距
<!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:nth-of-type(1) {
width: 200px;
height: 40px;
background-color: pink;
margin-bottom: -20px;
}
p:nth-of-type(2) {
width: 200px;
height: 40px;
background-color: blue;
margin-top: 30px;
}
/* 父子 */
.fa {
width: 300px;
height: 300px;
background-color: pink;
margin: 20px 30px 0 20px;
/* 方法1
border-top: 20px solid transparent;
box-sizing: border-box; */
/* 方法2 */
/* padding-top: 50px;
box-sizing: border-box; */
/* 方法3 overflow: auto;
overflow: hidden;
overflow: scroll; */
}
/* .son::before {
content: "hgjgj ";
}
.son::after {
content: "hgjgj ";
} */
/* 方法五
.fa::before {
content: "";
display: table;
} */
.son {
width: 100px;
height: 100px;
background-color: aqua;
margin-left: 50px;
margin-top: 50px;
}
.span1 {
/* margin-top: 30px; */
background-color: yellowgreen;
/* display: inline-block; */
margin-right: 20px;
}
.span2 {
background-color: bisque;
margin-left: -20px;
}
span{
display: inline-block;
}
</style>
</head>
<body>
<!-- margin:设置元素与元素之间距离 -->
<!-- <span class="span1">lorem</span>
<span class="span2">lorem</span> -->
<!-- 垂直方向
块元素
兄弟: 二个都是正值:谁大听谁的
一正一负:二者相加
二者都是负数:看绝对值大的
父子:出现的问题:外边距重叠问题=》父子元素的外边距开始位置重合在一起
解决思路:使父子外边距开始位置不同
解决方法:
1.给父元素设置边框=》问题:①.会撑开父元素;box-sizing:border-box
②.子元素的margin-top设置的距离是上边框离父元素上底边距离
2.给父元素设置内边距=》问题:会撑开父元素:box-sizing:border-box
3.给父元素开启bfc
bfc(Block Formatting Context,块级格式化环境),是css的隐形属性,将父元素开辟一个单独的空间,
开启bfc方法:
1.overflow的值非visible,但是副作用最小的是hidden
2.设置浮动,后面会说
3.设置绝对定位
4.在html里面添加一个空的table,影响到html结构
5.给父元素设置伪元素::before(注:伪元素选择器是在父元素里面添加一个子元素)添加一个table
行元素:在垂直方向可以设置外边距,但是没有效果
行内块:二者正值:二者相加
-->
<!-- 水平方向:
行元素&行内块元素:二者都是正值,一正一负:相加 ,注意行内块元素在默认情况有空白间隙-->
<!-- 兄弟 -->
<!-- <p>lorem1</p>
<p>lorem2</p>
<hr> -->
<!-- 父子
需求:1.子元素离父元素左边50px
2.子元素离父元素顶边50px
-->
<div class="fa">
<!-- 方法4
<table></table> -->
<div class="son">
<!-- <p>与宋判会,最。</p> -->
</div>
</div>
<span class="span1">
手目公位若定,变这。
</span>
<span class="span2">德变可人落。</span>
</body>
</html>
5.盒子的水平布局
<!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>
.fa {
width: 500px;
height: 500px;
background-color: pink;
}
.son {
/* 水平:200+20+20+20=260px<500px */
width: 200px;
/* width: auto; */
height: 200px;
margin:0 10px;
/* margin-left: auto;
margin-right: auto; */
padding: 0 10px;
/* padding: auto; */
border: 10px solid red;
background-color: aqua;
/* margin-top: auto;
margin-bottom: auto; */
}
/* 盒子模型:content padding border margin */
/* 水平布局:
width+padding+border+margin<父元素的width
1. width+padding+border+margin都设置了确定值
当小于父元素的width时,多余的部分给margin-right吸收
2.当width不设置或者设置为auto值时,多余的部分有宽度吸收
当margin-left=auto,多余的部分由margin-left吸收
当margin-right=auto,多余的部分由margin-right吸收
总结:当设置了auto就将多余的部分给它,注意能设置auto值的属性:width,margin-left,margin-right
3.有二个属性设置auto时
width与margin-left/width和margin-right同时设置auto值时,对于部分有width吸收,margin-left/margin-right没有值
margin-left和margin-right同时设置为auto时,多余部分平分给左右外边距=》块元素的水平居中
4.设置三个auto
width与margin同时设置auto时,多余部分由宽度吸收
*/
/* 垂直方向:设置多少就是多少 */
</style>
</head>
<body>
<div class="fa">
<div class="son"></div>
</div>
</body>
</html>