Making buttons stand out when clicked using CSS styling

Is there a way to make the selection of multiple items responsive on a /getmatch page? Currently, I have no visual indicator when I select things:

[]]1

What can be done to enhance the responsiveness of selecting multiple items?

Below is the code snippet from the page that generates these buttons:

{% extends "todo/base.html" %}

{% block content %}

   <style>
      .elements {
        display: block;
      }
      ul.items {
        display: flex;
        margin: 0;
        padding: 0;
        list-style: none;
      }
      li.item {
        flex: 1 0 20%;
        padding: 8px;
        margin: 2px;
        border-radius: 8px;
        border: 1px solid rgba(61, 86, 233, 0.3);
        text-align: center;
      }
      .col {
        display: flex;
        flex-direction: column;
        align-items: center;
      }
    </style>

  <!-- Header -->
  <header class="intro-header">
    <div class="container">
      <div class="col-lg-5 mr-auto order-lg-2">
      <h3><br>Tell me something about you... </h3>
      </div>
    </div>
  </header>

  <!-- Page Content -->
  <section class="content-section-a">

    <div class="container">
      <dt>
        <span>Pick the keywords you want to express when wearing perfume</span>
      </dt>

    <form action = "/getmatch" method="POST">
      {% for keyword in keywords %}
        <div class="elements">
            <ul class="items ">
               <li class="item col-xs-6 col-sm-3 col-md-3 col-lg-3">
                 <div class="box">
                 <div data-toogle="buttons" class="col">
                    <span>{{ keyword.title }}</span>
                 </div>
                 </div>
               </li>
            </ul>
        </div>
      {% endfor %}
      {% csrf_token %}
        <button type="submit">Envoyer</button>
    </form>
    </div>
  </section>

  <!-- Bootstrap core JavaScript -->
  <script src="static/vendor/jquery/jquery.min.js"></script>
  <script src="static/vendor/popper/popper.min.js"></script>
  <script src="static/vendor/bootstrap/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flexboxgrid/6.3.1/flexboxgrid.min.css" type="text/css" >


{% endblock %}

I attempted to customize the button styles with the following CSS:

button:hover{background-color:orange;}

button:focus{background-color:red;}

Unfortunately, it did not produce the desired effect.

Answer №1

I am unable to leave a comment because I do not have enough reputation points on Stack Overflow yet (apologies if this is against the rules). I am a bit confused about your intentions. If you want to visually indicate that a button has been selected among multiple buttons, you will need to retrieve the list of buttons, add an event listener for click events that toggles a specific class on and off, and then apply styling to that class for the desired effect.

Here's a suggestion on how to achieve this: I am not familiar with Django, but I believe you should consider the following approach as generating a ul element for each keyword seems unnecessary:

<form action = "/getmatch" method="POST">
    <div class="elements">
        <ul class="items " >
  {% for keyword in keywords %}

           <li class="item col-xs-6 col-sm-3 col-md-3 col-lg-3">
             <div class="box">
             <div data-toogle="buttons" class="col">
                <span>{{ keyword.title }}</span>
             </div>
             </div>
           </li>
  {% endfor %}
        </ul>
    </div>
  {% csrf_token %}
    <button type="submit">Submit</button>
</form>

To select the list elements, you might want to consider event delegation as it offers a more efficient solution. Learn more about it here: https://javascript.info/event-delegation. You can create a script tag or file and include it in your project with the following code snippet:

 //Select the UL list
const myItemsList = document.querySelector(".items");

//function passed to event listener as callback, toggles the selected class if the target is a list

const toggleListClass = (event) => {
  let target = event.target;
  if (target.tagName !== "li") return;
  target.classList.toggle("highlight");
};

//Add an event listener to the ul list
myItemsList.addEventListener("click", toggleListClass);

In your CSS, define a selector for the 'highlight' class:

.highlight {
  color: whatever;
}

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

Is it possible to modify the inactive color of just one radio button in Framework7?

Is there a way to change the inactive color of only one radio button in Framework7? I am aware that using the CSS variable --f7-radio-inactive-color allows me to set the inactive color for all radio buttons. However, I specifically want to modify the inac ...

The CSS component located just below the dropzone

I've encountered an issue with my infinite horizontal scrollbar. When I drag elements to the dropzone, they appear below it instead of above. Here's the HTML markup: <div class="wrapper"> <div class="header"> Drop here ...

When using ui-router in AngularJS, the page refuses to scroll to a specific ID if its hash is already present in the URL

Having trouble with hash-id linking using ui-router in AngularJS. When the URL already contains the desired hash (e.g., whatever.com/#/route1#des1) and an anchor tag with a link to #des1 is clicked, the link does not work and the page does not scroll down ...

Create CSS rules to draw lines that stretch to the edges of the container

As a skilled developer with expertise in CSS and coding styling, I have encountered a new design that has been approved. I am currently working on achieving a specific look with drawing borders and lines that need to extend both left and down. Here is a sc ...

WordPress WooCommerce causing the right bar to be pushed below

I'm currently facing an issue on my WordPress site with a purchased template and Woocommerce integration. The problem lies in the fact that the right sidebar gets pushed down on the shop page. When I reached out to the template developers, they sugges ...

Step by step guide on how to connect a stylesheet in CSS

Hey there, I recently attempted to link a style sheet in my HTML for the first time. Unfortunately, I encountered an issue where it didn't work. Here's the code I used: <link href="css/StyleSheet.css" rel="stylesheet" type="text/css" /> W ...

Guide to showing a dropdown menu or any div on the right side of the screen

I need help with displaying a menu that should appear from the right edge of the screen. Previously, I had a script to show the menu where the user clicked. However, for this specific menu, I adjusted the position by subtracting some pixels from the click ...

Issue with margin-top on <p> and <a> elements not displaying correctly?

I've been attempting to incorporate margin-top for a link button, but unfortunately it's not working as expected. I've even experimented with inline styles for both the 'p' and 'a' tags. There are three list elements, I ...

Bootstrap does not control the text family, weight, and line height

Currently, I am practicing adding grid layouts using Bootstrap. I have been trying to change the font styling within my project, but unfortunately, none of the changes seem to be taking effect. It's quite frustrating because even though I'm follo ...

Transform vertical and horizontal scrolling actions into solely horizontal scrolling using HTML and JS

I'm currently working on a unique React.js website that features a horizontal-scrolling portfolio. The website functions as a visual gallery, allowing users to easily scroll through images using their trackpad. Within the website, I have set up a lar ...

The object initialization in Django does not have the attribute 'kwargs'

Trying to implement a filter for a forms foreign key similar to the method described in this post set variable into queryset of forms.py from my generic view or url Encountering an error that states 'AddSubnet' object has no attribute 'kwa ...

Navigate to the following section on an HTML page by clicking a button using jQuery

Within my application using Jquery / Javascript, I am looking to implement a specific functionality. I currently have several div elements like the ones below: <div id="div1"></div> <div id="div2"></div> <div id="div3"></ ...

Ways to make all elements appear darker with the exception of one specific element

I am currently developing a Google Chrome extension and working on the right side, specifically in my friend's stories section. I am looking for a particular friend's story by searching for their name entered in the input field after clicking the ...

How can you dynamically disable the submit button on a form in Angular 7+ based on the form's current status?

Let's consider a scenario with a button in our code: <button type="button" class="menu-button" [disabled]="isInvalidForm()">Save</button isInvalidForm() { console.log('I am running!'); return this.nameValidator.errors || this.last ...

Modifying an ID on a different page using JavaScript while it is loading

I am looking to implement a functionality where the user can click on a button that will redirect them to another page. Depending on which button the user clicks, the content on the subsequent page should vary, even though it is the same physical page. Bel ...

Combining divs with identical values in Angular

I am working on creating my very own custom Calendar. Take a look at the full example of my component here I need help figuring out how to combine week divs that share the same value {{day.weekNumber}} so that there is only one div for each shared value, ...

Error encountered in personalizations field while sending multiple dynamic template emails through sendgrid

When attempting to send a bulk email using Sendgrid Dynamic Templates in Django, I encountered the following error message: The personalizations field is required and must have at least one personalization. The version of Sendgrid being used is sendgrid 6 ...

Creating a resilient CSS: DOM for seamless printing in PDF or on physical printers

I need help with formatting a list of col-6 elements (bootstrap) inside a row element so that the content of each block remains intact in a pdf preview generated using print(). I attempted to use various css properties like page-break and break as suggeste ...

In Angular 4 applications, all vendor CSS files are converted into style tags at the beginning of the HTML document

In my Angular 4 project, I have integrated Bootstrap, Fontawesome, and some custom CSS. However, all these styles are rendering as separate tags at the top of my index.html file. I would like them to be combined into a single bundled CSS file for better ...

Streamlining input options with a single label for multiple selections (radio buttons)

$('label').click(function() { id = this.id.split('-'); if (id[0] === '1') { id[0] = '2'; } else { id[0] = '1'; } $('#' + id[0] + '-' + id[1]).prop('chec ...