Achieving the ideal 3-column design

I'm currently struggling to achieve a proper 3 column layout with my HTML and CSS code. Here's what I have:

<div class="row">
    <div class="col">C1</div>
    <div class="col">C2</div>
    <div class="col">C3</div>
</div>

Here is the CSS:

.row {
   width: 964px;
}

.row:last-child {
   margin: 0;
}

.row .col {
  width: 33.33%
  float: left;
  margin-bottom: 20px;
  margin-right: 10px;
}

The columns are supposed to be equal in width at 33.33%, but there seems to be a gap on the right margin of the last column. Increasing the margin only pushes the column down to the next line.

Is there a way to make sure that all columns have the same width, without any margins on the first and last columns (meaning no left margin for the first column and no right margin for the third column)? Any suggestions would be appreciated!

Thanks in advance.

Answer №1

.row .col:last-child
{
    margin-right:0;
}

This code snippet will ensure that the right margin is removed for the last child element within a row column. It is important to remember to add this styling after the '.row.col' style declaration.

Answer №2

Replace px with % in the following CSS code: margin-right: 10px;

.block{
    width: 48%;
    margin-right: 5%;
}

.block:last-of-type{
    margin-right: 0%;
}

Answer №3

If you want to achieve a responsive layout, consider using the following CSS:

.row{
    display: table;
}
.col{
    display: table-cell;
}

For example, you can modify it like this:

.row{
   display: table;
   width: 100%;
}
.col{
    border: 1px solid #000;
    display: table-cell;
    width: 33%;
}

Check out the FIDDLE DEMO here!

Answer №4

When working with a fixed width row of 964px, you can easily calculate the column widths and add margins to achieve the desired layout.

It's worth noting that 964 doesn't evenly divide by three, so using 963px may help avoid rounding issues. Here's an example:

<div class="row">
    <div class="col">C1</div>
    <div class="col middle">C2</div>
    <div class="col">C3</div>
</div>

Style with CSS:

.row .col {
  width: 321px;
  float: left;
  margin-bottom: 20px;
}

.row .middle {
    margin-left: 10px;
    margin-right: 10px;
}

Answer №5

Thank you all for your helpful responses! I had to adjust the padding in my main website container to make everything align correctly. After changing the width to 970px, I utilized some CSS from Bootstrap to achieve the desired layout.

The final outcome looks like this:

.row {
   margin-right: -15px;
   margin-left: -15px;    
}

.col {
   float: left;
   width: 33.33333333%;
   position: relative;
   min-height: 1px;
   padding-right: 15px;
   padding-left: 15px;
}

This adjustment has resolved all alignment issues for me!

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

Issue encountered while attempting to delete dynamically added fields

I'm facing an issue where I am adding 3 fields dynamically (2 text fields and 1 remove button). The add button is functioning correctly, but the problem arises with the remove function. Currently, it only removes the remove button itself and not the o ...

Utilizing jQuery for AJAX requests triggered by mouse movement

I have a project where I am creating an image with gdimage, consisting of 40000 5x5 blocks that link to different user profiles. My goal is to implement a feature where hovering over one of these blocks triggers AJAX to fetch the corresponding user profile ...

Showing Div content from AngularJS response

Query -- I am currently using a Controller in AngularJs that performs an $http.get request and receives data as a response. The data includes an HTML DivID and corresponding classes. How can I extract this DivID from the response and transfer it to the vi ...

How to use jQuery to extract a particular text from an anchor tag

If I want to choose a specific anchor text and make changes to it, I can do so by targeting anchors with a certain href attribute. For example, on a page with multiple unordered lists, each containing different links: <ul> <li><a href="v ...

When I click the login button, a .ttf file is being downloaded to my computer

I have encountered an issue with my web application where custom font files in .ttf, .eot, and .otf formats are being downloaded to users' local machines when they try to log in as newly registered users. Despite attempting various solutions, I have b ...

Rowing and Columning with Bootstrap

Hey there! I'm currently working on creating some stylish boxes to showcase text and possibly an image background. However, I'm encountering an issue where the boxes are not aligning correctly on smaller screens. https://i.sstatic.net/JoYtj.jpg ...

Element is not perfectly aligned with the bottom; Slightly below the bottom edge

Why does an element positioned absolutely within a div that has relative positioning end up slightly lower than the bottom of the parent div? What can be done to fix this issue without just adjusting the bottom value on .hoverAction? function showFileUp ...

Unable to import global CSS in React; however, local components are working fine

My React application is having trouble loading global CSS styles. While local components are able to access their own styled-components, the global CSS styles are not being applied across all components. I have tried various import paths and different file ...

The form yields no response and fails to send any data

Ensuring that the form on this site successfully sends the name and phone number is crucial. However, upon clicking the send button, I encounter an empty modal window instead of a response indicating whether the data was sent or not. The contents of the fi ...

Tips for designing rainbow-themed elements (hyperlinks) using CSS to incorporate multiple colors

When customizing link styles in D3.js, it's common to adjust the color of the links. For instance, in the code snippet below, I've created thick links with a width of 10 and colored them grey. link = svg.append("g") . . . .style("stroke", "grey" ...

I need clarification on the following: content:''

I have a question regarding this particular code snippet .store { display: block; position: relative; } .store:before, .store:after { display: block; position:absolute; content:''; } .store:before { background: url(store-before.png); ...

What can be done to stop the Datepicker from exiting the Dialog box and causing it to malfunction?

While creating a form inside a dialog box, I encountered an issue with the date picker functionality. Whenever I try to select a date, it disappears and breaks, rendering the last days of the calendar inaccessible. You can view an example of this problem i ...

polygon with five or more sides

Can divs be created with shapes such as five or six corners? I am looking to create clickable zones on a map and any alternative methods would be greatly appreciated. ...

From Table to DIV: A Simple Conversion

I have encountered a major issue that has stumped me so far. Perhaps you can assist me. I am dealing with a table that appears as follows: __________________________________ | | time1 | time2 | time3 | +--------+-------+-------+-------+ | John | ...

Turn off the nicescroll scroll bar that appears as a default feature

Is there a way to disable the nicescroll scroll bar that appears in red by default on my html page? It's causing issues with zooming in and breaking the layout. ...

When I view the website on a tablet in landscape mode, the navigation bar vanishes

I just finished creating a responsive menu for a website. Here's how it works: when viewing the site on a regular browser with a width less than 768px, the navigation menu items are stacked and hidden. To reveal them, the user needs to click on a tog ...

What is the best way to center align and add an icon to the header in Ionic?

How can I center align an "ion-ios-arrow-up" icon in the header of my Ionic app, similar to the concept shown below? This is my HTML template: <ion-view cache-view="false" view-title="Historical HPP"> <ion-nav-bar class="nav-title-slide-ios7 ...

Switching the keyboard language on the client side of programming languages

I'm interested in altering the keyboard language when an input element changes. Is it possible to modify the keyboard language using client-side programming languages? And specifically, can JavaScript be used to change the keyboard language? ...

Converting a JavaScript IIFE library into a React Component

Hello, I am currently in the process of learning React JS and there are two JavaScript files that I am working on: Polyfill.js -> hosted on github CustomNavbar.js -> created by me Here is the structure of polyfill.js: export default (function(win ...

Using both Bing and Google geolocation services due to compatibility with Internet Explorer

Hey there! I'm currently working on implementing geolocation functionality into my project, and it's been smooth sailing except for one hiccup - Internet Explorer doesn't seem to play nice with Google Maps and prefers Bing instead. Has anyon ...