Display HTML attribute values in CSS with attr ()

Source: Internet
Author: User
Tags html tags

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:

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:

@media Print {

A:after {

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

}

}

Visit Our home page

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

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.