HTML and CSS styled accordion with nested checkboxes: handling long items

I am currently working on creating a multi-level accordion for an aside navigation section on a webpage. However, I have encountered an issue where the background color extends too far when hovering over a specific element. This occurs on a folder-type item with child elements, which is itself a child of another folder.

To see the problem in action, you can check out the JSFiddle linked here. The discrepancy between "Folder 3" and other items is quite noticeable. Can anyone suggest a way to make it consistent?

.accordion {
  list-style: none;
  margin: 0;
  padding: 0;
}

aside {
  background-color: orange;
  flex: 1;
  flex-direction: column;
  left: 0;
  margin-left: 20px;
  margin-right: 20px;
  position: absolute;
  width: 150px;
  z-index: 1;
}

.container {
  display: flex;
  position: relative;
}

.input {
  position: absolute;
  opacity: 0;
}

.label {
  display: inline-block;
  width: 100%;
}

.label:hover,
.item:not(.has-children):hover {
  background-color: red;
}

.label-l1 {
  padding-left: 20px;
}

.label-l2 {
  padding-left: 40px;
}

.main {
  background-color: yellow;
  flex: 1;
  flex-direction: column;
  margin-left: 170px;
  width: 200px;
}

.sub {
  display: none;
  list-style: none;
  margin-left: 0;
  padding-left: 0;
  width: 100%;
}

.input:checked~.sub {
  display: block;
}
<html>

<body>
  <div class="container">
    <aside>
      <ul class="accordion">
        <li class="item has-children">
          <input class="input" type="checkbox" name="c1" id="c1">
          <label class="label" for="c1"><span>Folder 1</span></label>
          <ul class="sub sub-l1">
            <li class="item"><a href="" class="label-l1">Item 1</a></li>
            <li class="item"><a href="" class="label-l1">Item 2</a></li>
          </ul>
        </li>
        <li class="item has-children">
          <input class="input" type="checkbox" name="c2" id="c2">
          <label class="label" for="c2"><span>Folder 2</span></label>
          <ul class="sub sub-l1">
            <li class="item has-children">
              <input class="input" type="checkbox" name="c3" id="c3">
              <label class="label label-l1" for="c3"><span>Folder 3</span></label>
              <ul class="sub sub-l1">
                <li class="item"><a href="" class="label-l2">Item 3</a></li>
                <li class="item"><a href="" class="label-l2">Item 4</a></li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </aside>
    <div class="main">
      <p>I must express my thoughts on this matter.</p>
    </div>
  </div>
</body>

</html>

Answer №1

The issue you are facing is due to the parent element having a width of 100%, while the element has a padding of 20px. This causes the right side margin to extend 20px past the right border. To fix this, add box-sizing: border-box; to the .label-l1 element, which will prevent overflow outside the border.

.label-l1 {
  padding-left: 20px;
  box-sizing: border-box;
}

Note: You can include this in a CSS reset so that you don't have to address each occurrence individually for other elements

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}


<!-- CSS Snippet -->
<!-- HTML Code Snippet -->

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

flask - Transfer data from Python to HTML using a database

I have written code to fetch data from a database and now I want to display it in an HTML format. Below is the code snippet from app.py @app.route('/news') def news(): import pymysql import re host='localhost' user = ...

Hide the menu when a user taps on an item on mobile or tablet devices

I'm facing a unique challenge and could use some guidance. The issue is with the mobile version of my website, accessible at . On tablets or mobile devices, the menu is condensed into a button. To navigate through the menu, I have to click the button ...

Discovering the clicked element using JavaScript: A complete guide

Let me start by saying that I have come across similar posts about tracking event listeners, but in my specific case, I am struggling to figure it out. While I am familiar with the event.target property, I just can't seem to make it work. Here is a s ...

Google Chrome applying its unique style compositions

I recently started transitioning a website from HTML to Wordpress, and encountered an unexpected challenge with Google Chrome applying unfamiliar styles. Specifically, the issue lies with the positioning of the background image bg.gif. While Internet Explo ...

Is it possible to directly add a child in HTML from nodejs?

I'm in the process of developing a chat application that requires users to select a room from a list of available rooms stored in <datalist> options, which are fetched from a SQL table on the server. Within my login.html file, users are prompte ...

The component I created seems to be having trouble recognizing the Slickr CSS that was

I have included Sick carousel's CSS in the root component by importing it like this: import React from 'react' import PropTypes from 'prop-types' import { ThemeProvider } from 'styled-components' import { ...

Guide to showcasing user input using a Javascript function

I need assistance to show the user input via the submit button. Users will enter tool types and the first five inputs will be displayed in list items (li). Once the limit of 5 tools is reached, a message 'Thanks for your suggestions' should appea ...

Is it possible to generate an HTML element by utilizing an array of coordinates?

I have a set of 4 x/y coordinates that looks like this: [{x: 10, y: 5}, {x:10, y:15}, {x:20, y:10}, {x:20, y:20}] Is there a way to create an HTML element where each corner matches one of the coordinates in the array? I am aware that this can be done usi ...

Populate the containing div with an image element

I am looking to fill a parent div with an img, but I want the image to be a standalone img element inside the div, rather than setting it as a background property. To clarify, instead of this: div { background-image: url('someimg.jpg'); ...

Overcoming Challenges with Ajax Success in Bootstrap DataTable Modal

Recently, I created a webpage featuring a top fixed navbar and a CRUD datatable with pagination supported by buttons that trigger modals. However, I'm encountering an issue where upon clicking the delete button in the modal to remove a record, the mod ...

Issue with Smooth Div Scroll on Chrome

I'm currently utilizing a plugin called Smooth Div Scroll, which can be found at Despite the automatic scrolling functionality working perfectly on Firefox, IE, and Safari, I'm encountering an issue with it not initiating when the page loads in ...

The div elements are constantly shifting positions when viewed in Internet Explorer

After completing my website and testing it across different browsers, I noticed an issue with IE. The contact div and navigation shift around the page in this browser specifically. Can anyone help me out with some code to fix this problem? Link to live ex ...

Angular 1.5 - Component for fetching HTML content with dynamic data

Help needed with using Angular Component method. Developing a main html file with its main controller containing a JSON list of client data: clients: [{ "name": "John Jackson", "age": "21", "hair": "brown", }, { "name": "Janet Doe", ...

Identify changes in attributes on elements that are updated automatically

On my webpage, I want to have two select boxes that are connected so their values always match. Here's an example: Imagine a page with two selects: <select class="myselector"> <option value='1'>1 <br> < ...

Creating semantic advertisement content using HTML5 tags

With the abundance of Semantic tags in html5 for sectioning content and their importance for Search Engines, I now have a need to incorporate advertisements into my articles. My query revolves around identifying a tag that can clearly indicate when child ...

Exploring the contrast between a hidden element and an element positioned outside the viewport through Selenium testing

When a selenium element is disabled by the ng-show attribute being false, it will have the values Displayed=false and Enabled=true. However, if a selenium element is enabled with the ng-show attribute set to true but is out of the viewport, it will also ha ...

"Struggling to position the bottom navbar at the center in Bootstrap?

Can someone help me center the icons in my second navigation bar? I've tried aligning them left and right, and that works, but when I try to center them, it doesn't work at all. Did I miss something in the code or make a mistake? Any assistance w ...

Tips for getting my scripts to function properly with my components in next.js?

I am facing an issue with my script while using React and Next.js in my pages/_app.js file. The code snippet is as follows: import axios from "axios"; import { $ } from "jquery"; import App from "next/app"; import Router from ...

Steps for adjusting the matMenuTriggerFor area so it only triggers when hovering over the arrow

Hello there! I'm currently working on adjusting the trigger area for opening the next menu panel. Right now, the next menu panel opens whenever I hover over either the title or the arrow. However, my goal is to have the menu open only when I hover ove ...

Adjusting the starting position of text within an HTML <li> element

I am currently in the process of designing a webpage layout with three columns. I have a specific vision for how I want the text to align within these columns but I am unsure of how to achieve it. Is there a way to make the text start at an exact position, ...