CSS
CSS
(引入)样式一般在 <head>
中。
样式位置
行内样式
写在标签 style
中, 名:值;
。(优先级最高)
1 | <h1 style="color: pink; font-size: 25px;">CSS STYLE LEARNING</h1> |
样式简单才使用。
内部样式
<style>...</style>
1 | <style> |
1 | <!-- 内部样式 --> |
外部样式
引入外部样式, 更改 href="url"
(优先级=内部样式,样式会覆盖)
1 | <link rel="stylesheet" href="style.css"> |
选择器
通配选择器
选择所有元素 *{}
。
1 | * { |
元素选择器
元素{}
统一设置样式。
1 | h1{ |
类选择器
.name{}
,class="name"
1 | .fname{ |
1 | <h2 class="fname">Let us start</h2> |
ID选择器
#name{}
1 | #fname{ |
可与 class
一起使用, ID 最好是唯一使用的。
1 | <h2 id="fname">Let us start</h2> |
交集选择器
元素.类名{}
1 | p.fname{ |
给元素为 <p>
且类名为 fname
。
1 | <p class="fname">alice</p> |
并集选择器
选择器1,选择器2,...{}
1 | .fname, |
后代选择器
选择器 后代选择器{}
1 | ul li { |
1 | <ul> |
属性选择器
[属性]{}
[属性="value"]{}
1 | [title] { |
1 | <div title="atui"> test1 </div> |
伪类
动态伪类
选择器:状态{}
1 | a:link { /* 超链接独有 */ |
link
表示未访问。
visited
表示已经访问。
hover
表示鼠标悬停在这上。
active
表示鼠标正在点击。(顺序一般这样)。
1 | <a href="https://www.bilibili.com/anime/">anime</a> |
focus
可输入聚焦点击时才有。
1 | input:focus { |
1 | <input type="text" name="username"> |
结构伪类
:first-child
伪类来选择父元素的第一个子元素。
:nth-of-type(n)
同类型的第几个元素。
否定伪类
:not(selector)
表示除了 selector。
目标伪类
:target
当锚点指向时。
1 | p:target { |
1 | <a href="#i1">to 1</a> |
伪元素
改变输入框提示的样式。
::placeholder
1 | input::placeholder { |
1 | <input type="text" placeholder="username"> |
改变鼠标选中元素的样式。
::selection
1 | div::selection { |
1 | <div>selection</div> |
在某元素前创建子元素。
::before
在某元素后创建子元素。
::after
属性
颜色
rgb(x, y, z)
rgba(x, y, z, k)
k 表示透明度[0, 1]
。
字体
一般情况字体都要明确大小。
可为 body 配置字体。
1 | body { |
未配置字体的会默认继承(优先级最低)祖先的某些(包含字体)样式。
某些需要单独改的单独写样式即可。
更改字体族。
优先使用前面的,如果没有就使用后面的。
1 | font-family: "Fira Code", "宋体"; |
字体风格。
italic
斜体。
normal
正常。
1 | font-style: italic; |
字体粗细。
bold
粗体。
narmal
正常。
lighter
细体。
1 | font-weight: bold; |
文本
颜色。
color
1 | color: pink; |
字间距。
letter-spacing
1 | letter-spacing: 20px; |
词间距。
word-spacing
1 | word-spacing: 50px; |
文本修饰。
text-decoration
underline
下划线。
line-through
中线。
overline
上划线。
none
无。
1 | text-decoration: underline; |
线种类
dotted
点线。
wavy
波浪线。
还可以选线的颜色。
1 | text-decoration: line-through dotted red; |
缩进
text-indent
1 | text-indent: 50px; |
文本对齐方式
text-align
left
center
right
1 | text-align: center; |
行高
line-height
1 | line-height: 40px; |
列表
list-style-type
可以设置列表元素的 marker(比如圆点、符号、或者自定义计数器样式)。
list-style-position
属性指定标记框在主体块框中的位置。
标记盒在主块盒的外面。
标记盒是主要块盒中的第一个行内盒,处于元素的内容流之后。
list-style-image
属性用来指定一个能用来作为列表元素标记的图片。
表格
border-width
、border-style
、border-color
(不是独有)
一起写。
1 | table { |
border-spacing
单元格之间的间距。
1 | border-spacing: 1px; |
border-collapse
合并相邻的边框。
1 | border-collapse: collapse; |
empty-cells
空单元格处理
1 | empty-cells: hide; |
背景
background-image
背景图片。
background-repeat
背景重复方式。
background-position
背景图片位置。
1 | background-color: pink; |
鼠标
cursor
1 | cursor: url(hand.cur), pointer; |
长度单位
em
相对 font-size 的大小。
rem
相对根 font-size 的大小。
%
相对父元素的大小。
元素显示模式
display
1 | display: inline-block; |
1 | /* 预组合值 */ |
隐藏,但占位。
visibility
1 | visibility: hidden; |
盒模型
content
子元素都在父元素的内容区内。
min-width
min-height
padding
1 | padding: 20px 0px 20px 0px; |
border
1 | border: 10px solid orange; |
margin
1 | margin: 20px 0px 20px 0px; |
内容溢出
在容器上加属性。
1 | overflow: hidden; |
浮动
float
1 | p { |
1 | <div> |
定位
position
relative
相对之前的。
1 | position: relative; |
absolute
绝对定位。
根据最近祖先的的定位(必须要有 position)(在盒内,包含 padding
不包含 border
)
1 | position: absolute; |
fixed
固定定位。
根据视口定位(会根据视口移动)。
sticky
粘性定位。
相对最近有滚动条的移动(body 有)。
1 | body { |
1 | <body> |
有定位才有
z-index
层级。
布局
竖直中线
justify-content
属性定义浏览器如何沿着弹性容器的主轴和网格容器的行向轴分配内容元素之间和周围的空间。
1 | justify-content: center; /* 居中排列 */ |
justify-items
属性为所有盒中的项目定义了默认的 justify-self
,可以使这些项目以默认方式沿适当轴线对齐到每个盒子。
1 | justify-items: center; /* Pack items around the center */ |
水平中线
align-content
属性设置了浏览器如何沿着弹性盒子布局的纵轴和网格布局的主轴在内容项之间和周围分配空间。
1 | align-content: center; |
align-items
属性设置了所有直接子元素的 align-self
值作为一个组。在 Flexbox 中,它控制子元素在交叉轴上的对齐。
1 | align-items: center; |
取消默认的 html 样式
全局选择器
1 | * { |
reset.css
引入自定义风格。