在线编辑平台
伪元素
使用伪元素before
和after
可以为元素添加内容.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <div class="author">待抉</div>
<style> .author:before { content: "Author: "; font-weight: bold; color: blue; } .author:after { content: " ✍️"; font-size: 1.2em; margin-left: 5px; } </style>
|
:nth-child
可用于对序列中不同的元素进行有序的样式设计:
1 2 3 4 5 6 7 8 9 10
| <div class="index-card">内容1</div> <div class="index-card">内容2</div> <div class="index-card">内容3</div> <div class="index-card">内容4</div>
<style> .index-card:nth-child(odd) { flex-direction: row-reverse; } </style>
|
- 选择所有类名为 “index-card” 的元素;
row-reverse
会让 flex 容器中的项目从右到左排列;同理, even
选择偶数位置的元素; 3n+1
等函数表达式也可以起作用.
基本的元素属性
布局属性
margin
: 控制元素的外边距;padding
: 控制元素的内边距;
文本属性
font-size
: 控制字体大小;font-weight
: 控制字体粗细;text-align
: 控制文本的对齐方式;text-decoration
: 控制文本的装饰;line-height
: 控制行高;letter-spacing
: 控制字符间距;text-transform
: 控制文本的大小写;
边框属性
border
: e.g. border: 1px solid red;
简写属性;border-radius
: 控制元素的圆角;border-style
: solid, dashed, dotted…
其他
overflow
: visible, hidden, scroll, auto;opacity
: 控制元素的透明度;cursor
: pointer, default, move, not-allowed…
动感魔法
hover效果
通过为class设置:hover
伪类,可以为元素添加鼠标悬停时的效果。
1 2 3 4 5 6
| .my-element:hover { background-color: green; color: red; transform: scale(1.1); font-size:20px; }
|
空格将不会被忽略, 需要确保类名与:hover
之间不存在空格;
其他的一些常用的伪类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| a:hover { color: red; } button:active { background-color: green; } input:focus { border-color: blue; } a:visited { color: purple; } p:first-child { font-weight: bold; } p:last-child { font-style: italic; } li:nth-child(2) { color: red; } p:only-child { color: green; } div:empty { background-color: yellow; }
|
Flex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .container { display: flex; flex-direction: row;
flex-wrap: wrap;
flex-flow: row wrap;
justify-content: space-between;
align-items: center;
align-content: space-between; height: 100vh; background-color: #f0f0f0; } .item { flex: 1; padding: 20px; background-color: lightblue; margin: 10px; text-align: center; flex-grow: 1; } </style> </head> <body> <div class="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> </div> </body> </html>
|