CSS常用实例
1. 全屏背景
1 | body{ |
2. 单行溢出
1 | a{ |
3. 多行溢出
1 | .content { |
4. 自动换行
word-wrap
可以控制换行,当取值break-word
时将强制换行,中英文文本都无任何问题,但是对长串的英文不起作用。也就是说break-word
是用来断词而不是断字符。而word-break
取值为break-all
时,可允许非亚洲语言文本的任意字符断开。
一般来说,使用word-wrap:break-word
声明可以确保所有文本正常显示。但在Firefox浏览器上,长串英文会出现问题(不换行)。为了解决长串英文问题,一般将word-wrap: break-word; word-break: break-all
一起使用。但是这样又造成了一个新的问题,会导致普通英文语句中的单词断行影响阅读。
综上所述,主要问题是长串英文不换行和英文单词会被断开。两者选一,应该使用word-wrap: break-word; overflow:hidden
结合,而不是word-wrap: break-word; word-break:break-all
结合。1
2
3
4element{
overflow: hidden;
word-wrap: break-word;
}
5. flex:上中下布局
Chrome, Firefox, Safari, IE11, IE10有效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<html>
<style>
.app{
display: flex
}
.wrapper{
display: flex;
flex-direction: column;
min-height: 100vh;
width: 100%;
}
section{
flex: 1 0 auto;
}
header, footer{
height: 60px;
background: blue;
}
</style>
<div class="app">
<div class="wrapper">
<header></header>
<section></section>
<footer></footer>
</div>
</div>
</html>
6. 设置Select下拉箭头
1 | select { |
7. <input> checkbox radio
样式自定义
方法1 图片替换
1
2方法2
8. 弹窗,遮罩后内容不滚动
Bootstrap, Ant design方法, 弹窗出现时给body添加行内样式{overflow: hidden}
, 弹窗消失时{overflow: auto}
9. 阻止因出现滚动条导致页面抖动
信息流页面,如新浪微博,开始只有头部一些信息加载,此时页面高度有限,没有滚动条;然后,更多内容显示,滚动条出现。 margin: 0 auto
主体元素自然会做偏移——跳动产生。1
2
3
4
5
6
7
8
9html {
overflow-x: hidden;
overflow-y: auto;
}
body {
width: 100vw;
overflow-x: hidden;
padding-left: calc(100vw - 100%);
}
10. flex保持内容不超出容器
在一个设置了flex: 1
或flex: 0 0 25%
的容器中,如果文字很长,这时候文字就会超出容器,而不是呆在设置好的动态剩余空间中。可以给.content
添加width:0
, .content就不会被自己的元素无限撑开。1
2
3
4<div class="main">
<div class="aside"></div>
<div class="content"></div>
</div>
1 | .main{ |
10. 纹理背景
11. 不定宽高元素水平垂直居中
不适合的方案
- text-align和line-height
- position:absolute, 50%, margin: -px
正确的方法
display:table和display:table-cell
1
2
3
4
5
6
7
8.container {
display: table;
}
.inner {
display: table-cell;
vertical-align:middle;
text-align:center;
}flex
1
2
3
4
5.container {
display: flex;
align-items: center;
justify-content: center;
}position:absolute, 50%, translate
1
2
3
4
5
6
7
8
9.container {
position: relative;
}
.inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}vw, vh和translate
1
2
3
4
5
6.inner {
position:fixed;
top: 50vh;
left: 50vw;
transform: translate(-50%, -50%);
}:before和display:inline-block
1
2
3
4
5
6
7
8
9
10
11
12.container{
text-align: center;
}
.container:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.inner {
display: inline-block;
}
12. 移动端1px问题
由于不同的手机有不同的像素密度导致的。如果移动显示屏的分辨率始终是普通屏幕的2倍,1px的边框在devicePixelRatio=2的移动显示屏下会显示成2px,所以在高清瓶下看着1px总是感觉变胖了。
在ios8+中当devicePixelRatio=2的时候使用0.5px
1
2
3
4
5
6
7.border { border: 1px solid #999 }
@media screen and (-webkit-min-device-pixel-ratio: 2) {
.border { border: 0.5px solid #999 }
}
@media screen and (-webkit-min-device-pixel-ratio: 3) {
.border { border: 0.333333px solid #999 }
}transform: scale(0.5)
参考资料
- CSS3 Patterns Gallery 纹路背景
- Flex 布局教程:实例篇
- 多行溢出
- 《图解CSS3·核心技术与案例实践》 大漠[著]
- 阻止因出现滚动条导致页面抖动
- 能够让不定宽高元素水平和垂直居中的方法