Mastering the art of gracefully transitioning to a fallback option when CSS grid

When it comes to creating a simple grid of squares (3x3 at most, filling in top to bottom left to right), I have encountered challenges with both flexbox and grid. The DOM structure required for each method seems incompatible or too complex to support both grids effectively.

Below is my flexbox code:

.wrapper {
display: flex;
width: 400px;
height: 400px;
}

.col {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 5px;
}

.tile {
  margin: 5px;
  width: 100%;
  flex: 1;
}

The corresponding HTML code creates the desired grid:

<div class="wrapper">
  <div class="col">
    <div class="tile" style="background: red"></div>
    <div class="tile" style="background: green"></div>
    <div class="tile" style="background: blue"></div>
  </div>
  <div class="col">
    <div class="tile" style="background: red"></div>
    <div class="tile" style="background: green"></div>
    <div class="tile" style="background: blue"></div>
  </div>
  <div class="col">
    <div class="tile" style="background: red"></div>
    <div class="tile" style="background: green"></div>
    <div class="tile" style="background: blue"></div>
  </div>
</div>

On the other hand, here is the grid CSS:

.grid {
  display: grid;
  width: 400px;
  height: 400px;
  grid-template-columns: repeat( 3, 1fr );
  grid-template-rows: repeat( 3, 1fr );
  grid-gap: 10px;
}

The respective HTML code renders the grid successfully:

<div class="grid">
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
</div>

My question is: How can I achieve the same grid layout without using div.col in flexbox, while maintaining a clean and organized grid CSS?

I attempted using flex-wrap: row to eliminate the need for div.col wrappers. Although this approach allowed the tiles to wrap properly and form a 3x3 grid, specifying a height became necessary, which defeats the purpose of having flexible dimensions for the tiles.

Answer №1

When I experimented with using flex-wrap: row to eliminate the necessity of div.col containers, it successfully wrapped the tiles in flexbox to form a neat 3x3 grid. However, I found it cumbersome as I had to specify a fixed height for the tiles, which is not my desired outcome. I want the tiles to adjust their dimensions automatically based on content.

Contrasting Grid and Flex Approaches:

In Grid layout, dimensions are defined at the container level, whereas in Flex layout they are set at the item level.

Grid Dimensions Example:

.grid {
  display: grid;
  width: 400px;
  height: 400px;
  grid-template-columns: repeat(3, 1fr);  
  grid-template-rows: repeat(3, 1fr);   
  grid-gap: 10px;
}

.grid {
  display: grid;
  width: 400px;
  height: 400px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-gap: 10px;
}
<div class="grid">
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
</div>

Dimensions in Flex Layout (as per my application):

.tile {
  flex: 1 0 30%;  
  height: 30%;    
  margin: 5px;
}

.grid {
  display: flex;
  flex-wrap: wrap;
  width: 400px;
  height: 400px;
}

.tile {
  flex: 1 0 30%;
  height: 30%;
  margin: 5px;
}
<div class="grid">
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
  <div class="tile" style="background: red"></div>
  <div class="tile" style="background: green"></div>
  <div class="tile" style="background: blue"></div>
</div>

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 on my website specifically occurring on Google Chrome and mobile devices

Hello, I seem to be having a CSS issue. Interestingly, my menu (burger menu) works perfectly fine on Firefox but is not functioning properly on Chrome and mobile devices. When I click the button, the menu doesn't open. Has anyone encountered a simil ...

What are the steps to create a login form that is responsive by utilizing the Bootstrap grid system

.custom-border { border: 2px groove #000 !important; padding: 0px 5.4em 1.4em 6.4em !important; margin: 0 0 1.5em 0 !important; box-shadow: 0px 0px 0px 0px #000; margin-top: 30px !important; } .custom-legend { font-size: 1.2em !important; te ...

Sorry, the provided text is already unique as it is an error message

I'm currently using the react-highlight-words package to highlight text inputted into a textbox After checking out the react-highlight-words documentation, I noticed that they are using searchWords as an array. https://www.npmjs.com/package/react-high ...

Step-by-step guide to swapping an element with a textarea element using javascript

My current project involves creating a user profile that includes a text box where users can describe themselves. I've already implemented a separate page for editing the profile, but now I want to add a feature where users can hover over their descri ...

Show or hide a specific element based on whether a certain radio button is selected or deselected

After successfully displaying an element on radio button selection, I encountered the issue of the selected element remaining visible when another radio button in the group is chosen. What aspect am I overlooking? Regarding hiding elements with vanilla Ja ...

3 Offset Columns in Bootstrap 1.5

I'm currently brainstorming a Bootstrap 3 grid design that features a top row with 4 columns and a bottom row with 3 columns centered against the first row like this... https://i.sstatic.net/e1V62.jpg Although I've considered using offsets, uti ...

Changing colors using JavaScript: A step-by-step guide

Hey there! I'm looking to change the color code in this script from $("#Tcounter").css("color","black") which uses the color word "black", to "#317D29". Can someone help me figure out how to do this? <script type="text/javascript"> $(document). ...

Enhance your Divi theme with Elegant Themes: Explore mobile-friendly background image zoom effects

I have a regular section module with an image as the background. The design looks great on desktop but appears zoomed in and blurry on mobile devices. I'm struggling to understand why the mobile view is not adjusting properly, and I am unsure if there ...

Sending JSON-encoded data using HTML5 Server-Sent Events (SSE) is a

I have a script that triggers an SSE event to fetch json encoded data from online.php. After some research, I discovered methods for sending JSON data via SSE by adding line breaks. My question is how to send JSON through SSE when the JSON array is genera ...

Switch the input direction from left to right

Check out my demonstration that is currently functional: JSFIDDLE $('#btn-search').on('click', function(e) { e.preventDefault(); $('#search').animate({width: 'toggle'}).focus(); }); Is there a way to modify ...

Adding an image to a two-column layout with text

Seeking assistance with my website creation using HTML, CSS, and jQuery. Currently, I have a text layout formatted in two columns with an image placed before the text as shown below: <div> <div style="float:right"> <img src="ima ...

What is the best way to design a polygon-shaped responsive div?

I've been attempting to design a unique layout with a pentagon-shaped div, however, I'm encountering some challenges. Despite using SVG for the shape, it does not display correctly in Firefox. Additionally, I need the div to be responsive and sca ...

The backend is serving HTML content, but the frontend is not initiating any redirects

After hitting an API endpoint and examining the network call responses, I identified that the HTML response returned with a status code of 302. Despite this, I am perplexed as I do not witness the expected redirect on the user interface. The intended redir ...

Removing the restriction on maximum width to 100% and allowing for automatic height adjustment

Within my project, I have implemented a CSS technique where all images within a container are set with the attributes max-width: 100%; and height: auto;. This is done to ensure that images scale down appropriately when the viewport and container size decre ...

Updating the navigation with a dynamic CSS class using jQuery

Hey there, I'm on the lookout for a simple and discreet method to dynamically add an "active" class to a navigation menu based on the current directory. Unfortunately, I am unable to modify the body tag with a custom class, ruling out a pure CSS solut ...

Text in various styles is layered on top of an image, ensuring that text in one style does not overlap text in another style

Struggling with placing text over an image? Trying to achieve different text styles but ending up with overlapping lines? Here's a snippet of HTML that works with only one text style: <li style="" class="portfolio-content-CV"> <div class ...

When utilizing ThreeJS video texture, it is necessary to ensure that CORS headers are included, regardless of whether the <video> tag typically requires it or

I am facing an issue with my localhost web server where a remote video from Firestore Storage is being played on a page using a <video> tag. The video plays correctly on the web page. However, I am now trying to use the same video as a texture in a T ...

What characterizes a dynamic webpage?

Can someone help me figure out how to determine the number of dynamic pages on a website? I'm not sure if the presence of a form means the page is dynamic. Any insights would be greatly appreciated. Thank you in advance for your assistance. ...

Mastering the Art of Line Breaks in WordPress

I am looking for a way to break a WordPress site title over two lines at a specific point in the text. Since the WordPress content filter 'wpautop' disables the ` ` tag (also known as ` ` or ` `), I'm wondering what the best alternative ...

Using HTML to design interactive buttons that can send API requests and display their current status

Seeking assistance with creating buttons to control a remote light's ON and OFF states while reflecting their status as well. The specific http API calls for the light are necessary to toggle its state. Turn On = http://192.168.102.22:3480/data_requ ...