Home Web Front-end CSS Tutorial Share 12 awesome CSS skills

Share 12 awesome CSS skills

Mar 21, 2017 pm 05:36 PM
css

The following CSS advanced skills

  • Use :not() to apply/unapply borders on menu

  • Add line height to body

  • Everything is centered vertically

  • Comma separated list

  • Use negative nth-child to select items

  • Use SVG

  • for icons Optimize display text

  • For pure CSS sliders use max-height

  • Inheritance box-sizing

  • TableUniform width of cells

  • Use Flexbox to get rid of various hacks of margins

  • Use attribute selector for empty link

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

First add a border to each menu item

1

2

3

4

/* add border */

.nav li {

  border-right: 1px solid #666;

}

Copy after login

...and then remove the last element...

//* remove border */

1

2

3

.nav li:last-child {

  border-right: none;

}

Copy after login

...You can directly use the :not() pseudo-class to apply elements:

1

2

3

.nav li:not(:last-child) {

  border-right: 1px solid #666;

}

Copy after login

This way the code is clean, readable, and easy to understand.

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

..nav li:first-child ~ li {

1

2

  border-left: 1px solid #666;

}

Copy after login

Add line height to body

You don't need to add line-height separately to each <p>, <h*>, etc. Just add it to body:

1

2

3

body {

  line-height: 1;

}

Copy after login

This way text elements can easily inherit from body .

Everything is centered vertically

To center all elements vertically, it's too easy:

1

2

3

4

5

6

7

8

9

10

11

12

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.

Comma separated list

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

1

2

3

ul &gt; li:not(:last-child)::after {

  content: ",";

}

Copy after login

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

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

8

li {

  display: none;

}

 

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

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

  display: block;

}

Copy after login

It's that easy.

Use SVG

for icons There is no reason not to use SVG for icons:

1

2

3

.logo {

  background: url("logo.svg");

}

Copy after login

SVG has good scalability for all resolution types and supports all browsers returning to IE9. This way you can avoid .png, .jpg or .gif files.

Optimize display text

Sometimes fonts don't display optimally on all devices, so let your device browser 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. Additionally, IE/Edge has no text-rendering support.

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

9

.slider ul {

  max-height: 0;

  overlow: hidden;

}

 

.slider:hover ul {

  max-height: 1000px;

  transition: .3s ease;

}

Copy after login

Inherit box-sizing

Let box-sizing inherit html:

1

2

3

4

5

6

7

html {

  box-sizing: border-box;

}

 

*, *:before, *:after {

  box-sizing: inherit;

}

Copy after login

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

Table cells are of equal width

Tables are cumbersome to work with, so be sure to use table-layout: fixed to keep cells of equal width:

1

2

3

.calendar {

  table-layout: fixed;

}

Copy after login

Various hacks to get rid of margins using Flexbox

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

1

2

3

4

5

6

7

8

.list {

  display: flex;

  justify-content: space-between;

}

 

.list .person {

  flex-basis: 23%;

}

Copy after login

List separators will now appear at evenly spaced positions.

Use 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.

support

These advanced techniques work effectively in current versions of Chrome, Firefox, Safari, and Edge, as well as IE11.

The above is the detailed content of Share 12 awesome CSS skills. 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 Article

Hot tools Tags

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)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

What does placeholder mean in vue

How to write spaces in vue How to write spaces in vue Apr 30, 2024 am 05:42 AM

How to write spaces in vue

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

What does span mean in js

How to get dom in vue How to get dom in vue Apr 30, 2024 am 05:36 AM

How to get dom in vue

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

What does rem mean in js

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

How to introduce images into vue

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

What language is the browser plug-in written in?

What is the function of span tag What is the function of span tag Apr 30, 2024 pm 01:54 PM

What is the function of span tag

See all articles