Developing alternatives for CSS Grid

Currently, I am faced with an intriguing challenge involving a layout that includes a container containing 9 div elements. Each of these divs is assigned one of three classes: column-1, column-2, or column-3.

<div class="grid cols-3">

   <div class="col column-1">
      column-1
   </div>
   <div class="col column-2">
      column-2
   </div>
   <div class="col column-3">
      column-3
   </div>
   <div class="col column-1">
    column-1
  </div>
  <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
  <div class="col column-1">
    column-1
  </div>
    <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
</div>

The task at hand involves splitting these 9 divs into 3 columns, each consisting of 3 divs with the same class.

While CSS Grid can accomplish this easily, I also need to account for browsers lacking GRID support, which is proving to be a challenge.

You can view the sample markup I'm dealing with here.

A project constraint requires that the markup remains untouched, necessitating a solution using CSS (or potentially javascript if necessary).

I briefly considered utilizing flexbox by setting flex-direction: column and assigning an order to each class like so: .column-1 { order: 1; }, .column-2 { order: 2; }, .column-3 { order: 3; }. However, although this reordered the divs correctly, they all ended up in a single column instead of 3, and I couldn't find a way to split them into separate columns using flex alone. :(

If you have any suggestions or ideas, I would greatly appreciate your input!

Answer №1

I have created this grid layout using the flexbox feature.

.grid {
  display: flex;
  flex-wrap: wrap;
}

.col {
  width: 31%;
  margin: 1%;
}

.column-1 {
  background-color: #ccc; 
}

.column-2 {
  background-color: yellow;
}

.column-3 {
  background-color: blue;
}
<div class="grid cols-3">
  <div class="col column-1">
    column-1
  </div>
  <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
  <div class="col column-1">
    column-1
  </div>
  <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
  <div class="col column-1">
    column-1
  </div>
    <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
  
</div>

Answer №2

.grid {
  width: 100%;
  margin-bottom: 10px;
}

.col {
  height: 100px;
  margin: 1%;
}

.column-1 {
  background-color: #ccc; 
}

.column-2 {
  background-color: yellow;
}

.column-3 {
  background-color: blue;
}


.column-1, .column-2, .column-3 {
  width: 31%;
  float: left;
}
@media only screen and (max-width: 600px) {
  .column-1, .column-2, .column-3 {
    width: 100%;
    float: left;
  }
}
<div class="grid cols-3">

  <div class="col column-1">
    column-1
  </div>
  <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
  <div class="col column-1">
    column-1
  </div>
  <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
  <div class="col column-1">
    column-1
  </div>
    <div class="col column-2">
    column-2
  </div>
  <div class="col column-3">
    column-3
  </div>
  
</div>

If you prefer not to use grid css and want different designs for mobile views, simply set widths for column1, column2, and column3, along with using media queries as described above.

Answer №3

Learn how to use @supports in CSS with this helpful CodePen example

The @supports CSS at-rule allows you to define styles that rely on a browser's support for specific CSS features, known as feature queries. This rule can be used either independently or within other conditional group at-rules.

@supports (display: grid) {
  /* Add your css grid style here */
}

 /* Define your css flex style or provide fallback css */

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

fewer security personnel leads to a higher success rate

I've tried searching for it online, but unfortunately, I couldn't find any helpful results. What I'm attempting to achieve is a mixin that can lighten or darken a color based on a given percentage. If the percentage is less than 0, then I w ...

Modify the background color of the entire page when the main div is clicked

I am attempting to alter the background color of my entire page when the div with id main is clicked. You can view the code here: $(document).ready(function() { var color = 1; $('#main').click(function(){ if (colo ...

Strangely shaped border appears when interacting with editable div block

I am attempting to build a textarea that has the capability to display multiple colors. To achieve this, I created a div and used the following JavaScript code: element.unselectable = 'off'; element.contentEditable = true; Now, the div can be e ...

Unable to submit form within a while loop in PHP table

After making some changes to my form, I noticed that only the hidden input field is being submitted when I try to submit the form. It's strange because it was working perfectly fine before. while ($row = mysqli_fetch_array($gettimesheets)) { $dat ...

The correlation between dynamic pricing and surge pricing in relation to PX

I'm exploring the translation of measurements from dp and sp to pixels within Google's new material design for a website I'm working on. Usually, my web designs have used pixel-based measurements for both grid and font sizing over the four y ...

Tips for displaying live data in the rows of Element UI's Table

Recently, I've been working with a table that looks like this. <v-table :data="data"> <v-table-column prop="data.attribute" label="Attribute"> </v-table-column> </v-table> Instead of displaying data.attribute in the co ...

jquery add to table id, not to a table within

Having trouble adding a table row to a specific table ID without it appending to other tables with different IDs nested inside. I've searched for a solution but can't seem to find one. Can someone help me figure out what I'm doing wrong? Her ...

Tips for transforming HTML to a dynatable or datatable

I have a PHP page that returns HTML table data. I’m looking to convert this table into either DataTable or Dynatable. I’ve attempted to include the necessary files (JavaScript and CSS) in the page where the PHP script generates the table. In my JavaScr ...

Unable to keep button contained within form's card design

Currently enhancing my Bootstrap skills and seeking assistance with a card and form alignment issue. Struggling to properly place a form within a card. Any suggestions or insights on resolving this? <div class="row row-content col-sm-8"> &l ...

Determine the number of lines in a textarea, taking into account both manual

I have a textarea that I need to validate for input exceeding 6 rows. My JavaScript code works perfectly by checking for scrollbar presence, indicating more than 6 lines of input. However, how can I achieve the same validation using PHP in case JavaScript ...

Searching for an index of a value fails after functions have been anonymized

I recently created a basic javascript animation that involves swapping images on a carousel and fading them in and out. My goal is to generalize the functions so that I can repurpose them for other projects. While most of the animation is functioning corr ...

Unusual shift in the modal's behavior occurs when the parent component's style.transform is applied

I have encountered an unusual issue with a modal's appearance and functionality. The modal is designed to enlarge images sent in a chat, with the chat upload preview div serving as the parent element and the modal as the child element. This strange be ...

Customize the appearance of a React component depending on its unique identifier

After creating a simple TODO app with drag and drop functionality, I am now looking to apply a line-through CSS style to any task added or dragged into the second column of my app. What is the best way to target these tasks using their unique ID: <div c ...

Is it possible to reset the value of a current request attribute?

I designed a homepage that consists of a header, a sidebar, and a div tag. When a user clicks on the "register" button, Registration.jsp is loaded into the div tag. After successfully submitting the registration form, I want to redirect it back to the sa ...

JavaScript function to toggle the visibility of navigation with a click of a button

I'm attempting to create a functionality in my code where the Open navigation button hides when the side navigation opens upon clicking. The desired behavior is for the openNav button to be hidden when it's clicked and then shown again when close ...

Is your browser tab failing to display the complete URL?

Encountering a strange issue while working on my current website project: When you click on "about," it does take you to the correct page, but upon refreshing the page, it redirects back to the home page. The same scenario is happening with other pages as ...

Blur effect on backdrop-filter causing shadowy inset borders (specific to Chrome and Windows)

Note: The issue mentioned below has been resolved in the latest version of Chrome (119.0.6045.200). While transitioning on backdrop-filter, I have observed a dark inset shadow that is only visible during the transition. This behavior seems to occur only ...

Transmit a communication from a customer to the server

I have created a NodeJs server that is listening on port 1234. I am looking to send requests from my HTML client to this server and receive responses. I attempted to use XMLHttpRequest: var http = new XMLHttpRequest(); var url = "127.0.0. ...

Troubleshooting problems with text areas in Android when using JQueryMobile and PhoneGap

When viewing my textarea control, I noticed that it breaks away from the .css theme and displays two boxes - one themed and one not. This issue only occurs on my HTC Evo device, as the code works fine on the emulator. You can see a screenshot of this behav ...

Responsive Image Resizing with Bootstrap for Smaller Screens

I'm struggling to resize a responsive image in Bootstrap using the carousel example on their official website. <div class="container"> <div class="other-div"> .... </div> <div id="carouselExampleCon ...