Guide to align elements (cards) side by side in a row within a container element (div)

I have a question that bears resemblance to another one found on this site.

I decided to create my own version of the Material UI Card example from https://material-ui.com/demos/cards/. You can view and edit my version on the sandbox editor by clicking here.

Essentially, I am trying to arrange Material UI cards horizontally in a single row within a parent container. If the cards don't fit in this row, they should automatically form a new row below.

When I use the following code:

render(
    <div className="row">
      <Demo /> <Demo />
    </div>,
    rootElement
  );

I anticipate seeing the two cards displayed side by side.

Even after adding styles using display: "inline-block", the cards still stack vertically as shown in the snippet below:

const styles = {
  card: {
    minWidth: 275,
    display: "inline-block"
  }
};

Answer №1

Some adjustments are required

1) The parent div should also be set to display inline-block to allow for side by side alignment

2) Ensure white-space is set to nowrap to prevent elements from wrapping to the next line. Even though divs are inline-block, they may wrap if their width exceeds the container. Setting white-space parameter will prevent this.

Visit this link for a live example

Answer №2

Consider including the following code snippet:

const styles = {
  card: {
    minWidth: 275,
    float: "left",
    marginRight: 10 // or something
  }
};

The issue here is that the element wraps its content in a div by default, which has a display property of block. This is why using display: inline-block won't work as expected on the Demo component.

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 increase the spacing between content boxes using Bootstrap

I currently have a row of three Bootstrap boxes structured like this: <div class="row"> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </div> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </di ...

Passing props from one Component to another Component that is not a child

My Project Components - ProfileHeader --Notifications Post --Comments --LikePost In my project, I want the Notifications Component within the ProfileHeader Component to receive props whenever a user interacts with a post by commenting or ...

Concentrating on Popper triggering upon MouseOver

I am having an issue with my Popper, which is activated on mouseOver and deactivated on mouseOut. Within the Popper, there is a link for users to interact with. The problem arises when the Popper disappears as soon as I move out of it, preventing the use ...

What is the reason for not being able to retrieve query parameters in "getStaticProps"?

Let's simplify things: URL: http://localhost:3000/product/bioCloths?activeCategory=medium&id=0 File location: pages/product/[product].js. Expected output: product: "bioCloths, activeCategory: "medium", id: "0" using g ...

Issue with displaying <div> elements using array.map() in React

I've been struggling with this issue for a few days and haven't had any success even after going through similar questions. The problem I'm facing is that when the page loads, only the first menu item appears. However, upon clicking on it, ...

Tips for avoiding accidentally selecting nearby text while holding down a button on your mobile device

My application requires the user to press and hold a button to record audio. However, on mobile devices, when the user holds the button, the browser attempts to select nearby text due to finger pressure. While this behavior is understandable, I want to pre ...

Building a sub route in React Router v4 allows developers to efficiently organize and manage

Before starting, I familiarized myself with the concept of react router by visiting https://reacttraining.com/react-router/web/guides/quick-start. I have developed a simple 3-page site in react and now want to create a list that will allow me to display so ...

implementing click functionality in a styled React component

There is a collection of simple divs containing names: I want to be able to click on one and have its background turn green. If I click on another one, the first should revert back to its original color so only one is colored at a time. I'm not sure ...

How to Align React Material UI Grid with Varying Row Counts in Each Column

Objective My goal is to design a component that showcases details about a device, including an icon and its current status. For instance: Approach I've Taken I am attempting to accomplish this by structuring a MUI Grid in the form of a 2x2 grid la ...

Enhancing your Vue web application with Bootstrap: A guide to customization

Utilizing bootstrap-4 within my Vue web application has been challenging. I am unable to customize it as explained here. My index.html utilizes Bootstrap CDN, as shown below: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

Challenges between Django and VSCode

As I dive into practicing website development with Django, I've encountered a problem while trying to save my work in VSCode and updating my local browser. It seems that only certain changes are being reflected. For example, when I delete code in my C ...

Setting the value in an Autocomplete Component using React-Hook-Forms in MUI

My form data contains: `agreementHeaderData.salesPerson ={{ id: "2", name: "Jhon,Snow", label: "Jhon,Snow", value: "<a href="/cdn-cgi/l/email-prot ...

Setting a div to occupy the entire height of a webpage using HTML and CSS

Is there a way to make the div (the blue box) fill the entire page? I tried setting the body tag to height:100%, but it didn't work. You can view an example at http://jsfiddle.net/rVyrX/1/ ...

React Hooks encountering issues with keydown/up events functionality

Currently, I am in the process of implementing arrow-based keyboard controls for a game that I have been developing. In order to stay updated with React, I decided to utilize function components and hooks. To showcase my progress, I have put together a dem ...

Error in JavaScript: Uncaught TypeError - Unable to access the "left" property of an undefined object

This error is really frustrating and I've come across several inquiries regarding this console issue. It's not providing enough information in the chrome console for me to effectively troubleshoot. /** * Adjustment of dropdown menu positio ...

Achieving a Transparent Flash overlay on a website without hindering its usability (attention, interaction, form submissions, etc.)

Currently, we are attempting to overlay a transparent flash on top of an iframe which loads external websites. Is there a method to configure the page in a way that allows the transparent flash to be displayed while still allowing interaction with the und ...

Enhancing the styling of checkboxes using CSS and Javascript

My Javascript skills have been put to the test with a simple code snippet that customizes checkboxes by hiding them and using background images in CSS to display checks and unchecks. It's a neat trick, but I'm wondering if it's HTML/CSS comp ...

Tips for displaying a tooltip when hovering over a label in a Material UI slider

I'm currently working on a slider quiz and my goal is to have the tooltip appear when hovering over the label on the slider. Currently, I can only see the tooltip when I hover directly on the thumb at the location of my mouse. Refer to the image belo ...

When the label is clicked, retrieve the value of the checkbox within

I have a checkbox inside a label, and for design purposes, I added a div there. My requirement is to get the checkbox value on the click event of the label. However, I am always getting false whenever the checkbox is clicked. <label class="text-xs col- ...

Change the color of specific elements in an SVG using React

Can I change the dynamic primary color from ReactJS to a specific class in an SVG file? If yes, how can it be done? Error.svg <!-- Generator: Adobe Illustrator 26.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1&qu ...