Skip to content
Advertisement

How to horizontally center the content of a div with css in React?

I have an HomeComponent as the following:

JavaScript

In the Home.css file I have:

JavaScript

As you can see in the following image the Home.css style is applied correctly but the content is not properly centered (the Play button is center aligned but the row is not)

enter image description here

Advertisement

Answer

Check out this answer from a similar question for more suggestions. I’d recommend approach #2, using flexbox. If you want everything, including the button, to be centred in a single row, apply the following in .center:

JavaScript

To apply the style only to the items in .row:

JavaScript

Flex will be applied to the children of whatever element you add it to. If you want all of the .rows to be centred, apply flex to the parent div surrounding the map function.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement