Emphasizing buttons that are in a "pressed" or "inactive" state

I've implemented three push buttons in my code using input with type="button", which act as toggle buttons (on/off).

In the past, I used images to indicate when a button was pressed or reset, but now I want to achieve this effect using CSS. However, I'm unsure how to do it.

Additionally, I need to remove the border highlight when a button is selected.

I've searched for examples of this but haven't found anything useful.

Update:

I have some progress to share now. The code is somewhat functional; it displays the "depressed" look when holding down the mouse button, but reverts back to normal immediately upon release instead of staying depressed.

To prevent confusion, I've eliminated any extraneous code unrelated to this issue.

Second Update:

I've observed that in Firefox (not sure about other browsers), the default appearance of the buttons includes a depresses look when clicked, but it returns to normal upon releasing the mouse button. I aim to maintain the depressed look even after releasing the mouse button until the button is clicked again or until a different button is chosen.

      input:hover { color: teal; }
      .push:active { background: gray; color: aquamarine; }
      .push:inactive { background: white; color: teal; }
      <INPUT type="button" class="push" style="font-weight: bold;">
      <INPUT type="button" class="push" style="font-weight: bold;">
      <INPUT type="button" class="push" style="font-weight: bold;">

Final Update:

Many thanks to everyone who assisted me with this matter. I have discovered a more suitable solution for my needs. Below is the final version of the code. In this iteration, I simply alter the font color when a button is clicked - turning teal on first click and black on subsequent clicks or when another button is clicked. This color change is handled entirely within the JavaScript code. As part of this fix, the rollover highlight stopped working properly, and the resolution involved using onMouseOver and onMouseOut instead of the :hover pseudo-class within style tags in the head section. Interestingly, it seems the code doesn't function correctly when separated into HTML and JavaScript components within the snippet runner.

The rollover behavior works fine, but the selected button fails to display teal letters when the mouse moves away. Nonetheless, the code is provided here for those interested in experimentation.`

<!DOCTYPE HTML>
<HTML>
  <HEAD>
    <TITLE>Sample Solution Page</TITLE>
    <SCRIPT type="text/javascript">
    <!--
      // Javascript functions here...
    -->
    </SCRIPT>
  </HEAD>
  <BODY>
    <!-- Main body content here... -->
  </BODY>
</HTML>

`

Answer №1

If you're looking to change the appearance of your buttons when they are clicked, here's a simple solution.

Start by creating two CSS classes in your stylesheet - one for active buttons and one for inactive buttons.

When your page loads, set your buttons to have the corresponding class based on their initial state.

Then, in your client-side code, simply toggle the class of the button when it is clicked, switching between active and inactive states.

With this method, your button styles will automatically update when they are clicked.

Answer №2

If you're looking for a simple HTML and CSS solution, one option is to create a checkbox that appears as a button. Here's an example:

HTML

<input type="checkbox" id="check"/>
<label for="check" id="btn">Click me</label>

CSS

input { 
    display: none;
}

#btn {
    border: 2px solid #47A1E9;
    background-color: #47A1E9;
    color: #000;
}

#check ~ #btn:active, #check:checked ~ #btn {
    background-color: lightgrey;
}

#btn {
  font-weight: bold;
  font-size: 12px;
  text-decoration: none;
  background-color: #F0F0F0;
  color: #444;
  padding: 3px 8px;
  border: 1px solid #CCC;
  border-radius: 4px;
}

Check out this demo on jsFiddle: http://jsfiddle.net/q86tspk5/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Locate the closing anchor tag following the image

I need to locate the closing anchor tag that wraps an image with the class name "cat-image" in order to move a div right after it. However, I am unable to modify the HTML by adding IDs or classes to the anchor or image tags. Below is an example of the HTM ...

Incorporating CSS into an HTML page created by a Python CGI script

Last week, I was handed a Python-based monitoring and notification tool to work on. My task is to develop a proof of concept dashboard using the data collected by this tool over the years. The catch is that I'm fairly new to Python and have never dabb ...

What are the steps to implement infinite scrolling in a Vue.js application?

Currently, I am attempting to implement an infinite horizontal scroll section in vuejs for a selection of products. However, I am facing difficulties achieving the infinite effect. My approach so far involved removing the card that goes out of view and add ...

A tag that does not adhere to the background color's rgba opacity restrictions

Can you believe what's happening to me? Check out this code snippet. I'm trying to apply an rgba color to my a tag, but it's acting like an rgb color instead of the intended rgba. The text background is solid, but the rest of the background ...

Updating Data in the Kohana Database

I am facing an issue while attempting to modify values in a database, and the error message I receive is as follows: Call to a member function values() on a non-object Controller public function action_options() { $this->template->style ...

Design a circular text element using Tailwind CSS

Just starting out as a web developer and running into some issues with Tailwind CSS. Here's what I'm trying to achieve: https://ibb.co/KL8cDR2 This is the code I have right now: <div class="w-1/2 h-10 rounded-full bg-gray-400" style ...

Achieve a safari-inspired checkbox appearance across all web browsers with custom CSS styling

In my asp.net mvc project, I am dealing with numerous checkboxes. The client has provided me with a PSD file in which the checkboxes are either white or black. In the absence of any check mark indicator, I assume the black ones are checked. My goal is to r ...

Problems with Bootstrap affix scrolling in Internet Explorer and Firefox

I'm encountering an issue with the sidebar on my website. I've implemented Bootstrap affix to keep it fixed until the end of the page, where it should move up along with the rest of the content at a specific point... Everything seems to be worki ...

When using jqueryprint.js to print from Firefox browser, an additional blank page appears before the content

Seeking assistance in printing a section of an HTML page using PHP with a large header space for empty space after printing. Despite multiple attempts, achieving the desired result in Chrome, but facing issues with Firefox, which adds an additional page be ...

Deactivating the submit button after a single click without compromising the form's functionality

In the past, I have posed this question without receiving a satisfactory solution. On my quiz website, I have four radio buttons for answer options. Upon clicking the submit button, a PHP script determines if the answer is accurate. My goal is to disable t ...

Finding a jQuery DOM element without using a loop

Can the element in variable d be identified directly instead of looping through each function? Take a look at the DOM below: <!DOCTYPE html> <html lang="en"> <head> <title></title> <script src="../jquery-ui ...

Setting the height of the text area in a three-column layout cannot be achieved using a percentage value

I'm working on creating a 3-column layout with two text areas. The issue I'm facing is that when I adjust the width of the text areas to create columns, the height shrinks. I need a solution that will be compatible with HTML5 browsers - support f ...

What is the best way to align the logo image to the left side of the Material UI app bar without any gaps?

Currently, I am working on a material UI app bar to serve as my navigation bar. The issue I am encountering involves the logo image on the left side of the page having excessive spacing from the edge, as shown in the image below. I've tried adjusting ...

Parent div not properly adjusting its height

I am currently working on developing a fluid layout, however I have encountered a minor issue with the height of the container. The outer <div> (highlighted in yellow, ip_A-1) is not adjusting its height according to its child elements. You can view ...

Utilize Vuetify to showcase a vertical layout consisting of a header and a v-card, occupying the full height

<template> <div> <div style="height: 20px; color: yellow"> test </div> <v-card class="markdown-preview" tile> <v-card-text v-html="previewText"> </v-card-text> ...

The <hr> element creating a line beneath a connected div

Currently, I am in the process of designing a website with three main divs all on one page. To visually separate these divs, I have included a subtle horizontal line. The horizontal line between the first and second div looks correct as intended. However ...

Floating point expansion caused by the addition of padding

Currently working on a basic 2 column layout design for a website, but ran into a hiccup. Whenever I try adding padding to the left-floated column, it ends up expanding beyond the width that I have set. Unfortunately, I haven't been able to find a sol ...

Different ways to adjust the positioning of the HTML canvas created using p5.js

I am attempting to integrate the output canvas from p5.js into my webpage by following a tutorial heretutorial. I am hoping that the canvas will appear where I have positioned it within the HTML body. However, no matter what I try with CSS properties like ...

iOS is not recognizing the <a> tag but is instead honoring the :hover property

Within my list, I have a div element wrapped in an anchor tag. Here is an example of the code: <li> <a href="http://google.com/"> <div id="tease-info"> <div class="inset-img-border fade"></div> <img src=" ...

Create tab title CSS design that coordinates with the tab content and features circular corners

I am currently working on creating html + css tabs with a unique design. The goal is to have the selected tab display the title connected to the tab content, with a "neck" rounding in like an arrow. After some progress, I have reached a point where the im ...