The width property in CSS is not having any impact when expressed in percentage

I've encountered a peculiar issue while attempting to specify the width for my search bar. Below is a snippet of my HTML code.

<header class="header-main">
     <div class="header-main-logo">
          <a href="index.php" class="logo">LOGO</a>
     </div>
     <div class="search-container">
      <form method="get" action="search.php?page" class="search-form">
           <input class="search-bar" type="search" name="query" value='<?php echo isset($_GET["query"]) ? trim($_GET["query"]) : "" ?>' placeholder="Search">
               <div class="search-btn">
           <input type="hidden" name="page" value="1">
               <button type="submit" name="search" value="1">Search</button>
               </div>
          </form>
     </div>
</header>

Here's my CSS code:

.header-main {
    box-sizing: border-box;
    width: 100%;
    height: 60px;
    background-color: rgb(107, 23, 23);
    display: flex;
    justify-content: space-between;
}

.search-container {
    box-sizing: border-box;
    width: 50%;
    height: 100%;
    background-color: rgb(51, 52, 59);
    display: flex;
    align-content: center;
}

.search-container .search-bar {
    box-sizing: border-box;
    border: 1px solid rgb(60, 255, 0);
    height: 40px;
    width: 100% !important;
    cursor: text;
    font-size: 14px;
    font-weight: 600;
    font-family: Arial, Helvetica, sans-serif;
    color: rgb(0, 0, 0);
    background-color: rgb(248, 248, 248);
    align-self: center;
}

Whenever I set the width in .search-container .search-bar to 100%, it doesn't seem to take effect. However, specifying a value like 400px works as expected. My aim is to have it in percentage so it adapts dynamically to the page size.

I initially thought it was a matter of setting the width on the HTML element. My goal is for the Search bar to span the entire width of its parent container, the search container.

Answer №1

The problem lies within the form as it does not occupy the entire width, causing the issue. To fix this, try setting a specific width for your form (consider using a class name instead).

.header-main {
    box-sizing: content-box;
    width: 100%;
    height: 60px;
    background-color: rgb(107, 23, 23);
    display: flex;
    justify-content: space-between;
}

.search-container {
    box-sizing: content-box;
    width: 50%;
    height: 100%;
    background-color: rgb(51, 52, 59);
    display: flex;
    align-items: center;
}
form{width: 100% !important;}
.search-container .search-bar {
    box-sizing: content-box;
    border: 1px solid rgb(60, 255, 0);
    height: 40px;
    width: 100% !important;
    cursor: text;
    font-size: 14px;
    font-weight: 600;
    font-family: Arial, Helvetica, sans-serif;
    color: rgb(0, 0, 0);
    background-color: rgb(248, 248, 248);
    align-self: center;
}
<header class="header-main">
     <div class="header-main-logo">
          <a href="home.php" class="logo">LOGO</a>
     </div>
     <div class="search-container">
      <form method="get" action="search-results.php?page" class="search-form">
           <input class="search-bar" type="search" name="query" value='12345' placeholder="Search">
               <div class="search-btn">
           <input type="hidden" name="page" value="1">
               <button type="submit" name="search" value="1">Search</button>
               </div>
          </form>
     </div>
</header>

Answer №2

The element is expanding to the entire width of its parent container, a form that does not have a specified width.

Answer №3

By setting a percentage as the width value, you are essentially determining that the element's width will be a certain percentage of its parent's width. This means that if the parent inherits the width from its grandparent, then so will the child element. In the code snippet below, I have adjusted the parent element's width so that the grandchild can inherit it.

.header-main {
    box-sizing: border-box;
   -webkit-box-sizing:border-box; 
width: 100%;
    height: 60px;
background-color: rgb(107, 23, 23);
display: flex;
justify-content: space-between;
}

.search-container {
box-sizing: border-box;
-webkit-box-sizing:border-box;
width: 50%;
height: 100%;
background-color: rgb(51, 52, 59);
display: flex;
align-content:center;}

.search-container .search-bar {
box-sizing: border-box;
border: 1px solid rgb(60, 255, 0);
height: 40px;
width: 100% !important;
cursor: text;
font-size: 14px;
font-weight: 600;
font-family: Arial, Helvetica, sans-serif;
color: rgb(0, 0, 0);
background-color: rgb(248, 248, 248);
align-self: center;} 

.header-main {
box-sizing: border-box;
-webkit-box-sizing:border-box;
width: 100%; 
height: 60px;
background-color: rgb(107, 23, 23); 
display: flex;
justify-content: space-between;}

.search-container { 
box-sizing: border-box; 
-webkit-box-sizing:border-box;
width: 50%; 
height: 100%; 
background-color: rgb(51, 52, 59); 
display: flex; 
align-content: center;} 

.search-container > form {
width: 100%;}

.search-container .search-bar {
box-sizing: border-box;
border: 1px solid rgb(60, 255, 0);
height: 40px; 
width: 100% !important; 
cursor: text; 
font-size: 14px; 
font-weight: 600; 
font-family: Arial, Helvetica, sans-serif;
color: rgb(0, 0, 0); 
background-color: rgb(248, 248, 248);
align-self: center;} 
<header class="header-main">
     <div class="header-main-logo">
         <a href="index.php" class="logo">LOGO</a>
     </div>
     <div class="search-container">
      <form method="get" action="search.php?page" class="search-form">
           <input class="search-bar" type="search" name="query" value='abc' placeholder="Search">
               <div class="search-btn"> 
              <input type="hidden" name="page" value="1">>
            <button type="submit" name="search" value="1">Search</button>></div>
          </form>
     </div>
</header>

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

I am experiencing an issue with mydaterangepicker and primeng where it is not displaying properly in the table header. Can anyone assist me with this

I am attempting to integrate mydaterangepicker () with primeng turbotable (since primeng calendar does not meet the requirements), but I am having trouble with its display. Could you please assist me with some CSS code or suggest an alternative solution? ...

Two div elements refusing to be positioned side by side

I'm struggling to position two div elements next to each other, with a third one to be added later. I've experimented with floating, display, and positioning options, but nothing seems to work. I've even copied code from other websites, but ...

Modifying object properties in JavaScript

Looking to update a boolean attribute for an object in JavaScript (in this case, the object is part of fullPage.js library). I am interested in turning off the navigation attribute by setting it to false, and ideally would like to do this from a separate f ...

The HTML <select> element is currently styled with a locked CSS, but I am looking to have it displayed with the default

Unfortunately, I am unable to make changes to the CSS file at this time. The current styles in the CSS file are as follows: font-size: 13px; padding: 6px 4px; background-color: rgb(238, 238, 238); border: 1px solid rgb(204, 204, 204); clear: both; margin- ...

What is the correct way to integrate a HTML/CSS/JS theme into a Vue project effectively?

As a newcomer, I recently acquired a bootstrap theme that comes with HTML, CSS, and JavaScript files. My goal now is to integrate this theme into Vue in order to make it fully functional. The challenge I am facing is how to successfully incorporate the the ...

Eliminating an unwelcome gap between two div elements

I couldn't find anything on Google, so I searched this site and came across a similar topic Unwanted space between divs. I tried applying margin:0px; in several places, but there is still space between the two divs. Here's my HTML: <!DOC ...

Conflicting CSS files in Next.js

I recently started using Next.js and I am in need of a solution to my current problem along with some advice. Problem: -When accessing the empty URL (just localhost:8081), a login form is displayed, with an option to switch to the Register component with ...

Retain the jQuery dropdown menu in an open state while navigating to a different webpage on the

I am encountering an issue with a drop-down menu on my website. Whenever I click on a submenu link, the new page opens but the menu automatically closes. However, I want the active menu to remain open even on the new page. To solve this problem, I believe ...

Extra space within table in Firefox

Experiencing an issue with excess whitespace in a table on Firefox. Visit the live example at The problem can also be seen on this jsFiddle: http://jsfiddle.net/DVbCC/ In Chrome and Safari, rows are neatly arranged with minimal spacing, but Firefox show ...

What is the best way to create a line break in a flex div container to ensure that overflowing items wrap onto the next line using

Using material-ui popper to display a list of avatars. Trying to arrange the avatars horizontally within the popper. <Popper style={{ display: 'flex', maxWidth: '200px', }}> <div style={{ marginRight: '20px' }}&g ...

Arrangement of div elements tailored to fit the size of the viewport

I am working on creating a grid of divs that will cover the entire viewport. To start, I want to have a grid that is 7 divs wide and 10 divs high. Below is the code snippet I've written so far to set the size of the div elements: function adjustHeig ...

Tips for merging two text boxes in a form with CSS

Is it possible to combine two text boxes in a form using CSS, similar to the design on Tumblr's login page at ? If CSS is not the solution, what alternative methods can be used? Any assistance would be greatly appreciated. I am aiming to enhance the ...

Is there a way to ensure that spans are inline with a div?

My structure is pretty straightforward: <div></div><span></span><span></span> However, I'm looking to have them all appear on a single line. Currently, they are rendering like this: <div /> <span />&l ...

Activate the full-screen video mode

On the website airbnb, there is a "play" button on the homepage that, when clicked, activates a fullscreen HTML video. Is it possible to design a button or function that can automatically trigger a full-screen HTML video? ...

What is the best way to adjust the size of an element with CSS?

I'm currently attempting to transform an image using CSS keyframes. Here's what I have: @-webkit-keyframes blinkscale { 0% { transform: scale(1,1) } 50% { transform: scale(0.1,0.1) } 100% { ...

Having trouble bringing in icons from the @mui/icons-material library

An error occurs when attempting to run the code. wait - compiling... error - ../../../../../node_modules/@mui/icons-material/esm/utils/createSvgIcon.js:1:0 Module not found: Can't resolve '@mui/material/utils' null Note: @mui/material pack ...

Mobile Mode Issue with Bootstrap 4 Navbar Example

Currently, I am exploring the Bootstrap material and trying to understand how to integrate their content with a jinja2 file that I am preparing for a django project. Upon attempting to copy and paste their example code (specifically this example http://get ...

I am struggling to run the jQuery animation that adjusts the width more than once

My goal is to allow the user to expand and retract a <div> element upon clicking. The desired behavior is for the <div> to expand when clicked, and then retract back to its original width when clicked again. However, when testing this function ...

The body element is not positioned at the top of the webpage in the HTML/CSS layout

I'm running into an issue with my HTML/CSS. It seems like my body tag has unexpectedly acquired a margin-top of 10px... However, I've searched high and low but can't seem to figure out how to remove it. This is something that has never happe ...

difficulty eliminating white space in HTML display using Bootstrap

Encountering an issue with the template view below. A scrollbar was added to the "Used in" table at the bottom, but there is excessive whitespace beneath it. Removing divs/elements one by one reveals that the problem seems to stem from the table itself. v ...