我有一个实时搜索栏,仅在移动屏幕上显示.

另请参阅以下内容:

.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/bootstrap@5.3.2/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>

使ul元素可在指定高度内垂直滚动,而不影响li元素. 我使用过Bootstrap 5.3

我已经try

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

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

我在这里也try 过解决方案

Overflow:hidden and overflow-y: scroll on ul not working;

推荐答案

我把max-height: 20vw;加到overflow-y: auto;加到.search-results.并且,从.search-results li中删除了overflow:hidden;.

.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/bootstrap@5.3.2/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>

Html相关问答推荐

HTML CSS如何使用图像作为中心点将元素置于中心

如何在伪元素背景中定位多个CSS径向梯度

在窄屏幕上显示表格,每个单元格占一行

无法从路径参数获取ID

如何在排序上重用参数?

从网页中提取URL

如何从elementor中的滑块中获取文本?

禁用所有行,但在16角中单击编辑按钮的行除外

带有排序元素的 CSS 网格

网格中的图像zoom 不正确

带有数据 URI 的音频在 Chrome 中失败

当我们在vue中使用截断大文本时如何显示标题属性?

使用flex wrap时,如何设置另一个元素的宽度与换行后的元素对齐?

BeautifulSoup用名称列表的findall无法找到另一个目标后面的目标

需要帮助个性化我的 CSS 导航栏:如何在鼠标悬停时突出显示
  • 元素?
  • 如何清除html输入中的文本

    有没有办法让 textarea 在调整大小时不显示 CSS 转换?

    样式不适用于来自 tailwindcss 的网络

    CSS 变量不适用于自定义属性回退值

    两个按钮范围滑块的 CSS