Three divs are set in place, with two of them of fixed width while the third div is

Looking for a layout with specific positioning and fixed widths:

One box on the left, one box on the right, each with a width of 200px. The middle div should adjust to take up the remaining space.

Any suggestions are appreciated. Thank you!

Answer №1

Check out this live example.

Using margin: 0 auto; will help center your element in most cases. Just remember, your element needs a defined width for it to work.

The CSS rule margin: 0 auto; is a shortcut for setting 0 margin on the top and bottom, and automatically calculating left and right margins. This combination aligns the element in the middle of its container.

While margin: 0 auto; may not be perfect for every centering scenario, it is effective in many situations.
Reference: You Can't Float Center with CSS

HTML

<div class="leftsidebar">a</div>
<div class="rightsidebar">b</div>
<div class="content">c</div>

CSS

.leftsidebar 
{ 
height: 608px; 
width: 60px; 
background:red; 
float:left; } 

.rightsidebar 
{ 
background:blue; 
height: 608px; 
width: 163px; 
float:right; 
} 

.content 
{ 
width: auto; //or any desired width
margin:0 auto;
background:yellow;
}

Answer №2

Check out the DEMO http://jsfiddle.net/yeyene/GYzVS/

This code works perfectly on both onReady and onResize events.

JavaScript

$(document).ready(function(){
    resizeMid();    
    $(window).resize(function() {
      resizeMid();
    });    
});  

function resizeMid(){
    var mid_width = $('#main').width() - ($('#left').width()+$('#right').width());
    $('#middle').css({'width':mid_width});
}

HTML

<div id="main">
    <div id="left">Left</div>
    <div id="middle">Middle</div>
    <div id="right">Right</div>
</div>  

CSS

html, body{
    margin:0;
    padding:0;
}
#main {
    float:left;
    width:100%;
    margin:0;
    padding:0;
}
#left {
    float:left;
    width:100px;
    height:300px;
    margin:0;
    background:red;
}
#middle {
    float:left;
    width:100%;
    height:300px;
    margin:0;
    background:blue;
}
#right {
    float:left;
    width:100px;
    height:300px;
    margin:0;
    background:red;
}

Answer №3

If you're looking to experiment with a simple layout using just HTML and CSS, check out this FIDDLE

<div class="container">
<div class="c1"></div>
<div class="c2"></div>
<div class="c3"></div>
</div>

CSS

div {
    height:500px;
    position:absolute;
    border:0px;
    padding:0px;
    margin:0px;
}
.c1, .c3 {
    width: 200px;
    background-color:red;
}
.c1, {
    left:0px;
}
.c3 {
    right:0px;
}
.c2 {
    margin-left:10px;
    margin-right:10px;
    background-color:blue;
    left:200px;
    right:200px;
}
.container {
    width:99%;
}

Answer №4

[updated]

To ensure proper width adjustment, consider using a table instead of the float style. Float style may not adjust the element's width as needed to fill in the gap.

Here is the HTML code:

<table>
    <tr>
        <td style="width:10%;"><div id="d1"></div></td>
        <td><div id="d2"></div></td>
        <td style="width:10%;"><div id="d3"></div></td>
    </tr>
</table>

CSS code:

#d1,#d3{
    background-color:red;
    width:100%;
    height:300px;
}
#d2{
    background-color:blue;
    width:100%;
    height:300px;
}
table{
    width:100%;
    height:100%;
}

See DEMO

Update:

If you prefer not to use tables or complicated JavaScript calculations, you can try this CSS approach:

#d1,#d3{
    float:left;
    background-color:red;
    width:10%;
    height:300px;
}
#d2{
    float:left;
    background-color:blue;
    width:80%;
    height:300px;
}

View DEMO

Answer №5

I would recommend avoiding the use of JavaScript and instead utilizing CSS to achieve this layout. You can create a #wrapper container and specify the desired width, then use percentages for the left, right, and middle divs.

Here is an example of CSS:

<div id="wrapper"> 
        <div id="left-column"> </div>
        <div id="middle-column"> <p>Content will dynamically stretch here</p></div>
        <div id="right-column"> </div>
</div>

#wrapper{
   float:left;
   width:1000px;
   *background-color:blue;
}
#left-column, #middle-column, #right-column{
        height:500px;
        float:left;
}
#left-column, #right-column {
    width:20%;
    background-color:red;
}

#middle-column {
    background-color:green;
    width:60%;
}

Answer №6

Sorry for being fashionably late, but I'm finally here!

All you need is some flexbox magic to make this happen.

Code Snippet in HTML:

<div class="flex">
  <div class="fixed-div"></div>
  <div class="dynamic-div"></div>
  <div class="fixed-div"></div>
</div>

CSS Styles:

.flex {
  display:flex;
}

.fixed-div {
  width:30px;
  background-color:blue;
  height:200px;
}

.dynamic-div {
  width:100%;
  background-color:red;
  height:200px;
  margin: 0px 10px;
}

Feel free to see the code in action here.

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

Retrieve the store location value when clicked (Javascript)

I'm currently struggling to capture the value of a click in my Javascript code and I could use some help understanding how to achieve this. <span class="s-link"> <a class="s-link-pim-data" href="javascript:void(0);" target="_blank" title="O ...

Node.js reads and writes a JSON file, encountering issues with another application in the process

I'm currently facing an issue with my node.js server (express.js) and a json file used for reading and writing data through a simple API. Interestingly, when the node.js server is stopped, another application can add data to the json file without any ...

Simple guide to returning values from PHP using AJAX

I have a script on my login page that uses AJAX to send data to PHP, and PHP then returns whether the login was successful or not. I am curious about how I can modify my scripts to also send back two variables and receive them in my login JavaScript so tha ...

What are the best practices for handling dynamic content internationalization in Angular?

According to Angular.io, the i18n tag is used to mark translatable content. It should be placed on every element tag that requires translation of fixed text. Now, what if we have an element with dynamic content? For example, consider a table displaying a ...

Encountering an issue with compiling Angular due to a Type Inference error

interface Course { name: string; lessonCount: number; } interface Named { name: string; } let named: Named = { name: 'Placeholder Name' }; let course: Course = { name: 'Developing Apps with Angular', lessonCount: 15 }; named = ...

The `this` keyword is incapable of accessing the object. It is instead pointing to the `window` object

Here is a sample of Javascript constructor code: function TestEngine() { this.id='Foo'; } TestEngine.prototype.fooBar = function() { this.id='bar'; return true; } TestEngine.prototype.start = function() { this.fooBar( ...

AngularJS Resource GET Request Unsuccessful

Is there a way to verify if a resource failed to be fetched in AngularJS? For instance: //this is valid syntax $scope.word = Word.get({ id : $routeParams.id },function() { //this is valid, but won't be triggered if the HTTP response is 404 or an ...

Add the bannercode snippet found on a webpage

I am currently facing an issue with appending a banner code to the end of a URL. The scenario is as follows: An individual receives an email with a link to a website (SITE1), where the URL includes a banner code, such as www.site1.com?banner=helloworld O ...

Why do CSS Variables have different syntax for setting and getting values?

When we use CSS Variables, also known as CSS Custom Properties, the syntax for setting and getting values is different. To set a value for --my-custom-width, we use: :root { --my-custom-width: 120px; } Similarly, to get the value of --my-custom-width, ...

Does v-if cause the jquery clock picker to malfunction?

Here is my unique div where the clockpicker library functions correctly. <div class="input-group clockpicker"> <input type="text" class="form-control" value="18:00"> <span class="input-group-addon"> <span class ...

Display a notification to the user prior to reloading the page

I have created a code for logging attendance at work using a barcode reader. The user simply needs to scan their card with a barcode to register their attendance. let $scannerInput = $(".scanner-input"); $(document).ready(function(){ $scannerInput.focu ...

Is it possible to create separate layouts for mobile devices and tablets using jQuery mobile?

Currently building a jQuery mobile app and looking to implement two distinct designs/layouts, one for mobile devices and another for tablets. Is this achievable? If yes, how can it be done? Any specific code required? Cheers! Noah ...

Ways to transfer a variable from a Node.js script to a JavaScript file on the client side

Seeking guidance on how to transfer the "myBlogs" variable from my Node.js file ("server.js") to a frontend JavaScript file ("blogs.js"). I intend to utilize this variable in the JS file to iterate through the array it contains, generating a template for e ...

Even as I create fresh references, my JavaScript array object continues to overwrite previous objects

Coming from a background in c# and c++, I am facing some confusion with a particular situation. Within the scope of my function, I am creating a new object before pushing it into an 'array'. Strangely, when I create the new object, it appears to ...

Creating a dual-column checkbox design with CSS only

I'm working with a dynamically generated form element that can only be styled using CSS. I need help achieving a specific layout without making any HTML changes, except for the classes. The "element" and "formField" classes are common and cannot be mo ...

Encountering difficulty extracting information from an XML document within the Parse Cloud utilizing the XMLReader in an Express application

My goal is to extract elements from an XML file that I have already stored on the Parse Cloud for my Express app. I began coding after finding a helpful resource on using XMLReader with XPath on cloud code, as well as a guide on Parse httpRequest for retri ...

All you need to know about fundamental HTML attributes

I've been struggling to resize images on my webpage. No matter how many times I change the attributes in the image tag, the image always shows up at its original size. For example, I tried this: <img src="url" alt="some text" style="width:40px;he ...

JavaScript Asynchronous Functions Not Handling Await Calls Correctly

The two fadeInList functions control the fading animation of a continuous list split into two lines. The typeOutText function displays text and is supposed to execute List1 first, wait for it to finish, and then proceed with List2. However, after adding ke ...

`Testing the jQuery UI slider widget using Selenium IDE: A step-by-step guide`

Our web application utilizes the jQuery UI slider widget, and we are looking to automate UI testing using Selenium IDE. However, we have encountered an issue in finding a way to manipulate the slider using Selenium commands. Is there a solution to this p ...

Displaying Image from Jquery Ajax Response

I have a PHP script that can resize images with specified dimensions by passing the width and height as variables like so: resize.php?width=100&height=200. The content-type of this PHP script is set to image/jpg. Additionally, I have an HTML page wher ...