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

Problem with translating a variable into a selector in JQuery

When attempting to make my Jquery code more flexible, I decided to extract the selector and access it through a variable. However, despite creating variables for both selectors, neither of them seem to be functioning properly. I am confident that the issue ...

The background color and border are not showing up in the <div> element

I am encountering an issue with 3 inline <div> elements where the one on the far left is not displaying the light blue background and black border that it should have. HTML: <!DOCTYPE html> </html> <head> <link rel= ...

At what point does a dynamically loaded CSS file in the head section of a webpage actually

If the answer is already out there somewhere, I would really appreciate a link because I just can't seem to find it. When loading .php pages, I either include 'header.php' directly with <?php include 'header.php'; ?> or on ...

Customize component designs using Box

The documentation for MUI mentions that a Box wrapper has the ability to override its child's styling using the "clone" option, as demonstrated here: <Box color="red" clone> <Button>Click me</Button> </Box> From w ...

``There seems to be a problem regarding the alignment of elements in

Having some trouble with centering the grid on my simple website. As a beginner in Bootstrap, I'm seeking guidance. Will be grateful for any help you can offer! Please bear with me as I learn. <html> <head> <meta charset="utf8"/& ...

choose particular column in every row from the table's header class name

Every <th> in a table has a class, while the <td> elements do not. Is there a way to style all corresponding <td> elements with the same styles as their <th> counterparts using only plain css without any script? Furthermore, since ...

Retrieve PHP variable from a PHP CSS file

I'm facing an issue where I am unable to retrieve a PHP variable from a CSS file that is loaded in this manner from my index.php: <link href="css/style.php" rel="stylesheet"> Within the style.php file, the code looks like this: <?php heade ...

The functionality of scrolling ceases to work once the bootstrap modal is closed

I am utilizing Bootstrap v3.1.1 to showcase a modal popup on one of my web pages. The following code shows how I have implemented it. <!--Start show add student popup here --> <div class="modal fade" id="add-student" tabindex="-1" role="dialog" a ...

Rearrange the middle column to be the top column on smaller screens

I am trying to create a layout with 3 columns in a row. The challenge is to prioritize the middle column on top when viewed on small screens, with the left and right columns below it. To clarify, on large screens the order should be 1 2 3, while on small s ...

Keep duplicating a single object until it fills up the entire screen

Is there a way to make an HTML/CSS element repeat until it covers the entire screen height, without repeating it indefinitely? #container{ height: 100vh; width: 100vw; } .dotts{ background-color: yellow; border-radius: 50%; height: 20px; width: 20p ...

Implementing functionality: Removing a row and applying a CSS class upon button click

Check out the fiddle below: https://jsfiddle.net/shaswatatripathy/enq0kfqL/2/ I need help with creating a remove function and adding a CSS class to a clicked row. 1. When I click on "remove", the clicked row should be deleted. When I click on a row, ...

Tips for transforming the <img> element into a Nextjs <Image> component

I have been working with Next.js for some time now, but I still find myself using the <img> tag for certain image components, like this one (using TailwindCSS). <div className="relative"> <div className= ...

Tips for eliminating stuttering when fixing the position of an element with JavaScript

I am currently facing an issue with a webpage I created where the text freezes when scrolled down to the last paragraph, while the images continue to scroll. Although my implementation is functional, there is noticeable jankiness when using the mouse wheel ...

Error: React input value exceeds specified range

Hi, I've been having some trouble formatting numbers while typing in a React application using the input type number element. Whenever the digit number goes above 4, it gives me an out of range error and then automatically resets the input to empty. ...

I am having trouble with data populating in an Array within the UseEffect hook in React

Within my useEffect function, I am populating two arrays, each containing two elements. However, only one element from each array is displaying on the page. https://i.stack.imgur.com/ZUeow.png This is the contents of the useEffect function: useEffect(( ...

Error encountered during unit testing: The function _reactRouterDom.useHistory.mockReturnValue is not a valid function

I'm having trouble writing unit tests for a React component implemented in TypeScript. I encountered an error when trying to mock some hook functions. Here is my current unit test implementation: import React from 'react'; import { useHisto ...

Tips for overlaying a webpage with several Angular components using an element for disabling user interactions

I currently have an asp.net core Angular SPA that is structured with a header menu and footer components always visible while the middle section serves as the main "page" - comprised of another angular component. What I am looking to achieve is ...

React Native state remains unchanged

I've been struggling for hours trying to pass a string from AsyncStorage to a state variable using hooks, but I can't seem to get it to work. Here is the code snippet: const [cartitems, cartitemsfunc] = useState(''); const d = async () ...

I am facing an issue where the images (IMG) do not align properly when I test the responsiveness using Chrome

Seeking assistance with a design challenge I encountered. The first image showcases a well-structured table on desktop view, while the second image displays an undesired layout when examined using Chrome DevTools. A link to the problematic layout can be fo ...

Steps to assign a default value to the KeyboardTimePicker

I am utilizing the KeyboardTimePicker component from material ui to fetch a time of service such as "10:20". How can I make this time ("10:20") the default selection? When attempting to set this time, I am receiving an error stating " ...