search
HomeWeb Front-endHTML Tutorial常用的CSS高级技巧_html/css_WEB-ITnose

CSS高级技巧:

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

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

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

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

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

//* remove border */

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

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

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

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

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

..nav li:first-child ~ li {

border-left: 1px solid #666;}

给 body添加行高

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

等。只要添加到 body 即可:

body {  line-height: 1;}

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

所有一切都垂直居中

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

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

看,是不是很简单。

注:在IE11中要小心flexbox。

逗号分隔的列表

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

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

对最后一个列表项使用 :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;}

就是这么容易。

对图标使用SVG

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

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

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

优化显示文本

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

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

注:请负责任地使用 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;}

继承 box-sizing

让 box-sizing 继承 html:

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

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

表格单元格等宽

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

.calendar {  table-layout: fixed;}

用Flexbox摆脱外边距的各种hack

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

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

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

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

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

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

相当方便。

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
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 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.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 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: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 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 OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 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: 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.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 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.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools