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

Is there a way to dynamically shift arrow key focus onto buttons in Angular using the left and right arrow keys?

I am facing an issue where pressing the arrow keys left and right does not focus or allow me to navigate through these buttons using the arrow keys. However, when checking the keycode values, they are printed according to the key pressed. I would like to k ...

Tips for aligning hyperlinks in the navigation bar using CSS3 and HTML5

Struggling to center the links in the navigation bar has been a challenge. I really want those buttons smack dab in the middle of the header bar, but despite trying everything, they stubbornly remain on the left-hand side. .header { overflow: ...

Connecting an external SVG file to its corresponding HTML code

Can I connect my SVG file to my CSS file for a line animation? I'm uncertain about including the SVG code directly in my HTML file. ...

Hide the div element once the timer runs out

Struggling to make a timer disappear when it reaches 00:00, every attempt results in the div being hidden immediately. Check out the code snippet I've been working with: $(document).ready(function(e) { var $worked = $("#worked"); function upd ...

IE6 disrupts stored layouts

I've encountered a strange issue with my design in IE6. It loads perfectly at first, but after reloading it a couple of times, everything suddenly collapses. The strange part is that when I upload an updated version, it fixes the issue, only to break ...

Boundaries on Maps: A guide to verifying addresses within a boundary

User provides address on the website. If the address falls within the defined boundary, it is marked as "Eligible". If outside the boundary, labeled as "Ineligible". Are there any existing widgets or code snippets available to achieve this functio ...

Create a distinct CSS animation 'origin' declaration for every <g> element

I am using CSS animation to apply a 'wobble' effect to each letter within a word. The letters are represented by SVG groups <g>. However, in the provided example, the wobbling effect becomes more pronounced with each subsequent letter, wher ...

Is there a regular expression that can be used to extract sentences from HTML

As I delve into the world of HTML parsing, I'm starting to understand that using regular expressions for this task is not always the best approach. However, in my Perl class, I am currently experimenting with utilizing regular expressions to extract a ...

Looking for a Plugin that Can Responsively Display Images in a Vertical Layout - Does One Exist using Jquery

Looking for a gallery slider plugin that is responsive both horizontally and vertically. Have tried Flexslider1/2, Galleria, and others but they do not adjust to vertical resizing. Changing the CSS did not help. If you resize the browser horizontally with ...

What's preventing my Angular list from appearing?

I am currently developing an Angular project that integrates Web API and entity framework code first. One of the views in my project features a table at the bottom, which is supposed to load data from the database upon view loading. After setting breakpoin ...

The HTML and body elements do not possess a width of 100% within the document

When viewing my portfolio site on smaller screens such as 425px and below, there is a white gap on the right side. This issue arises because the HTML and body elements do not inherit full width by default. Do you know why this happens and how I can resol ...

Solving the right-to-left orientation issue in the Bootstrap navbar

I have implemented a navbar using the code below, which aligns well from the right side. However, the menu items are sorted in ltr order and I need them to be in rtl order, so that "Dropdown 1" starts from the right side of the page. Currently, it appears ...

Tips for converting text from an HTML input field to a JSON file

After designing a form with four text fields and a submit button, my goal is to save the data into a JSON file upon submission. Additionally, I am looking for a way to display all of the JSON data on my webpage. ...

Three.js is currently rendering a blank canvas in pure white

After following the tutorial at , my browser only displays a white window. I attempted separating the files into js and html, but no luck. What I have already tried: experimenting with adding/deleting the nomodule parameter in the script tag utilizing a ...

What is the best way to prevent the first option in a <select> element from moving up and down using jQuery?

Is there a way to make the first option in a select list non-selectable and keep it at the top when using up and down buttons? The value option should always remain on top. If "Author" is selected and the up button is clicked, nothing should change. The ...

Guide to accessing the content within an h1 tag with JavaScript

I currently have a setup with 3 pages: 2 of them are WordPress pages while the other page is a custom page template featuring a form. The first two pages were created using the wp-job manager plugin. The first page includes a dropdown menu with a list of a ...

"Unexpected discrepancy: Bootstrap Glyphicon fails to appear on webpage, however, is visible

I am having some trouble getting the glyphicon to display properly in my side nav. The arrow head should rotate down, which is a pretty standard feature. Here is the link to the page: The glyphicon should be visible on the "Nicky's Folders" top leve ...

How can we identify if the user is utilizing a text-input control?

Incorporating keyboard shortcuts into my HTML + GWT application is a goal of mine, but I am hesitant about triggering actions when users are typing in a text area or utilizing the keyboard for select menu operations. I am curious if there exists a method ...

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

CSS slideshow with automatic horizontal image transition that seamlessly restarts

I am facing an issue with my HTML & CSS automatic horizontal slideshow. Everything works fine, except for when the last image finishes sliding and the slideshow loops back to the first image, there is a sudden jump in the appearance of the first image. How ...