What is the best way to ensure a floated div fills up the rest of the available horizontal

Apologies for the unclear title of my issue. I struggled to find the right words to describe it while searching for a solution.

I have fixed-size divs that need to be floated left and behave like inline blocks. On the right side of the page, there is a content area. Check out this example to see what I mean: http://jsfiddle.net/7sp5M/. Changing the width of the Result area causes the divs to adjust accordingly, but there's a gap between the blocks and the Content area. I want the Content area to have a minimum width and expand horizontally to fill this gap: .

For instance, let's say a block's width is 100px; the minimum width of the content area should be 200px. Ideally, I'd like the Content width to range from 200px to 299px based on the width of the blocks.

Is it feasible to achieve this using only HTML and CSS? I'm open to table solutions if necessary.

Update: Thank you all for your feedback. It appears that achieving this behavior solely with HTML/CSS may not be possible. As I'm still learning CSS, I ended up implementing the desired functionality using JavaScript and jQuery, which met my requirements perfectly.

Answer №1

To maintain the width of the block div while adding padding, I typically create an inner container within it:

.block { width: 20%; }
.block > .inner { padding-left: 10px; }
.block:fist-child > .inner { padding: 0; }
<div class="block">
    <div class="inner">
        block
    </div>
</div>

Answer №2

To achieve this layout, utilize the CSS properties display: table; and table-cell;

The parent container #main should be styled with display: table;, while its children should have table-cell;. Additionally, ensure that the div.right element is positioned after the div.left in the HTML markup.

<div class="left">
        <div class="block">Block1</div>
        <div class="block">Block2</div>
        <div class="block">Block3</div>
        <div class="block">Block4</div>
        <div class="block">Block5</div>
        <div class="block">Block6</div>
    </div>
    <div class="right">Content</div>

The block widths can be customized as needed, such as using percentages like 16%. Omitting a specific width declaration allows for dynamic resizing based on available space, simulating a table's behavior while maintaining semantic markup.

#main
{
    width: 100%;
    display: table;
}

.right {
    display: table-cell;
    width: 200px;
    height: 300px;
    background: #888;
}
.left {
    overflow: hidden;
    height: 300px;
    background: #ccc;
    display: table-cell;
}
.block {
    width: 16%;
    height: 50px;
    float: left;
    border: 1px solid blue;
    display: table-cell;
}​

http://jsfiddle.net/Kyle_Sevenoaks/7sp5M/31/

Answer №3

Give this a shot:

<div id="main" class="">
<div class="right">Content</div>
<div class="left">
    <div class="clearfix"></div>
    <div class="block">Block1</div>
    <div class="block">Block2</div>
    <div class="block">Block3</div>
    <div class="block">Block4</div>
    <div class="block">Block5</div>
    <div class="block">Block6</div>
</div>

</div>

In CSS:

.right {
  float: right;
  min-width: 200px;
  height: 300px;
  background: #888;
 }

.left {
  overflow: hidden;
  height: 300px;
  background: #ccc;
  width:300px;
  float: left;
 }

.block {

   width: 100px;
   height: 50px;
   float: left;
   border: 1px solid blue;
}
.clearfix{
   clear:both;
}

To change the width of the div, I utilized JQuery

$(document).ready(function(){

   var left_width = $(".left").width();
   var block_width = $(".block").width()+2;

   var count = Math.floor(left_width/block_width);

   var calc_left_width = count * block_width;
   var calc_right_width = $("#main").width() - calc_left_width;

   $(".left").width(calc_left_width);
   $(".right").width(calc_right_width);

});

I added 2 to the width of the block to account for the borders, alternatively, you can utilize outerwidth(). Here is an example of it in action

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

The value within the style.setProperty function does not get applied

I have a progress bar that dynamically increases based on user input: <div class="progressBarContainer percentBar"> <div class="progressBarPercent" style="--width:${gPercent}" id="${gName}-pbar">< ...

Uploading images using Ajax with HTML and PHP

I'm facing a small issue with a multi-image upload feature. Everything works perfectly when I select multiple photos, but if I upload three pictures and then add another one, only the last picture is taken into account. For example https://i.sstatic.n ...

How can I use Bootstrap to center footer text and display two images on the right side?

Looking for some assistance with the footer on my webpage. I would like to center the text in the footer with two images positioned on the right side. Thank you in advance. Footer Below is the code I am currently using: <div class="CopyRightFoote ...

Unresponsive JavaScript on specific element IDs

I'm experimenting with making three lines perform different animations: line 1 rotating 45deg and translating, line 2 becoming opaque, and line 3 rotating -45deg and translating. Check out my JS Fiddle here <a href="#"><div id="menu" onclic ...

Exploring Laravel 4's CSS Routing

Attempting to connect my CSS document in Laravel 4 has been a bit challenging. The location of my CSS file is: public/css/style.css In the view, I have the following link: <link type="text/css" rel="stylesheet" href="{{URL::asset('css/style.css ...

How can I add 0-5 images to a column without causing any distortion in the row?

I'm having trouble adding a series of images to rows in my Bootstrap 4 project. The source assets are large by default and I'm struggling with scaling them properly without affecting other columns. Here's a visual representation of what I cu ...

Exploring the 3D Carousel Effect with jQuery

After creating a 3D carousel using jQuery and CSS3, I am looking to enhance it with swiping support. While there are plenty of libraries available for detecting swipes, I specifically want the carousel to start rotating slowly when the user swipes slowly. ...

Modify the contents of a textbox by editing the text as you type

Text within the text box follows this format: login -u username -p password When entering this text, I want to substitute 'password' with a series of '*'s equal in length. For example: 'login -u <a href="/cdn-cgi/l/email-prot ...

Is it necessary to contain every element within a row and column in the Foundation or Bootstrap grid system?

I have been exploring both Foundation and Bootstrap frameworks lately. A theoretical question came to my mind - do I always need to place each element inside .row>.column (for Foundation) or .container>.row>.col-- (for Bootstrap), even if I don&ap ...

A step-by-step guide on capturing video for an HTML element

Are there any methods available for recording an HTML element? I know that you can use .captureStream() for canvas elements, but is there a similar option for other HTML elements? Is there a different approach for recording specific parts of a website? ...

Bootstrap tab toggling with checkbox

I'm attempting to create tabs with the ability to include checkboxes, but when using data-toggle="tabs", the checkbox functionality seems to be disabled. Here is the link to my fiddle. Is there a solution to make the checkbox work within the tab tog ...

Transferring data from multiple form datepickers to identical destinations

I'm encountering difficulties with setting up multiple distinct datepicker instances within a single div and then sending the dates through a form. The issue lies in the fact that my div currently contains multiple array elements constructing this fo ...

To determine whether a variable is an integer in PHP

Is there a more elegant solution to this problem? if( $_POST['id'] != (integer)$_POST['id'] ) echo 'not an integer'; I attempted if( !is_int($_POST['id']) ) However, I encountered issues with is_int(). This ...

I'm encountering an issue where my information doesn't seem to be getting through to the Django

For some reason, the data I am trying to post to /api/recipe/recipes/ is not showing up in my HTML {% extends 'base.html' %} {% block content %} <!DOCTYPE html> <html> <head> <script src="h ...

Issue with Bootstrap Grid functionality

I've been working solo on a project and it's been smooth sailing so far. However, after making some changes to my code, the grids are not displaying as desired. Instead of three columns, only one is showing up and I can't seem to pinpoint th ...

Issues with AngularJS radio button functionality

Hey there, I am currently using AngularJS in my project. However, when I attempted to add a span to one of my input fields, all the radio buttons related to it stopped functioning. Can anyone offer some suggestions on what might be going wrong here? < ...

IE11 has issues with properly scaling SVG within flexbox

Within my website, I have incorporated various blocks featuring SVG images and text descriptions. To enhance the visual appeal, I aim to make these blocks flexboxes for easy vertical alignment of text, similar to this example: <div class="decorated-fea ...

A unique feature where a bar emerges from the bottom of the screen, smoothly glides upwards, and then securely fastens to

I'm working on bringing to life a concept that I've been envisioning. The design would feature a long scrolling page with a unique navigation bar behavior. The idea is for the navigation bar to initially start at the bottom of the screen and the ...

Issue with !important keyword taking precedence not resolved

While working on a navigation bar button, I encountered a specificity conflict with the opacity property. I attempted to use the !important override but it doesn't seem to be taking effect. Any insights into why this might be happening? Here is the H ...

Convert the 'value' attribute in HTML into a clickable link

Is there a way to make the output value of an HTML input field into a clickable link? Right now, it appears as follows: <input type="email" class="form-control" name="contactEmail" value="<?php echo $row_rsContactD ...