欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!

 21个有用的CSS小技巧,来帮你把CSS技术提高到高档水平 [Github上有8000多个star]。包含Flexbox、REM、:not()挑选器、负的nth-child等。

 

01、运用CSS复位

 

CSS复位能够在不同的浏览器上坚持共同的款式风格。您能够运用CSS reset 库Normalize等,也能够运用一个更简化的复位办法:

 

* {
box-sizing: border-box;
 margin: 0; padding: 0;
}

 

现在元素的 margin 和 padding 已为0,box-sizing能够管理您的CSS盒模型布局。

 

留意:假如你遵从接下来承继 box-sizing讲解的这个技巧, 你不需求在以上代码中增加box-sizing 特点。

 

02、承继box-sizing

 

从 html 元素承继 box-sizing :

 

html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}

 

如此在插件或其它组件里改动 box-sizing 变得简略。

 

03、运用 :not() 挑选器来决定表单是否显现边框

 

先为元素增加边框

 

/* 增加边框 */
.nav li {
 border-right: 1px solid #666;
}

 

为最终一个元素去除边框

 

/* 去掉边框 */
.nav li:last-child {
 border-right: none;
}

 

不过不要这么做,运用 :not() 伪类来到达相同的作用:

 

.nav li:not(:last-child)
{
border-right: 1px solid #666;
}

 

当然,你也能够运用 .nav li + li 或许 .nav li:first-child ~ li ,可是 :not()更加明晰,具有可读性。

 

 

04、为 body 元素增加行高

 

不用为每一个

,<h*> 元素逐个增加 line-height,直接增加到 body 元素:

 

body {
 line-height: 1.5;
}

 

文本元素能够很简单地承继 body 的款式。

 

05、笔直居中任何元素

 

不!这绝不是黑魔法,真的能够笔直居中任何元素:

 

html, body {
 height: 100%; margin: 0;
}
 body {
 -webkit-align-items: center;
 -ms-flex-align: center;
 align-items: center;
 display: -webkit-flex;
 display: flex;
}

 

这还不够?笔直方向,水平方向?任何元素,任何时刻,任何地址?CSS-Tricks 有篇好文 讲到了各种居中的技巧。

留意: IE11 对 flexbox 的支撑有点 bug。

 

06、逗号分隔列表

 

使列表的每项都由逗号分隔:

 

ul > li:not(:last-child)::after {
 content: ",";
}

 

因最终一项不加逗号,能够运用 :not() 伪类。

留意:这一技巧对于无障碍,特别是屏幕阅读器而言并不抱负。并且复制粘贴并不会带走CSS生成的内容,需求留意。

 

07、运用负的 nth-child 来挑选元素

 

运用负的 nth-child 能够挑选 1 至 n 个元素。

 

li {
display: none;
}
/* 挑选第 1 至第 3 个元素并显现出来 */
li:nth-child(-n+3) {
display: block;
}

 

或许你已经把握了怎么运用 :not()这个技巧,试下这个:

 

/* 挑选第 1 至第 3 个元素并显现出来 */
li:not(:nth-child(-n+3)) {
 display: none;
}

 

如此简略!

 

08、运用 SVG 图标

 

没有理由不运用 SVG 图标:

 

.logo {
 background: url("logo.svg");
}

 

SVG 在一切分辨率下都能够杰出缩放,并且支撑一切 IE9 今后的浏览器,丢掉你的 .png, .jpg, 或 .gif-jif-whatev 文件吧。

 

留意: 针对仅有图标的按钮,假如 SVG 没有加载成功的话,以下款式对无障碍有所帮助:

 

.no-svg .icon-only:after {
 content: attr(aria-label);
}

09、运用 "形似猫头鹰" 的挑选器

 

这个姓名可能比较生疏,不过通用挑选器 (*) 和 相邻兄弟挑选器 (+) 一同运用,作用特殊:

 

* + * {
margin-top: 1.5em;
}

 

在此示例中,文档流中的一切的相邻兄弟元素将都将设置 margin-top: 1.5em 的款式。

更多 "形似猫头鹰" 的挑选器,可参阅 A List Apart 上面 Heydon Pickering 的文章。

 

10、运用 max-height 来树立纯 CSS 的滑块

 

max-height 与 overflow hidden 一同来树立纯 CSS 的滑块:

 

.slider {
 max-height: 200px;
 overflow-y: hidden;
 width: 300px;
}
 .slider:hover {
 max-height: 600px;
 overflow-y: scroll;
}

 

鼠标移入滑块元素时增大它的 max-height 值,便能够显现溢出部分。

 

11、发明格子等宽的表格

 

table-layout: fixed 能够让每个格子坚持等宽:

 

.calendar {
 table-layout: fixed;
}

无痛的 table 布局。

 

12、运用 Flexbox 去除剩余的外边距

 

与其运用 nth-, first-, 和 last-child 去除列之间剩余的空隙,不如运用 flexbox 的space-between 特点:

 

.list {
display: flex;
 justify-content: space-between;
}
 .list .person { flex-basis: 23%;
}

列之间的空隙总是均匀持平。

运用特点挑选器来挑选空链接

当 元素没有文本内容,但有 href 特点的时分,显现它的 href 特点:

a[href^="http"]
:empty::before {
 content: attr(href);
}

适当简洁。

13、给 "默许" 链接界说款式

给 "默许" 链接界说款式:

a[href]:not([class])
{
 color: #008000;
 text-decoration: underline;
}

经过 CMS 体系刺进的链接,通常没有 class 特点,以上款式能够鉴别它们,并且不会影响其它款式。

 

14、共同笔直节奏

 

通用挑选器 (*) 跟元素一同运用,能够坚持共同的笔直节奏:

 

.intro > * {
margin-bottom: 1.25rem;
}

共同的笔直节奏能够供给视觉美感,增强内容的可读性。

 

15、固定份额盒子

 

要创立具有固定份额的一个盒子,一切你需求做的就是给 div 设置一个 padding:

 

.container {
 height: 0;
 padding-bottom: 20%;
 position: relative;
}
 .container div {
 border: 2px dashed #ddd;
 height: 100%;
 left: 0;
 position: absolute;
 top: 0;
 width: 100%;
}

 

运用20%的padding-bottom使得框等于其宽度的20%的高度。与视口宽度无关,子元素的div将坚持其宽高比(100%/ 20%= 5:1)。

 

16、为破碎图象界说款式

 

只要一点CSS就能够美化破碎的图象:

 

img {
display: block;
font-family: Helvetica, Arial, sans-serif;
font-weight: 300;
height: auto;
 line-height: 2;
 position: relative;
 text-align: center;
 width: 100%;
}

 

以增加伪元素的规律来显现用户信息和URL的引用:

 

img:before {
 content:
"We're sorry, the image below is broken :
(";
display: block;
margin-bottom: 10px;
}img:after {
 content: "(url: " attr(src) ")";
display: block;
 font-size: 12px;
}

 

了解更多关于这类款式的技巧 Ire Aderinokun的 原帖.

 

17、用 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;
}

现在,每个模块变得独立,更简单、灵活的款式便于维护。

 

18、躲藏没有静音、自动播放的影片

 

这是一个自界说用户款式表的不错的技巧。防止在加载页面时自动播放。假如没有静音,则不显现视频:

 

video[autoplay]
:not([muted]) {
display: none;
}

 

再次,咱们运用了 :not() 的长处。

 

19、运用挑选器:root来操控字体弹性

 

在呼应式布局中,字体巨细应需求依据不同的视口进行调整。你能够核算字体巨细依据视口高度的字体巨细和宽度,这时需求用到:root:

 

:root {
font-size
: calc(1vw + 1vh + .5vmin);
}

 

现在,您能够运用 root em

 

body {
 font: 1rem/1.6 sans-serif;
}

 

20、为更好的移动体会,为表单元素设置字体巨细

 

当触发的下拉列表时,为了防止表单元素在移动浏览器(ios Safari 等等)上的缩放,加上font-size:

 

//code from
http://caibaojian.com/css-protips
.htmlinput[type="text"],
input[type="number"],
select,textarea {
 font-size: 16px;
}

支撑状况

这些技巧适用于最新版的 Chrome, Firefox, Safari, Opera, Edge, 以及 IE11。

21个有用的CSS小技巧,来帮你把CSS技术提高到高档水平 [Github上有8000多个star]。包含Flexbox、REM、:not()挑选器、负的nth-child等。

 

01、运用CSS复位

 

CSS复位能够在不同的浏览器上坚持共同的款式风格。您能够运用CSS reset 库Normalize等,也能够运用一个更简化的复位办法:

 

* {
box-sizing: border-box;
 margin: 0; padding: 0;
}

 

现在元素的 margin 和 padding 已为0,box-sizing能够管理您的CSS盒模型布局。

 

留意:假如你遵从接下来承继 box-sizing讲解的这个技巧, 你不需求在以上代码中增加box-sizing 特点。

 

02、承继box-sizing

 

从 html 元素承继 box-sizing :

 

html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}

 

如此在插件或其它组件里改动 box-sizing 变得简略。

 

03、运用 :not() 挑选器来决定表单是否显现边框

 

先为元素增加边框

 

/* 增加边框 */
.nav li {
 border-right: 1px solid #666;
}

 

为最终一个元素去除边框

 

/* 去掉边框 */
.nav li:last-child {
 border-right: none;
}

 

不过不要这么做,运用 :not() 伪类来到达相同的作用:

 

.nav li:not(:last-child)
{
border-right: 1px solid #666;
}

 

当然,你也能够运用 .nav li + li 或许 .nav li:first-child ~ li ,可是 :not()更加明晰,具有可读性。

 

04、为 body 元素增加行高

 

不用为每一个

,<h*> 元素逐个增加 line-height,直接增加到 body 元素:

 

body {
 line-height: 1.5;
}

 

文本元素能够很简单地承继 body 的款式。

 

05、笔直居中任何元素

 

不!这绝不是黑魔法,真的能够笔直居中任何元素:

 

html, body {
 height: 100%; margin: 0;
}
 body {
 -webkit-align-items: center;
 -ms-flex-align: center;
 align-items: center;
 display: -webkit-flex;
 display: flex;
}

 

这还不够?笔直方向,水平方向?任何元素,任何时刻,任何地址?CSS-Tricks 有篇好文 讲到了各种居中的技巧。

留意: IE11 对 flexbox 的支撑有点 bug。

 

06、逗号分隔列表

 

使列表的每项都由逗号分隔:

 

ul > li:not(:last-child)::after {
 content: ",";
}

 

因最终一项不加逗号,能够运用 :not() 伪类。

留意:这一技巧对于无障碍,特别是屏幕阅读器而言并不抱负。并且复制粘贴并不会带走CSS生成的内容,需求留意。

 

07、运用负的 nth-child 来挑选元素

 

运用负的 nth-child 能够挑选 1 至 n 个元素。

 

li {
display: none;
}
/* 挑选第 1 至第 3 个元素并显现出来 */
li:nth-child(-n+3) {
display: block;
}

 

或许你已经把握了怎么运用 :not()这个技巧,试下这个:

 

/* 挑选第 1 至第 3 个元素并显现出来 */
li:not(:nth-child(-n+3)) {
 display: none;
}

 

如此简略!

 

08、运用 SVG 图标

 

没有理由不运用 SVG 图标:

 

.logo {
 background: url("logo.svg");
}

 

SVG 在一切分辨率下都能够杰出缩放,并且支撑一切 IE9 今后的浏览器,丢掉你的 .png, .jpg, 或 .gif-jif-whatev 文件吧。

 

留意: 针对仅有图标的按钮,假如 SVG 没有加载成功的话,以下款式对无障碍有所帮助:

 

.no-svg .icon-only:after {
 content: attr(aria-label);
}

09、运用 "形似猫头鹰" 的挑选器

 

这个姓名可能比较生疏,不过通用挑选器 (*) 和 相邻兄弟挑选器 (+) 一同运用,作用特殊:

 

* + * {
margin-top: 1.5em;
}

 

在此示例中,文档流中的一切的相邻兄弟元素将都将设置 margin-top: 1.5em 的款式。

更多 "形似猫头鹰" 的挑选器,可参阅 A List Apart 上面 Heydon Pickering 的文章。

 

10、运用 max-height 来树立纯 CSS 的滑块

 

max-height 与 overflow hidden 一同来树立纯 CSS 的滑块:

 

.slider {
 max-height: 200px;
 overflow-y: hidden;
 width: 300px;
}
 .slider:hover {
 max-height: 600px;
 overflow-y: scroll;
}

 

鼠标移入滑块元素时增大它的 max-height 值,便能够显现溢出部分。

 

11、发明格子等宽的表格

 

table-layout: fixed 能够让每个格子坚持等宽:

 

.calendar {
 table-layout: fixed;
}

无痛的 table 布局。

 

12、运用 Flexbox 去除剩余的外边距

 

与其运用 nth-, first-, 和 last-child 去除列之间剩余的空隙,不如运用 flexbox 的space-between 特点:

 

.list {
display: flex;
 justify-content: space-between;
}
 .list .person { flex-basis: 23%;
}

列之间的空隙总是均匀持平。

运用特点挑选器来挑选空链接

当 元素没有文本内容,但有 href 特点的时分,显现它的 href 特点:

a[href^="http"]
:empty::before {
 content: attr(href);
}

适当简洁。

13、给 "默许" 链接界说款式

给 "默许" 链接界说款式:

a[href]:not([class])
{
 color: #008000;
 text-decoration: underline;
}

经过 CMS 体系刺进的链接,通常没有 class 特点,以上款式能够鉴别它们,并且不会影响其它款式。

 

14、共同笔直节奏

 

通用挑选器 (*) 跟元素一同运用,能够坚持共同的笔直节奏:

 

.intro > * {
margin-bottom: 1.25rem;
}

共同的笔直节奏能够供给视觉美感,增强内容的可读性。

 

15、固定份额盒子

 

要创立具有固定份额的一个盒子,一切你需求做的就是给 div 设置一个 padding:

 

.container {
 height: 0;
 padding-bottom: 20%;
 position: relative;
}
 .container div {
 border: 2px dashed #ddd;
 height: 100%;
 left: 0;
 position: absolute;
 top: 0;
 width: 100%;
}

 

运用20%的padding-bottom使得框等于其宽度的20%的高度。与视口宽度无关,子元素的div将坚持其宽高比(100%/ 20%= 5:1)。

 

16、为破碎图象界说款式

 

只要一点CSS就能够美化破碎的图象:

 

img {
display: block;
font-family: Helvetica, Arial, sans-serif;
font-weight: 300;
height: auto;
 line-height: 2;
 position: relative;
 text-align: center;
 width: 100%;
}

 

以增加伪元素的规律来显现用户信息和URL的引用:

 

img:before {
 content:
"We're sorry, the image below is broken :
(";
display: block;
margin-bottom: 10px;
}img:after {
 content: "(url: " attr(src) ")";
display: block;
 font-size: 12px;
}

 

了解更多关于这类款式的技巧 Ire Aderinokun的 原帖.

 

17、用 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;
}

现在,每个模块变得独立,更简单、灵活的款式便于维护。

 

18、躲藏没有静音、自动播放的影片

 

这是一个自界说用户款式表的不错的技巧。防止在加载页面时自动播放。假如没有静音,则不显现视频:

 

video[autoplay]
:not([muted]) {
display: none;
}

 

再次,咱们运用了 :not() 的长处。

 

19、运用挑选器:root来操控字体弹性

 

在呼应式布局中,字体巨细应需求依据不同的视口进行调整。你能够核算字体巨细依据视口高度的字体巨细和宽度,这时需求用到:root:

 

:root {
font-size
: calc(1vw + 1vh + .5vmin);
}

 

现在,您能够运用 root em

 

body {
 font: 1rem/1.6 sans-serif;
}

 

20、为更好的移动体会,为表单元素设置字体巨细

 

当触发的下拉列表时,为了防止表单元素在移动浏览器(ios Safari 等等)上的缩放,加上font-size:

 

//code from
http://caibaojian.com/css-protips
.htmlinput[type="text"],
input[type="number"],
select,textarea {
 font-size: 16px;
}

支撑状况

这些技巧适用于最新版的 Chrome, Firefox, Safari, Opera, Edge, 以及 IE11。

 

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/jiqiao/j50654.shtml