Home Web Front-end CSS Tutorial A collection of 20 advanced CSS tips

A collection of 20 advanced CSS tips

Dec 05, 2017 pm 01:29 PM
css Summary

Learning to use advanced CSS techniques during the development process is also very beneficial to development. In this article, we will share 20 advanced CSS techniques with you.

1. Black and white image

This code will make your color photo appear as a black and white photo, isn’t it cool?


1

2

3

4

5

6

7

img.desaturate {

    filter: grayscale(100%);

    -webkit-filter: grayscale(100%);

    -moz-filter: grayscale(100%);

    -ms-filter: grayscale(100%);

    -o-filter: grayscale(100%);

}

Copy after login


2. Use:not() to apply/unapply borders on the menu

First add a border to each menu item


1

2

3

4

5

6

7

8

9

10

11

12

13

/* 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;

}

Copy after login


This way the code will be clean, easy to read, and easy to Got it.

Of course, if your new element has sibling elements, you can also use the universal sibling selector (~):


1

2

3

.nav li:first-child ~ li {

  border-left: 1px solid #666;

}

Copy after login


3. Top shadow of the page

The following simple CSS3 code snippet can add a beautiful top shadow effect to the web page:


1

2

3

4

5

6

7

8

9

10

11

12

body:before {

          content: "";

          position: fixed;

          top: -10px;

          left: 0;

          width: 100%;

          height: 10px;

          -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);

          -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);

          box-shadow: 0px 0px 10px rgba(0,0,0,.8);

          z-index: 100;

}

Copy after login


4. Add row height to body

You don’t need to separate Add line-height to each p,h mark etc. Just add to body:


1

2

3

body {

  line-height: 1;

}

Copy after login


This way text elements can easily inherit from body.

5. Center everything vertically

To center everything vertically, it’s so easy:


1

2

3

4

5

6

7

8

9

10

11

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


Look, isn’t it very simple?

Note: Be careful with flexbox in IE11.

6. Comma-separated list

Make HTML list items look like a real, comma-separated list:


1

2

3

ul > li:not(:last-child)::after {

  content: ",";

}

Copy after login


Use the :not() pseudo-class for the last list item.

7. Use negative nth-child to select items

Use negative nth-child in CSS to select item 1 to item n.


1

2

3

4

5

6

7

li {

  display: none;

}

/* select items 1 through 3 and display them */

li:nth-child(-n+3) {

  display: block;

}

Copy after login


8. Use SVG for icons

us There's no reason not to use SVG for icons:


1

2

3

.logo {

  background: url("logo.svg");

}

Copy after login


SVG scales well to all resolution types and supports all browsers All browsers have returned to IE9. This way you can avoid .png, .jpg or .gif files.

9. Optimize the display of text

Sometimes, fonts do not display optimally on all devices, so you can let the device browse To help you:


1

2

3

4

5

html {

  -moz-osx-font-smoothing: grayscale;

  -webkit-font-smoothing: antialiased;

  text-rendering: optimizeLegibility;

}

Copy after login


Note: Please use optimizeLegibility responsibly. Also, IE/Edge has no text-rendering support.

10. Use max-height for pure CSS sliders

Use max-height and overflow hiding to implement a CSS-only slider:


1

2

3

4

5

6

7

8

.slider ul {

  max-height: 0;

  overlow: hidden;

}

.slider:hover ul {

  max-height: 1000px;

  transition: .3s ease;

}

Copy after login


11. Inherit box-sizing

Let box-sizing inherits html:


1

2

3

4

5

6

html {

  box-sizing: border-box;

}

*, *:before, *:after {

  box-sizing: inherit;

}

Copy after login


This makes it easier to change the box-sizing in plugins or other components that leverage other behaviors. sizing.

12. Table cells have the same width

Tables are troublesome to work with, so be sure to use table-layout: fixed to keep the cells Equal width:


1

2

3

.calendar {

  table-layout: fixed;

}

Copy after login


##13. Use Flexbox to get rid of various hacks on margins

When you need to use column separators, you can get rid of the nth-, first-, and last-child hacks through the space-between property of flexbox:


1

2

3

4

5

6

7

.list {

  display: flex;

  justify-content: space-between;

}

.list .person {

  flex-basis: 23%;

}

Copy after login


#List separators will now appear at evenly spaced positions.

14. Use the attribute selector for empty links

Display the link when the a element has no text value but the href attribute has a link:


1

2

3

a[href^="http"]:empty::before {

  content: attr(href);

}

Copy after login


Quite convenient.

15. Detect mouse double-click

HTML:

##

1

2

3

4

<p class="test3">

  <span><input type="text" value=" " readonly="true" />

  <a href="http://renpingjun.com">Double click me</a></span>

</p>

Copy after login


CSS:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

.test3 span {

  position: relative;

}

.test3 span a {

  position: relative;

  z-index: 2;

}

.test3 span a:hover, .test3 span a:active {

  z-index: 4;

}

.test3 span input {

  background: transparent;

  border: 0;

  cursor: pointer;

  position: absolute;

  top: -1px;

  left: 0;

  width: 101%;  /* Hacky */

  height: 301%; /* Hacky */

  z-index: 3;

}

.test3 span input:focus {

  background: transparent;

  border: 0;

  z-index: 1;

}

Copy after login


##16. CSS writing triangle

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

/* create an arrow that points up */

p.arrow-up {

  width:0px;

  height:0px;

  border-left:5px solid transparent;  /* left arrow slant */

  border-right:5px solid transparent; /* right arrow slant */

  border-bottom:5px solid #2f2f2f; /* bottom, add background color here */

  font-size:0px;

  line-height:0px;

}

/* create an arrow that points down */

p.arrow-down {

  width:0px;

  height:0px;

  border-left:5px solid transparent;

  border-right:5px solid transparent;

  border-top:5px solid #2f2f2f;

  font-size:0px;

  line-height:0px;

}

/* create an arrow that points left */

p.arrow-left {

  width:0px;

  height:0px;

  border-bottom:5px solid transparent;  /* left arrow slant */

  border-top:5px solid transparent; /* right arrow slant */

  border-right:5px solid #2f2f2f; /* bottom, add background color here */

  font-size:0px;

  line-height:0px;

}

/* create an arrow that points right */

p.arrow-right {

  width:0px;

  height:0px;

  border-bottom:5px solid transparent;  /* left arrow slant */

  border-top:5px solid transparent; /* right arrow slant */

  border-left:5px solid #2f2f2f; /* bottom, add background color here */

  font-size:0px;

  line-height:0px;

}

Copy after login



17. Use of CSS3 calc()

calc() usage is similar to a function and can set dynamic values ​​​​for elements:

1

2

3

4

5

6

7

8

9

10

/* basic calc */

.simpleBlock {

  width: calc(100% - 100px);

}

/* calc in calc */

.complexBlock {

  width: calc(100% - 50% / 3);

  padding: 5px calc(3% - 2px);

  margin-left: calc(10% + 10px);

}

Copy after login



##18 . Text gradient

The text gradient effect is very popular and can be easily achieved using CSS3:

1

2

3

4

5

6

7

8

9

10

11

h2[data-text] {

  position: relative;

}

h2[data-text]::after {

  content: attr(data-text);

  z-index: 10;

  color: #e3e3e3;

  position: absolute;

  top: 0;

  left: 0;

  -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));}

Copy after login



19. Disable mouse events

CSS3 新增的 pointer-events 让你能够禁用元素的鼠标事件,例如,一个连接如果设置了下面的样式就无法点击了。


1

.disabled { pointer-events: none; }

Copy after login


20. 模糊文本

简单但很漂亮的文本模糊效果,简单又好看!


1

2

3

4

.blur {

   color: transparent;

   text-shadow: 0 0 5px rgba(0,0,0,0.5);

}

Copy after login

以上内容就是20 个 CSS 高级技巧汇总,希望能帮助到大家。

相关推荐:

最实用的制作网页小技巧总结

css的开发小技巧

CSS布局有哪些技巧

几个好用的css函数小技巧

最全的CSS开发常用技巧

The above is the detailed content of A collection of 20 advanced CSS tips. For more information, please follow other related articles on the PHP Chinese website!

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 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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

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.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles