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

Maintain the basic structure while ensuring divs are properly aligned

Behold, my div structure! ONE // at the top level TWO THREE // two divs side by side in the middle level FOUR // residing at the bottom <div id="ONE"> <div></div> <div><img src="logo.png"></div> </div> < ...

There is a missing file extension "css" in the import/extensions for "typeface-roboto"

I have recently begun working on a new Ruby on Rails application with React, utilizing the webpacker gem. In addition to that, I have included material-ui and eslint-config-airbnb. However, when I ran the linter, it flagged the following error: Missing fi ...

Creating a navigation bar that smoothly slides into view from the top

In my Angular app (version 2+), the current code I have is: .header { background: rgba(white, 0); &.fixed-top { background: rgba(white, 1); border-bottom: solid whitesmoke 1px; position: fixed; top: 0; right: 0; left: 0; ...

How to Make a Div Tag Fade Effect with JavaScript in an External File

*** New Team Member Notice *** I'm currently working on an assignment and trying to implement a simple fade effect on a box. While it seems like this should be straightforward, I'm encountering some obstacles. Currently, the button causes the bo ...

Retrieve information from SWR mutate following a successful login

I'm currently working on a project that involves a nextJS application with a Laravel backend. I've been experimenting with Laravel-NextJS for this project. So far, all login and backend functions are functioning properly. Here's some code ...

Trouble with the Bootstrap navbar loading correctly

Recently I started using Bootstrap and decided to implement one of their templates. Everything is working fine, except for my navbar which is not loading correctly. It should look like this: Desired Navbar However, in both Chrome and Firefox, it appears l ...

Creating a navigation bar in React using react-scroll

Is anyone able to assist me with resolving the issue I am facing similar to what was discussed in React bootstrap navbar collapse not working? The problem is that although everything seems to be functioning properly, the navbar does not collapse when cli ...

How does leaving a comment in a different file impact the overall design of the app in Next.js?

I'm currently working on integrating light and dark themes into a Next.js application. For this, I am utilizing TailwindCSS in combination with next-themes. Strangely enough, even though my system preference theme is set to dark mode, the page initial ...

CSS and JavaScript Nav Menu Collapse (No Bootstrap)

I have written a navbar code using pure HTML/SASS, but I am facing a challenge in adding a collapse element to the navigation bar. Despite trying various solutions from Stack Overflow, I still haven't found one that works for me. Therefore, I am rea ...

Enabling communication between my React frontend and Express server

I currently have a Digital Ocean Ubuntu VPS set up with Apache, Node, and Mongo running. I can successfully retrieve data from my database using CURL on the server and my node backend is running on port 3000. For my React frontend, I am using Fetch and it ...

Change the background image when hovering over an element with vanilla JavaScript by utilizing data attributes

I would like to be able to change the background image of a <div> when hovering over a link. Initially, the <div> should not have a background image when the page loads for the first time. This is my current setup: <div class="background"& ...

Struggling to configure dynamic routing in NextJS for endpoints retrieved from Medium API

Having trouble configuring dynamic routing in my Next.js app. I'm attempting to showcase my Medium.com posts using the unofficial Medium API. Currently, I can display a list of cards on the homepage with the posts and each card should link to its corr ...

The TextField component displaying a date type input unexpectedly adds a calendar icon

The <TextField> component from material-ui is behaving strangely by automatically generating a small icon at the end of the input based on the specified type. For instance, when using type="date", a specific icon appears at the end of the ...

Setting the color of an element using CSS based on another element's style

I currently have several html elements embedded within my webpage, such as: <section id="top-bar"> <!-- html content --> </section> <section id="header"> <!-- html content --> </section> <div id="left"> &l ...

Troubleshooting problem with responsive image display and blurred effects

Initial problem I have a photo gallery on my website. When you click on a photo, you can view a larger preview. To cater to mobile users, I set the following CSS: .overview img { height: auto; width: 90%; } However, I am facing an issue where the hei ...

how can I apply a Linear Gradient color to the borderColor within expo react native?

Learn how to apply a linear gradient color on borders I am looking for guidance on using a linear gradient in stylesheet for border color. borderColor:['color1',color2] https://i.stack.imgur.com/oBCeu.png ...

Creating an AngularJS directive specifically for a certain <div> tag

Recently, I began learning angularjs and came across a script to change the font size. However, this script ended up changing all <p> tags on the entire webpage. Is there a way to modify the font size of <p> tags only within the <div class=" ...

What seems to be the problem at hand, and where exactly is the issue located?

import React from 'react'; const card = ({name, email, id}) => { return( <div className='tc bg-light-green dib br3 ma2 grow bw2 shadow-5'> <img alt="robots" src = {'https://uniqueimage. ...

Tips for customizing the appearance of an active link in a mapped navigation bar

I am currently working on a Next.js page and I want to add some styling to the NavBar to indicate to the user which page they are on. Although I found this interesting solution on Stack Overflow - Target Active Link when the route is active in Next.js ... ...

How come the last word in CSS nested flexbox wraps even though there is space available?

I am facing an issue with a nested flex container setup. Below is the code snippet: <div class="parent-container"> <div class="child-container"> <span class="color-block"></span> <span>T ...