Tips for arranging cards responsively in a vertical column

I am working on creating simple card items using HTML and CSS. I have successfully aligned the cards in a row on a PC, but I would like them to be displayed vertically in a column on smaller screens. Below is the code I am using.

body {
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: url("/images/mainpage.jpg");
    background-size: cover;
    overflow: auto;
}
...
@media screen and (max-width: 600px) {
    .card {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
        grid-template-areas:
            "image"
            "text"
            "stats";
        margin: 15px;
    }
}
...

You can inspect the element directly in the snippet to view the results. Thank you!

Answer №1

Utilize flex-direction or flex-wrap for better layout

Initially, it is recommended to place your content (such as cards) within a container element instead of directly in the <body>. This will help with organization and future adjustments. A basic structure could look like this:

<div class="container">
   <!-- your cards here -->
</div>

...and when styling using CSS, consider something similar to:

.container {
    display: flex;
    align-items: center;
    justify-content: center;
}

To arrange content vertically inside a flexbox container, you have several options to choose from:

1 - Use flex-direction

If you are already implementing breakpoints, you can simply add a rule that organizes the content in a vertical manner:

@media screen and (max-width: 600px) {

    .container {
        flex-direction: column;
    }

    /* additional styles */

}

2 - Try flex-wrap

Flexbox has the capability to wrap items automatically if they exceed the available space. This feature does not necessarily require breakpoints, making it convenient especially if the wrapping timing is not crucial:

.container {
    /* other styles */
    flex-wrap: wrap;
}

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

Guide to playing a gif solely through an onclick event using JavaScript

Below you will find a code that is used to load a gif animation from an array of characters, and then display it within a board class using the image src. I am looking to make the gif animation play only when the displayed gif is clicked. Currently, the ...

Developing an Easy-to-Use PDF Popup Interface

I need a simple PDF modal where I can input the text: 'The sky is blue' and have it link to a PDF that opens up when clicked. I found a similar sample online, but it includes an image plus an image modal that pops up. I want to replace the imag ...

Is there a more straightforward alternative method to retrieve the title attribute from an input within a table cell?

Could you please help me with extracting the title attribute of the input located in the 2nd column (th) of the row with the "row_selected" class from the following table? Here's a link to the table. <table id="tabla_descuentos" class="tablesorter ...

Warning: Conversion error encountered while converting array from PHP to JavaScript, please take note

I've been working on extracting arrays of latitudes and longitudes from the PHP variables $lat and $long, which are retrieved from a database. <?php ... $lat[$latlongindex] = $results_row['latitude']; $long[$latlongindex] = $results_ro ...

What is the best way to adjust the mobile screen size to display the most optimized version of a web app's

Currently, I am utilizing react, Bootstrap, and some custom sass to construct and style the front end of my web application. While the design appears good on mobile devices, there is an issue where users need to pinch the screen due to it looking slightly ...

Tips on maintaining the content of an element within a directive template

I'm currently facing an issue with adding the ng-click directive to a button. Here's my HTML: <button class="btn">clicky</button> This is the directive I am using: angular.module('app').directive('btn', function ...

Delete the div if it exists following another div and place it within the main div

<div class="error"> <input type="text" name="email"> <div class="error-message">Please Enter Email</div> </div> <div class="i-icon-div">Lorem Ipsum</div> I am trying to identify the div with the class of i-icon-di ...

Creating a countdown timer for a specific date and time using web technologies

I'm looking to create a timer countdown that is set to a specific date and time, such as June 1st at midnight. The code should display the remaining time until that date or time. ...

Tips for confining a float within a div with fluctuating space

I have a scenario where I have multiple divs ('group') containing text, and there is a floated div ('toggle') in the bottom corner. The current code works well if the text length in 'group' is consistent, but the position of t ...

Verify the position of the scrollbar without triggering any reflow

Here is a function I have: is_bottom: function() { if (settings.scrollBottomOffset === -1) { return false; } else { var scroll_height, scroll_top, height; scroll_height = ...

Learn how to send data from a MySQL server to a Node.js application using Socket.IO

I've been facing a challenge recently. I am attempting to listen for events from the @rodrigogs/my-sql events node package and then emit those events through socket-io to the react client. However, there seems to be a disconnect that I can't see ...

Sending arguments to browser.executeScript

How can I provide parameters to browser.executeScript static sortableDragAndDropByClassName(dragElementClassName: string, dropElementClassName: string) { return browser.executeScript(function () { console.log(dragElementClassName); conso ...

Visualize data from ajax call in tabular format

I want to display the results of an SQL query in a table using AJAX. I have written code that executes the query during the AJAX call, but when I try to display these values in a table, it shows null values on the div tag. What could be the reason for this ...

Utilizing data attributes to configure jQuery plugin settings

Having trouble assigning options to an array value in a jQuery plugin using data attributes. When referencing the data attribute with a class selector: $('.tm-input').tagsManager( { prefilled: $('.tm-input').data('loa ...

Error encountered in THREE.js due to the specific version being imported

At the beginning of my three.js project, I opted for version 0.118.0 and included the necessary imports as shown below. import * as THREE from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf ...

Leveraging Handlebars for templating in Node.js to incorporate a customized layout

app.js const exphbs = require('express-handlebars'); app.engine('handlebars', exphbs({defaultLayout: 'layout'})); app.set('view engine', 'handlebars'); app.use('/catalog', require('./routes/ ...

Is your Drag and Drop feature in HTML5 acting up?

I am currently exploring the drag and drop functionality in HTML5. I have successfully managed to make an element draggable and ensure that the designated target accepts the dragged element. However, I am facing issues with getting the ondrag, ondragenter ...

Placing a Tooltip in the Right Spot

I am currently experimenting with adjusting the positioning of my tooltips to appear on the left side of the cursor instead of the right side. I am using a jQuery plugin called EasyTooltip. My attempt to set a negative value in the header's call for ...

Use of number type input on mobile devices in HTML

My goal is to display the up/down arrows alongside an input element with type="number" in Chrome on Android. I've adjusted the CSS to set opacity=1, but unfortunately, they are not appearing. On desktop, however, they show up without any iss ...

Adding a line break using the character in a JavaScript template string that is conditionally rendered within a React Bootstrap Button component does not work

One of the React components I've created includes a React Bootstrap Button component. Inside this component, I conditionally render a string based on its toggled state, shown in the code snippet below: <Button size="sm" ...