Having trouble getting the focus-within CSS pseudo-class to work with an HTML label

I'm attempting to create a dropdown menu with checkboxes inside the dropdown area.

Below is the code snippet I am using:

.search-box {
  color: black;
  width: 450px;
}

.search-box .fake-tb {
  display: flex;
  flex-wrap: nowrap;
  background: #ffffff;
  border: 1px solid #e5e5e5;
  box-sizing: border-box;
  border-radius: 6px;
  padding: 2px 5px;
  margin: auto;
  height: 35px;
  max-width: 700px;
}

.search-box .fake-tb * {
  border: 0 !important;
  height: auto !important;
}

.search-box .fake-tb input[type=search] {
  border-radius: 6px;
  width: 100% !important;
  outline: none;
}

.search-box .fake-tb img {
  vertical-align: middle;
  margin: auto;
  padding: 2px;
}

.search-box .dropdown {
  display: none;
  position: absolute;
  border: 1px solid #e5e5e5;
  cursor: default;
  background: white;
  z-index: 99999;
  text-align: left;
}

.search-box:focus-within .dropdown {
  display: block;
  background-color: white;
  color: black;
}

.search-box .dropdown>table {
  padding: 3px 10px 3px 2px;
  width: 100%;
}

.search-box .dropdown table tr:hover {
  background-color: lightskyblue;
}
<div>line before ...</div>
<div class="search-box">
  <div class="fake-tb">
    <input name="ctl32$txtCusSearch" id="ctl32_txtCusSearch" auto-complete="off" type="search">
    <img src="search.svg" />
  </div>
  <div class="dropdown">
    <table id="ctl32_lstOptions">
      <tbody>
        <tr>
          <td><input id="ctl32_lstOptions_0" name="ctl32$lstOptions$0" type="checkbox" value="Value1"><label for="ctl32_lstOptions_0">Checkbox 1</label></td>
        </tr>
        <tr>
          <td><input id="ctl32_lstOptions_1" name="ctl32$lstOptions$1" type="checkbox" value="Value2"><label for="ctl32_lstOptions_1">Checkbox 2</label></td>
        </tr>
        <tr>
          <td><input id="ctl32_lstOptions_2" name="ctl32$lstOptions$2" type="checkbox" value="Value3"><label for="ctl32_lstOptions_2">Checkbox 3</label></td>
        </tr>
        <tr>
          <td><input id="ctl32_lstOptions_3" name="ctl32$lstOptions$3" type="checkbox" value="Value4"><label for="ctl32_lstOptions_3">Checkbox 4</label></td>
        </tr>
        <tr>
          <td><input id="ctl32_lstOptions_4" name="ctl32$lstOptions$4" type="checkbox" value="Value5"><label for="ctl32_lstOptions_4">Checkbox 5</label></td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
<div>line after ...</div>

https://i.sstatic.net/FCCuC.png

I'm facing two challenges with this implementation:

  1. When clicking on the checkbox label, the dropdown closes. How can I prevent this behavior?

  2. Is there a way to make the dropdown width match the textbox area dynamically, without specifying a fixed width?

If possible, I'd prefer a solution using only CSS.

Answer №1

:focus-within is triggered when an element inside your div receives focus. However, it's important to note that certain elements, such as label, cannot actually be focused on. On the other hand, input elements can be focused, which explains why your dropdown menu doesn't disappear when you check a checkbox.

To resolve this issue, simply include the tabindex="0" attribute in your label elements.

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

Can the server determine if a Parse user is currently logged in?

My current system allows users to log in or sign up client side, but I want to verify their login status from the server when they land on a page through a GET request. Is it feasible to do this? ...

Do not apply tailwindcss styles to Material-UI

I've been struggling to apply styling from tailwindcss to my MUI button. My setup includes babel and webpack, with the npm run dev script as "webpack --mode development --watch". tailwind.css module.exports = { content: ["./src/**/*.{js, jsx, t ...

Guide on developing a Vue modal for transferring user input to a separate component

I have been working on creating a modal component that allows users to input data and then displays it in another component. For example, the user is prompted to enter their first and last name in a modal component (Modal.vue). Once the user saves this inf ...

The data variable in Vue.js does not show up in the view even when it is populated through an AJAX request

I am facing an issue with my code, where the 'tarefas' variable is not appearing in my v-for loop. I have verified that the response from the server is correct and the data is being received. Interestingly, when I add new data in the input field, ...

Tips for preventing Unknown event codes in POST request responses

When my client makes POST requests to the nodejs server I am running, I receive "unknown event 72" messages in the POST response, as shown in the Wireshark screenshot below. These extra data points are causing unnecessary bandwidth usage for my application ...

I am sometimes experiencing issues with activating ajax code using Bootstrap 3 modal

I'm stumped trying to find a solution for this issue. Currently, I am utilizing the bootstrap modal to retrieve ajax content from a specified URL. To prevent content overlap, I am using $.removeData() when reloading the content. The problem arises w ...

Searching for a file in Mongoose using an array field of IDs

I am working on a system with two models, Trade and Work, that serve as categories and subcategories of labor. I am trying to implement a feature where new additions automatically select a Trade based on the given Work. However, I am facing challenges in f ...

"Learn how to showcase a picture in full-screen mode when the webpage is opened

I recently came across a script on Stack Overflow that allows me to select an image at random from an array. The script can be found here: Script to display an image selected at random from an array on page load However, I want to take this concept furthe ...

Troubleshooting: AngularJS routing issue

I'm facing an issue with my AngularJS routing code. Here is the code snippet: /// <reference path="C:\Users\elwany\documents\visual studio 2015\Projects\spaapplication\spaapplication\scripts/angular.js" /& ...

Learn how to conditionally disable an input element in Angular using simple steps!

My task involves creating a grid of input elements by using a numeric array named board. I need to selectively disable specific elements based on their values. Whenever board[i][j] contains a non-zero value, the corresponding input element should start off ...

Create a new Chart.js Chart by using the data retrieved from an AJAX request and encoded in JSON

I am currently working on drawing a chart using the chart.js library. The initial draw works perfectly fine, but I am facing issues when trying to redraw the doughnut chart with new data retrieved from an ajax call. My approach involves PHP and Codeignite ...

A guide on implementing the groupby attribute in React-widget Dropdownlist

I am struggling to implement the react-widgets dropdownlist with groupby attribute using a different data structure. Instead of the typical example provided in the documentation, my data array is structured like this: [ { name:'test one' ...

Using a for-loop to add HTML Ids sequentially

I'm a beginner in JavaScript and HTML, and I've been working on reading my JSON file and appending it to various HTML IDs. I was curious if there is a more efficient way of doing this using a for-loop? for(let i=0; i<5; i++){ $(`#vid${i ...

Is it possible to nest a <dl> list within a <li> item?

Would it be considered semantically accurate to include a dl (along with some dt / dd elements) inside a li tag? While it's common to add block or inline elements within an li element, the feasibility of including dl, dt, and dd elements is uncertain ...

Combine entities in Firebase when the names match

Currently, I am developing a simple shopping cart application. Whenever a user clicks on the Add To Cart button, I save the product in the database as an array item. If a user tries to add multiple items of the same product, I aim to group all products, ...

Tips for resolving the error message "An issue occurred with the skill's response" within the Alexa Developer Console

Looking to develop a basic Alexa app where users can interact with voice commands, but encountering an issue with the response in the Test tab of Alexa Developer Console. The error message states "There was a problem with the requested skill's respons ...

Unable to dynamically append items to Owl Carousel using JavaScript

I am currently working on adding items dynamically to an Owl carousel. This is how I am approaching it: HTML <div id="avatar-carousel" class="owl-carousel lesson-carousel"> <div class="item item-logo"& ...

Creating an inclusive h1 header with a logo: A step-by-step guide

Is there a way to have a logo linked to the homepage and also have an h1 element that is invisible? This is my current layout: <header id="pageHeader"> <h1> <a href="<?php bloginfo('url'); ?>"> & ...

Tips for synchronizing with animation completion in ui-router's $stateChangeStart event

Incorporating AngularJs and ui-router in my project has been a smooth process. One particular requirement I have is to gently fade out the current view when a user clicks on a link, before navigating them to another view. To achieve this, I have created ...

Updating Angular components manually involves making direct changes to the component code,

Whenever I refresh a component in my Angular application implemented with version 17, the URL 'localhost:4200/(path)' reverts back to 'localhost:4200'. Interestingly, this behavior is consistent across three components except for the Ho ...