creating a responsive grid layout in react for mobile devices

My goal is to ensure that the videos displayed in a grid appear horizontally one after the other on mobile devices, while maintaining a vertical alignment on desktop view.

I have experimented with setting values for rows and columns as "auto".


    return (
          <div>
            <h1 className="text">trending in youtube</h1>

            <div className="grid-container">
              <div className="one">
                <YouTube
                  videoId="y6fThXQPT6I"
                  opts={opts}
                  onReady={this._onReady}
                />
              </div>
              <div className="two">
                <YouTube
                  videoId="bo_efYhYU2A"
                  opts={opts}
                  onReady={this._onReady}
                />
              </div>
              <div className="three">
                <YouTube
                  videoId="3AtDnEC4zak"
                  opts={opts}
                  onReady={this._onReady}
                />
              </div>
            </div>
          </div>
        );
      }

CSS File:

      
    .grid-container {
      display: grid;
      grid-template-columns: repeat(auto, 1fr);
      grid-gap: 5px;
      grid-auto-rows: minmax(100px, auto);
    }

    .text {
      margin-top: 50px;
      margin-left: 40%;
    }
    
    .one, .two, .three {
      grid-column: auto;
      grid-row: 1;
    }

Answer №1

Since you've tagged your post with bootstrap-4, I assume you're using it.

Bootstrap provides everything you need, especially the grid system. Check it out here: https://getbootstrap.com/docs/4.3/layout/grid/

In your scenario, you can implement it like this:

return (
  <div className="container">
    <h1 className="text">trending in youtube</h1>

    <div className="row justify-content-center">
      <div className="col-xs-12 col-md-4">
        <YouTube
          videoId="y6fThXQPT6I"
          opts={opts}
          onReady={this._onReady}
        />
      </div>
      <div className="col-xs-12 col-md-4">
        <YouTube
          videoId="bo_efYhYU2A"
          opts={opts}
          onReady={this._onReady}
        />
      </div>
      <div className="col-xs-12 col-md-4">
        <YouTube
          videoId="3AtDnEC4zak"
          opts={opts}
          onReady={this._onReady}
        />
      </div>
    </div>
  </div>
);

No additional CSS required! 😉

Answer №2

To implement the grid-auto-flow property in your CSS, follow these steps:

   .grid-container {
      display: grid;
      grid-gap: 5px;
      grid-auto-flow: row; /* items will appear vertically */
      justify-items: center;
    }
    @media (max-width: 767px) { 
      .grid-container { 
        grid-auto-flow: column; /* items will appear horizontally on mobile devices */
      }
    }

For a practical demonstration of the grid-auto-flow property, consider this scenario: When viewed as a snippet, the items will be arranged horizontally. However, when the snippet is displayed in full screen, the items will be aligned vertically.

   .grid-container {
      display: grid;
      grid-gap: 5px;
      grid-auto-flow: row;
      justify-items: center;
    }
    @media (max-width: 767px) { 
      .grid-container { 
        grid-auto-flow: column;
      }
    }
    .item {
      width: 250px;
      height: 100px;
    }

    .one {
      background-color: lightblue;
    }

    .two {
      background-color: lightpink;
    }

    .three {
      background-color: lightcoral;
    }
  <div class="grid-container">
      <div class="item one"></div>
      <div class="item two"></div>
      <div class="item three"></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

What are some ways to decrease the dimensions of my dateTimePicker?

I'm looking to decrease the dimensions of my datetime picker box. Here's an image of my current dateTimePicker: https://i.stack.imgur.com/MeJlq.png Below is the component I'm using: import React, { useState } from 'react'; import ...

Caching files with service worker in React app

Currently working on my very first React app created with create-react-app. While the production build already includes a service-worker.js, I need to manually add a couple of JavaScript worker files that are not being cached by the service worker as of no ...

hide input type with minimal display

Can someone help me find the less equivalent to this css code snippet? I'm not familiar with less and this code is part of a larger global css file that includes generated code from all less files. input[type="checkbox"] { display: none; } Any a ...

What is the best way to pass value to a URL in a React JS application?

Is there a way to properly use history.push in React to push text to the URL route manually? I am trying to achieve this in my onChange method for an input field: function Header(props) { return ( <div> <input type="radio" onChan ...

Getting rid of a blue border from a button upon clicking

Here is the problematic section of code :root { --color-foreground-text-main: rgb(255, 72, 142); --color-foreground-text-sub: rgb(0, 114, 153); } html, body { height: 100%; } img { max-width: 100%; } #cover { background: #222 url('Reso ...

Modifying jQuery CSS causes certain CSS rules from a file to be deleted

When using jQuery to change the css of an element, I've noticed that it can remove some previously specified css rules for that element. For example, .className, .className2, etc... { background-color: black; color : silver; } .className: ...

Social Engine is incapable of analyzing the HTML code

Currently working on a website using Social Engine 4.8.9 version that is still in development mode. Upon inspecting the html code with the chrome inspector, I find it incredibly complex to decipher. For instance, when I click on the login page link http:// ...

The sidebar vanishes when you move your cursor over the text contained within

My issue is that the side menu keeps closing when I hover over the text inside it. The "About" text functions correctly, but the other three don't seem to work as intended. Despite trying various solutions, I am unable to identify the root cause of th ...

`When a link is clicked, show one div overlapping another.`

Is it possible to have one div display over another when a link is clicked? Here is the HTML and CSS code: <div id="desc" class="desc">some text</div> <div id="awards" class="awards">some other text</div> <a href="#">click< ...

How can one go about incorporating the feature "updating list upon clicking a label" on a webpage?

On my website, I currently display a comprehensive list of publications. However, I am looking to organize these publications by various research topics using labels. Ideally, when clicking on a specific label, only the papers related to that topic will be ...

The integration of redux-thunk is causing a conflict with the functionality of remote-redux-devtools

After integrating redux-thunk into my React Native project, I noticed that my Redux remote devtools are not displaying the state properly. The state appears as undefined in the remote devtools window. Previously (when devtools were working): const store = ...

Encountering unhandled promise rejection error with email field in NextJS when using React Hook Form

I am encountering a bizarre issue where, on my form with two fields (name and email), whenever the email field is focused and onSubmit is called, I receive the following error message: content.js:1 Uncaught (in promise) Error: Something went wrong. Please ...

Tips for utilizing Axios for a secondary API request after the componentDidMount cycle, triggered by user engagement with your application

Utilizing Axios, I am making an API call (get request) within the life cycle method componentDidMount. The process is running smoothly as I am receiving the desired result and storing the data using setState. componentDidMount(){ axios.get("https://my ...

The modal is functioning properly on Firefox and Internet Explorer, but it is experiencing issues

Having an issue with my modal not functioning properly in Chrome. When I click on it, only the background fades and both the before and after content load in the Chrome Dev tools simultaneously with no visible content in between. Here is a link to the Cod ...

Generating a new division element with a unique identifier and subsequently making adjustments to it

Just starting out with HTML and JS here, so please excuse my lack of experience and not-so-great English. I have an ID on an HTML element that I want to add content to using a function. Additionally, I need to create another element (inside the first one) ...

When utilizing a Slick carousel with three slides and setting autoPlay to true, the slider-nav behaves as if there are five slides

I am currently using the Slick carousel plugin and I want to feature a display of 3 photos. The issue is that when I set slidesToShow=2, everything works perfectly fine, but when I try to set slidesToShow=3, which equals the total number of slides, the bot ...

Choosing a group of kids who all share the same element tag

I currently have a situation where I need to display only the first two paragraphs in a div when the page loads, with the remaining paragraphs hidden. I am struggling to achieve this and am seeking guidance on how to do so. In the jsfiddle example provided ...

What is the reason for all the buttons activating the same component instead of triggering separate components for each button?

I am facing an issue with my parent component that has 3 buttons and 3 children components. Each button is supposed to open a specific child component, but currently all the buttons are opening the same child component when clicked. The children components ...

What's the reason for these bootstrap buttons being in such close proximity to one another

Why are these two bootstrap buttons so close together? Should I use some custom CSS to create space between them? On the documentation page here, they aren't touching. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.mi ...

Guide to dividing a URL in reactjs/nextjs

Here is the complete URL: /search-results?query=home+floor&categories=All+Categories. I am looking to separate it into two sections - /search-results and query=home+floor&categories=All+Categories. My objective is to extract the second part of t ...