search
Advanced CSS TipsNov 22, 2016 am 10:00 AM
css

Use :not() to add/cancel the border on the menu

Many people will add a border to the navigation like this, and then cancel it for the last one:

/* add border */
.nav li {
  border-right: 1px solid #666;
}
/* remove border */
.nav li:last-child {
  border-right: none;
}

In fact, using CSS3’s :not() can be simplified to the following code:

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

Of course, you can also use .nav li + li or even .nav li:first-child ~ li, but using :not() can make the intention more clear
All major browsers support the :not selector, except IE8 and earlier versions

Add line-height attributes to body

You don’t need to add line-height attributes to

and separately. On the contrary, you only need to add them to the body:

body {
  line-height: 1;
}

In this way, the text element can easily inherit this attribute from the body

Vertical centering

You can vertically center any element:

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

Note: flexbox has some bugs under IE11

Use commas to separate the list

The list looks like it is comma-separated:

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

Remove the comma after the last element via the :not() pseudo-class

Use negative nth-child to select elements

Use negative nth-child between 1 and n Select elements between:

li {
  display: none;
}
/* 选择第1到3个元素并显示它们 */
li:nth-child(-n+3) {
  display: block;
}

Of course, if you understand :not(), you can also do this:

li:not(:nth-child(-n+3)) {
  display: none;
}

Use SVG for icons

There is no reason not to use SVG for icons:

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

SVG for any resolution The zoom effect is very good and supports all IE9+ browsers, so give up using png, jpg, and gif files
Note: The following code can improve accessibility for users who use assistive devices to access the Internet:

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

Optimize display text

Sometimes fonts don’t display optimally on all devices, so let your device browser help you:

html {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

Note: Please use optimizeLegibility responsibly. In addition, IE/Edge does not support text-rendering

Use max-height to implement pure CSS slideshow

Use max-height with beyond hiding to implement pure CSS slideshow:

.slider ul {
  max-height: 0;
  overlow: hidden;
}
.slider:hover ul {
  max-height: 1000px;
  transition: .3s ease; /* animate to max-height */
}

继承box-sizing

让box-sizing继承自html:

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

这使得在插件或者其他组件中修改box-sizing属性变得更加容易

设置表格相同宽度

.calendar {
  table-layout: fixed;
}

使用Flexbox来避免Margin Hacks

在做多列布局的时候,可以通过Flexbox的space-between属性来避免nth-、first-、 last-child等hacks:

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

这样,列之间的空白就会被均匀的填满

对空链接使用属性选择器

中没有文本而href不为空的时候,显示其链接:

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

文本溢出省略的处理方法

单行文本溢出

.inline{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

多行文本溢出

.foo{
    display: -webkit-box!important;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
    -webkit-box-orient: vertical;/*方向*/
    -webkit-line-clamp:4;/*显示多少行文本*/
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.