When adjusting the max-height and overflow-y in a Boostrap 4 Navbar with a search input/icon placement set to absolute position, the functionality seems to break

While attempting to create a search input with an embedded search icon that works within the Bootstrap 4 navbar when the mobile menu is displayed, I encountered some challenges. The process was much simpler in my previous version using Bootstrap 3...

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

Due to the nature of the application, I have a lengthy 'mobile' menu and therefore would like to set a max-height and overflow-y property for the navbar-collapse to prevent it from covering the entire screen.

However, upon implementing these styles in Bootstrap 4, my search icon no longer remains confined within the input field:

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

The CSS styles I have used for my search inputs are as follows:

<div class="has-search">
    <span class="fa fa-search form-control-feedback execute-quick-search"></span>
    <input type="text" class="form-control quick-search" placeholder="Quick Search">
</div>

.has-search .form-control {
  padding-right: 2.375rem;
}

.has-search .form-control-feedback {
  position: absolute;
  z-index: 2;
  display: block;
  width: 2.375rem;
  height: 2.375rem;
  line-height: 2.375rem;
  text-align: center;
  pointer-events: auto;
  cursor: pointer;
  right: 1.1875rem;
}

I found that by applying max-height/overflow-y styles to the entire .navbar element, I can keep the search icon within the input field. However, this is not the desired outcome as it causes the mobile toggler button to be scrolled off the screen. You can view a fiddle demonstrating this issue here:

Fiddle

Answer №1

To enhance the design, include position: relative to the has-search element. Additionally, implement styles such as max-height selectively for the navbar-collapse only when viewing on a mobile device.

Explore the demonstration provided below:

.has-search { /* UPDATED */
  position: relative;
}

.has-search .form-control {
  padding-right: 2.375rem;
}

.has-search .form-control-feedback {
  position: absolute;
  z-index: 2;
  display: block;
  width: 2.375rem;
  height: 2.375rem;
  line-height: 2.375rem;
  text-align: center;
  pointer-events: auto;
  cursor: pointer;
  right: 1.1875rem;
}
@media (max-width: 991px) { /* UPDATED */
  .navbar-collapse {
    max-height: 150px;
    overflow-y: auto;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Dropdown
            </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
    </ul>
    <div class="has-search">
      <span class="fa fa-search form-control-feedback execute-quick-search"></span>
      <input type="text" class="form-control quick-search" placeholder="Quick Search">
    </div>
  </div>
</nav>
<h1>Hello, world!</h1>

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

Using jQuery sortable with the overflow property set to hidden to sort items between two lists

I need help with a jQuery sortable feature where I have two lists and can move items between them: $( '#productsList, #orderList' ) .sortable({connectWith: '.containerDiv'}) .disableSelection(); My issue arises when I try to implement ...

Sliding a DIV using Jquery to occupy the top half of the page

I am attempting to create a functionality similar to what is found on Microsoft's Support Website. You can check it out here: (Visit Here) By clicking on any product listed there, a drop-down menu appears. (Please note that the purpose of this articl ...

Enhance your table layout with Bootstrap 3 by adjusting textarea size to fill

I am currently in the process of developing a forum and bulletin board software. I am facing some challenges while trying to integrate a reply section into the thread page. You can view what I have implemented so far here. Html: <div class="panel pane ...

In Internet Explorer, the list items are overlapping, while functioning perfectly in all other web browsers

I've encountered a strange issue with my list item in Internet Explorer. Despite working perfectly in Chrome and other browsers, it fails to function properly in IE. I've built this application using the Vue framework and have faced various probl ...

Tips for applying the same style to multiple classes:

I have experience with applying the same styling to multiple classes by using a comma between the class names. However, I am unsure about the following code: .navbar-inverse .navbar-toggle { background-color: #333; } ...

Troubleshooting Problems with CSS Span Placement

Challenges with Span Positioning in CSS Currently, I am encountering difficulties while working on the index.html.erb file for an example Rails website. Below is a snippet of the HTML code I am struggling with (please note that this is simply a fabricated ...

Is it possible to use styled-components to specifically style a child class within a component?

Hey there, I'm currently working with a mui Badge component and am trying to customize the appearance of the small circle. This is what I have so far: const BadgeStyled = styled(Badge)` & > .muibadge-badge: { background-color: #d3d4d7; ...

Change all lowercase characters to uppercase in the font file

: I recently created a .ttf font file, but unfortunately only included capital characters. Is there a tool or simple method I could use to automatically create lowercase versions of each letter? For instance, when typing "hello" the font should display ...

Show two <p>'s next to each other

I am trying to arrange 2 paragraphs side by side. <p class = "firstClass">This is the first paragraph.</p> <p class = "secondClass">This is the second paragraph.</p> Result: This is the first paragraph. This is the second paragra ...

What is the best way to incorporate a Bootstrap spinner icon in the center of a webpage's background?

Every time my HTML page reloads, I want to display a background loader icon. Although I have tried using the Bootstrap spinner icon, I'm facing difficulties in positioning it at the center of the page. <html> <style> .overlay { back ...

Creating two tables in JavaScript and styling them with separate CSS designs

I am in the process of creating two tables dynamically using JScript. Instead of using the traditional method of separating them with assigned tags in HTML, I have approached the creation of the first table in a different way: function makeCells() { v ...

Failure of ngClass in Angular 7 due to the presence of multiple classes (spaces)

Many frontend frameworks have a practice of encapsulating their CSS styling by adding another class as a prefix. For example, in Bootstrap: btn btn-primary where btn is the prefix. If I were to conditionally apply this using [ngClass] in Angular, it woul ...

Designing a layout with one box on top and two beneath it, covering the entire page, can be achieved by following these steps

https://i.sstatic.net/A1eUh.png I am currently working on a project where I want to divide the screen into three sections. One section will cover half of the screen to display an image slider, and the remaining half will have two sections which is already ...

JavaScript for creating dropdown menus using Twitter Bootstrap

I've been struggling to get the dropdown menus in my Twitter Bootstrap project to function properly. Below is the code I have for the navbar: <div class="container-fluid"> <div class="row-fluid"> <div class="span12"> < ...

use bootstrap to align text vertically according to the image

I am trying to figure out how to center a text vertically next to an image. While I know how to align text horizontally using text-center in bootstrap, I am unsure about how to align it vertically. Is there a way to accomplish this using bootstrap? < ...

Design of web pages, Cascading Style Sheets

I want to add another table next to my dataGrid, floating to the left of it. Like this representation in the main div: | | | | A | B | | | | Here, A represents the current dataGrid containing all user information, and B will be the ...

Position the list-group to the right using a float

I am new to Bootstrap and I'm facing a challenge with arranging my list items. I have 15 items in total, and I want the last 7 items to be aligned on the right side (see the image for reference). Despite trying out various tutorials, I haven't be ...

Experiencing an issue while rendering due to the presence of display: table-cell;

I am currently in the process of developing a website. The HTML Header section is structured as follows: <div id="page"> <header> <a href="#" class="redsquare">&nbsp;</a> <div class="head-wrapper"> ...

IOS Troubleshooting: Ineffectiveness of the Transform

So I am encountering a slight issue while trying to incorporate this code on iOS because I am not familiar with how iOS operates. On my website, I have implemented a circle that works perfectly fine on browsers and Android devices. However, when it comes t ...

The percentage height setting for a div is not functioning properly, but setting the height in pixels or viewport

Within a dialog box body, I am attempting to display a table and have applied a CSS class to the wrapping div. When specifying the height in pixels or viewport height units, it works as expected. However, when using a percentage like 50%, the height of the ...