Grid system that is responsive, while also maintaining a fixed column width

My problem seems simple, but I can't find an easy solution.

What I need is to create a grid with fixed widths and a fixed distance between each column.

Each column should be 400px wide, and as the browser window is resized, the grid should adjust by reducing the number of columns while maintaining their width and spacing.

The content within the grid should flow seamlessly over all the columns.

I've explored open-source grid systems like Bootstrap and Skeleton, but they all use percentage widths which change with resizing, which is not what I need.

Is there a simpler way to achieve this?

Edit/Clarification:

Here's how it currently looks without columns: http://jsfiddle.net/xjrt8qrm/16/show/

<div>See the fiddle</div>

I want to have x columns, where x is the maximum number of 400px columns based on the user's resolution. The content should flow like a newspaper from top to bottom in a single row of columns.

This is how I imagine it looking on a PC: https://i.sstatic.net/ycF2m.png. (Please ignore the text/comments in the image).

Answer №1

Understanding the concept is quite straightforward. The container plays the vital role of holding the contents together. By applying a float left property, the contents will align from left to right. As the container reaches its width limit, the contents will gracefully drop to a new row, one by one. To prevent the float from affecting other elements nearby, the clear div can be utilized. Of course, adjusting padding, margins, and other styles will be necessary according to your design preferences.

For a newspaper-style vertical layout, a helpful solution can be found in this resource.

If the requirement is to hide columns that exceed the available space, techniques like media queries can be employed along with overflow:none property.

Below is a simple code snippet that demonstrates the implementation:

HTML:

<div class="container">
      <div class="fourhundred">
          Div 1
      </div>
      <div class="fourhundred">
          Div 2
      </div>
      <div class="fourhundred">
          Div 3
      </div>
      <div class="clear"></div>
    </div>

CSS:

.fourhundred { 
  width: 400px;
  margin: 10px;
  float: left;
}
.clear { clear: left; }
.container { width: 100%; }

Answer №2

The reason behind the creation of flexbox is to provide more flexibility in laying out elements. To implement flexbox in your design, make sure to include the following code in your container:

.container {
  display: flex;
  justify-content: space-between;
  align-content: space-between;
  width: 100%;
}

You can also refer to this example on Fiddle

Answer №3

To divide the whole width into 3 equal parts, you can simply use the CSS property width: calc(100% / 3);. Feel free to replace the value 3 with any number you prefer.

For a demonstration, check out the Fiddle Demo

<div id = "main">
    <div id ="sub">One
    </div>
    <div id ="sub">Two
    </div>
    <div id ="sub">Three
    </div>
</div>

Here's the CSS part:

#main{
    border: 2px solid black;
    height: 100px;
    width: 100%;
    position: relative;
    display: flex;
}
#sub{
    border: 1px solid red;
    width: calc(100% / 3);
    height: calc(100% - 40px);
    padding: 10px;
    margin: 5px;   
    display: inline-block;
}

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

The functionality of `config.assets.debug = true` fails to

I've encountered an issue with the configuration on development where config.assets.debug = true doesn't seem to work correctly. Instead of having multiple separate JavaScript and CSS file inclusions, I'm getting a consolidated one: <li ...

Tips for implementing a CSS class on a select dropdown in multiple web pages

Having multiple dropdowns (select boxes) on my website has led me to desire different alignments for them. For instance, I would like one dropdown to be centered on a specific page while another should appear on the right side of a separate page. Since my ...

Using CSS to align the position of a div with the last word position of an h4 element

Trying to figure out how to position a div at the end of an h4 text element has been challenging. When the h4 text is on a single line, it's easy to place the div correctly. However, things get complicated when the text spans multiple lines. In that c ...

Tips for centering a button within the body of a webpage

Despite my efforts, the alignment is not working as expected. The CSS I am using specifies that every <div> button inside the body should be centered both vertically and horizontally. However, it seems like something is preventing this from happeni ...

Adjusting Font Size for Buttons on Mobile Devices in HTML

I am currently working on a website that I want to ensure is mobile-friendly. When testing it on my phone and in Chrome's device mode, I noticed that the majority of the text on the screen was displayed at an easily readable size without needing to zo ...

Ways to ensure even spacing in a responsive header design

On my homepage, I have a large header image that is responsive in the mobile version. However, I am having trouble managing the spacing between the button and text below it. The spacing varies on different mobile screens and sometimes appears larger than i ...

Is there a way to display a bootstrap modal when the page is refreshed?

Is there a way to automatically display a Bootstrap modal box without the need for clicking anywhere? I want the modal to appear every time the page is refreshed, rather than requiring a click event. Can anyone provide guidance on achieving this? < ...

Utilizing Material UI with Appbar logo aligned to the left and Tabs featured at the center

I am currently working on creating a react material ui AppBar. The design includes a logo and tabs, with the tabs supposed to be centered in the AppBar and the logo positioned on the left side. However, I have been facing difficulty in making the logo al ...

Having trouble sending data from the controller to the view in Laravel 8

I am struggling to display data retrieved from the database in my view through the controller. Despite trying various solutions found on similar questions, none of them seem to be effective. My main goal is to showcase a list of employees on my admin page. ...

Issue with applying CSS properties of 'top' and 'left' to a dynamically created div using

After an extensive search on both the site and the internet, I have not had any luck so far. I am dealing with a series of dynamically-generated divs using JQuery, cloned from the original HTML source. While I can manipulate the positioning properties of ...

Combining mouse interactions with animated bar transitions

I want to create a graph with dynamic bar height transitions whenever it is drawn or redrawn. Once the bars are displayed, I would like mouse events (mouseenter, mouseleave, and mousemove) to trigger a tooltip showing information about the specific bar bei ...

Tips for refreshing the default style of Material UI select

I'm having trouble customizing the default background color of the first menuItem in the select component. The class I need is not visible when inspecting the element, as the background color disappears upon inspection. Steps to reproduce: 1. Click ...

React JS makes it simple to create user-friendly cards that are optimized

I have a collection of objects that include a name and description. The name is short and consists of only a few characters, while the description can vary in length. I am looking to showcase this data within Cards in my React project, and have tried using ...

Effortlessly apply CSS styles to multiple cached jQuery variables without redundancy

Hey there, I've got this piece of code written in Javascript for a classic ASP page. Since there are no CSS classes defined, everything is handled within the Javascript itself. var $a= $('#a'), $v= $('#v'), $s= $('#s'), ...

Extracting data from a tiered website within a specialized niche

I am currently attempting to scrape data from the webpage: "https://esco.ec.europa.eu/en/classification/skill_main". Specifically, I want to click on all the plus buttons under S-skills until no more plus buttons can be found, and then save the page source ...

Scroll the div that is dynamically filled without affecting the scrolling of the main page

My current struggle involves using iScroll in my web project. The goal is to populate a list with articles and slide in a div over the list to display the selected article. While the basic functionality is in place, I face an issue where scrolling through ...

Aligning a div with absolute positioning vertically

There are two elements positioned side by side: an input field and a div. The div is absolutely positioned inside a relative element and placed to the right of the input. The input field has a fixed height, while the height of the div depends on its conte ...

How can I enhance a JavaScript Calendar with more features?

I recently acquired a JavaScript and jQuery calendar from CodeCanyon, which can be found here. I am now faced with the task of integrating this calendar into my workplace website. The current calendar on our ASPX-based site is limited to read-only functio ...

What is preventing Facebook from merging its CSS/JS files together?

I wonder about the reasoning behind Facebook developers' decision not to consolidate their scripts and stylesheets into single files, opting instead to load them on demand through their CDN. Considering the complexity of Facebook as an application, I ...

Why does the Element.style.left property keep rejecting my input value?

I have encountered a problem with the positioning of elements, specifically with the 'left' property. I've designed a rectangular block using CSS and rotated it by 0.17 radians in JavaScript. Now, my aim is to make the block move diagonally ...