Small example of using CSS to modify the checkbox effect of HTML to share

Source: Internet
Author: User

Using a checkbox to control subsequent elements

The checkbox control can toggle its state depending on the mouse's click, and can be used in CSS to set the style for a checkbox in the selected state, using the checked pseudo class. In conjunction with the "+" or "~" selector in CSS, you can control the element after it from the checkbox operation. Even with label labels, you can achieve more results.

Run

 CSS Code copy content to clipboard

    1. <! DOCTYPE html>
    2. <style>
    3. [type=checkbox]+* {display:none;}
    4. [type=checkbox]:checked+* {display:inline-block;}
    5. </style>
    6. <input type="checkbox" /><input/>

In this example, the text box is hidden by default, and the text box is displayed only when the checkbox is checked. This allows you to achieve no JavaScript switching effect.

Someone might think it's too much of an eyesore to put a checkbox on the page. In fact, even if the checkbox itself is hidden, CSS: Checked's judgment is still in force. So we can hide this checkbox and let the label label set the response area for it.

Run

  CSS Code copy content to clipboard

  1. <! DOCTYPE html>
  2. <style>
  3. Body {font:14px/1.5 Microsoft James Black;}
  4. [Type=checkbox] {display:none;}
  5. [Type=checkbox]~input {display:none;}
  6. [Type=checkbox]:checked~input {display:inline-block;}
  7. [Type=checkbox]~span {cursor:pointer; Margin-right:10px;}
  8. [Type=checkbox]~span:before {content:' dot I show text box ';}
  9. [Type=checkbox]~span:hover {color:#C30;}
  10. [Type=checkbox]:checked~span:before {content:' dot I hide text box ';}
  11. </style>
  12. <label><input type="checkbox" /><span></span><input/></label>

However, this approach is somewhat limited, because the current CSS does not support: has,:p arent, and so on, so the supported operations are limited to the label, and the default behavior of the label will always affect its internal first control, the above code even if the text box pop-up, It is also possible to trigger the default action of the label when clicking on the text box, causing the text box to be hidden.

All in all, here's just the way to do it, and it's up to everyone to study how to use it.

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.