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

The "if" statement carries out the identical action each time it is executed

Despite my efforts to toggle the value of the turn variable every time the if statement runs, I keep encountering the same outcome. It appears that turn consistently evaluates as 2. Below is the code snippet in question: $(function() { var turn = 2; ...

The TypeScript Type inside the Node Module Doesn't Seem to Be Functioning

In my React project, I am using the material-ui@next library along with typescript. Below is the code snippet that I have written: <CardMedia image={item.image_url} style={{ width: 238, height: 124.5 }} /> However, when I try to compile this code, ...

Error message: "When using REACTJS.NET server-side rendering, an issue arises where the object does not have support for the 'forwardRef'

Looking to implement server-side rendering for my REACT components using REACTJS.NET within an ASP.NET project. In the SSR JavaScript file, I am including the SimpleBar component from the simplebar-react package, which is triggering the error message Type ...

Tips for aligning a menu that expands from the side to the center

I'm trying to center the menu on my webpage, but the issue is that it's stretching from the sides. Here's a sample image of the menu layout: I'm struggling to figure out how to fill the empty space on the left side of the menu. ...

Issue encountered while requesting data from API in ReactJS

I've been trying to fetch data from using the react useEffect hook. However, instead of displaying the data, I keep getting an error message that says, "Error: Objects are not valid as a React child (found: object with keys {number, name}). If you me ...

What is the proper way to invoke a function that is part of a child component as a property in a React application?

In my app.js file, I have included a unique component called "SigningComponent" with the following code: onSign = () => { this.setState({ route: "home" }); }; registerFunction = () => { this.setState({ route: "registration" }); }; render() { ...

Building React JS frontend and RESTful PHP API from the ground up

Hello there, I am new to the world of Web development and currently a 3rd year student. I have a question about how to upload an image file from my FrontEnd React JS to my local server PHP API and store it in MySQL. I came across this tutorial here, but I ...

Ways to retrieve headers from server-side rendered (SSR) response in Next.js

I am currently developing a web application using getServerSideProps in Next.js. I have successfully set an authorization token in the header within the getServerSideProps function, but now I am facing challenges retrieving this token from the header. Her ...

Are You Able to Develop a Floating Window That Stays Persistent in JavaScript?

Looking to create a persistent floating window in a NextJS 14 app. This window needs to remain on top, even when navigating between browser windows. Want it to stay visible even when the browser window is minimized, like a Picture-In-Picture feature. Mos ...

The CSS code is effective on one page but fails to render on the other

I am in the process of creating a website for my jewelry business and it's my first time doing so. I'm facing an issue where the CSS styles are being applied to one page but not to the other, even though both HTML files have the same code linking ...

How to select a different component when hovering in Emotion/styled MaterialUi

I recently customized my Drawer Component using MUI and styled it using the styled function imported from import { styled } from "@mui/material/styles"; Is there a way to specifically target the ListButton inside the ListItem? const ListButton = styled(Li ...

What is the best way to position an image in the center over a video using HTML and CSS?

I am currently developing a website with a background video and I want to overlay a company logo on top of it while keeping it centered. My coding language of choice for this project is using HTML and CSS. Here's the HTML code snippet I have for this ...

The art of perfect alignment: Mastering the positioning of Material-UI Grid

This issue has been resolved Hey there, I'm currently working on a React App with Material-UI and I'm trying to center a Grid item that includes a Card along with two additional Grid items. Below is the relevant code snippet. Although the Cards ...

What is the method for equalizing the height of the first block with the second block?

Is there a way to ensure the first grid item is the same height as the second grid item? <Grid container spacing={2}> <Grid item xs={6}> <div> {[...Array(20).keys()].map(item => ( <div style={styles.container}& ...

Offspring element overrides styling of its parent

#popBox{ width:100%; height:100%; position:fixed; left:0; top:0; border-collapse:collapse; background:black; opacity:0.8; display:none; } <table id="popBox"> <tr> <td></td> <td></td> ...

Uncovering the HTTP Cookie in Node.JS with JWT

Let me preface this question by saying it may sound repetitive, but I appreciate your patience in hearing me out: I'm currently tackling a project involving a React Frontend and a Node Backend. User authentication is managed using JWT tokens. However ...

Creating resizable SVG elements using HTML or CSS for dynamic width and height

Is there a way to give my SVG element dynamic width and height in order to display the entire SVG image? For example, take a look at this CodePen link. <svg width="250" height="250" viewBox="0 0 250 250"> Alternatively, .svg { width : 250px; ...

The access to the HTTP request has been restricted

Currently, I am developing multiple applications that need to communicate with each other. To test these apps, I am using both Chrome and Firefox browsers. Strangely, the issue persists in both browsers. The issue at hand: In one of my applications (let& ...

Tips for passing down CSS styles through Less stylesheets

In an effort to create a legend below a <table> that matches the colors defined in Bootstrap stylesheets, I am struggling with the following: .table > thead > tr > td.warning, .table > tbody > tr > td.warning, .table > tfoot ...

Hover effect not activating on image within modal

only img:hover is working, but after adding this to the CSS: #user-profile #user-myModal .modal-body img:hover #user-profile #user-myModal .modal-body .change-pic i{ display: block; } it's not working. I changed the class and id names, tried eve ...