Using HTML and CSS, create a layout with three columns where the side columns are flexible in width and the middle

Currently, I am facing an issue with the header design of my website. The layout consists of 3 columns of divs and my goal is to have a middle column with a fixed width of 980px. In addition, I want the left side of the header to span across the entire browser window with a blue background color, while the right side should extend to the edge of the browser with a black background color.

The desired look is as follows:

<------------------------------[blue][center header][black]---------------------------->

After conducting some research, I came across examples featuring two columns where the left column has a fixed width and the right fills up the remaining space. I am curious how this concept can be adapted to fit my current situation.

Would the solution involve code similar to the following:

<div style="width:100%;">
     <div style="display:table-cell; background-color:blue;"></div>
     <div style="width: 980px;">my header</div>
     <div style="display:table-cell; background-color:black;"></div>
</div>

I appreciate any guidance or feedback on this matter!

Answer №1

A straightforward fix - essentially maintaining your exact styling, but incorporating another block within the central table-cell element, such as this span here:

<div class="wrapper">
   <div class="left"></div>
   <div class="center"><span>my header</span></div>
   <div class="right"></div>
</div>

I have transferred all inline styles to a separate CSS block and utilized class selectors:

.wrapper {
    display:table;
    width:100%;
}
.left {
    display:table-cell;
    width:50%;
    background-color:blue;
}
.right {
    display:table-cell;
    width:50%;
    background-color:black;
}
.center {
    display:table-cell;
}
.center span {
    display:inline-block;
    width:900px;
}

Here is a jsfiddle

Furthermore, I adjusted the center to be much narrower for clearer visualization: jsfiddle

Trust this guidance proves helpful =)

Answer №2

Regrettably, there is no seamless method to achieve this with wide cross compatibility support. The CSS spec for display known as flexbox could provide a beautiful and elegant solution, but its current support is very limited. Here are some resources on flexbox for further reading...

http://css-tricks.com/old-flexbox-and-new-flexbox/

In the meantime, you can create the desired layout using basic CSS techniques that involve absolute positioning of your middle div.

Here is the JSFiddle: http://jsfiddle.net/CW5dW/

The CSS code is as follows:

.left {
    width: 50%;
    height: 300px;
    float: left;
    padding-right: 160px;
    box-sizing: border-box;
    background: red;
}

.right {
    width: 50%;
    height: 300px;
    float: right;
    padding-left: 160px;
    box-sizing: border-box;
    background: blue;
}

.middle {
    position: absolute;
    width: 300px;
    height: 300px;
    left: 50%;
    padding: 10px;
    margin-left: -150px;
    box-sizing: border-box;
    background: orange;
}

You might be wondering what's happening here?

In essence, by removing the middle div from the document flow, we can float the left and right divs with a width of 50% each to fully occupy the browser window. We then position the middle div to take up 300px (or 980px in your case) at 50% from the left edge. To center it, we use a negative margin equal to half its width. Additionally, we add padding to the left and right sides of the left and right divs to prevent content overlap with the middle div.

Answer №3

This response serves as an extended commentary to @Michael's original post, rather than a direct answer. I have provided another response containing a jQuery solution.

In reference to @Michael's well-organized solution, there is a minor issue that may arise when the height declaration is removed (which is highly likely for the OP). This method heavily relies on ensuring that all column backgrounds align perfectly at their bottom edge to maintain design cohesion. If the OP's design does not include backgrounds behind the columns, then this solution should work without any problems. However, if background elements are necessary (as indicated by the question), complications may arise. There are two possible solutions to consider...

  1. Implementing a simple JavaScript code that scans the page for column length, identifies the longest one, and adjusts all shorter columns to match the maximum length.

  2. The alternative (and potentially better) solution involves inserting a background within your <div> element with pre-existing columns set up on it (the background only needs to be 1px in height, presumably) - ensure that the central white band is 980px wide while the side columns extend past a thousand pixels or so to accommodate various browser sizes.

Answer №4

Alright, check out my solution. I've created a simple three-column fixed width layout that works for all users and adjusts for those with JavaScript enabled (which is the majority). This solution avoids any quirks that can arise from using more complex CSS techniques like absolute positioning or fixed heights.

Take a look at the demo here: http://jsfiddle.net/vuary/

You can inspect the HTML, CSS, and jQuery in the fiddle to see how it's set up. It's pretty basic. Here's a snippet of the jQuery code:

$(document).ready(function(){

    // Get the browser window width
    var docuWidth = $(window).width();

    // Get the central column width specified in the CSS
    var centerWidth = $('#center').width();

    // Calculate the side column widths
    sideColWidth = (docuWidth - centerWidth) / 2;

    // Set the width of the side columns
    $('#left,#right').css({
        width:sideColWidth+'px'
    });
})

UPDATE

I've converted the jQuery code into a function that runs when the document is ready and also on resize to ensure everything stays in place:

http://jsfiddle.net/aKeqf/

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

Border only at the bottom of the outline

I need help creating a bottom outline border when the cursor hovers over an image. I prefer to use this inner border style to avoid any layout issues with a traditional border-bottom. Below is my current code, which includes outline margins: .img-lightbox ...

What is the best way to save geolocation coordinates in a Javascript array?

I am attempting to utilize HTML5 geolocation to determine a user's location and then store the latitude and longitude coordinates in an array for future use with Google Maps and SQL statements. However, when I attempt to add these coordinates to the a ...

Access the website by logging in with idhttp

I'm attempting to log in to a website using Delphi / Indy. Currently, I only have a button (used to send a password / username) that calls this procedure. procedure TForm5.Login(name: string; Pass: string); var UserID :string; Password :string; res : ...

Using the cordova-plugin-file to access and play video files stored on an external SD

I am in need of assistance with displaying an image and playing a video file from the root directory of an external SD card in my Cordova wrapped HTML project. I couldn't find a suitable place to ask these questions on the GitHub page for the cordova- ...

InvalidSelectorError: The specified selector is not valid //button[contains(., 'buttonText')] while running JavaScript code

Here's an example of HTML code that I am working with: <button id="toolbar-item-17-update-id" type="submit" name="action" value="update" class="button-action-update btn btn-secondary"> Ed ...

What is the process of adding information to a database from a table using JSP?

Is it possible to store data into a database using JSP? Here is the code I have written, can you please help me find and correct any errors in it? My goal is to fetch data from form fields in a JSP page and store it into a database. Database name: test T ...

Is it possible to include a class in a 'label' element?

Forgive me if this is a simple question, but I'm curious - is it possible to add a class directly to a 'label' tag without needing to enclose it in a 'span' tag for styling? As I delve into "CSS: The Missing Manual," the book instr ...

The font size of the textarea dynamically adjusts based on the size of the screen

Some strange behavior is occurring in the Android default browser when I set the width and height of a textarea to 100%. The font size of the textarea seems to increase based on the screen size, and even though I attempted to alert the font-size using jQue ...

Shift the sleek navigation arrows to the interior of the slides

Is there a way to relocate the navigation arrows on the slider images? I have tried various methods found online with no success. Currently using ASP.NET, but doubt it matters. Employing the latest version of SLICK. Here is the HTML code snippet: <%@ ...

Concealing Bootstrap 3 elements on a split-screen layout

I encountered a peculiar issue with Bootstrap 3. When I resize my browser width to half the screen, there is a slight 1px threshold where both "hidden-xs" and "hidden-sm" elements become visible simultaneously. Is there a way to fix this so that only one o ...

observing numerous directories in Sass

Is there an efficient way to monitor multiple directories in sass using a shell script? This is what I currently have in my shell script: sass --style compressed --watch branches/mysite/assets/css/sass:branches/mysite/assets/css & sass --style compre ...

Guide to customizing color themes using Twitter Bootstrap 3

Recently, I dove into the world of Twitter Bootstrap 3 without any prior experience with earlier versions. It's been a learning curve, but I managed to set up a WordPress theme using it as the framework. All my elements are in place, but they all see ...

What is the proper way to align text based on the text above using CSS?

I have a container with a width of 50% of the viewport width, which contains some text. I want to align some other text below it to the right side. You can find an example here: https://jsfiddle.net/hadr4ytt/1/ Here is the current CSS for this: .containe ...

Creating dynamic email templates in Node.js

I am working on a project using the MEAN stack and I need to implement email functionality. I have created separate email templates, but I want to include a common header and footer in all of them. Route.js router .route('/api/user/register&a ...

A guide on incorporating a customized Google map into your website

Recently, I utilized the Google Map editing service from this site: https://developers.google.com/maps/documentation/javascript/styling This link provided me with two things: 1. A JSON code 2. The Google API link However, I am unsure about how to incorpo ...

Design a Bootstrap dropdown menu featuring various options within an icon container

I have designed a blade with a signboard containing multiple columns, each displaying its name, color, and assigned cards (screenshot). To enable sorting of these cards, I added an icon next to each column title. My goal is to allow users to select sorting ...

Techniques for Adding Background Color to Fieldset Border

Recently starting to learn HTML and stumbled upon a question: How can I create a background color or image for a field set border? Can I simply use regular color values, or do I need special codes for creating a background color in a field set? Any insig ...

Arranging my form input fields in a neat vertical stack

@gnagy @Satwik Nadkarny I appreciate the help you both provided, and I wish I could give a plus to your responses (requires 15 rep sadly), but I have encountered another issue. After the first two text input fields, I added an additional input field for e ...

Is there a way for me to produce a random choice depending on the option selected by the user in the <select> menu?

As a beginner in JavaScript, I am attempting to create a feature where users can select a genre from a dropdown list and receive a random recommendation from that genre displayed on the screen. In essence, my goal is to allow users to get a random suggest ...

Utilizing ng-bind-html alongside ng-controller

Injecting insecure html into a <div> is part of my current task: <div class="category-wrapper" ng-bind-html="content"></div> The angularjs "code" in this html snippet ($scope.content) includes the following: <script type='text/ ...