I have a two-column HTML layout. Is there a way to maximize the width of the `#left` div so that it can be resizable, while keeping the `#right` div a fixed size?

Check out this HTML snippet to demonstrate the issue:

<div id="content">
  <div id="left" style="float: left">
    left content
  </div>
  <div id="right" style="float: left; width: 250px">
    right content
  </div>
 </div>

Is there a way to have #left fill up all the available space on the left while keeping #right confined to its designated spot (taking up 250px on the right)?

I want #left to resize when necessary, but keep #right in place without shifting.

If you have any suggestions or ideas on how to achieve this, please let me know!

I've experimented with a few approaches, but none seem to work:

1. Setting width to 100% for #left

<div id="content">
  <div id="left" style="float: left; width: 100%">
    left content
  </div>
  <div id="right" style="float: left; width: 250px">
    right content
  </div>
 </div>

This method fails as now #left takes up the entire width, causing #right to drop below it.

2. Trying width of 70% for #left

<div id="content">
  <div id="left" style="float: left; width: 70%">
    left content
  </div>
  <div id="right" style="float: left; width: 250px">
    right content
  </div>
 </div>

This approach creates a gap on the right side because #left does not take up the exact available space.

Additionally, resizing leads to #right collapsing beneath #left once #left goes below 70% + 250px.

3. Adding min-width: 100% to #left

<div id="content">
  <div id="left" style="float: left; min-width: 100%">
    left content
  </div>
  <div id="right" style="float: left; width: 250px">
    right content
  </div>
 </div>

Unfortunately, this also results in #left taking up the full width.

4. Using max-width: 100% for #left

<div id="content">
  <div id="left" style="float: left; max-width: 100%">
    left content
  </div>
  <div id="right" style="float: left; width: 250px">
    right content
  </div>
 </div>

Similar to other attempts, #left still consumes the entire width.

5. Floating #right to the right

<div id="content">
  <div id="left" style="float: left;">
    left content
  </div>
  <div id="right" style="float: right; width: 250px">
    right content
  </div>
 </div>

However, this doesn't solve the problem as #left continues to occupy minimal space.

6. Trying display: inline-block

<div id="content">
  <div id="left" style="display: inline-block">
    left content
  </div>
  <div id="right" style="display: inline-block; width: 250px">
    right content
  </div>
 </div>

The outcome matches the original question where #left only occupies the text within "left content".

Any assistance or insights would be greatly appreciated!

Answer №1

<div id="wrapper">
  <div id="top" >
    top content
  </div>
  <div id="bottom">
    bottom content
  </div>
 </div>

CSS:

#wrapper {
    display:flex;
    flex-direction:column;
    justify-content:space-between;
    width:100%;
    height:100%;
}
#top {
    background-color:green;
}
#bottom {
    background-color:yellow;
    height:250px;

}

Try it out here: http://example.com/abcd1234/

According to your requirements, this should work well with modern CSS flex properties.

Answer №2

It seems like you have a layout with two columns - one that needs to take up all available space and be resizable, and another that should remain fixed at 250px width.

To achieve this, you can utilize the power of CSS Flexbox.

Here's how you can structure your HTML and CSS:

<div id="content">
    <div id="left">left content</div>
    <div id="right">right content</div>
 </div>
#content { display: flex; }
#left { flex-grow: 1; }
#right { flex: 0 0 250px; }
@media (max-width: 300px) { #content { flex-direction: column; } }

If you'd like to see this in action, check out this demo.

How it works:

  1. By setting display: flex, the container (div#content) becomes a flexbox, arranging its children in a row by default.

  2. The flex-grow: 1 property on div#left allows it to take up all available space.

  3. For div#right, we use flex: 0 0 250px. This shorthand sets it to not grow or shrink, with an initial size of 250px.

  4. The media query switches to a column layout when the screen width is below 300px, as per your specifications.

For more details on flex properties, you might find this guide to Flexbox helpful.

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

Is it possible for a typo3 website to automatically redirect users to a mobile-friendly version?

I have put together a landing page for my website with numerous content elements using only HTML and then pasted it into my typo3 backend. I ended up with separate HTML codes for desktop view and mobile view because I couldn't figure out how to make t ...

Tips on clearing notices and warnings at the bottom of a PHP questionnaire

Below is the code I am working with: <?php include_once 'init/init.funcs.php'; $_SESSION['pollid'] = (int) $_GET['pollid']; $questions = array(); if (!isset($_SESSION['answering'])) { $result = mysql_query ...

Floating a DIV causes it to shift down in a responsive layout at specific window sizes

My issue lies with a fluid layout that works well for the most part. However, at specific window widths, one of four floating div-elements unexpectedly shifts down. This inconsistency happens at nearly 20 different widths, differing by only one pixel. For ...

Link Formatting Instructions

https://i.sstatic.net/PZVKx.png I am trying to style the right element to look like the one in the image, but when I apply the 'pull-right' class, it unexpectedly affects the width of the element below it. <a class="pull-right">My link< ...

Exploring the world of three-dimensional graphics with the power of HTML5, canvas,

Here is the HTML markup I am working with: <canvas id="container" name ="container" width="300" height="300"></canvas> This is the JavaScript code: var WIDTH = 400, HEIGHT = 300; var VIEW_ANGLE = 90, ASPECT = WIDTH / HEIGHT, NEAR = 1, FAR = ...

Contrast between action="" and action="#" attributes in HTML

There are two ways I've come across for setting a form's action attribute. #1. Using an empty string as the action attribute value: action="" #2. Specifying a hash symbol (#) as the action attribute value: action="#" What distingishes these ...

Form containing unchecked checkbox

I am trying to send a false value via post for a checkbox that is not checked by default. Here is how my checkbox is defined: <label><input type="checkbox" id="rez" name="rezerwowanie" value="false" />Rezerwowanie</label> After submitti ...

I am seeking guidance on how to update the HTML content being viewed in the browser when a user changes the selected option within a <select> element in ASP.NET

Hello, I'm just starting to learn about web development and ASP.Net. Can anyone help me figure out how to dynamically change the HTML content displayed in the browser when the user selects a different option from a <select> element? @*DropDown: ...

Display Loading Spinner with Bootstrap on Form Submission

This Django project features a Bootstrap spinner within a button, as seen in the code snippet below: <form method="POST" id="pdfUploadForm" enctype="multipart/form-data"> {% csrf_token %} {{ form|crispy }} <b ...

Expanding the reach of the navigation bar

Hello Everyone, Is there a way to stretch my navigation bar so that the links are evenly spaced out across the browser window instead of being clustered together? I want it to be responsive rather than fixed in size. This is my HTML code: <div class= ...

Numerous HTML documents being uploaded to the server for a multitude of individuals

Currently, I am developing a game on a website where players create new rooms and are assigned specific roles with individual powers. Some players need to wait for input from others, creating a dynamic gameplay experience. Additionally, there are certain ...

When scrolling, all sections display above the stationary header

On my home page, I have a header that remains fixed at the top of all sections. However, when I scroll down, the contents and sections appear above the fixed header, making it look like a background. Is there a way to ensure that all images and content go ...

Enhance Your Website with HTML and JavaScript

Here is the code snippet I am working with: sTAT **javascript** var acc = document.getElementsByClassName("item-wrapper"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function(){ this.classList.toggle("selected"); this.nextElementS ...

Ways to retrieve form data following a post request using JavaScript

Does anyone know how to read form data from testPage.html in testPageResult.html? I have created a html page called testPage.html with a form that has a post action like the following: <h2>HTML Forms</h2> <form action="http://test.com/tes ...

What is the best way to display lengthy content using a pair of HTML tags?

I am facing an issue with my Angular4 app where I have a <md-card-content> tag on part of my page. Inside this tag, I am trying to display some content within a <p> tag which has a maximum height of 20cm. While this setup works fine for short ...

When I click away from the screen, the bottom border of the button disappears

view my fiddle Here is the HTML code snippet I am working with: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> <button> <i class="fa fa-5x fa-motorcycle gobumpr_icon"></ ...

What might be causing my background image not to appear?

I seem to be having trouble with the background image not displaying on my screen. I am confident that the URL is accurate, as I have successfully used it in other instances. Here is a snippet from my CSS file: body{ background : url(assets/image/mai ...

Ways to verify if the CSS background-color is set to white?

I am looking for a solution: $('*').click(function() { if($(this).css("background-color") == "#ffffff") { $(this).css("background-color") == "#000000" } });​​​​ that will execute on click of a specific cla ...

Add flair to the ButtonBase element within a Tab component

I'm currently exploring how to override Material UI styles with a nested component. For instance, what if I want to increase the bottom border height on an active Tab, which is applied by the underlying ButtonBase. Below is the style definition: con ...

Using a 90% zoom level in the browser does not trigger any media queries

When addressing the width of a specific class, I utilized CSS media queries as shown below: @media (max-width:1920px) { .slides { width: 860px; } } @media (max-width:1500px) { .slides { width: 852px; } } @media (max-width:1 ...