The input field stubbornly refuses to conform to the specified width

I have been working on making the search bar in the top right container responsive so that it shrinks down enough to prevent overlap with other icons. Here is an example of what I am trying to achieve:

Image showing issue at 600px window size

To achieve this, I am utilizing media queries and CSS variables for a responsive design.

My attempts so far include:

  • Setting a max-width with a specific pixel value for the input field using CSS variables
  • Adjusting the widths of all items in the row manually and then applying CSS variables
  • Adding a margin-right to the text input field (which solved the overlapping issue but doesn't feel like the right approach as the icons still interfere with the text field)

Any suggestions or solutions to this problem would be greatly appreciated.

Desired look of the search bar row for all window sizes

PS. Note that the icons may not load, however, when testing the code snippet on Stack Overflow, the layout remained consistent without the icons.

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}

/*---------------MEDIA QUERIES-----------------*/
@media screen and (max-width: 360px) {
    :root {
        --sidebar_heading: 20px;
        --sidebar_sub_heading: 15px;
        --container_padding: 15px;
        --header_height: 16.5vh;
        --content_height: 83.5vh;
        --search_max_width: 100px;
        --bg: orange;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 30px;
    }
}

...
{/* Styling information omitted */}
... 

<!DOCTYPE html>

<html>
  <head>
    <title>Admin Dashboard</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="sidebar-container">
        ...
        {/* Sidebar details omitted */}
      </div>

      <div class="header-container">
        ...
        {/* Header details omitted */}
      </div>

      <div class="content-container">
        <div class="box"></div>
      </div>
    </div>
    <script src="" async defer></script>
  </body>
</html>

Answer №1

In my opinion, achieving the desired layout could be much simpler without relying on @media queries. Instead, utilizing display: grid provides a more elegant solution and avoids the complexity of managing multiple width breakpoints.

For instance:

*{
  font-size: 3vmin;
}
header{
  display: grid;
  grid-template:  1fr / 1fr 2fr 1fr 1fr 1fr;
}
h3{
  align-self: center;
  justify-self: center;
}
<header>
  <button>search</button>
  <input type="search"/>
  <button>icon</button>
  <button>icon 2</button>
  <h3>name here</h3>
</header>

By using fractional units to set the sizes in the grid, elements will not overlap, and instead trigger overflow-x if the viewport is too small. To address overflow, adjust the size of elements inside the grid relative to the viewport. In this example, the font-size was set to 3vmin, but it can be applied to icons, input boxes, or other components. Additionally, setting a minimum height for the grid ensures proportional scaling within the document.

Another advantage of adopting grid is its consistent behavior with justify-self and align-self, which may vary with children of a flexbox.

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

Enhance the bubble charts in Kendo UI Graph by adding dimension and depth to the bubbles for a more visually

Is there a way to create a relief effect on the bubbles in Kendo UI Bubble charts? Currently, all bubbles appear flat with the 15 provided themes. You can see an example here: It would be great to have a 3D-style option similar to the pie chart (Uniform s ...

Is there a way to sort a table using a dropdown menu selection as a filter?

I am currently working on a project that involves a table with three columns, which are typically populated from a SQL database. My goal is to filter the third column based on the value selected from a dropdown menu. I came across a helpful tutorial on W3 ...

Implementing a click event listener to target a specific div using JavaScript

In my JavaScript code, I have implemented a snowfall effect. I am looking to create a click event specifically on the 10th snowflake that falls down. Additionally, I want to add a class called "clickable" to this snowflake. The goal is to redirect the user ...

How to Create a Speech Bubble in SVG Using SnapSVG

In the process of developing a chat program, I have animated figures moving across the screen engaging in conversations. One crucial aspect I am yet to implement is creating scalable speech bubbles for when users interact. Being relatively new to SVG and ...

Using WebDriver Selenium to choose an element within a table following another element

The webpage features a user details table with a functionality to delete users. To remove a user, I must select the row based on the username and then click the "Delete" button. The structure of the HTML table is as follows: <table id="tblUsersGrid" ce ...

What is the best method for positioning span content above all other elements without having it wrap around them?

Each form element on my page is accompanied by a span that displays an error message, if applicable. The layout consists of two columns: left and right. I am struggling to make the span display seamlessly from the left column all the way across the page wi ...

Guide to adding a Json file in a PHP file with PHP

I have a PHP file with an embedded JSON file, and I want to update the JSON file with new information from a form. The form looks like this: <form action="process.php" method="POST"> First name:<br> <input type="text" name="firstName"> ...

Replace Choose File with Image Upload

Instead of the standard "choose file" option, I want the image browser to function like the choose file button. The code I'm using doesn't seem to be working properly. print ("<tr><td align='right' style='font-weight ...

Fetch data using Ajax without the need for a hash signal

My current goal is to load content through Ajax on a website. Let's say our main page is example.com/blankpage (I've eliminated the .html extension using htaccess). At the moment, I have it set up so that when a link on the page directs to mysite ...

Is the attribute of the label malfunctioning within an Angular directive?

As I delve into the world of angular directives, I have encountered success in many aspects. However, there is one minor issue that has been troubling me lately. Within my directive, I have set a for attribute to match the id of an input field. Strangely, ...

The margins within the div of the new tab are not applying as expected

Greetings! Currently, I am in the process of generating a dynamic form within a new tab using JavaScript. For some reason, the margin effects on the div element are not being applied correctly. Despite my attempts to set the margins using both classes and ...

Execute a task on Mobile Safari (after a delay)

I'm currently experimenting with HTML5 on Mobile Safari for iOS 6 and I'm curious about executing JavaScript code after a certain amount of time has elapsed. One specific task I am interested in is redirecting to a different page after 60 seconds ...

Increasing a local variable in the view using ASP.NET

Despite its seeming simplicity, I'm struggling to find the solution to this problem. When adding a number (like 2) to a page variable, it's being recognized as a string and displaying: instead of 3 : 1+2 @{ var page= 1 ; } <li>@ ...

tips for displaying a label and text side by side on a single line

Is there a way to keep my label and text on the same line by adjusting the CSS? I've tried using float based on suggestions from other posts, but they still end up on different lines. .indentColumn { width: 71px; padding-top: 5%; } .labelColumn ...

What are the steps to add SVG effects to an image or div element?

Embarking on my first venture into using SVG graphics has left me feeling a bit bewildered. Despite extensive research, I have yet to stumble upon the answer to my current quandary. The objective at hand is to transform an image, assuming that the source ...

No content in Django's Bootstrap dropdown menu

I need assistance with extracting a list of objects and placing them in a dropdown menu. I have successfully achieved this, but the issue seems to be related to my HTML structure. Here is the code snippet from my HTML: <div class="dropdown"> < ...

Enhance User Experience by Making Each Background Slideshow Image Clickable

I want to create a background image slideshow for my photography portfolio. Each image should fade in and link to its respective gallery. I also want the text box to fade with the images and link to the corresponding gallery. The challenge is adding the li ...

How to Right Align Text in a Bootstrap Table

Exploring Bootstrap, I challenged myself to create a table without the usual table tags, using only the grid system. While I managed to make it look decent, I'm struggling to align the numbers to the right. Here's a JSFiddle link to what I'v ...

hiding an "input type="file" element by using a button to conceal it

Is it possible to create a button that triggers file upload functionality? I couldn't find any solutions on Google. I've heard it can be done with labels, but I specifically need to use a button for better UX/UI. <button style="position:fixe ...

Extract data from the HTML source code in JavaScript and transfer it to a personalized JavaScript variable within Google Tag Manager

Running an e-commerce website on Prestashop and facing an issue with data layer set up. Instead of a standard data layer, the platform generates a javascript variable called MBG for Enhanced E-Commerce implementation. <script type="text/javascript"> ...