How to Create a Depressed Toggle Button with CSS and JavaScript?
Styling Interactive Toggle Buttons
Creating a toggle button that stays depressed when clicked is not directly achievable with pure CSS. To achieve this effect, a combination of HTML, CSS, and JavaScript is required.
Using HTML and CSS:
Consider using a checkbox as the underlying semantic element. Style it differently based on whether it's checked or not. However, this approach may involve additional HTML and CSS complexity.
Using jQuery:
A better solution is to employ jQuery. Here's a compact function that toggles a button's styling between two states:
<code class="js">$(document).ready(function() { $('a#button').click(function() { $(this).toggleClass("down"); }); });</code>
Associated CSS:
To style the toggle button, use the following CSS:
<code class="css">a { background: #ccc; cursor: pointer; border: solid 2px; padding: 5px; } a.down { background: #bbb; border-color: #777 #777 #eaeaea #eaeaea; }</code>
HTML:
Finally, include the jQuery library and use the following HTML snippet to implement the button:
<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>
The above is the detailed content of How to Create a Depressed Toggle Button with CSS and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

One thing that caught my eye on the list of features for Lea Verou's conic-gradient() polyfill was the last item:

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

PHP templating often gets a bad rap for facilitating subpar code — but that doesn't have to be the case. Let’s look at how PHP projects can enforce a basic

Every time I start a new project, I organize the code I’m looking at into three types, or categories if you like. And I think these types can be applied to
