Animating the search bar expansion using CSS transition in Bootstrap 4

I am currently experimenting with creating an expand-on-hover search form within a Bootstrap 4 navbar. While I have achieved a smooth transition effect on the search form upon hover, I have noticed that the menu next to it abruptly jumps. In the past, I successfully implemented a similar layout using Bootstrap 3 and floated elements, resulting in a seamless sliding effect. However, with flexbox, I am encountering a different behavior. As I continue to delve into flexbox, I wonder if there is a way for all elements in the line to slide over smoothly without relying on floats.

View Example JSFiddle Here

#secondaryNav {
  justify-content: flex-end;
  transition: all 0.5s;
}

#secondaryNav #searchform {

  .input-group {
    justify-content: flex-end;
  }

  .search-form-control {
     transition: all 0.5s;
     max-width: 0;
     overflow: hidden;
  }    

}

#secondaryNav #searchform:hover {

    .form-control {
        max-width: 100%;
    }

}

Answer №1

Check out this complete example showcasing a functional solution. Make sure to specify the width for the input field in both its normal and focus states. Don't forget to include transitions for a seamless effect.

#search{width:75px;transition:width .5s ease}
#search:focus{width:200px}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<nav class="navbar navbar-expand-sm navbar-dark bg-dark fixed-top">
  <div class="collapse navbar-collapse">
    <ul class="navbar-nav mr-auto"></ul>
    <div class="text-light mr-4">Lorem Ipsum</div>
    <form class="form-inline my-2 my-lg-0">
      <input id="search" class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

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

The blur() function in -webkit-filter does not seem to function properly in Vue.js

I'm having trouble implementing the -webkit-filter: blur() property. I tried using "filter" instead, but it still isn't working. Could this be a limitation of Vue.js or are there any other solutions available? <template> <div class=&qu ...

Direction of hover effect (crossfade image)

My goal is to create a hover effect that involves crossfading images. The setup includes two images: img-2 and img-1, along with text overlayed on top, creating layers as follows: [img2]>[img1]>[text]. When hovering over the text layer or the front i ...

Learn the steps to achieve the following outcome with Bootstrap 4

After experimenting with progress bars, I found that the problem lies in displaying the remaining part of the progress bar. https://i.sstatic.net/S35jd.png For instance, when I specify a width for the progress bar, the remaining percentage appears as gra ...

Styling Tip: Removing a header from the initial page of a chapter using CSS

Using CSS to generate HTML to PDF, I have successfully added page numbers at the bottom and chapter names at the top of each page using @top-center in the CSS and HTML code below. The rendering is done with Prince. However, I need a way to exclude the hea ...

Attempting to embed an image within the datepicker input

After successfully implementing the datepicker to select a date, I decided to add a buttonImage option for opening the datepicker on image click. However, I encountered an issue when trying to place my calendar image inside my input element, floated to th ...

grab the content from a text editor and insert it into a div element

Currently, I am trying to extract text from my content editable div and place it in another div (similar to the one seen on stack overflow). However, I am encountering a frustrating issue. 1. My content div seems to be lagging behind. When I type in my ed ...

firefox is experiencing lag issues with react-spring and framer-motion

Encountering an issue with two animation libraries, react-spring and framer-motion. Attempting to create a basic animation that triggers upon the component's initial visibility (simplified version). <motion.div initial={{x: -25, opacity: 0}} anima ...

Trouble with the navbar collapse functionality in Bootstrap

3 I made some changes to the navbar, now the navbar-header is showing but the collapse navbar-collapse is not. Here is my code. Can you spot where I went wrong? <nav class="navbar navbar-default"> <div class="container-fluid" ...

Uniquely Designed JQuery Alert Box

One issue I am facing is that I have two types of alert boxes, one for success and the other for error. In my Zend-based application, these alert boxes show up when users fill out forms such as profile updates or new submissions. My goal is to customize th ...

Adjust time according to specified time zone using JavaScript

I am working on a project involving clocks and timezones. The user should be able to choose a timezone from a combobox, and upon selection, the main digital clock should update to display the time for that timezone. I have a text file called 'zone.txt ...

Entering credit card information in a modal popup using Bootstrap form

I have implemented a bootstrap modal for my credit card payment form and now I want to validate the credit card information. Currently, I am using basic validation in jQuery. <script> $(function() { $('#error_message_reg').text(""); //$( " ...

Ways to enhance an image by zooming in when the user reaches a designated area on the webpage

I have implemented a feature where an image zooms in to letter L when the user scrolls on the page. However, I want this zoom effect to occur only when the user reaches a specific section of the site, rather than immediately when the image loads. In my exa ...

Activate a CSS class on click using JavaScript

Having a bit of trouble as a beginner with this. Any help would be much appreciated. This is the code in question: HTML: <div class='zone11'> <div class='book11'> <div class='cover11'></d ...

Tips and tricks for ensuring images maintain their aspect ratio and height when resizing for responsiveness

I am facing an issue with my two-column layout. One column contains an image and the other column has text and buttons with a fixed height. To ensure the image is responsive, I have set the width to 100% and height to auto. However, upon resizing, the aspe ...

The entire title serves as a hyperlink, not just the text portion

Apologies if I'm making mistakes, I'm just starting out with coding and navigating this website. So here's the deal: I am trying to add advertisements to my website on the left and right sides of the centered "Logo" at the top. The issue i ...

Please refrain from initiating the next action until the previous action has been completed

As a beginner, I am currently working on a photo viewer project using JavaScript and jQuery. My main issue arises when clicking on the arrow to view the next picture. I want the current picture to fade out smoothly before the new one fades in. However, th ...

Eliminating unnecessary CSS from the codebase of a website

Currently, I am making adjustments to a website template that I downloaded for free online. I have noticed that even if I delete a div from the code, the corresponding CSS styles remain in one or more files. Is there any tool available that can automatic ...

"Trouble with Material-UI DataGrid: Column headers are being cut off

I am facing an issue where my column names are getting cut off even though they should fit in the available space. https://i.sstatic.net/kmKFi.png ...

Steps to apply styles to an iframe element directly from its inner HTML code

Looking for help with this line of code: <iframe src="http://www.google.com" name="google" style="Z-INDEX: 0; MARGIN-TOP: -1px; WIDTH: 100%; HEIGHT: 1070px"> <html> <head></head> <body> < ...

Scrolling horizontally with Bootstrap 4

I'm having trouble with this issue: I need my "Factuur" (Dutch for invoice) to always be at a width of 800px, but on mobile devices, the rows are collapsing into each other. I want the invoice to remain static and be scrollable. Below is my CSS code: ...