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

What is the best way to retrieve a value from a database and display it in a radio

Can you help me figure out how to implement this functionality? I have a database with a "boolean" field that stores 0 or 1 values. On an HTML page, there is a radioButton with options for OK or NO. I need to dynamically load the boolean value from the dat ...

Integrate an input field with a copy function similar to the GitHub clone view

Can anyone help me create a view with a tooltip similar to the one on Github? You can see the example here: I attempted to use CSS but couldn't quite replicate the exact UI. Here is my current CSS code: [tooltip] { display: inline; position: rel ...

Trigger the function in ng-change each time the same option is clicked repeatedly

Here is the code I am working with: angular.module("myApp", []).controller("myController", function($scope) { $scope.currentOption; $scope.showWindow = false; $scope.myOptions = [{ id: 1, name: 'This opens the window' }, { ...

The Chrome browser's default stylesheet is overriding the styles of my website

I'm experiencing an issue with the CSS styling on one of my website pages. Here is the CSS code for formatting links: a:link, a:visited, a:active { color: white; text-decoration: none; font-weight: bold; } However, despite these styles, ...

Animate the border to move to the next list item after it is hovered over

Seeking assistance to animate border-bottom movement in navbar Desiring animation effect for hover event on second li item The code for the navbar is as follows: .top-bar { background:#0d0d0d; width:100%; height:85px; position:fixed; top:0; z-index:99 ...

How can you achieve three layers of nested quotes in Dynamic HTML?

Working on an app that utilizes JQuery and JQTouch for iOS. The issue I'm facing involves dynamically generated HTML lists where the user clicks a row that needs to be highlighted. However, achieving this has proven tricky due to nesting 3 sets of quo ...

Height problem with the data-reactroot component

On one of my web pages, there is a data-reactroot element with a height of approximately 1906px. However, the surrounding divs and html tags have a height of only 915px. I am trying to figure out how to make all the enclosing elements match the same heigh ...

Unable to retrieve data from PHP using AJAX request

My project consists of 3 interconnected files: index.php, functions.js, and compute.php In index.php, there is a div that triggers a function in functions.js called compute(), which sends an AJAX request to perform a task in compute.php The code in index ...

Struggling with organizing the layout of your HTML page? Let us lend

I am having difficulty with the layout of a page I'm working on. The lower page navigation is not positioning properly - it wraps below the left column instead of staying below the data columns. Additionally, the border for the navigation appears at t ...

Encountering the issue "Error: Uncaught PDOException - SQLSTATE[42000] - Syntax error or access violation: 1064"

I encountered an error that says: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064. It seems to be a syntax error in my SQL statement. Can you help me figure out what's wrong with it? require_once "dbconnect.inc.php"; $e ...

CSS - Apply a captivating background to specific columns within a grid layout

Wow, I'm really struggling with CSS and this particular issue has me perplexed. I need to add background colors to specific columns but not all of them. The problem is that the padding gets messed up when I do this. Imagine having a headache while try ...

Why does tagging P trigger popups in the DIV when the DIV contains another DIV?

JSBIN HTML <p> <div>123</div> </p> CSS p div{ background:#f00; } Encountering an odd issue here. When I input the HTML structure as shown below: <p></p><div>123</div> The CSS code fails to produce ...

Reveal/Conceal footer upon vertical scrolling

I am attempting to achieve the following goals: Display the div element when the scrolling position is greater than 20 Apply a fadeOut effect after a certain delay Prevent the fadeOut effect when hovering over the sticky footer This is my implementation ...

Using the arrow keys to navigate through a list of items without using jQuery

Exploring ways to develop a basic autocomplete feature without relying on third-party dependencies has been my recent project. So far, I have managed to populate a results list using an ajax call and complete fields with mouse onclick events for each optio ...

Test your knowledge of Javascript with this innerHtml quiz and see

How can I display the output of a score from a three button radio button quiz without using an alert popup? I want the output to be displayed within a modal for a cleaner look. I tried using InnerHTML but now there is no output when the button is clicked. ...

When words are enclosed in a table cell, the fixed table layout ensures that all columns are the same size

There are times when long words in my HTML table cells need to be broken if they overflow their designated area. To learn how to break table cell words, check out this question: Word-wrap in an HTML table The recommended CSS style is: style="word-wrap: ...

Troubleshooting Problem with Responsive Bootstrap Navbar

I've been working on creating a menubar using Bootstrap where the logo image is centered instead of being on the left side of the bar. However, I'm facing an issue where the right links in the menu go off the screen. When I view the page with a w ...

"Aligning two images side by side in a horizontal

[enter image description here][1]I am trying to place two images on my website, but I am struggling to vertically align them properly. I have attempted using line-height and vertical alignment, however, I cannot seem to pinpoint the issue or identify any m ...

Utilizing CSS to target the ID of an anchor element

I am a beginner in the world of HTML and CSS. I recently attempted to target an element using an id selector in my CSS code, but unfortunately, it did not work as expected. Can someone shed some light on why this might be happening? <!DOCTYPE html> ...

Is there a more efficient method for creating HTML with coffeescript / jQuery besides using strings?

Today marks my first attempt at creating a "answer your own question" post, so I hope I am on the right track. The burning question in my mind was, "Is there a more efficient way to generate HTML with jQuery rather than using tag strings?" My goal is to c ...