Arrange the divs alongside one another within a container and ensure that the container adjusts its width to fit the children

I am in need of a container div:

  • This div should adjust its width based on the content within.
  • I would like the child elements to align horizontally.
  • The container itself should form a single horizontal line.

I prefer not to use flex because it tends to resize the children according to the container's width. I have come up with this solution which appears to be working, however, I seek a better understanding of the properties used.

    import React from 'react';
    import './style.css';
    
    export default function App() {
      return (
        <div className="container">
          {[1, 2, 3, 4, 5, 6, 7, 8, 9].map((x) => {
            return <div className="card">{x}</div>;
          })}
        </div>
      );
    }

related css code

    .container {
      display: inline-block;
      background: red;
      white-space: nowrap;
    }
    
    .card {
      display: inline-block;
      width: 100px;
      height: 100px;
    }

  • My primary inquiry is: why are two inline-block usages necessary? (one for the container and one for the cards)
  • You may notice that I had to include white-space: nowrap;. Was it essential given this scenario?

I am mostly focused on the above questions, but if you have an additional more elegant solution, feel free to share it as well.

Answer №1

One notable distinction between display: inline and display: inline-block is that the latter enables setting a specific width and height for the element.

Moreover, when using display: inline-block, the top and bottom margins/paddings are taken into account, whereas with display: inline, they are ignored.

Answer №2

.wrapper {
    display: flex;
    background-color: blue;
    // Increase space between the elements
    gap: 4rem;
}

.item {
    flex: 0 1 120px; 
    max-width: 120px;
}

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

Enhance npm package by implementing custom functionality with React Component

I have designed an app that bears a striking resemblance to the following code: class MyCustomApp extends React.Component { constructor (props) { super(props) this.state = { tags: [ { id: 1, name: "Oranges" }, { id: 2, ...

Incorporating a YouTube video

Having trouble integrating a YouTube video that won't play automatically on your website? The video is visible, but just doesn't start playing on its own. What could be causing this issue? Appreciate any help you can provide! ...

Issue: TypeError: Unable to access the 'getBoundingClientRect' property of an undefined value

Is there anyone who can lend me a hand? I encountered an issue: TypeError: Cannot read property 'getBoundingClientRect' of null https://i.stack.imgur.com/Jnfox.png Here is the code snippet I am trying to render: <div className="box&qu ...

Creating a layout of <video> components in vue.js, complete with the ability to rearrange and resize them through drag and drop functionality

Despite trying numerous libraries and Vue.js plugins, I have yet to find one that meets all of my requirements. I am in need of creating a grid of video HTML elements that are draggable and resizable with a consistent aspect ratio of 16:9. Additionally, I ...

Intellisense for dispatch types in Redux Toolkit

After following the documentation for redux toolkit with typescript, I implemented my own useDispatch hook as shown below export const useAppDispatch = () => useDispatch<AppDispatch>() and used it in components like this const dispatch = useAppDi ...

Set the background color of the container row to extend the full width, encompassing

I am working on a grid container and I want to change the background color of the second row within it. Check out my Fiddle This is the HTML markup: <div class="totalContainer totalContainer__space"> <div class="totalContainer__ ...

Is it possible to incorporate Material-UI Lab into my project without having to install the Core components as well?

I'm hoping to incorporate the Slider component from Material UI into my React application. The Slider is one of the components housed in the 'Lab' package, which acts as an incubator for features that aren't quite ready for the core. n ...

Following the migration to Typescript, the React component is having trouble locating the redux store props and actions

Here is the structure of my app: export default class App extends Component { render() { return ( <Provider store={store}> <Router> <Header/> ...

Issues arise when attempting to alter the background image using jQuery without browserSync being activated

I am facing an issue with my slider that uses background-images and BrowserSync. The slider works fine when BrowserSync is running, but without it, the animations work properly but the .slide background image does not appear at all. Can someone help me ide ...

What is the computed value of HTML and body?

Is there a specific reason why the body tag has a default margin while HTML does not? Could there be any practical explanation for this design choice? ...

Applying personalized CSS to an element based on the condition of a child element in the previous sibling element

I am trying to apply styles to a span element only when a child element in a preceding sibling div element is active, which means a radio button has been checked. I am unable to modify the code and can only use CSS for this task. Any ideas on how to achi ...

Updating the state on change for an array of objects: A step-by-step guide

In my current scenario, I have a state variable defined as: const [budget, setBudget] = React.useState<{ name: string; budget: number | null }[]>(); My goal is to update this state by using a TextField based on the name and value of each input ...

Issue with popup display in React Big Calendar

I'm currently working on a calendar project using React-Big-Calendar, but I've run into an issue with the popup feature not functioning properly. <div className={styles.calendarContainer} style={{ height: "700px" }}> <C ...

Date Selector Tool for Input Field

Does anyone know how to change the color of the date picker that appears in certain browsers but is unsure about the selector's name? HTML: <form action="pledge.html" method="post"> <input type="date" id="birthday" name="user_bda ...

Creating visually pleasing HTML emails with uniform table data heights

Struggling with Outlook while designing an HTML template. My row has 2 td elements with different content, and setting equal heights is proving to be a challenge. The fixed height on the td causes issues when switching to mobile view in Outlook as text wra ...

Changing the color of a selected React Material-UI ToggleButton: A Step-by-Step Guide

I am trying to customize the color of the selected <ToggleButton ... /> to my theme's primary color, but I am facing difficulties in achieving this. Despite my efforts, the code below does not change the color to primary: ... import { ToggleBut ...

Having trouble with ion-item click not functioning in Ionic 3?

Working on my Ionic 3 project, I have encountered an issue with the ion-item click listener. The scenario is that I am adding text over a canvas and then attempting to change the style of the text (such as font-family, font-size, bold, and italic). The UI ...

Step-by-step guide on creating a clickable button that triggers the appearance of a block showcasing a dimmed photo upon activation

Is there a way to create a button that triggers a pop-up block featuring a darkened photo when clicked? ...

How can Redux help persist input value through re-rendering?

Handling Input Value Persistence in Redux despite Re-rendering? I am currently able to store and save input values, but only the data from one step ago. For example, when I click on the second input field, it displays the value from the first input fiel ...

Is there a correlation between eliminating unnecessary imports and the size of the bundle as well as the speed of the build process

I am working on a Reactjs application built with Create React app, and I often encounter warnings during the startup and build process indicating that there are unused variables or imports in my Components. ./src/components/home/Header.js Line 10: & ...