参考
- http://caibaojian.com/30-seconds-of-css/
- https://github.com/AllThingsSmitty/css-protips/blob/master/translations/zh-CN/README.md
样式
Box-sizing全局重置
html {box-sizing: border-box;}*,*::before,*::after {box-sizing: inherit;/* 根元素的boxsizing为border,子元素全部继承于根 */}
清除浮动
浮动:仅限于浮动布局才有必要清除浮动,尽量请使用现代开发工具 flex or grid
注意事项:
1、container容器里 没有 非浮动元素
2、在统一格式化上下文中 没有先于该clearfixed container的浮动元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.container .float{
float: left;/* 三个div在一行 各自由内容撑开*/
}
/*
why :::after? [注]
defines a pseudo-element.
*/
.clearfix::after{/* 清除浮动 父级高度不再塌陷 */
content: '';
display: block;
clear: both;
}
</style>
</head>
<body>
<div class="container clearfix" style="border:2px solid black">
<div class="float left" style="border:2px solid red">
1effete
</div>
<div class="float center" style="border:2px solid blue">
2
</div>
<div class="float right" style="border:2px solid darkgreen">
3
</div>
</div>
</body>
</html>
截断文本
仅适用于单个行元素。
<p class="truncate-text">If I exceed one line's width, I will be truncated.</p>
.truncate-text {
overflow: hidden;
white-space: nowrap;/*防止文本高度超过一行*/
text-overflow: ellipsis;
width: 200px;
}
上下文菜单
文本单行居左,多行居右
<h2><p>单行居中,多行居左</p></h2>
p {
display: inline-block;
text-align: left;//内层居左
}
h2{
text-align: center;//外层居中
}
消失的边界线

<div class="div_class_Grid">
<div class="div_class_GridCol_Col33">
<p>154.4万</p>
<p>关注人数</p>
</div>
<div class="div_class_GridCol_Col33">
<p>98</p>
<p>全部商品</p>
</div>
<div class="div_class_GridCol_Col33">
<p>1</p>
<p>店铺动态</p>
</div>
</div>
// 方法1
.div_class_GridCol_Col33:nth-child(2){
border-left:1px solid $bordercolor;
border-right:1px solid $bordercolor;
}
// 方法2
给每个item添加border,最后的边界item去掉border
/* 添加边框 */
.nav li {
border-right: 1px solid #666;
}
/* 去掉边框 */
.nav li:last-child {
border-right: none;
}
同样的思路,只是利用选择器,给除了最后一个item的元素 加上border
.nav li:not(:last-child) {
border-right: 1px solid #666;
}
// 方法3
思路:实现把左边的那个边框想办法给移出去
实现:把每列向左移动1px, 然后父容器规定超出的就隐藏,然后get √
.div_class_Grid{
overflow: hidden;
}
.div_class_GridCol_Col33{
margin-left:-1px;
border-left:1px solid $bordercolor;
}
视觉效果
高度过渡
<div class="trigger">
Hover me to see a height transition.
<div class="el">content</div>
</div>
.el {
transition: max-height 0.5s;// 针对max-height的改变 有一个动画
overflow: hidden;
max-height: 0; // 指定元素最初没有高度
}
/* */
.trigger:hover > .el {
max-height: var(--max-height); // 这个变量来自js动态获取的,从js传值给css的用法
// 当父元素接受到hover事件 el作为目标接受动作的响应
// 响应结果为 将max-height 更改为动态的值
}
var el = document.querySelector('.el')
var height = el.scrollHeight
el.style.setProperty('--max-height', height + 'px')// 从js传值给css的用法
移动端1px
<div class="hairline-border">text</div>
.hairline-border {
box-shadow: 0 0 0 1px;
}
//设备像素比(1dppx 等于96 DPI ),将box-shadow的分布设置为1 / dppx 。 。
@media (min-resolution: 2dppx) {
.hairline-border {
box-shadow: 0 0 0 0.5px;
}
}
@media (min-resolution: 3dppx) {
.hairline-border {
box-shadow: 0 0 0 0.33333333px;
}
}
@media (min-resolution: 4dppx) {
.hairline-border {
box-shadow: 0 0 0 0.25px;
}
}
支持度:
hover效果
溢出滚动渐变
动态阴影
类似的阴影box-shadow 而是基于元素本身的颜色
同级变淡
选择器妙用
not
:not选择器 解决消失的边界线问题
/* 去掉边框 */
.nav li:last-child {
border-right: none;
}
不过不要这么做,使用 :not() 伪类来达到同样的效果:
.nav li:not(:last-child) {
border-right: 1px solid #666;
}
hover+not

负nth-child
负的nth-child
li {
display: none;
}
/* 选择第 1 至第 3 个元素并显示出来 */
li:nth-child(-n+3) {
display: block;
}
或许你已经掌握了如何使用 :not()这个技巧,试下这个:
/* 选择除前3个之外的所有项目,并显示它们 */
li:not(:nth-child(-n+3)) {
display: none;
}
猫头鹰选择器
通用选择器 (``) 和 相邻兄弟选择
http://alistapart.com/article/axiomatic-css-and-lobotomized-owls/css新变量
计数器
<h1>html</h1>
<h1>css</h1>
<h1>js</h1>
<style>
h1::before{
content: '第' counter(mycounter) '章';;
margin-right: 5px;
}
h1{
counter-increment: mycounter;
}
</style>
calc()
var
用户体验友好
表单focus
a:focus,
button:focus,
input:focus,
select:focus,
textarea:focus {
box-shadow: none;
outline: #000 dotted 2px;
outline-offset: .05em;
}
单位变量
使用选择器:root来控制字体弹性
在响应式布局中,字体大小应需要根据不同的视口进行调整。你可以计算字体大小根据视口高度的字体大小和宽度,这时需要用到:root:
:root {
font-size: calc(1vw + 1vh + .5vmin);
}
body {
font: 1rem/1.6 sans-serif;
}
现在,您可以使用 root em
用 rem 来调整全局大小;用 em 来调整局部大小
在根元素设置基本字体大小后 (html { font-size: 100%; }), 使用 em 设置文本元素的字体大小:
h2 {
font-size: 2em;
}
p {
font-size: 1em;
}
然后设置模块的字体大小为 rem:
article {
font-size: 1.25rem;
}
aside .module {
font-size: .9rem;
}
现在,每个模块变得独立,更容易、灵活的样式便于维护。
为表单元素设置字体大小
当触发<select>的下拉列表时,为了避免表单元素在移动浏览器(IOS Safari 等等)上的缩放,加上font-size:
input[type="text"],
input[type="number"],
select,
textarea {
font-size: 16px;
}
