Expansive width of form field

I have a yellow top bar that is fixed in position and spans 100% of the width. Inside this bar, there is a red div with fluid width on the left and a green div containing an input element on the right.

  1. The input element needs to take up the remaining space in the top bar, covering all of the yellow area regardless of the width of the red div.
  2. I prefer not to add any additional HTML elements, so I am looking for a CSS solution using the existing structure provided below.
  3. While JavaScript could easily solve this problem, I am trying to avoid using it for now.

Sample code: http://jsfiddle.net/qhoc/jBQDB/2/

In the current setup, setting right: 0 for .input input did not work as expected. There is still some yellow space visible on the right side, indicating that the width of the input element did not expand properly. I also experimented with position: absolute without much success.

Any help would be greatly appreciated!

Answer №1

Check out this code on JSFiddle

.input {
    overflow: hidden;
}
.input input {
    width: 100%;
    height: 50px;
    border: 0;
    outline: 0;
    /* This is necessary if there's any border/padding on the input */
    -moz-box-sizing: border-box;
         box-sizing: border-box;
}

Curious about why overflow: hidden helps? Find out here.


If you can't use overflow: hidden on the parent of your input, there are other methods to achieve the same result. One common reason for this limitation is when you need a box-shadow on input:focus, or similar.

Take a look at an alternative approach using display: table-cell: View example here

Answer №2

Consider utilizing the following CSS properties:

display: table;

and

display: table-cell;

These properties will automatically adjust the size of your elements.

You can view a revised sample here: http://jsfiddle.net/2VmFT/

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

Refresh the page when the dropdown selection is changed and send the new value

I'm currently working on a page that includes a dropdown menu. My goal is to have the page reload after selecting an option from the dropdown so that it returns to the same page with the selected value passed through. Here's the PHP code for th ...

Delete a product from the cart when the selection in the dropdown changes

In the process of creating an e-commerce platform, I am implementing a feature that allows users to choose items from a dropdown menu based on their category. However, I want to restrict users from adding more than one item per category. If a user tries to ...

Retrieve data from multiple inputs with the same name in a Spring MVC form

I have a JSP page with input fields that can be dynamically added or removed. I need to map these input fields to the @ModelAttributes in Spring MVC, but I'm unsure of how to do this. I want to submit a form that includes: <form enctype="multipar ...

Saving the output of an AngularJS expression in an input field

Looking at the calculations below, I am currently evaluating an expression, {{ annualIncome * 4.5 }}, in one input and then re-evaluating the same expression in another input. Instead of saving the result and transferring it to the other input, I am repeat ...

The Challenge of Referencing Javascript Files (including jQuery)

Previously, I had the following code snippet: <head> <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="text/javascript"> var optPrompt = "- Select One -"; var subCats ...

Significant challenge - issues arise with the page refresh and ajax functionality

When using a navigation that loads content dynamically via AJAX, encountering issues like the current content disappearing after refreshing the page or navigating to another URL and returning to find old content under the initial menu tab can be frustratin ...

Introducing an exception to every jQuery post() method

Our app utilizes AJAX with jQuery to manage user information. We determine the success or failure of an API call by checking data.success or data.error respectively. Additionally, we implement the jQuery error() function in each post() method to handle any ...

Button that vanishes when clicked, using PHP and HTML

I am in the process of creating an online store and I am seeking to implement a button that directs users to a new page, but then disappears permanently from all pages within the store. Here is the code snippet I have developed so far: <input class="b ...

Use PHP to post videos or images online

I'm having trouble uploading videos or pictures to my server using PHP. Every time I try, I receive an error stating that the file name is empty. I don't understand why this is happening, as the code continues to run the script, displaying an err ...

Improving Rails coupling by integrating AJAX

I am currently in the process of developing a small blog application to enhance my skills with Rails. This app will allow users to log in, create posts, and comment on other users' posts. As of now, I am working on integrating Ajax into the 'add ...

Steps to turn off Bootstrap's fixed navigation on mobile devices

When a user visits the mobile version of the website or scales the webpage, the Bootstrap navbar will no longer remain fixed at the top. I am looking to set it as a normally scrolling view in the navbar. The Bootstrap class is "navbar-fixed-top" https:// ...

Hiding Button in code behind

I have around 100 buttons in a list. Initially, I have set their visibility to false in the ASPX page. Now, I need to change the visibility to true from the code-behind when required. Since there are too many buttons, it's not feasible to individuall ...

There seems to be some odd behavior with the page design

When you visit my website at lookaroundyou.net and select USA (where there are more videos than any other section), the entire layout of the website transforms. Can anyone explain why this sudden change occurs? ...

An error was encountered: $controllerProvider provider is not recognized within the productsApp

Here is the link to my code: http://plnkr.co/edit/8muxhJmvFiIqRJgzuT0O?p=preview I encountered an error: Uncaught Error: Unknown provider: $controllerProvider from productsApp Additionally, I am unable to view the JSON file in the console. index.html ...

Setting up the frontend and backend with Apache HTTP Server

I am still exploring the world of web development and trying to grasp the inner workings of it all. Currently, I have a remote Debian server with the domain www.example.com. This server hosts a java application running as a daemon process on port 4321. Ad ...

What was the recommended dimensions for CSS containers in 2018?

In the realm of responsive layout design, what is the recommended size for a max-width container as of 2018? My current choice is to use 1140px, which I find works well with the standard screen size of 1366px. ...

Is there a way to implement this toolbar in Ionic Angular?

https://i.sstatic.net/sGd1o.png I am looking to replicate the toolbar shown in the image above using Ionic. As a beginner in Ionic development, I am finding it challenging to translate the design into code. I attempted to use a grid layout, but the varyin ...

Creating a unique bullet style using CSS

I've got a collection of student links that take users to their profiles, and I want to replace the usual circular bullet points with smiley faces. Each picture is 14x14 pixels in size. How can I use CSS to achieve this effect for the bullets? ul { ...

Styling images with text alignment in CSS

I'm trying to figure out how to align an image to the left, some text to the top right, and some text to the bottom right. The parent div contains a background image. <div id="parent" style="background: url(bg.jpg) repeat-x left top"> <i ...

Display the value of the dynamically added selected element only

Is there a way to display the selected element value on click without showing all element values? In the code below, when I add usernames and click on any label, it should only show the corresponding delete icon. However, currently, it shows delete icons f ...