Table of Contents
使用 :not() 在菜单上应用/取消应用边框
给 body添加行高
所有一切都垂直居中
逗号分隔的列表
使用负的 nth-child 选择项目
对图标使用SVG
优化显示文本
对纯CSS滑块使用 max-height
继承 box-sizing
表格单元格等宽
用Flexbox摆脱外边距的各种hack
使用属性选择器用于空链接
支持
Home Web Front-end HTML Tutorial 12 个 CSS 高级技巧汇总_html/css_WEB-ITnose

12 个 CSS 高级技巧汇总_html/css_WEB-ITnose

Jun 21, 2016 am 09:05 AM

下面这些CSS高级技巧,一般人我可不告诉他哦。

  • 使用 :not() 在菜单上应用/取消应用边框
  • 给body添加行高
  • 所有一切都垂直居中
  • 逗号分隔的列表
  • 使用负的 nth-child 选择项目
  • 对图标使用SVG
  • 优化显示文本
  • 对纯CSS滑块使用 max-height
  • 继承 box-sizing
  • 表格单元格等宽
  • 用Flexbox摆脱外边距的各种hack
  • 使用属性选择器用于空链接

使用 :not() 在菜单上应用/取消应用边框

先给每一个菜单项添加边框

/* add border */.nav li {  border-right: 1px solid #666;}
Copy after login

……然后再除去最后一个元素……

//* remove border */

.nav li:last-child {  border-right: none;}
Copy after login

……可以直接使用 :not() 伪类来应用元素:

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

这样代码就干净,易读,易于理解了。

当然,如果你的新元素有兄弟元素的话,也可以使用通用的兄弟选择符(~):

..nav li:first-child ~ li {

  border-left: 1px solid #666;}
Copy after login

给 body添加行高

你不需要分别添加 line-height 到每个

等。只要添加到 body 即可:

body {  line-height: 1;}
Copy after login

这样文本元素就可以很容易地从 body 继承。

所有一切都垂直居中

要将所有元素垂直居中,太简单了:

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

看,是不是很简单。

注:在IE11中要小心flexbox。

逗号分隔的列表

让HTML列表项看上去像一个真正的,用逗号分隔的列表:

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

对最后一个列表项使用 :not() 伪类。

使用负的 nth-child 选择项目

在CSS中使用负的 nth-child 选择项目1到项目n。

li {  display: none;}/* select items 1 through 3 and display them */li:nth-child(-n+3) {  display: block;}
Copy after login

就是这么容易。

对图标使用SVG

我们没有理由不对图标使用SVG:

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

SVG对所有的分辨率类型都具有良好的扩展性,并支持所有浏览器都回归到IE9。这样可以避开.png、.jpg或.gif文件了。

优化显示文本

有时,字体并不能在所有设备上都达到最佳的显示,所以可以让设备浏览器来帮助你:

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

注:请负责任地使用 optimizeLegibility。此外,IE /Edge没有 text-rendering 支持。

对纯CSS滑块使用 max-height

使用 max-height 和溢出隐藏来实现只有CSS的滑块:

.slider ul {  max-height: 0;  overlow: hidden;}.slider:hover ul {  max-height: 1000px;  transition: .3s ease;}
Copy after login

继承 box-sizing

让 box-sizing 继承 html:

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

这样在插件或杠杆其他行为的其他组件中就能更容易地改变 box-sizing 了。

表格单元格等宽

表格工作起来很麻烦,所以务必尽量使用 table-layout: fixed 来保持单元格的等宽:

.calendar {  table-layout: fixed;}
Copy after login

用Flexbox摆脱外边距的各种hack

当需要用到列分隔符时,通过flexbox的 space-between 属性,你就可以摆脱nth-,first-,和 last-child 的hack了:

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

现在,列表分隔符就会在均匀间隔的位置出现。

使用属性选择器用于空链接

元素没有文本值,但 href 属性有链接的时候显示链接:

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

相当方便。

支持

这些高级技巧在Chrome、Firefox、Safari、Edge的当前版本,以及IE11中都能有效工作。

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles