Adaptable triple-column design

After completing the layout of my website, everything seems to be functioning well. However, I would like to ensure that I haven't overcomplicated it or used poor techniques.

The structure involves a main DIV with a nested table DIV, inside of which there are three table-cells as sub-divs.

<div id="container-main">
    <div id="columns-3">
        <div class="main-col-left-s"></div>
        <div class="main-col-center-s"></div>
        <div class="main-col-right-s"></div>
     </div>
</div>

Each column contains different elements, resembling a layout similar to Pinterest's website.

When the screen size is reduced, I want only two columns to display initially, and eventually just one. While the transition from 3 to 1 column happens automatically, I faced challenges when going from 3 to 2 columns. To address this, I utilized the CSS property display: none on the middle column when the screen becomes too small, ensuring it is not visible. Additionally, I assumed that data will be transferred to the visible column if the current one is hidden.

Is this solution considered effective, or are there better alternatives?

I aim to avoid situations where one row displays 2 columns while the next only shows 1, as illustrated below:

ǀdataǀdataǀ
ǀdataǀ    ǀ
-----------
ǀdataǀdataǀ
ǀdataǀ    ǀ

Instead, the layout should always adhere to the following pattern:

ǀdataǀdataǀ
ǀdataǀdataǀ
-----------
ǀdataǀdataǀ
ǀdataǀdataǀ

Answer №1

Thank you for elaborating on your query. Essentially, a solution using only CSS is both feasible and advisable. This can be easily accomplished with CSS3 and the innovative Flexible Box Model, although uniform browser support is lacking across major browsers. Therefore, to ensure compatibility with all major browsers, I recommend adhering to the Traditional Box Model, which forms the basis of my response.

The approach is quite simple:

  • One main block div acting as the container element
  • Multiple inner divs (within the container div) displayed as inline-block elements representing "table cells"

The HTML structure would resemble the following:

<div class="container">
    <div class="box"></div>
    <div class="box"></div>
</div>

This structure would then be styled as follows...

div.container{
    width:100%;
    display:block;
    font-size:0;
}
div.box{
    width:30%;
    font-size:40px;
    margin:20px 0 0 2.5%;
    display:inline-block;
}

These styles will present 3 boxes aligned horizontally in a responsive manner on one row. The font-size:0 declaration serves as a workaround for the inline-block font spacing issue; further insights can be found by conducting a quick search online.

To adjust the display to show only 2 boxes per row when the screen width is restricted, media queries can be employed, as demonstrated below...

@media all and (max-width:950px){
    div.box{
        width:47%;
       margin:20px 0 0 2%;
    }
}

The max-width constraint can naturally be modified to suit specific requirements. Lastly, a visual representation has been constructed in the fiddler snippet provided below...

div.container{
    width:100%;
    height:100%;
    padding-bottom:40px;
    display:block;
    background-color:#eee;
    font-family:Arial;
    font-size:0;
} 
/* Additional CSS code continued... */
<div class="container">
    <div class="box">1</div>
    <div class="box">2</div>
    /* More box elements displayed here */
</div>

Note the intentional height adjustment made to box 8 in the example above.

Answer №2

To make your website responsive, utilize media queries. Implement code similar to the following in your css stylesheet:

.main-col-left-s, .main-col-center-s, .main-col-right-s {
  width: 100%;
}

@media (min-width: 600px) {
  .main-col-left-s, .main-col-center-s, .main-col-right-s {
    width: 50%;
  }
}

@media (min-width: 960px) {
  .main-col-left-s, .main-col-center-s, .main-col-right-s {
    width: 33.33%;
  }
}

Answer №3

If you utilize the float: left attribute in CSS for your table cells, it will achieve the desired effect.

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

Use an external javascript file in AngularJS if permitted

To ensure that my HTML page only loads an external JavaScript file when the variable $scope.jsallowed is set to true, I attempted the following code implementation within my AngularJS based page: <script src="assets/js/slider.min.js" data-ng-if="jsallo ...

How can I position two bootstrap tables side by side based on their columns

Currently, I am utilizing bootstrap and Laravel form control to generate the table structures below. The one issue I am encountering is figuring out how to align the delete and edit buttons from two separate tables. Here is a snapshot of the current layout ...

Guide to align elements (cards) side by side in a row within a container element (div)

I have a question that bears resemblance to another one found on this site. I decided to create my own version of the Material UI Card example from https://material-ui.com/demos/cards/. You can view and edit my version on the sandbox editor by clicking he ...

When HTML/JS code is utilized, the submit button or image should function to open the same page as

In addition to the left and right mouse buttons, you also have a middle mouse button. If you click on an image or hyperlink with this middle mouse button while browsing any website, it will open a new window in the background. Now, I want to achieve a sim ...

Can you style a radio button input using only CSS without a label?

<input id="customize1" class="customize1" name="test" type="radio" checked="checked"> <input id="customize2" class="customize2" name="test2" type="radio"> Can these two elements be styled without directly targeting their labels, using only CSS ...

Use CSS to manipulate the dimensions of an overlay

Looking for a way to ensure that the height of your overlay matches the height of your carousel, even on smaller screen sizes? The HTML code below demonstrates how to combine simple HTML with a Bootstrap carousel featuring three images, along with an overl ...

Add a unique shape to the CSS clip property

Recently, I've been delving into the clip property of CSS and its ability to apply a rectangle or square object. Despite my research efforts, I haven't come across a clear answer to my question. Can you actually use a customized shape with the c ...

What could be the reason behind an Ajax Auto-Complete feature suddenly malfunctioning when moving to a new page within the same website?

Working on a website featuring an auto-complete search box powered by Ajax, Javascript, and PHP. As the user types into the search box, an ajax request triggers a php file to query a database and return possible results. Everything works smoothly until the ...

Developing a customized WordPress walker to specifically target the first and last links

I've implemented a custom walker in my Wordpress site: class Custom_Walker_Nav_Menu extends Walker_Nav_Menu { function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { if ( $depth ) $indent = str_repeat("& ...

Organizing textual maps within a 2D array for rendering on an HTML5 Canvas

In my spare time, I am working on creating an HTML5 RPG game. The map is implemented using a <canvas> element with specific dimensions (512px width, 352px height | 16 tiles across, 11 tiles from top to bottom), and I'm wondering if there's ...

Use jQuery to change the background color when clicked

Below is the HTML code with UL and LI elements: <UL> <LI><span id='select1'>Text</span></LI> <LI><span id='select2'>Text</span></LI> <LI><span id='select3'>Tex ...

How can I stick the footer to the bottom of the page?

I've tried numerous codes in an attempt to make the footer stay at the bottom of the page, however, none of them seemed to work. Here's my footer tag: <footer class="container py-5 navbar-fixed-bottom"> The following div is: <di ...

Print Vue page with the same styling as the original

How can I add a print button to my application that prints the page with the original CSS styles? I am currently using window.print() function and have a separate file called print.scss with specific print styles. @media print { header {display:none; ...

Using *ngFor with a condition in Angular 4 to assign different values to ngModel

I am new to Angular 4 and encountering an issue with using *ngFor in conjunction with HTML drop-down select and option elements. My categories array-object looks like this - categories = [ { id:1, title: 'c/c++'}, { id:2, title: 'JavaScri ...

"Trouble with Express.js: CSS isn't loading, yet HTML displays

Click here to view folders and files server.js is the code I have created so far //importing packages const express = require('express'); const admin = require('firebase-admin'); const bcrypt = require('bcrypt'); const path = ...

The outline none property on the Material UI IconButton is malfunctioning

https://i.sstatic.net/S5zWD.png Attempting to style with CSS as shown in the following example: <CustomColorIconButton> <DeleteForeverIcon /> </CustomColorIconButton> const CustomColorIconButton = withStyles({ root: { c ...

Discrepancy in Code Outputs

Last night, I spent some time testing out code for basic functions. To preview my work, I used two platforms - Dash and JSFiddle. Everything seemed to be running smoothly on both sites. However, when I uploaded the code to my live website, none of the butt ...

Why won't divs align vertically in the center?

I'm struggling to create a navigation bar layout where the icons are centered both horizontally and vertically within their cells. http://jsfiddle.net/digorydoo/j2v5m7gr/ I can't seem to pinpoint what's causing the issue with my current la ...

Enhance your inline content by adding adjacent inline blocks

My objective is to include a "Read More" link alongside a text block, which should appear next to the text without forming a new line or block. The challenge lies in the fact that the content of the text block is created using TinyMCE, resulting in various ...

Looking to manipulate li elements using jQuery

Within my parent list, I have several nested lists with child lists inside li elements that contain specific classes. The titles for these nested lists are located in adjacent li elements within the parent list. Here is how the markup looks: <div> ...