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

What is the best way to iterate through two arrays and display the common elements as buttons?

I have a list of keywords and a collection of objects. Each object in the collection has a title that may match one or more keywords from the list. I am trying to loop through all the objects, check for keyword matches, and return the titles of the objects ...

Error: The data received from the Axios GET request cannot be assigned to the parameter type of SetState

Currently I am in the process of building my initial TypeScript application after transitioning from a JavaScript background. While I am still adjusting to the concept of declaring types, there is a specific issue I am encountering at the moment. The sni ...

The file AccessAlarm.js in the node_modules folder could not find the module 'material-ui/SvgIcon'

Recently, I encountered the following errors which appeared out of nowhere. Everything was working fine until now. ./node_modules/material-ui-icons/AccessAlarm.js Module not found: Can't resolve 'material-ui/SvgIcon' I am using Material UI ...

Is there a method to accurately detect the rendering of individual elements within a React Component, rather than the entire component itself?

While it's known that Components rendering can be detected through React's Developer Tool, I came across other methods in this source. However, what I'm specifically interested in is detecting the rendering of individual elements within a Co ...

Update the text within a textarea by clicking with the power of jquery

Is there a way to clear the content of my textarea on click? Below is the code snippet: http://jsfiddle.net/QMfyP/ <textarea name="adventage" style="width:400px; border-radius: 3px; border-left: 4px solid #6C3" id="adventage" cols="45" rows="5">Sh ...

When using the <object> tag, it may not always render at 100% height as intended

Application Inquiry I am seeking assistance with a web page that features a title, and a sticky menu bar at the top, allowing the rest of the space for displaying content. When a menu item is clicked, the objective is to load a page in the content at full ...

Adding EventListeners for Click Handling: A Step-by-Step Guide

Here is the part of my code in HTML: <!DOCTYPE html> <html> <head> <h> <title> This page</title> </h> </head> <body> <button id = "go-button" >GO ...

Utilizing a React component within an Express server: A comprehensive guide

I need to create a way for users to upload files to my express backend, but the form is within a react component. How can I incorporate my react ConverterSec2 component into the get function? server.js: import ConverterSec2 from "./ihertz_website/src ...

The information retrieved from Firebase through the use of useSWR is coming back as null

During my course, I am utilizing useSWR to fetch data from Firebase and then using it to update the state. However, when I attempt to log the data in the console, it shows up as undefined and the screen displays a loading message. This is happening even ...

Step-by-step guide on aligning an image to the left of text in a Material UI table column

After experimenting with inline styling, I attempted to move the images next to the text in a specific column. However, there are some rows where the images overlap or the text is positioned too far to the right, as shown in this image: https://i.stack.im ...

Utilizing the setState hook to completely replace the existing data in state

Currently, I am utilizing the Cotext API within Next.js and am seeking guidance on how to effectively modify the state in this particular code snippet. The desired functionality involves updating the SearchInput state with the value obtained from the getSe ...

Darken the backdrop of the bootstrap modal

When the modal is opened, the background turns black instead of having an opacity of 0.5 or something similar. How can I resolve this issue? I am using Bootstrap 3.2. Below is some CSS code snippet: .fade.in { opacity: 1; } .modal-open .modal { overflow ...

Easy Peasy CSS Placement

Struggling with CSS and in desperate need of assistance. I've provided my code attempts along with pictures showing what I currently have versus the desired outcome. HTML <div class="thumbposter"><img src="http://tipmypicks.com/cssmovie ...

Customize React Material UI styling with withStyles

I'm currently involved in a project where I am using React Material UI and need to change the border bottom color of the Select component when it is focused. Here's the code snippet that I have implemented so far. import { Select as MuiSelect, w ...

use css to align multiple div elements

I am struggling to line up more than five div tags on the same line. Here is my CSS: <style> #yes { float: left; width: 18%; margin:1px; } </style> Example of HTML code: echo '<div id="yes">'.'<b>'.'Job T ...

What is the best way to fit the text into a DIV row as efficiently as possible?

Looking for a solution to prevent a span from dropping down to the next line when text is too long within a fixed width and height row. The row consists of three elements, two with fixed widths and the third containing a span and text. Constraints include ...

Tips for configuring a dismissing alert to display only one time

I'm trying to implement a feature that displays an alert only the first time a user visits a page, and then never shows it again during subsequent visits. I found some code on a website here that gave me ideas. Here's my implementation: th ...

Navigation Bar Search Box Alignment Problem

Hi there, I'm new to programming and I'm trying to center my search bar with the navigation links beside it within a fixed navigation bar. However, I'm having trouble getting it to work properly. Can someone take a look at my HTML and CSS co ...

Webpage refreshing when resizing browser

Hey there, I'm facing an issue where my HTML website restarts whenever the browser size changes. Can someone please help me fix this? You can check out my website here I have uploaded my code files here: Code Files Link ...

Tips for storing an array consisting of 4 elements (objects) in local storage using React

Is there a way to replicate and add 4 objects with the same properties in an array, then store them in localStorage? I need to ensure that each object has identical properties. componentDidMount(){ const productData = Array(4).fill({ product ...