Unable to use column formatting on a row using flexbox

I am working on creating a layout with rows and columns where the content in each column is vertically aligned.

Here is an example of what I am trying to achieve:

Currently, I am using react and have multiple components. The approach I believe will work to keep "a" and "b" in the same row is:

.container {
    display: flex;
    flex-direction: row;
}

.content {
    display: flex;
    flex-direction: column;
}

For the container class:

const Field2 = (props) => {

    return (

        <div className={"container"}>
            {props.texturasEscolhidas.map(a => {

                return (
                        <Field2Content>
                            {a.labelNormalize}
                            {a.name}
                        </Field2Content>
                )
            })}
        </div>

    )
};

export default Field2;

For the content class:

const Field2Content = (props) => {
    return(
        <div className={"content "}>
                {props.children}
        </div>
    )
};

export default Field2Content;

Answer №1

Close but not quite there yet. Take a look at the sample code I've provided below with detailed documentation to guide you in the right direction.

.wrapper {
  display: flex;
  flex-direction: row;
  background-color: lightblue; /* visual aid */
  justify-content: space-around; /* evenly distribute items horizontally */
}

.box {
  display: flex;
  flex-direction: column;
  background-color: lightgreen; /* for better visibility */
  width: 10%;
  align-items: center; /* center paragraphs horizontally */
  margin: 2em 0; /* top and bottom spacing */
}
<div class="wrapper">
  <div class="box">
    <p>a</p>
    <p>b</p>
  </div>
  <div class="box">
    <p>a</p>
    <p>b</p>
  </div>
  <div class="box">
    <p>a</p>
    <p>b</p>
  </div>
  <div class="box">
    <p>a</p>
    <p>b</p>
  </div>
</div>

Answer №2

You have the option to create a wrapper containing items and utilize flexbox with a row layout while using space-around for your design. Inside these items, you can add children (such as a span element with text) and apply flexbox in a column layout. To position everything correctly, use align-items: center;.

For more information on flexbox, you may find this link helpful: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Below is a functional code snippet that demonstrates the concept:

.wrapper {
  width: 500px;
  border: 2px solid black;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  padding: 20px;
}

.item {
  display: flex;
  flex-direction: column;
  border: 2px solid black;
  padding: 5px;
  width: 50px;
  align-items: center;
}
<div class="wrapper">
  <div class="item">
    <span>a</span>
    <span>b</span>
  </div>
  <div class="item">
    <span>a</span>
    <span>b</span>
  </div>
  <div class="item">
    <span>a</span>
    <span>b</spa>
  </div>
  <div class="item">
    <span>a</span>
    <span>b</span>
  </div>
</div>

Answer №3

Give this code snippet a try

<div class="container">
            <div class="content">
                <div>A</div>
                <div>B</div>
            </div>
            <div class="content">
                <div>A</div>
                <div>B</div>
            </div>
            <div class="content">
                <div>A</div>
                <div>B</div>
            </div>
            <div class="content">
                <div>A</div>
                <div>B</div>
            </div>
        </div>

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

Discover which dependency has changed with the React useEffect hook

I'm encountering an issue with the useEffect hook where I want a conditional statement inside the callback to run only when a specific dependency is changed. Here's an example: const [index, setIndex] = React.useState(3); const [dep, setDep] = Re ...

Tips for adjusting the vertical position of an image within a bootstrap column by a small amount

Apologies in advance if this question has already been addressed, and I am struggling to adapt it to my specific scenario. My objective is to adjust the positioning of the two images shown in the screenshot example below so that they align with the grey b ...

Troubleshoot: Difficulty with accessing nested tag name using getElementsByTagName

I'm currently working with a div that contains a table and its data pulled from a database. <div id="content"> <table> <tbody> <tr> <th class="header" colspan="2">Food items include:</th> </tr> ...

What is the process for turning off bootstrap form validation?

I am facing an issue with utilizing the default input email validation in my development project. It seems that Bootstrap, which is being used for the website, has its own validation mechanism. Whenever I begin typing in a Bootstrap input field, an error m ...

Having trouble retrieving the "history" from props?

I am working with a React component. import * as React from 'react'; import { Component } from 'react'; import { FormControl, Button } from 'react-bootstrap'; type Props = { history: any[]; }; // Question on defining Prop ...

The UI is not reflecting the updated value from the Spotify API in the component

Currently, I am able to successfully retrieve the playlist values and display them in the console. However, I am facing an issue when it comes to displaying these values on the UI. function Sidebar() { const [{ playlists }, dispatch] = useStateProvid ...

How can I choose an item from a list once the page has finished loading in React Js and Material UI?

When using React Js & Material UI (Utilizing functional Components), I am faced with the task of selecting an option from a pre-loaded select list. My goal is to retrieve the option from the URL, and once the data has been loaded into the select list, auto ...

Using CSS nth-of-type on the same level of the DOM allows for specific

I was having trouble using the nth-of-type(2n+1) selector to target odd and even rows in the scenario below. What I wanted was for the nth-of-type selector to apply different styles to the odd rows with the classes "row-data row-header" and "row-data row-c ...

Reactstrap: Keep drop down menu open after an item is selected

In a DropdownItem, there is a search Input that should stay open when clicked to allow the user to type in their search query: import React, { Component } from "react"; import { connect } from "react-redux"; import { Dropdown, Dropd ...

Why opt for ref.current over directly applying the condition?

I'm curious as to why the code uses if (ref.current && !ref.current.contains(event.target)) rather than if (!ref.current.contains(event.target) function useOutsideAlerter(ref) { useEffect(() => { // Function for click event function hand ...

Exploring the Firebase authentication and implementing public and private routing in a React project

I am currently working on incorporating Public and Private routes using react-router-dom in my react application. To retrieve the authentication state, I am utilizing the firebase.auth().onAuthStateChanged() function. The issue arises because the firebase ...

Error encountered in Typescript parsing when setting EXTEND_ESLINT to true in create-react-app

Demo on GitHub - Simplified Version We are currently in the process of migrating our create-react-app project from Flow to Typescript incrementally. As part of this migration, I had to disable some ESLint rules that were causing issues. To customize ESLin ...

Troubination of Bootstrap CSS is causing issues on Floating Action Menu

I've implemented a Floating Action Menu using JQuery and CSS: HTML: <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <div id="hamburger" class="waves-effect waves-light"> <div id="wrapper"> <span ...

Sass issue: extending compound selectors is no longer supported

Encountered an error in my React app while running npm start. Using node version: v14.17.6 and npm version: 6.14.9 Error message: Failed to compile. Issue found in ./src/index.scss file with the following error: SassError: compound selectors may no long ...

When using NextJS, the dynamically generated HTML elements do not get affected by CSS repaint

Recently, I encountered an issue while working on a Next.js project involving the addition of an external script that uses vanilla JavaScript to dynamically create nodes. Despite importing global CSS at the top of my _app.js file, I noticed that the styles ...

What is the best way to specify the dimensions of an image with CSS or Bootstrap?

I am facing some challenges while building my website. The ideal look of the page is represented by home_page_ideal. The issue arises with the small boxed image on the top left corner. Once I scroll to the top, the page displays as home_page_issue. The CSS ...

The autocomplete feature in Codemirror seems to be failing specifically when writing JavaScript code

I am having trouble implementing the autocomplete feature in my JavaScript code editor using Codemirror. Can someone please help me figure out what I'm doing wrong? Here is the snippet of code : var editor = CodeMirror.fromTextArea(myTextarea, { ...

Adapt the stylesheet for mobile devices using CSS

I am struggling with updating file paths in CSS depending on whether the end user is accessing my site from a PC or mobile device. Below is my CSS code, where I attempted to redirect users if they are on a handheld device: <link rel="stylesheet" type=" ...

Error during minification process for file opentok.js at line 1310: react-scripts build

I encountered an error while trying to minify the code in my React project using npm run build. The snippet below seems to be the cause of the issue. Any suggestions on how I can resolve this problem? const createLogger = memoize(namespace => { /** ...

Using Props.store does not function properly when passed down to child components

Can anyone offer assistance with this issue? The problem I'm encountering is that 'this.props.store' does not seem to be accessible in child components. However, the connect(mapStateToProps, mapDispatchToProps) function works perfectly fin ...