CSS implementation Click to change element background color

Source: Internet
Author: User

You can use CSS pseudo-class to achieve the effect of clicking on the color of elements, two pseudo-classes are: Active,: Focus

: Active

: The active selector is used to select the active link. When clicked on a link, it becomes active (active): The active selector applies to all elements, not just the link a element

: Focus

: The focus selector is used to pick the element that gets focus. only elements that receive keyboard events or other user input are allowed: the focus selector.

Because of the above characteristics, if you want to achieve the click-to-color effect, there are the following two ways, the difference between

: Active, the element is discolored when clicked, but the color disappears after clicking

: Focus, the element is discolored after being clicked, and the color does not disappear after clicking

    button:active{    background:olive;    }    button:focus{    background:olive;    }

Online Example: http://jsfiddle.net/vt1bzpsq/

Because elements such as Div cannot accept keyboard or other user events, that is not supported: Focus pseudo-class, which can be supported by adding the TabIndex property: Focus

<DivTabIndex= "1">Section 1</Div><DivTabIndex= "2">Section 2</Div><DivTabIndex= "3">Section 3</Div>
div:focus { background-color:red;}

Online Example: http://jsfiddle.net/mwbbcyja/

CSS implementation Click to change element background color

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.