Styling the sacred grail layout with organized columns

In my search for answers, I have come across many general responses to this common question. However, none of them address the specific constraints that I am facing with this version.

The task at hand is to implement the holy grail layout, but with a key requirement: The columns must maintain their order from left to right, L (left), C (center), R (right), without relying on absolute positioning.

Another important consideration for me is browser compatibility. While I do not need to support outdated browsers like IE6, ensuring functionality for at least IE7 is preferable. Thus, using the calc() CSS function is not an option.

The standard practice, as seen in the A List Apart article, involves arranging the columns in the order of C-L-R for SEO purposes. Many argue that this helps with search engine optimization and visual display. However, when it comes to accessibility tools like JAWS, the DOM order matters more.

Below is a typical markup structure for L-C-R:

<div class="wrapper">
    <div class="container">
        <div class="column-l">
            <div class="cell">Column L</div>
        </div>
        <div class="column-c">
            <div class="cell">Column C</div>
        </div>
        <div class="column-r">
            <div class="cell">Column R</div>
        </div>
    </div>
</div>

Accompanying this structure is some CSS styling:

.wrapper {
    width: 100%;
}
.container {
    width: 90%;
    margin-left: auto;
    margin-right: auto;
}
.cell {
    padding: 1em;
}

My goal is to fix the widths of .column-l and

.column-r</code while keeping <code>.column-c
fluid. By floating .column-l left and applying a margin left to .column-c, these two columns fall into place as intended:

.column-l {
    width: 300px;
    float: left;
}
.column-c {
    margin-left: 300px;
}

The issue arises with .column-r. Despite attempting various adjustments, such as applying a float right or a margin-right property, it does not align correctly with the other columns. Even if a margin-right is given to .column-c, .column-r still refuses to cooperate.

Answer №1

.column-l {
    width: 300px;
    background: red;
    display:table-cell;
}
.column-c {
    background: green;
    display:table-cell;
}
.column-r {
    width: 300px;
    background: blue;
    display:table-cell;
}

Is this solution helpful? No need for floating elements, the left and right columns are fixed at 300px width each, while the center column expands to fill the remaining space. Additionally, all columns have the same height.

Answer №3

This answer has been improved. Here's the original version.

Presented below is a simple layout pattern that was constructed with the help of chat participants and some experimenting. It fulfills the requirements outlined in my initial query, although it does involve using one non-semantic <div> (which is an improvement from the previous solution). However, I'm not too concerned since it only appears once (or a few times) on each page. Non-semantic <div>s can often be replaced with HTML5 semantic sectioning blocks, so it's not a major issue.

The bug highlighted in my original response, where the center column would shrink when lacking content, has been resolved.

Summary

This method employs 3 columns: L (left), R (right), and C (center), where L and R have fixed widths while C is fluid.

The key to this approach lies in utilizing positive and negative margins along with the .with-r/.with-l modifier classes.

The .with-l/.with-r modifier classes are added to the .column-container. They introduce positive left and right margins respectively, matching the width of the left and right columns (in this case, both are set at 240px).

Both the left and right columns are given fixed widths of 240px and a corresponding negative margin equal to their width. The left column has the negative margin on the left side, and the right one on the right side.

An excellent feature of this solution is its flexibility. If I wish to switch to a 2-column layout by removing the R column and its associated .with-r modifier, or eliminate the L column, it can be easily accomplished.

JSFiddle

http://jsfiddle.net/5yq7J/ (similar to the example below but with Lorem Ipsum text)

SCSS

/*************************/
/* quick and dirty reset */
/*************************/
* { 
    margin: 0;
    padding: 0;
    outline: 1px dotted #f00; 
}

/*************************/
/* some boilerplate crap */
/*************************/
%block {
    position: relative;
    display: block;
}

%clear {
    &:after {
        @extend %block;
        content: "";
        float: none;
        clear: both;
        width: 100%;
    }
}

%layer {
    @extend %block;
    @extend %clear;
}

/***********************************/
/* all the important stuff follows */
/***********************************/

.container { 
    @extend %layer;
    margin-left: auto;
    margin-right: auto;
    min-width: 960px;
    width: 90%;
}

.column-container {
    @extend %layer;
    &.with-l { margin-left: 240px; }
    &.with-r { margin-right: 240px; }
}

%column { 
    @extend %block;
    float: left; 
}

.column-l {
    @extend %column;
    width: 240px;
    margin-left: -240px;
}

.column-c {
    @extend %column;
    width: 100%;
}

.column-r {
    @extend %column;
    width: 240px;
    margin-right: -240px;
}

.cell { 
    @extend %layer;
    padding: 1em;
}

HTML

<h1>Columns L, C &amp; R</h1>
<div class="container">
    <div class="column-container with-l with-r">
        <div class="column-l">
            <div class="cell">
                <h2>Column L</h2>
                <p>Lorem ipsum dolor...</p>
            </div>
        </div>
        <div class="column-c">
            <div class="cell">
                <h2>Column C</h2>
                <p>Lorem ipsum dolor...</p>
            </div>
        </div>
        <div class="column-r">
            <div class="cell">
                <h2>Column R</h2>
                <p>Lorem ipsum dolor...</p>
            </div>
        </div>
    </div>
</div>

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

jQuery - Enlarge Tree View to the level of the selected node

I am struggling to figure out how to expand the tree view up to a selected node level. If anyone has any ideas on how to accomplish this, please let me know. For instance, if we click on 'Parent d' in the 'Category list', I want to exp ...

Revise the data model by using option IDs instead of option values in the selection dropdown

Just getting started with angular js and facing an issue. I have a json value that will determine the content of my html. Specifically, I need to populate a select dropdown with two options and have one automatically selected based on the flag provided in ...

How to Use Laravel to Create HTML Divs Without Images

While working with a text editor, we have the ability to incorporate various HTML tags such as images and videos. However, I am only interested in displaying text fields. When I use {{$loop->content}}, it displays: <p><strong>EXAMPLE</st ...

Creating two adjacent squares can be achieved by arranging columns and rows in a specific way

Looking to create a website with a gallery block but running into some issues. I only want to utilize Bootstrap along with my custom CSS for the finishing touches. The layout should consist of 2 squares next to each other, with the left square being 2 colu ...

CSS hover effect not working while mouse is in motion

As a beginner in HTML, CSS, jQuery, and JavaScript, I've been trying to trigger a div when hovering over one area using the adjacent siblings code. However, I'm encountering an issue where if I move my mouse after triggering the event, it keeps r ...

the child div within a Bootstrap 3 column is not being properly styled

Experiencing a strange issue with a relative div within a column in Bootstrap 3. Here's my code: <div class="col-lg-6"> <div class="large" style="background: url(img/bg.jpg);"> <h1 class="grid-header">Content 1</h1> ...

What is the process for creating a fade out effect with JavaScript?

https://i.sstatic.net/1qgWt.png Is there a way to create a fade out effect in JavaScript? In the image provided, you can see that when you click on a day, a box slides down. If you click on another day, the previous box slides back up while a new box slid ...

Troubleshooting Firefox HTML Positioning Problem (Functioning Properly in IE and Chrome)

After implementing the code below, I encountered an issue. In IE and Chrome, when hovering over the "CODE" section, the div correctly appears at position 100 x 100. However, in Firefox, the div fails to move to the specified position. What changes should b ...

The accuracy of getBoundingClientRect in calculating the width of table cells (td)

Currently, I am tackling a feature that necessitates me to specify the CSS width in pixels for each td element of a table upon clicking a button. My approach involves using getBoundingClientRect to compute the td width and retrieving the value in pixels (e ...

Utilize JavaScript and PHP to dynamically modify the contents of an HTML file

After not receiving a satisfactory answer to my question yesterday, I decided to find a solution. However, I am now facing an issue with the new program and unsure of the mistake. The program involves retrieving the contents of announcement.html and displ ...

When it comes to HTML5 drag and drop functionality, only the dragstart event is triggered

I have successfully implemented HTML5 drag&drop using jQuery UI, but now I want to switch to using pure HTML5 API. Currently, only the dragstart event is firing for me. I am not able to catch the rest of the events. However, everything seems to be fun ...

Solving issues with flexboxes in Safari 5.1.7 on Windows

I am currently utilizing flex boxes for managing the layouts of my website. However, it seems that the Safari version for Windows (5.1.7) is outdated and causing all flex boxes to be dysfunctional. On MacOS systems, the same version 12.x works correctly. ...

The Safari browser displays the mobile-friendly version of the website

Everything appears correctly when looking at the website in Firefox and Chrome. However, Safari is displaying the mobile version of the site instead. I did not make any CSS changes specifically for desktop screens while working on the responsive design for ...

How can I display a pre-existing div with a dropdown button?

I have individual div elements for each type of clothing item (shirt, pant, suit) that I want to display when the corresponding service is selected. This means that when I click on one of them, only that specific div will be shown. I am looking for a solut ...

Enhance Your Website Design: Implement an expanded input field that overlaps menu items

I have implemented a feature in my Bootstrap .navbar where an expanding input field appears only when the search button is clicked. However, the expanded input field currently pushes the menu-items to the left. When I position #search-form absolutely, the ...

I desire to incorporate a subtle fading effect into the script

I have written the script code and now I am looking to add a fade effect to it. Can anyone guide me on how to achieve this? Your help is much appreciated! â€ğI used an online translator as English is not my native language. Please bear with any awkward ph ...

AngularJS $timeout function failing to update variable

I'm currently working on implementing a shaking animation for a button when it's clicked and the form fields above it are invalid. This is the snippet of code from the controller: if(valid){.. do valid stuff here ..} else{ console.log(this.sha ...

Styling tables within HTML emails for Gmail and then utilizing PHPMailer to send the emails

I've been racking my brain over this for hours with no luck! Objective: Implementing inline styles for table, td, th, p elements in an HTML email intended for Gmail using PHPMailer. Challenge: Inline styles not being rendered HTML Snippet: <sec ...

Adjusting the height of an Ionic list item when the text overflows is not functioning properly

Snippet of Code: <ion-header-bar align-title="center" class="bar-positive"> <div class="button button-clear" ng-click="closeGlobalTaskModal()">Cancel</div> <h1 class="title">Add Global Task</h1> <div class="bu ...

I am having trouble getting my dropdown to line up with the drop button labeled "MORE"

Despite trying multiple approaches, I am still struggling to align the dropdown content with the dropbtn. My goal is to have the content consistently positioned below the more menu. HTML: ` <html> <meta name="viewport" content="width=device-widt ...