CSS: Create a form with two input fields where the left field is adjustable in width and the right field fills up

------------- --------------------------------------------------
| some unique text | | some more original content, varying length |
------------- --------------------------------------------------

|<---------------------- 100% width ------------------------->|

In the layout above, I have a design where the left box should be minimized while the right box expands to fill the remaining space. Normally, using float: left would achieve this.

The issue arises when the right box contains a lot of text and the left box needs to remain considerably smaller (sizing varies and must be flexible). In such cases, the behavior should adapt as shown below:

------------- --------------------------------------------------
| some text | | quite a lengthy amount of text, in fact, a       |
------------- | multitude of words, you might say it's truly     |
              | an extensive chunk of text                      |
              --------------------------------------------------

When using float:left, the excess text in the right box causes it to wrap beneath the left box, creating an undesirable layout:

-------------
| some text |                                       (undesirable)
-------------
-------------------------------------------------------------------
| quite a lengthy amount of text, in fact, a multitude of words   |
| you might say it's truly an extensive chunk of text             |
-------------------------------------------------------------------

Alternately, if both boxes are placed within a table, the left box may unnecessarily expand even with minimal text content:

                                                      (undesirable)
--------------------------------- -----------------------------------
| some text                    | | not much text here            |
--------------------------------- -----------------------------------

In addition, the left box should not break onto a new line. However, due to containing HTML elements rather than pure text, applying no-wrap does not work consistently across all browsers.

What is a recommended solution to address this challenge?

UPDATE

Precisely filling the remaining width with the right box is not crucial; instead, maintaining its alignment with the right edge of the left box is the primary goal.

Answer №1

When styling the right box, consider using overflow: hidden; width: auto; instead of floating it. This will ensure that it takes up the remaining space without dropping below even when there is a lot of content. Remember to continue floating the left box.

Answer №2

Establish a set width for the left column, along with float:left; make sure to position it after the main column in the HTML code.

Utilize two separate divs for the right column; the outer div should also be floated left and have a width of 100%, while the inner div should have a left margin equal to the width of the left column.

Answer №3

Following Stobor's advice, consider nesting the left box within the right box

<div id="rightBox">
   <div id="leftBox">Include this text to expand the left box</div>
   <div style="float:left">
      Insert content for the right box here<br>
      Even breaking works fine...
   </div>
</div>

CSS:

#rightBox {
   width: 100%;
   background: red;
}
#leftBox {
   width: auto;
   float: left;
   background: blue;
}

Seems to be effective in my case.

Answer №4

When utilizing a pure CSS solution, it appears necessary to impose some sort of width constraint on one of the columns. Is it absolutely ruled out that this cannot be expressed as a percentage, as previously suggested?

The issue is twofold: If one column is floated while the other is given a width of 100%, the floated column, being removed from the document flow, causes the other to span the entire viewport width (disregarding the floated div). If both are floated without specifying the second div's width, the div will naturally shrink or expand to accommodate its widest child element. The W3C specification may offer some guidance, although it can be quite dense.

An alternative approach could involve using JavaScript to determine the viewport width and dynamically adjusting the column widths accordingly. Otherwise, you might encounter difficulties with achieving the desired layout solely through CSS.

Answer №5

Another option to consider is changing the wrapper display property to table and setting the child elements to table-cell. You can then specify one div's width and height with fixed pixel values, while setting the other (dynamic) div's width to 100%.

<style>
* {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

#container{
  border: solid #222 1px;
  width: 400px;
  height: 360px;
  resize: both;
  overflow: auto;
}

#wrap{
  border: solid red 1px;
  width: 100%;
  height: 100%;
  display: table;
  resize: both;
  overflow: auto;
}

video{
  border: solid #222 4px;
  display: table-cell;
  width: 160px;
  height: 160px;
}

#list{
  border: solid #222 4px;
  display: table-cell;
  width: 100%;
  height: 100%;
}
</style>
<div id="container">
  <div id="wrap">
    <video controls></video>
    <br>
    <div id="list"></div>
  </div>
</div>

Implement this solution!

ps: On a side note, vertical alignment might require some more tweaking :(

UPDATE!!!

VERTICAL METHOD: * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }

#container{
  border: solid #222 1px;
  width: 400px;
  height: 360px;
  resize: both;
  overflow: auto;
}

#wrap{
  border: solid red 1px;
  width: 100%;
  height: 100%;
  display: table;
  resize: both;
  overflow: auto;
}

video{
  border: solid #222 4px;
  display: table-cell;
  width: 160px;
  height: 160px;
}

#median{
  display: table-row;
  width: 100%;
}

#list{
  border: solid #222 4px;
  display: table-cell;
  width: 100%;
  height: 100%;
}
</style>
<div id="container">
  <div id="wrap">
    <video controls></video>
    <div id="median"></div>
    <div id="list"></div>
  </div>
</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

Tips for adjusting the width of ngx bootstrap modal?

Is there a way to adjust the width of my ngx bootstrap modal so that it is not fixed? I have attempted to modify it in the HTML code below but without success: Here's the HTML snippet: <div bsModal #childModal="bs-modal" class=" ...

The website link cannot be seen on Internet Explorer 8, however it is visible on other browsers like Firefox and Chrome

Please verify the following link: The link "Return to login page" is displayed correctly on Firefox, Chrome, etc., but it appears higher on IE8. How can this be fixed? To resolve this issue, you can adjust the CSS for the 'lost_password' div as ...

Creating a vertical scrollable content using FlexBox

Struggling to create a scrollable box using flex The Box element with the id=scroll is not behaving as expected, causing content overflow How do I set the Box to overflow=auto and enable scrolling for the content? Spending countless hours trying to unrav ...

Incorporate the class directly into the datepicker input element in a React JS component

Adding a class directly to the input element is what I am trying to achieve here.https://i.sstatic.net/qGGwD.png The class MuiInputBase-input needs to be added. This code pertains to a date picker. import { DateTimePicker, MuiPickersUtilsProvider } from & ...

Adjusting the line-height and baseline to accommodate various screen sizes on both mobile and desktop

This problem seems to keep coming back for me. I can't figure out what's causing it. Here are two images showing the issue: On desktops: On mobile devices: As you can see, the text is not vertically centered on mobile devices. Strangely, thi ...

Tips for updating the color of buttons after they have been selected, along with a question regarding input

I need help with a code that changes the color of selected buttons on each line. Each line should have only one selected button, and I also want to add an input button using AngularJS ng-click. I am using AngularJS for summarizing the buttons at the end ...

prismjs plugin for highlighting code displays code in a horizontal format

If you're looking for a way to showcase source code on your website with highlighted syntax, prismjs.com may be just what you need. It offers a similar style to monokai... However, I've encountered an issue where the plugin displays my code in a ...

Tips for creating an editable div that can also incorporate a textarea and the option to upload or delete photos

On my website, users can upload images, names, and descriptions which are saved in a MySQL database. The information is then fetched to display on the website. The code below allows users to enter this information and see it displayed when they click the s ...

What is the best way to display both an employee's name and ID on a single line using CSS and HTML?

I am currently working on an asp.net MVC app and facing an issue with adding employee ID and employee name on the same line, similar to the web design provided. I need to make modifications using HTML and CSS to achieve this. The problem is that I am unab ...

Serving static files in Django

Currently diving into Django and encountering a hurdle with static files. I've followed the setup in both the settings and HTML, but unfortunately, they are not loading on the website. Seeking assistance to resolve this issue! **settings.py:** STATIC ...

What is the process for utilizing Sass in Vue CLI rather than node-sass (which is the default for sass-loader)?

I found a solution, but it's specifically for Nuxt.js and I'm using the Vue CLI. Is there any way to use dart sass instead of node-sass (default for sass-loader) in Nuxt.js? While the Vue CLI official documentation displays an example utilizing ...

What is the best way to position a button directly to the right of a text box with no space in

Is there a way to perfectly align a submit button to the right of a text or search box, matching the height of the search box, and eliminating any unwanted white space between them? This is my current setup: <input type="text" value="" placehold ...

What is the best way to specify the border color of a fieldset?

Is there a way to set the border color of a fieldset while removing the default border color? I have tried using a class but it doesn't seem to work as expected. How can I achieve setting the border color for the fieldset? <fieldset class="field_s ...

Animating a dropdown menu with jQuery

I have a typical submenu structure set up like this: <ul> <li> <a href="#">Parent link</a> <ul> <li><a href="#">Submenu link</a></li> </ul> </li> </ul> In my de ...

What is the process for choosing and making changes to a specific portion of a textContent?

<h1 id="block ">This is my header block</h1> To dynamically change the color of the "block" word every 2 seconds using JavaScript: let colors = ["blue", "green", "pink", "purple"]; let curColor = 0; const changeColor = () => { ...

Harnessing the power of flexbox for data visualization

Trying to use flexbox to display data from a dataset in my code. Here is the code snippet: <div ng-app="myapp" ng-controller="FirstCtrl"> <div ng-repeat="name in names" class="graph-wrapper"> <div class="aside-1 content"> ...

A horizontal navigation bar featuring a centrally aligned vertical layout and full-height display

I want to align everything on my menu bar vertically in the center, with the a elements taking up 100% of the available height of the menubar (text would be vertically-centered). However, I do not want to fix the height of my menu bar. (If I were to fix th ...

Trick for using :checked in CSS

Is it possible to change the color of a deep linking div using an input checkbox hack in the code below? <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /&g ...

Using AJAX to Access PHP Variables

I am working on creating a progress bar and have the following CSS code: #result{ background:#8c8f91; margin-left: 15px; margin-right: auto; table-layout: fixed; border-collapse: collapse; z-index: -1; position:relative; width: ...

modify rectangular section in drupal rather than numerical block

Currently, I am attempting to modify blocks within my Drupal 7.x website using the Zen base theme. While I have been successful in adjusting the CSS styles of the blocks based on their numbers in the block.css file, I find this method quite confusing. Is ...