How to Align Title in the Center of a Section Containing Multiple Images?

I have a situation where I am using a StyledHead section to display 10 images from an API. The images are wrapped in another section called StyledHeader, which also contains an h1 element. How can I center the h1 element both vertically and horizontally so that it appears centered over the images?

Styling:

const StyledHeader = styled.header`
  width: 60%;
  max-width: 1200px;
  margin: auto;
`;

const StyledHead = styled.section`
  display: grid;
  grid-template-columns: repeat(5, auto);
  gap: 5px;
  grid-template-rows: 1fr;
  width: 100%;
  opacity: 0.7;
  img {
    height: 100%;
    width: 100%;
  }
`;

The code:

<StyledHeader>
        <h1>Page Title</h1>
        {loading ? <p>Loading...</p> : null}
        <StyledHead>
          {data.Search?.length > 0
            ? data.Search.slice(0, 10).map((items) => (
                <img key={uuid()} alt="movie poster" src={items.Poster} />
              ))
            : null}
        </StyledHead>
      </StyledHeader>

Answer №1

To achieve the desired centering effect, you can set the h1 position to either absolute or fixed and then center it within the parent element.

If you want to center something in the middle of the screen, follow these steps:

h1 {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

However, if you need the h1 element to be centered within its parent element, you may have to adjust the top and left properties by using margin or padding instead.

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

Do we need to invoke the unmount function after every test case in React testing library?

describe('<CustomTooltip />', () => { it('should display the tooltip text', async () => { const { container, unmount } = render(<CustomTooltip text='Tooltip Text' />) userEvent.hover(container.quer ...

React: Is there a way to customize the form?

I have implemented the use of localStorage to store form details. Upon mounting the component, I am able to retrieve the data and display it in the console. However, I am currently facing a challenge on how to populate the form with this data for editing ...

Stop the background color of Material UI Textfield from changing when editing

I'm currently using the '@material-ui/core: ^4.12.3' package and I'm working with the Textfield component. I have one text field: https://i.stack.imgur.com/uktLw.png After editing, the background color of the text field changes from ...

Update the content of an HTML element without having direct access to the HTML code

I am currently in the process of creating a website using a website builder, and I am interested in customizing the default message that is displayed when a required field in the website form is left empty. The form in question is an integral part of the w ...

Error: MongoDB connection rejected on port 3001 while using ReactJS

I have encountered an issue with my MongoDB connection being refused at port 3001. It was working fine previously but suddenly stopped. My database is hosted on MongoDB atlas and I am using MongoDB compass to connect with the link in my code. Despite fol ...

JavaScript: Navigating function passing between multiple React components

I'm currently working on a React Native application utilizing TypeScript. In my project, there is a component named EmotionsRater that can accept two types: either Emotion or Need. It should also be able to receive a function of type rateNeed or rate ...

Why won't the CSS update in Next.js when the local state variable changes on page load?

I seem to be facing an issue with a variable stored in localStorage that changes when using a toggle button. The color changes correctly upon toggling the button, but upon page refresh, it doesn't display the correct color saved in local storage. Eve ...

The CSV/PDF print export feature in Material-UI is not functioning properly when using the "@mui/x-data-grid" module

Is it just me or am I missing something here? I tried to export my data as a PDF using the available export option, but all I see is the CSV option. Why is that? https://i.stack.imgur.com/j6LW6.png In their example, they have access to both CSV and Print ...

Is it possible to scroll the grid horizontally?

I have a grid that scrolls vertically, with columns adjusting their width automatically. .grid { display: grid; overflow-y: auto; grid-template-columns: repeat(auto-fit, minmax($min-column-width, 1fr)); } Now, I'm attempting to create a horizon ...

Can we implement attribute selectors in Material-UI while utilizing makeStyles?

Is it possible to extract all the div elements with specific Mui class names such as access-MuiPickersCalendarHeader-switchHeader, access-MuiPickersDay-day? My components are styled using StylesProvider which adds "access" as a prefix to the original Mater ...

How can I access the value of `var` from inside the return statement of a React component?

I am facing an issue where I need to retrieve the value of googleToken from within a <div> element in the return statement of my React Component. Despite the fact that the value has been updated, it keeps showing the initial state as null const Layou ...

Gallery of Disappearing Images

I am encountering issues with the image gallery on my website, as it seems to create a space to the right when viewed on screens smaller than 350px. This results in a gap on the entire right side of the page. My goal is to make this image gallery responsiv ...

Guide on how to force a child component to re-render when refreshing with RefreshControl in React Native

Here is how the components are structured in my React Native app: <View> <ScrollView> <BannerContainer /> <CampaignContainer /> <BrandContainer /> <Infographic /> <Seo /> &l ...

I need help figuring out how to set the country code as uneditable and also display a placeholder in react-phone-input-2 with React

How can I display the placeholder after the country code without allowing it to be edited? Currently, it's not showing up in my React functional components. Here is the code snippet: <PhoneInput className="inputTextDirLeft" ...

How can I transform <TabbedShowLayout> into an editable interface?

Our project requires us to implement an edit view instead of a show view in a tabbed show layout, similar to the image below. https://i.stack.imgur.com/sJM52.png The react-admin documentation clearly states that The Show view displays a record fetched fro ...

Troubleshooting issue: JSON object is returning undefined despite its presence in NASA API integration with ReactJS

{this.imageRenderer(item.data[0].media_type, item.links[1], item.links[0])} When trying to access item.links, it shows as undefined even though it is present. (Data is retrieved from NASA API) Image: As seen above, the links attribute exists. I am puzzle ...

Ways to Adjust Website Scaling for Varying Screen Sizes

Is there a way to adjust my website's scale based on the device width, such as changing it to 0.7 for certain devices? The only information I've found so far is using the <meta> tag: <meta name="viewport" content="width=device-width, i ...

The function cloneElement does not share any properties with the type Partial<P> and Attributes

I'm encountering a perplexing issue with my code. When I attempt to call cloneElement with the second parameter being of type Type { foo: number } has no properties in common with type 'Partial<Child> & Attributes', TypeScript thro ...

CSS code that fixes a textarea at the bottom of a div

I need help placing a textarea at the bottom of a fixed div: <div id=msg> <div id=content> aaaaaaaannnnnnnnnn<br> aaaaaaaannnnnnnnnn<br> aaaaaaaannnnnnnnnn<br> </div> <div id=text> <textar ...

Tips for altering the color of a specific bar in the Material UI BarChart component

I have implemented the following code to generate a Graph: const namesArray = Object.values(tableData).map(item => item.name); const valuesArray = Object.values(tableData).map(item => item.value); return ( <Box> <SimpleCard ti ...