Create a vertical scrollable feature for the <ul> element when it is set to flex, limiting its height within the specified dimensions

In order to enhance user experience, I have implemented a live search bar that is only visible on mobile screens.

For the CSS styling:

.search-results {
  z-index: 9999;
  width: 80vw;
  position: absolute;
  top: 80%;
  left: 10%;
}

.search-results li {
  width: 100%;
  overflow: hidden;
}

.link-p-colr {
  color: #333;
  text-decoration: none;
  padding-bottom: 1rem;
}

.live-outer {
  display: flex;
  align-items: center;
  /* Vertically center the content */
  width: 100%;
  height: 60px;
  border-bottom: 1px solid #ccc;
  background: #fff;
  padding-bottom: 1rem;
}

.live-im img {
  width: 60px;
  /* Set a fixed width for the image */
  height: 60px;
  flex-shrink: 0;
  /* Ensure the image container doesn't shrink */
}

.live-product-det {
  flex-grow: 1;
  /* Allow the product details container to grow to fill the remaining space */
  height: 60px;
  overflow: hidden;
}

.live-product-name p {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98faf7f7ecebeceaf9e8d8adb6abb6aa">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

<form class="py-3 d-flex justify-content-end d-md-none custom-form position-relative" style="background-color:#FFDB58;" method="get" autocomplete="off" id="search2">
  <input class="form-control w-50 ms-auto" name="search" type="search" placeholder="Search" aria-label="Search" id="mu">
  <ul class="search-results list-group list-unstyled bg-white" id="livesearch2">
    <li class="p-3 d-inline">
      <a class="link-p-colr" href="http://gadgets92.test/mobiles/product.php?id=2">
        <div class="live-outer">
          <div class="live-im">
            <img style="width: fit-content; height: 60px;" src="http://gadgets92.test/admin/images/products/s7 edge.jpeg">
          </div>
          <div class="live-product-det ps-3">
            <div class="live-product-name">
              <p>Samsung Galaxy S7 Edge (4GB RAM +32)</p>
            </div>
            <div class="live-product-price">
              <div class="live-product-price-text">
                <p>Rs.50,999</p>
              </div>
            </div>
          </div>
        </div>
      </a>
    </li>

    <li class="p-3 d-inline">
      <a class="link-p-colr" href="http://gadgets92.test/watches/product.php?id=5">
        <div class="live-outer">
          <div class="live-im">
            <img style="width: fit-content; height: 60px;" src="http://gadgets92.test/admin/images/products/samsung-watch-4.png">
          </div>
          <div class="live-product-det ps-3">
            <div class="live-product-name">
              <p>Samsung Watch 4 Smartwatch</p>
            </div>
            <div class="live-product-price">
              <div class="live-product-price-text">
                <p>Rs.36,499</p>
              </div>
            </div>
          </div>
        </div>
      </a>
    </li>

    <li class="p-3 d-inline">
      <a class="link-p-colr" href="http://gadgets92.test/televisions/product.php?id=15">
        <div class="live-outer">
          <div class="live-im">
            <img style="width: fit-content; height: 60px;" src="http://gadgets92.test/admin/images/products/samsung-ua85cu8000kxxl.webp">
          </div>
          <div class="live-product-det ps-3">
            <div class="live-product-name">
              <p>Samsung UA85CU8000KXXL 85 inches 4K Smart TV</p>
            </div>
            <div class="live-product-price">
              <div class="live-product-price-text">
                <p>Rs.8,46,999</p>
              </div>
            </div>
          </div>
        </div>
      </a>
    </li>
  </ul>
  <button class="btn btn-success custom-btn me-auto" type="submit">Search</button>
</form>

To ensure a scrollable ul element within a specified height without affecting li elements, I utilized Bootstrap 5.3 and tried the following CSS solution:

<style>
    .search-results {
        max-height: 500px;
        overflow: hidden;
        overflow-y: scroll;
        padding: 0;
    }

    .search-results li {
        padding: 5px 10px;
    }
</style>

I also explored alternative solutions provided in this Stack Overflow thread.

Answer №1

I made modifications to the CSS by adding max-height: 20vw; and overflow-y: auto; to the .search-results class. Additionally, I removed overflow:hidden; from the .search-results li class.

.search-results {
  z-index: 9999;
  width: 80vw;
  position: absolute;
  top: 80%;
  left: 10%;
   max-height: 20vw; /* Set a maximum height for scroll */
  overflow-y: auto; /* Enable vertical scrolling */
}

.search-results li {
  width: 100% !important;
}

.link-p-colr {
  color: #333;
  text-decoration: none;
  padding-bottom: 1rem;
}

.live-outer {
  display: flex;
  align-items: center;
  /* Vertically center the content */
  width: 100%;
  height: 60px;
  border-bottom: 1px solid #ccc;
  background: #fff;
  padding-bottom: 1rem;
}

.live-im img {
  width: 60px;
  /* Set a fixed width for the image */
  height: 60px;
  flex-shrink: 0;
  /* Ensure the image container doesn't shrink */
}

.live-product-det {
  flex-grow: 1;
  /* Allow the product details container to grow to fill the remaining space */
  height: 60px;
  overflow: hidden;
}

.live-product-name p {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56343939222522243726166378657864">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

<form class="py-3 d-flex justify-content-end d-md-none custom-form position-relative" style="background-color:#FFDB58;" method="get" autocomplete="off" id="search2">
  <input class="form-control w-50 ms-auto" name="search" type="search" placeholder="Search" aria-label="Search" id="mu">
  <ul class="search-results list-group list-unstyled bg-white" id="livesearch2">
    <li class="p-3 d-inline">
      <a class="link-p-colr" href="http://gadgets92.test/mobiles/product.php?id=2">
        <div class="live-outer">
          <div class="live-im">
            <img style="width: fit-content; height: 60px;" src="http://gadgets92.test/admin/images/products/s7 edge.jpeg">
          </div>
          <div class="live-product-det ps-3">
            <div class="live-product-name">
              <p>Samsung Galaxy S7 Edge (4GB RAM +32)</p>
            </div>
            <div class="live-product-price">
              <div class="live-product-price-text">
                <p>Rs.50,999</p>
              </div>
            </div>
          </div>
        </div>
      </a>
    </li>

    <li class="p-3 d-inline">
      <a class="link-p-colr" href="http://gadgets92.test/watches/product.php?id=5">
        <div class="live-outer">
          <div class="live-im">
            <img style="width: fit-content; height: 60px;" src="http://gadgets92.test/admin/images/products/samsung-watch-4.png">
          </div>
          <div class="live-product-det ps-3">
            <div class="live-product-name">
              <p>Samsung Watch 4 Smartwatch</p>
            </div>
            <div class="live-product-price">
              <div class="live-product-price-text">
                <p>Rs.36,499</p>
              </div>
            </div>
          </div>
        </div>
      </a>
    </li>

    <li class="p-3 d-inline">
      <a class="link-p-colr" href="http://gadgets92.test/televisions/product.php?id=15">
        <div class="live-outer">
          <div class="live-im">
            <img style="width: fit-content; height: 60px;" src="http://gadgets92.test/admin/images/products/samsung-ua85cu8000kxxl.webp">
          </div>
          <div class="live-product-det ps-3">
            <div class="live-product-name">
              <p>Samsung UA85CU8000KXXL 85 inches 4K Smart TV</p>
            </div>
            <div class="live-product-price">
              <div class="live-product-price-text">
                <p>Rs.8,46,999</p>
              </div>
            </div>
          </div>
        </div>
      </a>
    </li>
  </ul>
  <button class="btn btn-success custom-btn me-auto" type="submit">Search</button>
</form>

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

What are the best practices for avoiding text wrapping around a DIV?

I have scoured this site extensively, searching for a solution to my problem. What I thought was a simple issue has left me frustrated after hours of trying to figure it out on my own. So, I've finally admitted defeat and turned to seek help... Here& ...

Enhance link with dynamic content using an AJAX call

I need help appending a picture after a link with the same URL as the link. The current AJAX code is producing the following result: <a href="images/Draadloos.png" data-lightbox="gallerij"></a> Here is an example of what I would like to achie ...

Having trouble with CSS styling not applying to the header tag

Take a look at this snippet of HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/test.css" rel="stylesheet"> </head> <p></p> <h2>Meeting the Old Guard ...

Why is it not possible for me to choose an element after it has been placed within a grid component?

Struggling to eliminate the blur effect on an image inside a grid element? It seems modifying the properties of an element within a grid class is causing some issues. To simplify, I conducted a basic test: I managed to change the ppp2 class when hovering ...

Varied elevations dependent on specific screen dimensions

There seems to be a minor issue with the height of the portfolio container divs at specific window widths. The problematic widths range from 1025 to 1041 and from 768 to 784. To visualize this, try resizing your browser window to these dimensions on the fo ...

Fixing Sticky Navigation Bar on Scroll (JavaScript, HTML, CSS)

I'm working on this site as a fun side project, but I've hit a roadblock. The sticky nav bar I implemented jumps when the user scrolls down far enough. Despite reading through other discussions, I can't seem to figure out the solution. I su ...

Confirm before closing the window

How can I get this code to function properly and show a confirmation alert after the user clicks on a button? This is essentially an "exit website button". The confirmation pop-up will have: If "OK" is clicked > the current window will close; If ...

Overlap of sub menus

Seeking assistance from a CSS expert for a challenge I am facing. I am currently working on a toggle menu for mobile view, and encountering issues with the submenu. Any list item placed below another one with children is not visible. Removing the parent l ...

Is there a method to uncover the code that controls the display of a <div> element?

As a fresh face at the company, I've been given the responsibility of developing a chart that is similar to one already present on their website. While I have the knowledge and skills to create it, I am struggling to locate the specific code where the ...

When you hover over the button, a picture will appear

This is an example code snippet from W3Schools. It showcases an animated button that reveals a small arrow next to the text when hovered over. The specific line of code responsible for this effect is .button span:after {content: '\00bb'. How ...

How can I resize or break the overflowing Bootstrap pagination within the parent div based on the resolution?

When utilizing boostrap4 theming for my pagination, a problem arises when loading the page on smaller resolutions than my monitor. The paginate section exceeds the parent div, resulting in an unsightly display. Below is a snippet of my code: <div clas ...

Steps for Implementing a Delay on Bootstrap Dropdown Hover

Is there a way to create a delay for the bootstrap dropdown feature while ensuring that it still appears on hover? If you want to test this, click here: http://www.bootply.com/YcVBzvXqrR This is the HTML code I'm using: <div class="container"&g ...

IE7 encountering a mixed content error when using html5shiv and SSL

In the process of developing my ASP.Net application that utilizes html5shiv and SSL, I encountered mixed content errors in IE7 specifically when using html5shiv. Interestingly, removing html5shiv eliminated the errors. Additionally, my app incorporates upd ...

Styling text using CSS depending on the displayed text

Utilizing Awesome Tables (AT), I am extracting data from a Google Sheets document to display on a website. The HTML template in the sheets is used for formatting the data, specifically focusing on the table output with inline CSS styling. Since the templat ...

The functionality of the Jquery mobile panel ceases to work properly when navigating between pages within a multi-page

Within a multi-page template setup in a jQuery mobile application, an issue arises after navigating away from the first page using panel navigation. Upon returning to the first page through another form of navigation, the panel appears "hanged." Upon clos ...

Is it possible to continuously loop and gradually fade the same image URL using JavaScript?

I have a dynamic image stored on my web server at example.com/webcam.jpg that is continuously updated by the server. My goal is to display this image on a static HTML page with a looping effect that smoothly transitions from the previous image to the upda ...

Learning to extract information from a JSON file with various key and value combinations

I am facing a challenge with my JSON data file, which contains UserIDs as keys and Passwords as values. My goal is to use JavaScript for validation by reading this JSON file. The structure of my JSON is as follows: IDsNPass = '[{"A":"A15"},{"B":"B15" ...

How can I ensure that Chakra UI MenuList items are always visible on the screen?

Currently, I am utilizing Chakra UI to design a menu and here is what I have so far: <Menu> <MenuButton>hover over this</MenuButton> <MenuList> <Flex>To show/hide this</Flex> </MenuList> </ ...

After fetching an HTML snippet using the $.get() method, Font Awesome icons are no longer visible

Experimenting with creating a seamless 'single-page' experience, I've managed to achieve this by implementing an event listener in jQuery for menu link clicks. This triggers the replacement of the existing content in my main div with an HTML ...

Tips for preventing Razor from interpreting special characters as HTML code

I'm encountering an issue with special characters displaying as HTML codes on the page I'm working on. The content is retrieved from a database and shown in readonly input boxes. For example, & is displayed as &. How can I ensure that the ...