Three columns of equal width, with the center column aligned in the middle

My layout consists of 3 columns, each with a width set to 225px. Here's how it's structured:

<div id="main">
     <div id="col1">
         <p>...</p>
    </div>
    <div id="col2">
        <p>...</p>
    </div>
    <div id="col3">
        <p>...</p>
    </div>
</div>

When resizing the browser window, I want col1 to float left, col3 to float right, and col2 to be centered with the margins between col1/2 adjusting accordingly.

However, my CSS isn't working as expected. Here's what I have:

#main {
    width:100%;
}

.col1, .col2, .col3 {
    width:225px;
}

.col1, .col2 {
    float:left;
}

.col3 {
    float:right;
}

.col2 {
    margin-left:auto;
    margin-right:auto;
}

Answer №1

Take a look at the functioning example: http://jsfiddle.net/NXRzP/

To begin, I inserted a div within another div.

<div class="primary">
    <div class="column">
        <div class="column1">...</div>
    </div>
...

In the CSS code, there are several key points to observe:

  • Your original CSS contained errors such as spaces in class names and incorrect selector usage (use "#" for id and "." for class).
  • The outer Div has a width of 33%, distributing it across the entire page.
  • The inner Divs are floated left/right with center alignment using margin auto.
  • A minimum width was added to the "primary" div to prevent column overlap when too narrow.

Below is the complete CSS code:

.primary {
  width:100%;
  min-width: 700px;
}

.column {
  width:33%;
  float:left; 
}

.column1, .column2, .column3 { 
  width:225px; 
  background-color: #999 
}

.column1  { float:left; }
.column2  { margin: 0 auto; }
.column3  { float: right }

Answer №2

Update all div IDs to classes. You mistakenly used IDs instead of classes for the col divs. Here is the link

 <div id="main">
<div class="col1">
<p>...</p>
</div>
<div class="col2">
<p>...</p>
</div>
<div class="col3">
<p>...</p>
</div>
</div>

Answer №3

Your code contains numerous errors. You have mistakenly used class instead of id in your CSS.

Check out this revised version: jsFiddle example

HTML

<div id="main">
    <div id="col1">
        <p>...</p>
    </div>
    <div id="col2">
        <p>...</p>
    </div>
    <div id="col3">
        <p>...</p>
    </div>
</div>

CSS (color codes are for illustration purposes only)

#main {
    width:100%;
    background : red;
}

#col1, #col2, #col3 {
    width:33%; // or 225px
    float:left;
}

#col1{
    background:green;
}

#col2{
    background:blue;
}

#col3{
    background:purple;
}

Answer №4

Make sure to use a class selector instead of an id selector like #. I have provided an example where you don't need to specify the width for #col2, but you must update the HTML structure:

<div id="main">
     <div id="col1">
         <p>...</p>
    </div>
    <div id="col3">
        <p>...</p>
    </div>
    <div id="col2">
        <p>...</p>
    </div>
</div>

CSS code:

#col1, #col3 {
    width:225px;
}

#col1  {
    float:left;
}

#col3{
    float:right;
}

#col2 {
    overflow: hidden;
}

Answer №5

Eliminate any spaces in your CSS class names such as .col 1:

.col1, .col2 {
float:left;
}

.col3{
float:right;
}

Also, ensure that your HTML uses IDs instead of classes.

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

Tips for adding and executing animations in JavaScript/jQuery on your webpage

Within the code snippet given below, I am creating and appending two canvas tags to the body. The purpose of this animation is to draw a check mark. Upon loading the page, both canvas tags are added to the page; however, only one check mark is actually dra ...

What is the most effective method for populating an array with all images from a particular directory?

I recently installed a pdf viewer plugin on my website (check it out here: ), and I have a question regarding its functionality. I am looking to include approximately 60 - 70 pages in this flip book, but I am unsure of how to proceed. I have attempted var ...

Creating a personalized .hasError condition in Angular 4

I am currently in the process of modifying an html form that previously had mandatory fields to now have optional fields. Previously, the validation for these fields used .hasError('required') which would disable the submit button by triggering a ...

The function Rotate() does not exist

Within my project, I have set up a canvas where I am able to: Load an image onto the canvas Resize the uploaded image Currently, I am working on implementing a rotate function that allows me to rotate the loaded image within the canvas by holding down th ...

What is the best way to efficiently pass a prop from a Parent styled component to its children's styled components, regardless of how deeply nested they are?

I am utilizing these customized components: import styled, { css } from 'styled-components' const Container = styled.div` background-color: ${props => props.theme === "light" ? "hsl(0, 0%, 98%)" : "hsl(235, 24%, 19% ...

Achieving (keyup.enter) event on an option within a datalist in Angular 6

How can I trigger the (keyup.enter) event on an option within a datalist in Angular 6? This is my HTML Code: <input list="filteredArray" (keyup)="filterEmployee(empForm.controls['empname'].value)" type="text" formControlName="empname" /> ...

Ways to center-align this menu (Spanish language)

I am working on optimizing the alignment of the menu on my website: My goal is to center the menu, as the English version looks good. #site-navigation { width: 980px; margin: 0 auto; text-align: left; } After analyzing the code, we have iden ...

Deactivating The Canvas Element

I am currently working on a project that involves using Three.js alongside a menu placed above the canvas element, which is essentially a regular div. However, I have encountered an issue where the canvas element still registers input when interacting with ...

What is the best way to maintain the value of variables across different HTML files?

Imagine having a scenario where a page is connected to a .js file. This file contains code that assigns a value to a variable like this: var foo; function bar() { foo = //value generated through user input } bar(); Now the goal is to move t ...

Executing a specific block of Python code is contingent on selecting a row from a MySQL database

I have innovatively created a unique project involving musical stairs using Python, motion sensors, Raspberry Pi, and a web application. The web app allows users to select the type of instrument sound they want to produce, which is stored in a MySQL databa ...

Transforming the hue of a radio-button

When it comes to the default CSS code for radio buttons, they appear gray when unselected and blue when selected: https://i.stack.imgur.com/hsazr.png However, I have a specific requirement for them to be black in both states. In order to achieve this, I ...

Debugging ImageMapster: troubleshooting jQuery/HTML problems

I am currently experiencing difficulties with the implementation of the jquery plugin known as ImageMapster. To assist in troubleshooting, ImageMapster has provided a demonstration through jsfiddle which can be accessed here: http://jsfiddle.net/nYkAG/396/ ...

Is there a way to populate rows in a data table on subsequent pages using JavaScript?

Here is the ajax function I have written to change values of a row on one page while updating the total on another page of my data table. $.ajax({ type : "Post", contentType : "application/json; charset= ...

Tips for managing a series of images in a line within a bootstrap form

Can someone help me troubleshoot my bootstrap row handling? Here is my code: <div class="form-group col-md-6"> <?php if($saved_image != ''): ?> <div style="width:100px; height:100px"> <img class="i ...

JavaScript function to add or remove a class upon clicking

Currently experiencing an issue with my accordion functionality. I have successfully implemented code that allows for toggling one heading open at a time, but I am struggling to add an open/close image beside each heading. At the moment, when a heading is ...

When the URL is modified, triggering an event to load

Is there a way to make a page load directly to a specific section and automatically open an accordion? For instance, I have a page with multiple accordions displayed vertically. I already know that using id's in the URL like page#accordion1 will scro ...

Issue with Bootstrap: unable to align columns vertically

I am currently learning how to use bootstrap, but I am facing a challenge with vertical alignment of columns. Despite trying various methods, I can't seem to get the content to align anywhere other than the top. Even starting from scratch with code fr ...

Conceal the footer when the website is scrolling, reveal it when the website is not scrolling

Before I pose my question, I need to establish two constraints: Must be compatible with IE 7+ Only able to modify the CSS, cannot adjust the HTML/JS So, I have two divs: <div id="content"></div> <div id="footer"></div> "#conten ...

The drop-down menu appears to be falling behind the 'content' div element

Whenever I try to hover over the dropdown menu, it frustratingly appears behind the content div that I painstakingly created. Despite all my efforts with z-index adjustments, the issue still persists. Below you'll find all the code for my website, but ...

The ng-route feature seems to be malfunctioning and I am unable to identify the error, as no information is being displayed in the console

Here is the code in my boot.php file where I have set up the links <ul class="nav nav-pills nav-stacked"> <li role="presentation"><a href="#/valley">Valley</a></li> <li role="presentation"><a href="#/beach"> ...