Three very special CSS tips to share

Source: Internet
Author: User
Tags arithmetic html tags mozilla developer network

The white-hot rivalry between browsers means that more and more people are now starting to use devices that support the latest and most advanced Web standards for the Internet, in a more interactive way. This means that we are finally able to use more powerful and flexible CSS to create a more concise, better maintained browser front-end code. Now let's take a look at some of the exciting CSS features that you may not know yet.

1. Display HTML attribute values in CSS with attr ()

The attr () feature has already appeared in the CSS 2.1 standard, but it is now popular. It provides an ingenious way to use the attributes on HTML tags in CSS, and in many cases it saves you the process of needing JavaScript processing in the past.

To use this feature, you need to use three elements: one: Before or: After CSS pseudo-class style,. Content property, and a attr () expression with the name of the HTML attribute you want to use. For example, want to show

The value of the Data-prefix attribute on the title, you can write this:

The code is as follows:

H3:before {

Content:attr (Data-prefix) "";

}

This is a heading

Obviously, this example does not show how useful it is, just shows its basic usage. Let's try a more useful example, an excellent application of attr () is to display the page links when the user prints the page. To achieve this, you can write this:

The code is as follows:

@media Print {

A:after {

Content: "(Link to" attr (href)) ";

}

}

Visit ">http://example.com" >visit Our home page

Once you know the technique, you'll be amazed at how much time it can bring to your work!

Tip: In the new CSS3 standard, the attr () feature is extended and can be used in a variety of CSS tags.

2. Use counter () to automatically add serial numbers to the list

Another feature that has been supported in CSS 2.1 is counter (), which allows you to easily add serial numbers to page titles, blocks, and other types of pages that appear consecutively. With it, you don't have to limit yourself to using

To achieve this effect, you can more flexibly use a custom number sequence on the page.

The point is that it's really simple: in: The content attribute in the before pseudo class is added to counter ():

The code is as follows:

Body {

counter-reset:heading;

}

H4:before {

counter-increment:heading;

Content: "Heading #" counter (Heading) "."

}

If you would like to know more about this counter method, please refer to the Mozilla Developer Network page on this topic. There's an excellent example of how to use nested counter.

3. Use Calc () to do arithmetic

Last but not least, let's say the Calc () feature. This function allows you to perform simple arithmetic calculations, such as calculating the length of the elements, and eliminates the ability to write JavaScript code that is not easy to maintain. This function supports all simple basic arithmetic operations, including subtraction.

For example, you want to create an element that fills its parent element with its width, but it also leaves a portion of the pixel width for other uses:

The code is as follows:

. Parent {

width:100%;

Border:solid black 1px;

position:relative;

}

. Child {

Position:absolute;

left:100px;

Width:calc (90%-100px);

Background-color: #ff8;

Text-align:center;

}

Pretty, isn't it? For more detailed information, please refer to the CSS Calc Specification for the consortium.

We can see more and more clearly that CSS is ripe to replace JavaScript in some ways, greatly simplifying the work of web developers. If you don't start using these features, you're just being silly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.