When you hover over nested HTML-lists in a webpage, make the tree's long text lines expand gracefully using a combination of HTML, CSS

In my Angular 4 application, I have a left div that displays a tree of items as recursive HTML lists. When there is a long text, it gets cut off by the border of the div and a scrollbar appears. I want to have the text expand beyond the border and show in a shadowed box when the user hovers over it, similar to how Windows Explorer handles it.

Here is the layout without mouse over:

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

And here is how it should look with mouse over:

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

I tried implementing Matthias' solution for flat lists, but since I need a recursive tree-like list, I need to make some improvements.

.sidebar {
  position: fixed;
  top: 51px;
  bottom: 0;
  left: 0;
  z-index: 1000;
  padding: 20px 0;
  border-right: 1px solid #eee;
}

.sidebar li {
  overflow-x: hidden;
  background: white;
}

.sidebar li:hover {
  list-style-type: none;
  width: -moz-fit-content;
  width: -webkit-fit-content;
  width: fit-content;
  overflow: visible;
  border: 1px solid #cacaca;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

<div class="container-fluid">
  <div class="row">
    <nav class="col-sm-4 col-md-3 d-none d-sm-block sidebar">
      <div>
        <ul>
          <li>
            <span>mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</span>
          </li>
          <li>
            <span>oooooooooooooooooooooooooooooooooooo</span>
            <ul>
              <li>
                <span>mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</span>
              </li>
            </ul>
        </ul>
      </div>

    </nav>

    <main role="main" class="col-sm-8 ml-sm-auto col-md-9 pt-3">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
    </main>
  </div>
</div>

Answer №1

If you want to make a quick and simple change to the overflow and width properties when hovering over an element, you can try out the following code snippet. Keep in mind that using width: fit-content may not work in Internet Explorer and Edge due to it being considered an experimental feature.

Latest Browser Support for width: fit-content (As of April 2018)

+-----------------------+-------------------+
| Google Chrome         | 22.0 (-webkit)    |
|                       | 46.0              |
+-----------------------+-------------------|
| Mozilla Firefox       | 3.0 (-moz)        |
+-----------------------+-------------------|
| Internet Explorer     | X                 |
+-----------------------+-------------------|
| Opera                 | 15 (-webkit)      |
+-----------------------+-------------------|
| Safari                | 6.1 (-webkit)     |
+-----------------------+-------------------|
| Microsoft Edge        | X                 |
+-----------------------+-------------------|

Source 1

Source 2

Edit

I've modified the code based on your latest question. Take a look at the updated snippet below where I applied the overflow style only to the <li> element instead of the entire sidebar container.

Please note: The default behavior places the dot in a list outside of the element, causing the overflow-x property to hide it. To keep the dot visible on hover, you can either use list-style-position: inside (which may result in line breaks) or simulate dots using the ::before pseudo-element.

.sidebar {
  position: fixed;
  top: 51px;
  bottom: 0;
  left: 0;
  z-index: 1000;
  padding: 20px 0;
  border-right: 1px solid #eee;
}

.sidebar li {
  overflow-x: hidden;
  background: white;
}

.sidebar li:hover {
  list-style-type: none;
  width: -moz-fit-content;
  width: -webkit-fit-content;
  width: fit-content;
  overflow: visible;
  border: 1px solid #cacaca;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

<div class="container-fluid">
  <div class="row">
    <nav class="col-sm-4 col-md-3 d-none d-sm-block sidebar">
      <div>
        <ul>
          <li>mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</li>
          <li>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</li>
          <li>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</li>
        </ul>
      </div>

    </nav>

    <main role="main" class="col-sm-8 ml-sm-auto col-md-9 pt-3">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
    </main>
  </div>
</div>

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

How to Overlay a Semi-Transparent Object on a Background Video using CSS and HTML

Hey there, I'm encountering a puzzling issue with my website. I have a background video set up, and I wanted to overlay a floating textbox on top of it. However, no matter what I do, the background color and borders of the textbox seem to be hiding be ...

Unable to fetch the data from HTML input field using autocomplete feature of jQuery UI

I am facing an issue with my html autocomplete textbox. Whenever I try to enter an address like "New York" and click on the submit button, it does not retrieve the input value and returns no value. Test.aspx <!--Address--> <div class="form-g ...

Comprehending the importance of a selector in a dropdown menu

Trying to figure out how to create a drop-down menu from CSS tricks, I found this code snippet: <nav role="navigation"> <ul> <li><a href="#">One</a></li> <li><a href="#"&g ...

Ensuring that hover remains active despite mousedown in JavaScript, CSS, and HTML

It appears that in browser-javascript hover events are deactivated when the mouse button is pressed down, as demonstrated by the following example: (Please run the code snippet to witness what I am referring to.) * { user-select: none; } #click, #drag, ...

Navigating with Angular 2 and configuring the .htaccess file

I've been experiencing some issues with my routing. Everything seems to be working fine on localhost, but when I upload it to the server and refresh the page, I keep getting a 404 Error. To address this problem, I created an .htaccess file and placed ...

Utilizing CSS to showcase sub-categories alongside primary categories following PHP rendering

1: In my database I have a hierarchical table of categories: 2: I created a PHP script to convert this table into HTML: 3: The resulting HTML code is as follows: <script> function toggleSubCategories(button) { ...

"Experience the ngx-youtube-player Angular component that allows for easy toggling between play and

My goal with ngx-youtube-player is to create a transparent div on top of it that toggles between playing and pausing the video when clicked. I am implementing this because my Angular project consists of numerous components, and one method of navigation is ...

What is the process for integrating ng-bootstrap into an Angular 5 project?

Has anyone encountered issues loading ng-bootstrap in their Angular project? I'm experiencing difficulties and would appreciate any insights. Thank you for your help! This is my app.module.ts file: import { BrowserModule } from '@angular/platf ...

Angular Error: Control not located at path 'instructions -> 1 ->action'

I am attempting to construct a form with the following structure. Users should have the ability to create multiple entries for the instruction fields. ... this.buttonForm = this.fb.group({ instructions: this.fb.array([ this.fb.group({ ...

How can I achieve a smooth background-image transition in Firefox?

Currently, I'm on the hunt for a substitute for this specific code snippet: "transition:background-image 1s whatever" This particular transition only seems to function on webkit browsers, leaving Firefox users out of luck. I experimented with utili ...

Is it recommended to place JavaScript/CSS at the bottom of an HTML document?

I am currently utilizing jQuery Mobile, and at the moment, my <head> section looks like this: <head> <meta charset="UTF-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1"> <title>help</title> <link ...

Step by step guide on incorporating two background images in a single header

I'm looking to create a header with a background image that appears twice, like this: To achieve the effect, I added opacity of 0.5 to one of the images, but it is affecting everything in my header including text and logo. Is there a way to prevent t ...

What is the best way to specify the dimensions of an image with CSS or Bootstrap?

I am facing some challenges while building my website. The ideal look of the page is represented by home_page_ideal. The issue arises with the small boxed image on the top left corner. Once I scroll to the top, the page displays as home_page_issue. The CSS ...

JavaScript influences CSS in EasySlider 1.7, creating a harmonious relationship between the

Struggling to achieve a captivating overlay effect with a slider using EasySlider 1.7, incorporating both images and corresponding text. However, encountering difficulties in aligning the text correctly on the page. The black overlay visible on the main s ...

Styling pagination using CSS and jQuery

I am looking to display 3 sections in a simple, paginated way that mimics tabs. I am trying to implement the logic where when a pagination item is clicked and has the 'active' class, it should show the corresponding block. However, I am strugglin ...

Enable Google Chart to visualize multiple sets of data beyond just 2

This Google chart displays 2 data sets (Tea, Coffee). I've attempted to modify it to show 5 data sets but encountered difficulties. I experimented with changing the button.onclick function and the button value. Below you will find the original code (2 ...

Updating a JSON file locally within an Angular application

I am facing a challenge with modifying a json file. Although I have successfully mapped it into a model, the issue lies in saving or overwriting the existing json file. Despite trying to use file-saver, I realized that changing the save directory is not po ...

Transferring information from parent page to child page using Angular version 8.2.4

As a newcomer to Angular, I am facing a challenge in sharing data between pages upon loading the main page. The structure involves using dynamic forms to generate dynamic pages within the main page. However, when trying to pass data from the main page to t ...

Trigger a Bootstrap modal in React after making an HTTP request with Axios

After successfully making an HTTP call in react using Axios, I encountered an issue when trying to open a Bootstrap 4 modal. The error message 'modal is not a function' appeared despite multiple attempts to troubleshoot and resolve the issue. Due ...

What could be the reason for the crash caused by ngModel?

The usage of [(ngModel)] within a *ngFor-Loop is causing an endless loop and crashing the browser. This is how my HTML looks: <div class="container"> <div class="row" *ngFor="let item of controlSystemTargetViewModel.values; let index = i ...