Trouble arises when attempting to make an expanding search bar with the :focus-within pseudo-class

I am looking to create a search bar similar to the one on this website: .

I attempted to do so using the following code: (Here's a codepen)

HTML:

<div class="col-md-4 d-flex justify-content-end all-buttons">
  <div class="col-md-4 d-flex justify-content-end search-bar">
    <a  nbButton  outline class="text-decoration-none add-btn" id="add-btn-hide" >
      <i class="nb-plus fa-1x"> </i> Add User
    </a>

    <button nbButton class=" multi-delete-btn" id="delete-btn-hide" >
      <i class="nb-trash fa-2x"></i>
      Delete All
    </button>

    <input type="text" class="search-input" id="search-input" fullWidth nbInput placeholder="Enter Search" (keyup)="onKey($event)" />
    <button nbButton class="search-btn" nbTooltip="Quick Search " nbTooltipPlacement="top" status="primary">
<!--       <nb-icon icon="search-outline"></nb-icon> -->icon
    </button>
  </div>
</div>

CSS:

body{
  margin:40px;
}

.all-buttons {
  height: 30px;
  padding-right: 0;
}

.info-btn {
  padding: 0.4rem;
  border-radius: 0.25rem;
  margin-right: 3px;
  background-color: blue;
  border-color: blue;
}

.advanced-filters {
  background-color: green;
  padding: 0.6rem 0 0 0;
  border-radius: 0.5rem;
  margin-left: 1px;
}

.multi-delete-btn {
  margin: 0 8px 0 3px;
  color:white;
  background-color: orange;
  border-color:orange;
  border-radius: 0.25rem ;
  padding: 0 5px 0 0 ;
  opacity: 1 !important;
   transition: width 0.5s ease-out;
}
.add-btn {
  border-color:blue ;
  background-color: blue ;
  color: white;
  padding: 0.5rem;
  border-radius: 0.25rem;
  margin: 0 ;
  opacity: 1 !important;
   transition: width 0.5s ease-out;
}
.search-bar {
  border: none !important;
  margin-right: 2px !important;
  border-radius: 100px ;
  min-width: 33px !important;
  position: relative;
  transition: width 0.5s ease-out;
}
.search-input {
  background: transparent;
  border: 0;
  background-color: lightblue;
  opacity: 0;
  width:40px !important;
  transition: width 0.5s ease-out;

  &:focus {
    outline: 0;
    
  }
}
.search-btn {
  cursor: pointer;
  border: 2px solid blue;
  border-radius: 0.25rem !important;
  background: transparent;
  margin: 0 !important;
  position: absolute ;
  top:0 ;
  bottom:0;
  right: 0;
}

.search-bar:focus-within{
  width:50% !important;
  border: 2px solid brown !important;

  .search-input{
    width: 100% !important ;
    background-color: rgba(196, 158, 233, 0.205);
    color: black !important;
    cursor: initial ;
    opacity: 1;
  }
  .add-btn{
   display: none !important;

  }
  .multi-delete-btn{
   display: none!important;

  }
}

The problem:
After clicking outside of the search icon, the "Add" button and "Delete-all" button should appear. However, in my case, they are only moving. (When I click the search icon, the add-button and delete-all button disappear – which is correct)

Answer №1

Exploring a unique method to isolate the search input and its container. The approach involves positioning the input absolutely within a relative container, allowing for expansion from right to left by anchoring it to the right side of the parent container using right: 0.

A combination of focus-within and placeholder-shown is utilized to adjust the size of the input. Implementing placeholder-shown prevents the input from shrinking if text has been entered. To conceal the placeholder text and only display the search icon when the input is shrunk, the color of the placeholder text is set to transparent.

.search-input-container {
  position: relative;
}

.search-input {
  --search-icon-width: 24px;
  --search-max-width-expanded: 20rem;
  --search-max-width-collapsed: 2.3rem;
  --search-color: #ccc;
  background-color: var(--search-color);
  max-width: var(--search-max-width-collapsed);
  padding: .5rem 0.75rem;
  overflow: hidden;
  transition: 0.3s max-width, 0.3s margin;
  border: none;
  background-image: url(https://image.flaticon.com/icons/png/512/93/93642.png);
  background-size: 18px;
  background-repeat: no-repeat;
  background-position: calc(100% - 10px) 50%;
  position: absolute;
  right: 0;
  font-size: 16px;
  border-radius: 15px;
}

.search-input:focus-within,
.search-input:not(:placeholder-shown) {
  max-width: var(--search-max-width-expanded);
  padding-right: var(--search-icon-width);
}

.search-input:not(:focus-within)::placeholder {
  color: transparent;
}


/* Ignore */

body {
  display: grid;
  place-items: center;
}
<div class="search-input-container">
  <input placeholder="search" type="search" class="search-input">
</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 come the pop-up isn't displaying in the middle of the screen?

I have encountered a positioning issue while using a semantic modal with Angular. The problem arises when I try to display the modal as it does not appear in the correct position. https://i.sstatic.net/o033E.png Below is the code snippet from my HTML fil ...

issue with the margin-bottom property not functioning as expected

Question about CSS code: .dropdown-menu { position:absolute; top: 100%; left: 0; z-index: 1000; display: none; float: left; min-width: 160px; padding: 5px 0; margin: 2px 0 0; list-style: none; font-size: 14px; background-color: #ff ...

The widths specified in the td tags do not take effect on tables with auto width

Check out this fiddle: http://jsfiddle.net/LHsLM/1/. I'm attempting to set the height and width of an auto-width table using inline CSS, as shown in the fiddle. However, the styles are not taking effect. Here's the HTML: <div id="a">< ...

Is there a way to showcase the newly added state object on the page without having to reload the page?

I am currently showcasing the attributes (recipe title, image URL) of items from a state. These items originate from mongodb. Below is my current code flow: Create an empty array state variable named initialRecipes using const [initialRecipes, setRecipes] ...

PHP and mysqli combine to create a versatile image slider with changing images

I am looking to implement an image slider that can be updated by the admin using php and mysqli. The admin should have the ability to easily add or remove images as needed. ...

Encountering a z-index problem while viewing videos in 768 resolution on Safari and IE8

Currently, I am encountering a z-index problem with videos in IE8 and Safari browsers. Click here to visit the link Even after reducing the z-index, the video on this page appears on top at 760 resolution. It does not seem to respond to any properties. F ...

React's struggle with implementing flexbox functionality

Struggling to implement a calculator using React and flexbox, but running into issues with my flexbox layout. I've attempted to troubleshoot using the Chrome Dev Tools but haven't made any progress. import React, { Component } from "react"; imp ...

Troubleshooting problem with scrollbar in HTML table within a div element

I'm utilizing Bootstrap for the majority of my CSS. I've split a section into two columns, with the left column displaying a table with data and the right side featuring a Google map. The issue arises when the table row grows, as it doesn't ...

Tips for retrieving the image source value with TypeScript

I've been attempting to access a JSON API in order to fetch some specific data such as titles, categories, and images. The issue I'm facing revolves around retrieving the image sources. Here's what I've tried: field_image[0].src fie ...

Interactive Show Map with Autocompletion Feature

I attempted to implement autocompletion for my application and integrate it with Google Maps, but I'm encountering issues. The code for autocompletion is located in a separate JavaScript file called autocomplete.js. Since I already have a Google Map ...

Troubleshooting: Absence of Link Style in Lotus Notes 8.5 Email Client

While creating an HTML email and testing it with Litmus, I noticed that Lotus Notes 8.5 is not showing any link styles. Despite using traditional methods to ensure compatibility with older mail clients, the links are still not displaying correctly in Lotus ...

Troubleshooting problem with CSS hover effect on background images

Can anyone assist me with creating a simple mouse hover effect on these two images? I am having trouble getting it to work. I am looking for a fade transition to occur when the mouse is over the images. Thank you in advance! * { padding: 0; margin: ...

Customizing nvd3 chart colors with CSS: A step-by-step guide

Currently, I am faced with a challenge in SugarCRM as I try to modify the colors of nvd3 charts using CSS. The pie chart colors are structured differently compared to other nvd3 charts, making it difficult for me to figure out how to customize them through ...

Incorporating an unique identification code within the input field

I have a HTML code that displays geolocation information: <span id="currentLat"></span>, <span id="currentLon"></span> When combined with JavaScript, it works perfectly. However, I am trying to display the above value in a text fi ...

Ways to ensure that a div's height matches its width

Is there a way to make three divs, each with the CSS property display:inline-block, have a 1:1 ratio of width to height using only CSS? Currently, the width is set to 30% and the height to 75%, causing the width to be greater than the height. #our_servi ...

Changing the special characters in ASP.NET MVC

I'm facing an issue with c# and html code that involves special French characters : <html> <head> <link rel="stylesheet" href="~/Content/jquery.treeview.css" /> <script src="~/Content/jquery.cookie.js" type="text/javascri ...

Troubleshooting Error in Angular 2 API: Issue with Reading Property "1" from Undefined

I have encountered an issue with my project as I am fetching data from an API on a server and trying to populate one of the objects with the retrieved data. However, the console is displaying an error stating that the Object is Undefined. Here is the erro ...

Creating a full-width tab card with Bootstrap-Vue: A step-by-step guide

I stumbled upon something similar in the documentation for bootstrap-vue: A card with tabs: https://i.sstatic.net/HoVEC.png Now, how can I style the tabs to look like this: https://i.sstatic.net/dbsRj.png This is my current code: <b-card no-body ...

The Select2 choices are not being updated correctly

I am in the process of implementing a feature on my website that allows users to select a Country -> State -> City. The state and city select boxes are initially set to disabled, and when a user chooses a Country, it should display the corresponding ...

Implementing a text field to initiate an Ajax request

I need help with triggering my Ajax function whenever a text field is changed. As someone who is new to Ajax, I'm not sure if this is even possible. Any guidance on how to achieve this would be greatly appreciated! Html <form> <a>Ent ...