React: The row flex-direction is not being applied as expected

Although I know there are countless identical questions out there, mine lacks context and is as generic as they come. Why isn't the flex-direction row property working?

React View

return (
    <Fragment>
        <h1>The About us page.</h1>
        <Fragment className="container">
          <p>Dog</p>
          <p>Cat</p>
          <p>Other Dog</p>
        </Fragment>
    </Fragment>
  );

CSS

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

Despite setting the flex-direction: row; property in the CSS, the result shows those 3 <p> tags stacked vertically. I would include a screenshot but technical difficulties prevent me from doing so. The desired outcome is to have "Cat", "Dog", and "Other dog" displayed in a single row instead of individual columns.

From all the sources I've consulted online, it seems like the CSS syntax should be correct: https://www.w3schools.com/cssref/css3_pr_flex-direction.asp

Can anyone identify what might be the issue?

Answer №1

When working with the Fragment component, keep in mind that the className attribute belongs in the props object and should not be used as an actual style.

To apply a class to a group of items within a Fragment, simply wrap them in a div and assign the desired class:

<Fragment>
  <h1>The About Us page.</h1>
  <Fragment>
    <div className="container">
      <p>Dog</p>
      <p>Cat</p>
      <p>Other Dog</p>
    </div>
  </Fragment>
</Fragment>

Answer №2

The issue lies in using Fragment instead of div. React.Fragment serves as a convenient way to return multiple elements from a component. However, once React.JS constructs the DOM, the fragment essentially disappears, leaving your elements without a wrapper.

For more information, visit https://reactjs.org/docs/fragments.html

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

What is the best method for modifying an XML document without altering its associated XSL stylesheet?

I am working with XML data retrieved from a URL: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="/style.xsl"?> ....etc... To display the data in HTML format, I added the second line referencing the style.xsl file ...

I cannot access the 'isLoading' state in React Query because it is undefined

Since updating to the latest version of react query, I've been encountering an issue where the 'isLoading' state is returning undefined when using useMutation. Here's the code snippet in question: const useAddUserNote = (owner: string) ...

Caution: Prop type validation failed due to an invalid value being passed to ForwardRef(Slider)

Currently, I'm in the process of building a Material UI range Slider with two values. It functions correctly if initialized like this: const [value, setValue] = React.useState([5,20]); const [value, setValue] = React.useState([val]); const handl ...

React failing to render due to an empty props object

Currently working on learning React by following a tutorial. Struggling to retrieve data from my API (MongoDB using whatwg-fetch) and display the items on the screen. The database is up and running, no errors in React. Developer tools indicate "props objec ...

Adjusting the color scheme of a Checkbox Button in Bootstrap 4

Seeking advice on locating overrides for certain css properties. Take a look at this illustration on https://getbootstrap.com/docs/4.1/components/buttons/#checkbox-and-radio-buttons. The contrast in color between the activated and deactivated states is n ...

The alignment of the image background is malfunctioning on the iPad 3 device

Hey there, I need some assistance resolving an issue with a header background image on iPad3. The problem is that the image is not displaying correctly on iPad3 browsers, even though it works fine on desktop browsers. Below you can find my CSS and HTML cod ...

Issue with Bootstrap 5 dropdown menu on mobile devices when not sliding down upon clicking

Important things to know before getting started: Utilizing Django (python) Implementing Bootstrap Additionally, using html and css Steps taken (in sequence leading up to encountering an error): Copied the navbar template from the bootstrap navbar docume ...

When an li element is styled with a width of 50% and floated to the left, it does not display the bullet points

When attempting to create a 2-column list, I implemented the following CSS: ul { overflow: hidden; } ul li { float: left; width: 50%; } However, I noticed that when I have 2 items in the list, the one on the right displays a list bullet while th ...

Unlocking the Sound: Using a Custom Button to Activate Audio on HTML Video in React

While working on a project, I encountered a small issue: I have a video tag in my code: <video muted autoPlay loop src={video}> I simply want to add a single custom button/control to toggle between muting and unmuting the video. I'm thinking of ...

Show picture during the process of loading an external webpage

Utilizing a script to load an external page (external meaning a page within my website) inside a div. Loading the page takes time, so I want to display an image until the page is fully loaded. JAVASCRIPT function HideLoader() { $('#loader' ...

Utilizing the <style scoped> feature within my Angular template

When adding CSS styles in the specified htm file's templateUrl while loading a directive, I wonder if this is a bad idea. Will it be repeated every time the template is instantiated on the rendered page, or does Angular handle this differently? By usi ...

Determine the last ListItem in a List using React and Material-UI

Utilizing material-ui alongside React has allowed me to create a dynamic listview with ease: <List> <Subheader>List Title</Subheader> <ListItem primaryText="Option One" /> <Divider /> <ListItem primaryText= ...

Determining the height of a component in React Native

https://i.sstatic.net/VLqIb.jpg The image demonstrates the styling of the component: <TouchableOpacity style={{ height: listDataSource[item.key].isExpanded ? 500 : 140, width: widthw * 0.8, marginHorizontal: 12, backgroundColor: 'red', }} Wh ...

Tips for implementing themes in Material UI with React and Next.js

I have created three files which are displayed below. The color of Paper will change based on the value of palette.type that is defined in theme.js. Page with palette.type: 'dark' Page with palette.type: 'light' However, I'm ha ...

Unable to retrieve JSON element in ReactJS

Here is the code snippet that I am currently working with: const [questions, setQuestions] = useState([]) useEffect(() => { let idVar = localStorage.getItem('idVarianta'); idVar = JSON.parse(idVar) axios({ ...

The div is empty when trying to display Google Maps

When I set the height of my Google map div in pixels, it displays correctly. However, when I try to set it as a percentage, specifically 100%, the map does not show up. I have tried setting the height of all parent divs as well, based on suggestions from S ...

How can you trigger useEffect using a context variable as a dependency in React?

Struggling to get a useEffect hook to trigger when its dependency is a useContext variable. The Context contains an "update" variable that changes, but the effect doesn't fire as expected. import { useEffect, useState, useContext } from "react" import ...

How can I dynamically change the default value of the selected option dropdown in React-Select when a new option is chosen?

Can you help me understand how to update the default displayed value on a dropdown in a react-select component? When I choose a different option from one dropdown, the select dropdown value does not change. I've attempted this.defaultValue = option.va ...

Utilizing CSS Grid to create a modern layout design featuring a fullwidth header and footer,

I have a traditional website layout with a header, main content area with a sidebar, and a footer. I want the header and footer to span the entire width on wider screens, while the main content and sidebar are fixed width, surrounded by whitespace. When th ...

Encountering a Typescript error when trying to invoke a redux action

I have created a redux action to show an alert message export const showAlertConfirm = (msg) => (dispatch) => { dispatch({ type: SHOW_ALERT_CONFIRM, payload: { title: msg.title, body: msg.body, ...