Home > Web Front-end > CSS Tutorial > How to Create a Toggle Button with a Push-In and Pop-Out Effect Using HTML and CSS?

How to Create a Toggle Button with a Push-In and Pop-Out Effect Using HTML and CSS?

DDD
Release: 2024-10-31 09:02:01
Original
504 people have browsed it

How to Create a Toggle Button with a Push-In and Pop-Out Effect Using HTML and CSS?

Creating a Toggle Button in HTML and CSS

Question:

How can I create a toggle button in HTML using CSS that remains pushed in when clicked and pops out when clicked again?

Answer:

Implementing a pure CSS solution for a functional toggle button is challenging. An alternative approach is to utilize a checkbox styled using CSS and supplemented with JavaScript.

jQuery Implementation:

The recommended solution involves a simple jQuery function and two background images to denote the different button states. Here's an example:

<code class="javascript">$(document).ready(function() {
  $('a#button').click(function() {
    $(this).toggleClass("down");
  });
});</code>
Copy after login
<code class="css">a {
  background: #ccc;
  cursor: pointer;
  border-top: solid 2px #eaeaea;
  border-left: solid 2px #eaeaea;
  border-bottom: solid 2px #777;
  border-right: solid 2px #777;
  padding: 5px 5px;
}

a.down {
  background: #bbb;
  border-top: solid 2px #777;
  border-left: solid 2px #777;
  border-bottom: solid 2px #eaeaea;
  border-right: solid 2px #eaeaea;
}</code>
Copy after login
<code class="html"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="button" title="button">Press Me</a></code>
Copy after login

The above is the detailed content of How to Create a Toggle Button with a Push-In and Pop-Out Effect Using HTML and CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template