Expanding the width of three floating divs in the center to match the width of the parent container div

In my design, I have a parent container with three inner divs that are floated. The left and right divs have fixed sizes, however, I need the center div to expand and fill the space available within the parent container.

<div class="parent-container">
    <div class="left"></div>
    <div class="center">I want this div to automatically adjust its size to fit the parent container</div>
    <div class="right"></div>
<div>

.parent-container
{
    overflow: auto;
    width: 100%;
    border: 1px solid pink;
}

.left { width: 50px; }
.right { width: 80px; }

.left, .center, .right
{ 
    float: left; 
    border: 1px solid black; 
    height: 100px; 
}

http://jsfiddle.net/fvuyQ/

I attempted setting the width to 100%, but it didn't produce the desired result. Does anyone know of a straightforward solution to achieve this layout?

Answer №1

How about floating only the left and right elements and adding margins to the center one

<div class="main-wrapper">
    <div class="left-side"></div>
    <div class="right-side"></div>
    <div class="center-block">this should stretch to fit the main wrapper</div>
<div>

css

.left-side { width: 50px;float: left; }
.right-side { width: 80px;float: right; }
.center-block{border:1px solid green;margin:0 80px 0 50px;}
.left-side, .right-side
{ 
    border: 1px solid black; 
    height: 100px; 
}

Demo available at http://jsfiddle.net/gaby/fvuyQ/4/

Answer №2

If you're comfortable with utilizing CSS3, you have the option to employ the calc() function alongside box-sizing: border-box

Here is a working fiddle for reference: http://jsfiddle.net/EfrainReyes/4usMS/

Please keep in mind: The inclusion of box-sizing: border-box was implemented to simplify the process of determining the center div's width. If you opt not to utilize box-sizing: border-box, you can calculate the width by subtracting the combined widths of the left and right borders on the floating divs, totaling 6px, as shown below:

.center { width: calc(100% - 50px - 80px - 6px); }

Update: I have revised the example to adopt CSS2 positioning instead of relying on CSS3 calc(). You can view the updated version here: http://jsfiddle.net/EfrainReyes/4usMS/1

Answer №3

Consider implementing the following changes in your CSS file

.center { 
    width: 100%;
    min-width: 100%; //Include this line if you always want it to occupy 100% of the parent element
}

.left, .right, .center {
    display: inline-block;
    position: relative;
}

It is recommended to also include the following code snippet

.parent-container {
     float: left;
     display: block;
     position: relative;
}

In addition, based on my interpretation of your query, you might find the below code helpful

.right, .left { position: absolute; }
.right { top: 0; right: 0; }
.left { top: 0; left: 0; }

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

Problem with applying ng-class directive in AngularJS

I am currently developing an application using AngularJs and Bootstrap. My goal is to display the title of a 'scenario' with different styling based on its status, along with a specific icon. Initially, I attempted the following approach: <s ...

Resolving a CSS Layout Dilemma: How to Overlay Sidebar on Wrappers

I've run into a challenge while attempting to position a sidebar over page wrappers. The issue with absolute positioning arises when the sidebar needs to align with the corner of the blue header. I have contemplated using JavaScript to maintain its cu ...

What is the best way to implement nested iterations in Jade?

ul li a(href='') menu1 li a(href='') menu2 ul li a(href='') sub-menu2 li a(href='') menu3 ul li a(href=&apos ...

Buttons containing two lines of text appear bigger compared to those with only one line of text, but it is essential for all buttons to be consistent in size with the single line buttons

I am facing an issue with the buttons on my website, as they seem to resize based on the amount of text present. When there are 2 lines of text, the buttons appear larger compared to those with just one line. How can I rectify this situation? Although thi ...

Tips for accurately obtaining row counts from a dynamic table when the `<tr>` numbers are constantly fluctuating

One issue that I encountered as a tester involved verifying the total number of rows on a dynamic table. Despite having 50 rows in the table, the HTML only displayed a maximum of 22 <tr>. This discrepancy caused my automation code to return an incorr ...

Applying custom CSS designs to images using Thymeleaf

Currently working on a web application using Spring MVC and Thymeleaf for templating. I'm struggling to figure out how to apply rules to an image, especially when using an external CSS file. I have tested code that works: page.html <a class="nav ...

How to Make a React JS Component Shift to the Next Row as the Window Shrinks

I am facing an issue with the layout of my product detail page. Currently, I have two components - an image gallery on the left and product information on the right. However, when the window size gets smaller, I want the product information component to mo ...

Which is more optimal for importing CSS - using the link tag or style tag?

Is this a matter of personal preference or is there a specific advantage to using one method over the other? <link href="main.css" rel="stylesheet" type="text/css"> versus <style type="text/css> @import url('main.css'); </style& ...

Implementing CSS in Laravel 8

After placing my CSS file in the public/css folder, I included it in the head section with the following code. <link href="{{ url('/css/app.css') }}" rel="stylesheet"> Unfortunately, the CSS styles are not being applied ...

What is the method for assigning a class name to a child element within a parent element?

<custom-img class="decrease-item" img-src="images/minus-green.svg" @click="event => callDecreaseItem(event, itemId)" /> Here we have the code snippet where an image component is being referenced. <template&g ...

Using checkboxes to select all in AngularJS

Recently, I started working with Angularjs and came across some legacy code. I am trying to figure out a way to select all checkboxes once the parent checkbox is selected. Here is the parent checkbox: <div class="form-group"> <label for="test ...

Pressing a specific button triggers a broadcast signal, prompting a modal window to pop up and display relevant information

Within my website's HTML page named rollup.html, there exists a button that triggers the opening of a modal. <button id="myBtn" ng-click="printDivModal('rollup-tab')">ModalTest</button> In the accompanying JavaScript file, roll ...

Building an HTML Form for Requests

Having some trouble creating an HTML form, particularly with resizing the Fieldset and legend elements. As a novice programmer, I am practicing building HTML forms. My goal is to have the form adjust its size based on the window without displacing the cont ...

Modifying the Vue page background directly from a page: a step-by-step guide

UPDATE: I recently discovered that I need to modify the body background-color, but I am unsure of how to accomplish this from within the style of this page based on the wrapper's class. The class is determined by the data values. I aim to change the ...

Guide to showcasing associated information in HTML using Angular 7

I am a beginner with Angular 7 and I am attempting to showcase a product's color on the HTML side using Angular 7 but have not been successful. Below are my tables; Product Id Name Color Id Name ProductColorRelation Id ProductId ColorId In ...

Show picture during the process of loading an external webpage

Utilizing a script to load an external page (external meaning a page within my website) inside a div. Loading the page takes time, so I want to display an image until the page is fully loaded. JAVASCRIPT function HideLoader() { $('#loader' ...

Problem with the show/hide feature on jQuery. Automatically scrolls to the beginning of the page

On my website, I have successfully implemented two basic Show / Hide links that are working great. Here is the HTML code: <!DOCTYPE html> <html lang="en"> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" conte ...

The Regex.Replace function is not functioning as expected

I am currently faced with the challenge of replacing HTML tags within a string. For example, I need to convert <strong>text</strong> to <b>text</b>, among other similar replacements (although I understand that the b tag is considere ...

Tips on how to change the color scheme of a webpage

I am currently working with basic HTML and CSS, but I am facing an issue where my background color is only filling a third of the page instead of the entire page. Despite attempting to use the html and body classes for the background color, it did not prod ...

What are some methods for converting data from data tables into alternative formats?

I need assistance exporting data from data tables in various formats such as Copy, CSV, Excel, PDF, and Print. Can someone show me how to do this for the example provided below? <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4 ...