Why isn't the max width applying to an input element wrapped within a Div?

Hey there! I recently created a Search Bar and wrapped it inside a div with a max-width of 500px, but for some reason, the max-width is not working as expected. I searched on Stack Overflow and came across a helpful link on Why would max-width not work on this? but unfortunately, the solution provided didn't work for me.

Can someone please shed some light on why it's not working and how I can fix it? Any guidance or assistance would be greatly appreciated!


        *{
          margin: 0;
          padding: 0;
          box-sizing:border-box;
        }
        body{
          font-family:arial;
        }
        header{
          width: 100%;
          padding:20px 20px;
        }
        nav{
          display:flex;
          justify-content:space-between;
          align-items:center;
        }
        .menu-container{
          display:flex;
          justify-content:space-between;
          align-items:center;
          position:relative;
        }
        .search-input-container{
          width:100%;
          max-width:500px;
        }
        input[type="search"]{
          border-radius:500px;
          padding:5px 10px;
          width:100%;
        }

        ul li{
          list-style:none;
        }
        ul{
          margin:10px;
        }
        button{
          outline:none;
          border:none;
          padding:8px 15px;
        }

        a{
          text-decoration:none;
        }
        .submenu-items{
          position:absolute;
          top:100%;
          border:2px solid red;
        }
      
<!DOCTYPE html>
        <html lang="en">

        <head>
          <meta charset="UTF-8>
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Document</title>
        </head>

        <body>
          <header>
            <nav>

              <!--  Logo Wrapper -->
              <div class="logo">
                <a href=""><img src="" alt="Test Logo"></a>
              </div>

              <!--  Search Wrapper || Issue Area-->
              <div class="search-container">

                <form action="get">
                  <div class="search-input-container">
                    <input type="search" class="search">
                  </div>
                </form>

              </div>
              <!--  Search Wrapper Ends ↑ -->

              <div class="menu-container">
                <ul>
                  <li>
                    <a href="">Help</a>
                  </li>
                </ul>
                <ul>
                  <li>
                    <a href="">Campaigns</a>
                  </li>
                </ul>
              </div>

              <div class="buttons-container">
                <a href="#"><button class="Login-btn">Log in</button></a>
                <a href="#"><button class="signup-btn">Sign up</button></a>
              </div>

            </nav>
          </header>
        </body>

        </html>

Answer №1

Instead of relying on max-width, consider utilizing width: clamp() for more flexibility (MDN)

For your specific situation, you could use the following code:

.search-input-container {
  width: clamp(150px, 400px, 500px);
}

With this code, you set the minimum size to 150px, the ideal size to 400px, and the maximum size to 500px, ensuring that the element won't exceed 500px in width.

By using this approach, the input will aim to be 400px wide, but will adjust down to 150px if needed due to lack of space.

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

Guidelines on integrating Admob into Ionic framework

I tried following the steps outlined in this post: AdMob not loading ads in ionic/angular app After running the app using "ionic build ios && ionic emulate ios," I still see no ads, no black bar, nothing at all. Can someone help me figure out wha ...

Sharing the input value with a service in Angular 4

I am a beginner when it comes to Angular 4. I currently have a variable named "md_id" which is connected to the view in the following way. HTML: <tr *ngFor="let item of driverData"> <td class="align-ri ...

Looking for assistance with CSS selectors for the following structure

I am currently working on locating the anchor element "a" that is nested inside a table with the class name table.ic-table-creditReportProduct table tr. I have attempted to find it, but my attempts have been unsuccessful so far. Any suggestions on where th ...

angular ui-grid event: column clicked

I want to retrieve the selected column value based on the selection of a column in a UI grid. Once I have this value, I need to filter another UI grid table using it. How can I trigger a function when a column is selected and access the selected column val ...

Encountered an error in React where the declaration file for a module could not be located

New to Typescript and trying to incorporate a splitter into my project. I'm utilizing SplitPane from "react-split-pane/lib/SplitPane" and Pane from "react-split-pane/lib/Pane" in my Typescript project, but encountering an error: Could not find a de ...

What is the method for ensuring a variable returns a number and not a function?

I've encountered a perplexing issue that I can't seem to solve. What could be causing the code below to output a function instead of a number? let second = function(){ return 100 }; console.log(second); ...

Modify the selected toggle buttons' color by utilizing the MUI ThemeProvider

I am currently working on customizing the color of selected toggle buttons within my React app using TypeScript and ThemeProvider from @mui/material 5.11.13. Despite my efforts, when a toggle button is selected, it still retains the default color #1976d2, ...

Typescript encountering onClick function error during the build process

My current challenge involves creating a submit function for a button in my application. However, when I attempt to build the project, I encounter a typing error that is perplexing me. Despite trying various methods, I am unable to decipher how to resolve ...

What is the best way to create an arrow that tracks the browser window, but only goes as far as the end of its designated container

Currently, I am facing a challenge in implementing a scroll down arrow. While this is typically a simple task, it has proven to be a bit more complex this time around. The complexity arises from the fact that my customer desires a homepage with an image of ...

$scope.apply is triggering both "catch" and "then" handlers

I am trying to ensure that the content of a page in Angular 1.6.2 and UI router is only displayed once it has been confirmed on the server that the user has the appropriate role/permissions. It seems like without using $scope.apply(), the catch() function ...

Using AngularJS to filter through multiple criteria within an ng-repeat loop

I need to filter the messages data to exclude certain messages based on the userid. For example, in the scenario below, only messages from Paul (userid: 11) & Kate (userid:12) are displayed. What I want to achieve is to filter out more than just one useri ...

React components featuring Material UI icons

I'm in need of assistance. I am looking for the Material UI icons, but I can't seem to find any information on how to obtain them. https://i.stack.imgur.com/FAUc7.jpg ...

How can I update the image source using Angular?

<div class="float-right"> <span class="language dashboard" data-toggle="dropdown"> <img class="current" src="us-flag.png" /> </span> <div class="dropdown dashboar ...

Deactivate the backface culling feature when loading an imported .obj file in three.js

I'm struggling with disabling wireframe rendering on the rear side of my vehicle. Any ideas on how to deactivate backface culling in wireframe mode for three.js? Check out this link for a helpful image ...

What is the most effective way to incorporate the Google Analytics script into a Django project?

Is it recommended to add the Google Analytics script directly in <script> tag and push it to GitHub, or should it be kept secret by adding it to the .env file and called in? When I attempted the second method, it appeared in the source page as is an ...

Adding various image files to the canvas

I am facing an issue with my code where I need to find images inserted by the user into a div and then combine them into one image in a canvas when the "snap" button is clicked. While I can successfully locate, position, and resize the images inside the ca ...

What is the best practice for incorporating CSS and JavaScript files into a PHP template?

I've created a basic template for my PHP website, but I'm struggling to find the best way to include my CSS and JavaScript files. Take a look at my index.php file below: <?php include'core/template.php'; $temp=new Template(); $sett ...

Issue with Angular: Unable to locate a differ that supports the object '[object Object]' of type 'object'. NgFor is only compatible with binding to Iterables such as Arrays

As someone who is new to Angular, I am facing a challenge while working on my portfolio project. The issue arises when trying to receive a list of nested objects structured like this: "$id": "1", "data": { &quo ...

Switching between all tabs simultaneously in an MDX page (React + Gatsby + MDX) using @reach/tabs

Creating a Tab component for a Gatsby site has proved to be a bit tricky. Imagine having a page with multiple tabs all labeled the same: Heading 1 First tab block Tab 1 | Tab 2 Content Tab 1 Second tab block Tab 1 | Tab 2 Content Tab 1 for the second bl ...

submitting a form including a file through ajax communication

For the past two weeks, I've been trying to find a solution to my unique problem. I've created a custom form that I need to upload along with one or more files. Our backend is running ruby on Rails, and the data must be formatted into a specific ...