Guide to adjusting margin size according to specific screen size thresholds?

I've set up a series of reactstrap cards that are dynamically organized into rows. On "md" screen sizes and larger (bootstrap), there will be 3 cards per row, while fewer screens will display 2 cards.

Here's the component:

<Card
    outline
    as='a'
    style={{ cursor: 'pointer', margin: '1rem' }}
  >
    <CardImg top width='100%' src={img}' />
    <CardBody>
      <CardTitle tag='h5'>{this.props.title}</CardTitle>
    </CardBody>
  </Card>

The issue I'm facing is that I only want the middle card to have margins on both sides (marginLeft: '1rem', marginRight: '1rem').

Since the number of cards displayed in each row changes based on screen size, I can't simply apply a style based on multiples of x. Unless I have screen size information, is there a way to create a prop in my parent component that I could pass down to the card component to determine the correct margin setting?

Thank you!

(Any alternative suggestions are appreciated)

More code:

render () {
    return (
        ...
        <div className='row' style={{margin:'0rem'}}>
            {
                disp.map((d, index) => {
                    return (
                        <div className='col-md-4 col-6 d-flex'>
                            <the card component here>
                        </div
                    )
                    ...
                }
            }
        </div>
    ....
                

Answer №1

If you desire spacing between the children, one option is to utilize the gap property along with the flexbox layout. However, it's worth noting that the gap property may not be fully supported in all browsers.

html, body{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.card-container {
  background-color: aquamarine;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.card {
  border: 1px solid grey;
  padding: 1rem;
  height: 100px;
}

@media only screen and (min-width: 600px) {
  .card-container {
    flex-direction: row;
  }
  .card {
    flex: 1;
  }
}
<div class="card-container">
  <div class="card">card1</div>
  <div class="card">card1</div>
  <div class="card">card1</div>
</div>

To achieve this, you can use the following selector:

[parent-selector] > * + * 

This selector targets all sibling elements that are direct children of the specified parent-selector (which can be a class or HTML element).

For a top-to-bottom or row-oriented layout, you can utilize:

.card-container > * + * {
    margin-top: 1rem;
}

And if you want space between elements arranged from left to right or in columns, you can use:

.card-container > * + * {
    margin-left: 1rem;
}

html, body{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.card-container {
  background-color: aquamarine;
  display: flex;
  flex-direction: column;
}

.card {
  border: 1px solid grey;
  padding: 1rem;
  height: 100px;
}

.card-container>*+* {
  margin-top: 1rem;
}

@media only screen and (min-width: 600px) {
  .card-container {
    flex-direction: row;
  }
  .card {
    flex: 1;
  }
  .card-container>*+* {
    margin-top: 0;
    margin-left: 1rem;
  }
}
<div class="card-container">
  <div class="card">card1</div>
  <div class="card">card1</div>
  <div class="card">card1</div>
</div>

Answer №2

"How can I adjust margin size depending on different screen sizes?"

The Bootstrap responsive spacing classes are designed for exactly that purpose. However, if you're utilizing the grid system (Row > Col), it's recommended to use padding instead of margins to control the space between columns.

If you are using column classes like col-6 col-md-4 to achieve a layout where "there are 3 cards per row for 'md' screen sizes and above, and 2 cards per row for smaller screens.",...

You have the option to dynamically adjust padding on the columns or margins on the cards. For instance, you could apply 3 margin units on medium screens and above (mx-md-3), and 1 margin unit on smaller screens (mx-1):

   <Col className="col-6 col-md-4 py-3">
        <Card className="mx-1 mx-md-3">
           ...
        </Card>
   </Col>

Check out this example in React on Codeply

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

Ways to remove the address bar from a mobile browser like Chrome or an Android browser

Is there a way to make videojs go full screen on Android devices when the URL is entered, without displaying the address bar? ...

AngularJS NG-Grid displaying incorrect value for select cell

I'm working on creating a table with a column that needs to be selected based on a value received from the server. The server sends me 4 as the value, but the first option is always selected instead. $scope.lotteryOptions = { data: 'myDa ...

During the build process, Next.js encounters difficulty loading dynamic pages

My Next.js application is utilizing dynamic routes. While the dynamic routes function properly in development mode, I encounter a 404 error when deploying the built app to Netlify. https://i.stack.imgur.com/45NS3.png Here is my current code setup: In _ ...

Submitting an ajax form with the use of the symbol '&'

I am currently working on a form submission using the .ajax() method. Within my script, I have the following code: data: dataString, The variable dataString is composed of: var list = $('.listsummary').val() The listsummary class is assoc ...

Using a combination of a switch navigator, stack navigator, and tab navigator in React Navigation leads to the presence of two headers

In my React Native app using react-navigation, I have set up a combination of tab navigator, stack navigator, and switch navigator. Each screen in the tab navigator is nested inside its own stack navigator to utilize the built-in header feature. Although ...

Tips for keeping the main section from scrolling while scrolling through the side navigation

Within my Angular application, I have implemented a sidenav and a main section. My desired behavior is to prevent any scrolling in the main section while I am scrolling in the sidenav, similar to the functionality seen on the Angular Material Design docume ...

Utilizing MERN Stack in a LAMP Droplet Environment on DigitalOcean

I'm currently working on a MERN application that is split into two main folders: 1. client folder : This contains the Front-End code (REACT app) 2. server folder : Here lies the API Code (Express, NodeJs) The issue I'm facing is with the f ...

Arrange the elements within the anchor tag in a vertical line using Angular Material

Is there a way to style the elements within an anchor tag in Angular Material so that they display in a single line? I am looking for the CSS solution to achieve this outcome. This is my current HTML code: <a mat-list-item [routerLink]="['das ...

The tooltip function is not functioning properly on Internet Explorer when the button is disabled

I have been utilizing a similar solution found at http://jsfiddle.net/cSSUA/209/ to add tooltips to disabled buttons. However, I am encountering an issue specifically in Internet Explorer (IE11). The workaround involves wrapping the button within a span: ...

Transform the text upon hovering over the image, with the text positioned separate from the image

I'm looking for a solution where hovering over images in a grid area will change the text on the left side of the screen to display a predefined description of that image. I've been working on this problem for hours and would appreciate any help. ...

Submitting information to an HTML page for processing with a JavaScript function

I am currently working on an HTML page that includes a method operating at set intervals. window.setInterval(updateMake, 2000); function updateMake() { console.log(a); console.log(b); } The variables a and b are global variables on the HTML page. ...

Partial button clickability issue

There is a puzzling error where the button in the 'red area' is not clickable, and I can't seem to figure out why. Below are images illustrating the issue: https://i.stack.imgur.com/aPfcR.png https://i.stack.imgur.com/v5knZ.png Upon reac ...

Tips for utilizing variables exported from a .scss file to establish a Material-UI theme

I'm facing an issue while trying to utilize variables exported from a global .scss file to build a Material-UI theme. The problem arises because these variables are showing up as undefined on the server (I'm using next.js which pre-renders pages) ...

What is the best way to position text below an image icon in a menu?

I am having trouble creating a menu that has the same style as shown in this image: . Unfortunately, my menu ends up looking like this instead: . Can someone please help me fix this issue? Here is the HTML code I am working with: <header> ...

Converting JavaScript variable values to PHP variables by extracting the content of a textarea

Currently, I'm developing a data filtering mask which triggers when the button <input type="button" /> is clicked. In this process, I have: School classes (1, 2, 3, 4, 5) Sections (A, B, C....Z) Checkboxes for male and female sexes. This ma ...

The ng-model remains unchanged when the <select> element is modified using JavaScript

I am currently working on creating a customized dropdown box with the help of JavaScript and anglersJS to connect the data with the select element. The user interaction involves clicking on a div element that is styled to act as the dropdown option. This d ...

The remnants of the previous state linger on in React, even after new updates have been

This React application features a dynamic list with the ability to add and delete tasks. The list is maintained in both this.state.tasks and a database. When rendering, the code maps through the tasks array to display each task as a GridListTile component ...

Positioning icon/image beside the input box

Currently, I have a form with two buttons in a row and an icon (paper clip) that I am attempting to align next to the buttons using UIKit. However, I am facing challenges in getting the alignment right without affecting the size of the elements. Below is a ...

Setting up Electron with React and TypeScript: A Comprehensive Guide

I've been developing an app using Electron, React (jsx), and Babel. However, I recently made the switch to TypeScript and I'm struggling to get everything functioning properly. The npm packages I've tried only work for either React or TypeSc ...

Learn how to display or conceal the HTML for 'Share this' buttons on specific routes defined in the index.html file

Currently, I am in the process of updating an existing Angular application. One of the requirements is to hide the "Share this buttons" on specific routes within the application. The "Share" module typically appears on the left side of the browser window a ...