Having trouble getting the form input max-width to work properly within a flex-box

My webpage features a header containing various icons, a title, and a search box that triggers an animation when the search button is clicked. While this setup works seamlessly on larger screens, I encounter issues on smaller screens. My goal is for the search field to expand fully on smaller screens, hiding the icons and title on the left edge. However, I have been unsuccessful in applying the max-width property to the input field.

I have experimented with setting a fixed width for the search field, adjusting flex properties like flex-basis and flex-shrink on different containers, but none of these solutions seem to produce the desired outcome. I am considering simplifying the markup as it currently involves multiple nested flex containers, although this structure was implemented to achieve centered content, responsiveness, and neat spacing.

.wrapper {
  background-color: #DDD;
  overflow: hidden;
  resize: both;
}

header {
  background-color: #AAA;
  justify-content: space-between;
  margin: 0 auto;
  max-width: 1000px;
  padding: 0 2rem;
}

.flex-center {
  align-items: center;
  display: flex;
  gap: 1rem;
}

.icon {
  height: 2rem;
  width: 2rem;
}

.icon {
  flex: 0 0 auto;
  border: none;
  background-color: red;
}

.search-field {
  background-color: green;
  border: none;
  flex: 0 0 auto;
  height: 2rem;
  outline: none;
  padding: 0;
  transition: 0.3s;
  width: 0;
}

.search-field:focus {
  max-width: 300px;
  width: 100%;
}

.search {
  justify-content: flex-end;
}
<div class="wrapper">
  <header class="flex-center">
    <div class="flex-left flex-center">
      <button class="icon"></button>
      <h1 class="title">Title</h1>
    </div>
    <div class="flex-right flex-center">
      <form class="search-form flex-center">
        <input id="search-field" class="search-field" type="search">
        <label class="icon" for="search-field"></label>
      </form>
      <button class="icon"></button>
    </div>
  </header>
</div>

If necessary, I am open to exploring vanilla JavaScript solutions if resolving this issue through CSS alone proves to be unfeasible.

Answer №1

For optimal viewing, ensure your viewport width is at or below 600px. It's important to note that the :has() pseudo-class may not work in Firefox due to limited browser support.

Check browser compatibility on Caniuse

Comments within the code provide additional necessary details.

.wrapper {
    background-color: #DDD;
    ...
}
<!DOCTYPE html>
<html lang="en">
...
            <h1 class="title">Title</h1>
          </div>
          <div class="flex-right flex-center">
            <form class="search-form flex-center">
              <input id="search-field" class="search-field" type="search">
              <label class="icon" for="search-field"></label>
            </form>
            <button class="icon"></button>
          </div>
        </header>
      </div>
</body>
</html>

Answer №2

After some experimentation, I managed to find a solution. By removing display: flex from the header and using position: absolute on the left div, I was able to manipulate the layout effectively. With this setup, I could expand the right flex box to full width without any issues. I have included the updated markup and CSS below. With the addition of a simple media query, I can make the layout adapt to smaller screen sizes seamlessly, eliminating the need for complicated pseudo classes.

.wrapper {
  background-color: #DDD;
  overflow: hidden;
  resize: both;
}

header {
  background-color: #AAA;
  justify-content: space-between;
  margin: 0 auto;
  max-width: 1000px;
  padding: 2rem;
  position: relative;
}

.title {
  font-size: 2rem;
  line-height: 2rem;
  margin: 0;
}

.flex-center {
  align-items: center;
  display: flex;
  gap: 1rem;
}

.left {
  left: 2rem;
  position: absolute;
  top: 2rem;
  z-index: 1;
}

.right {
  flex: 1;
  justify-content: flex-end;
  position: relative;
  z-index: 2;
}

.icon {
  border: none;
  background-color: red;
  flex: 0 0 auto;
  height: 2rem;
  width: 2rem;
}

.search-btn {
  background-color: blue;
}

.search-field {
  background-color: green;
  border: none;
  height: 2rem;
  outline: none;
  padding: 0;
  transition: 0.3s;
  width: 0;
}

.search-field:focus {
  width: 200px;
}

@media only screen and (max-width: 600px) {
  .search-field:focus {
    width: 100%;
  }
}
<div class="wrapper">
  <header>
    <div class="left flex-center">
      <button class="icon"></button>
      <h1 class="title">Title</h1>
    </div>
    <form class="right flex-center">
      <input id="search-field" class="search-field" type="search">
      <label class="icon search-btn" for="search-field"></label>
      <button class="icon"></button>
    </form>
  </header>
</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

Is it possible to trigger a server-side event using jQuery in SharePoint 2010?

For my Sharepoint 2010 application, I have been utilizing jQuery to handle most events on the client-side. However, when it comes to saving data to the Sharepoint list and generating a PDF file with that data, I need to switch to server-side processing usi ...

Trigger a JavaScript function when a key is pressed down

Hey everyone, I'm trying to figure out how to trigger a JavaScript function when a particular key is pressed. For example, I want the function down() to be executed when the down key is pressed and function left() when the left key is pressed. Is ther ...

Issue with Element's Response to JQuery Click Event

After pulling elements from the database in PHP, I utilized Jquery to add Elements to the page. Each button has two classes; one for controlling the GUI and another for handling the click event of that specific button. Here is the code snippet: echo " ...

Utilizing cookies to track the read status of articles - markers for reference

Currently, I am in the process of developing a website and am interested in implementing a feature that allows users to track which articles they have read. I am considering adding a small circle next to each article heading to signify whether it has been ...

Tips for creating a full screen div layout with fixed top and bottom navigation bars

Can someone help me achieve the following layout style? +-----------------------------------------------------------+ | Top Sticky Nav | +--------------------------------------------------------+--+ | ...

Angular 6 - Unlocking the Secrets of Filtered Results

I need help accessing the filteredArray in my .ts component. Currently, it is only accessible within the ng-container. <ng-container *ngIf="(userList | filter: 'name' : value) as filteredArray"> <tr *ngFor="let user of filteredArray ...

Can you help me with sorting asynchronous line points for KineticJS?

For the past couple of days, I've been grappling with a peculiar issue that I found difficult to articulate in the title. The challenge I'm facing involves a KineticJs Line, which contains an array of points (line.attrs.points) represented as ob ...

Internal VS External Stylesheets: What you need to know

I'm currently in the process of developing a website and I have started utilizing the style tag at the beginning: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...

perfecting the styling of a tab management system

For reference, please check out the following link: http://jsfiddle.net/XEbKy/3/ In my design, I have organized two layers of tabs. The top layer consists of two tabs while the bottom layer has three tabs. My goal is to have all these tabs neatly enclosed ...

How to easily incorporate images after text in Bootstrap 4 beta

I seem to be having trouble inserting an image in the "menu" section for the last item "Kava so sebou" after the text. I want the picture to be centered vertically and the row to maintain consistency with the other items above it. Any advice or suggestions ...

Develop a stylish contact form using a mixture of HTML5, Bootstrap, and Php

On my website, I have created a contact us form using HTML, Bootstrap, and PHP. The form functions correctly, and messages are successfully received. However, the issue is that upon clicking the submit button, a new page opens up displaying a "Thank you me ...

Creating a Full-Width Dropdown in Bootstrap 4 Navbar: A Step-by-Step Guide

I'm attempting to create a dropdown that spans the full width of the screen and has a collapsible feature like many modern websites. However, I am having difficulty figuring out how to achieve this. I experimented with using Bootstrap's collapse ...

Troubleshooting: Why isn't External CSS Functioning Properly in Broadleaf

I have encountered a question regarding the use of a custom email template in broadleaf. It seems that I am unable to apply an external CSS file and can only use inline styles. Is this normal behavior, or am I missing something? Below is how I am attempti ...

Splitting hyphenations

Check out my Codepen project I'm attempting to prevent word-breaks inside my .threadTitle class by using display: inline-block. While this solution works, the addition of a hyphen between two letters still causes a break. https://i.sstatic.net/q6ZMk ...

Use jQuery to swap out images and witness the image loading in real time

Currently, I am using jQuery to dynamically change images by using the code $('img').attr('src','newUrl'); However, whenever I do this, the image is only displayed once it has completely loaded. Due to my slow internet conne ...

Positioning MDL cards in HTML documents

Seeking guidance on positioning MDL cards alongside existing text. I've attempted various methods without success so far, and the desired outcome has not been achieved. The goal is to have text aligned to the left (which is currently satisfactory) wi ...

Assigning a class to a header upon loading the page from various sections at random

After scrolling, I used JavaScript to add a class to <header>. Here is the code snippet: $(window).scroll(function(){ var top = $(window).scrollTop(); if(top>=1){ $("header").addClass('bg-header'); } else ...

Using CSS in Rails based on a certain condition

My project involves creating an app with a specific view. Within this view, I must check a condition and if it is met, I need to color a table row accordingly. While the simplest solution would be to modify the header within the view, I prefer to keep al ...

Tips on how to transform an <input type="text"> element into a <tr> element using jquery

I am currently working on updating a row using jQuery Ajax. Whenever the Edit button is clicked, it switches the <tr> element to <input type='text'>. I have utilized the .Change method which triggers successfully, but my goal is to r ...

Tips for Maintaining Table Headers in Place While Scrolling Through a Table

Check out my jsfiddle: http://jsfiddle.net/7vv9e/2/ In the fiddle, click on the "Add Question" button multiple times until a scroll bar appears next to the table. I am looking to keep the header fixed while scrolling. How can this be achieved? Below is ...