目录
垂直居中 - 使用 position 和 transform
盒子模型
所有HTML元素可以看作盒子,在CSS中,"box model"这一术语是用来设计和布局时使用。
CSS盒模型本质上是一个盒子,封装周围的HTML元素,它包括:边距,边框,填充,和实际内容。
盒模型允许我们在其它元素和周围元素边框之间的空间放置元素。
- 外边距Margin
- 边框之外的部分,显示透明
- 边框Border
- 盒子的边框
- 内边距Padding
- 边框与内容间的部分,显示透明
- 内容Content
- html元素
border属性
样式border-style
- none: 默认无边框
- dotted: 定义一个点线边框
- dashed: 定义一个虚线边框
- solid: 定义实线边框
- double: 定义两个边框。 两个边框的宽度和 border-width 的值相同
- groove: 定义3D沟槽边框。效果取决于边框的颜色值
- ridge: 定义3D脊边框。效果取决于边框的颜色值
- inset:定义一个3D的嵌入边框。效果取决于边框的颜色值
- outset: 定义一个3D突出边框。 效果取决于边框的颜色值
outline属性
位于内容与内边距中间的,可突出内容,与border基本相同
margin(外边距)
一般确定它的宽度(用top,left,right,bottom)
宽度border-width
颜色border-color
可单独设置各边:上边,top;下边,bottom;左边,left;右边,right;
padding(填充)
当元素的 padding(填充)内边距被清除时,所释放的区域将会受到元素背景颜色的填充。
单独使用 padding 属性可以改变上下左右的填充。
padding属性也可以通过top等确定单个边
浮动
CSS 的 float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列。
float(浮动),往往是用于图像,但它在布局时一样非常有用。
用float属性,用left/right确定方向
清除浮动用clear
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>浮动</title>
<style>
div
{
float:right;
width:120px;
margin:0 0 15px 20px;
padding:15px;
border:1px solid black;
text-align:center;
}
</style>
</head>
<body>
<div>
<img src="logocss.gif" width="95" height="84" /><br>
CSS is fun!
</div>
<p>
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</p>
<p>
在上面的段落中,div元素是120像素宽,它包含了图像。
div元素会向右浮动。
Margins 被添加到div使得文本远离div。
Borders和padding被添加到div框架的图片和标题中
</p>
</body>
</html>
效果即为图片在右边,文字环绕
定位
水平&垂直居中
要水平居中对齐一个元素(如 <div>), 可以使用 margin: auto;
设置到元素的宽度将防止它溢出到容器的边缘。
文本居中对齐
如果仅仅是为了文本在元素内居中对齐,可以使用 text-align: center;
图片居中对齐
要让图片居中对齐, 可以使用 margin: auto; 并将它放到 块 元素中;
左右对齐 -
- 使用定位方式
我们可以使用 position: absolute; 属性来对齐元素;
-
使用 float 方式
我们也可以使用 float 属性来对齐元素;
垂直居中对齐 - 使用 padding
CSS 中有很多方式可以实现垂直居中对齐。 一个简单的方式就是头部顶部使用 padding:
.center {
padding: 70px 0;
border: 3px solid green;
}
垂直居中 - 使用 line-height
.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}
/* 如果文本有多行,添加以下代码: */
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
垂直居中 - 使用 position 和 transform
.center {
height: 200px;
position: relative;
border: 3px solid green;
}
.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
伪类
CSS伪类是用来添加一些选择器的特殊效果。
伪类的语法:
selector:pseudo-class {property:value;}
CSS类也可以使用伪类:
selector.class:pseudo-class {property:value;}
伪元素
CSS 伪元素是用来添加一些选择器的特殊效果。
伪元素的语法:
selector:pseudo-element {property:value;}
CSS类也可以使用伪元素:
selector.class:pseudo-element {property:value;}
:first-line 伪元素
"first-line" 伪元素用于向文本的首行设置特殊样式。
浏览器会根据 "first-line" 伪元素中的样式对 p 元素的第一行文本进行格式化。
:first-letter 伪元素
"first-letter" 伪元素用于向文本的首字母设置特殊样式。
:before 伪元素
":before" 伪元素可以在元素的内容前面插入新内容。