The application of position "absolute" to center content is ineffective

When I have an h1 element with position "absolute" inside a div with position "relative", setting h1's top:50% and left:50% places it in the middle of the viewport instead of the parent div. It seems that the nearest parent is being ignored for some reason.

HTML:

<div class="container">
            <div class="mshobeli">
                <div class="shvili black"></div>
                <div class="shvili red"></div>
                <div class="shvili green"></div>
            </div>
            <div class="pinkfloyd">
                <img src="https://c4.wallpaperflare.com/wallpaper/785/284/983/music-pink-floyd-wallpaper-preview.jpg"
                    alt="pink floyd logo">
                <h2>Pink Floyd</h2>
            </div>

Here is the CSS:

.pinkfloyd {
    display: inline-block;
    position: relative;

}

h2 {

    font-size: 30px;
    color: orangered;
    position: absolute;
    top: 50%;
    left: 50%;

}

Answer №1

Setting the top/left to 50% will move the h2 element itself, not the text within it. This means that the top left corner of the box containing the h2 element will be shifted to the specified position. To properly center it, you need to apply a transformation to the element (in this case, using -50% on the X axis to move it half of its width to the left). You can find the code snippet below. Make sure to understand the box model and block display for elements to fully grasp this concept.

.pinkfloyd {
    display: inline-block;
    position: relative;
}

h2 {
    font-size: 30px;
    color: orangered;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
}
<div class="container">
            <div class="mshobeli">
                <div class="shvili black"></div>
                <div class="shvili red"></div>
                <div class="shvili green"></div>
            </div>
            <div class="pinkfloyd">
                <img src="https://c4.wallpaperflare.com/wallpaper/785/284/983/music-pink-floyd-wallpaper-preview.jpg" alt="pink floyd logo">
                <h2>Pink Floyd</h2>
            </div>

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

The div does not allow elements to be centered

I am facing an issue where elements are not centering on my div. I have tried numerous solutions (so many that I finally decided to create a Stack Overflow account), but nothing seems to work. Interestingly, the elements center on PC but for some reason, ...

Adjusting the tab size and space size to their default settings within VSCode

I'm currently facing an issue with my VSCode setup. I am trying to configure the space size to be equal to 1 and tab size to be equal to 2 as the default for every project. However, despite my efforts, it keeps reverting back to spaces: 4 for each new ...

Stop closing the bootstrap modal popup when the space key is pressed

Is there a way to prevent the model popup from closing when the space or enter key is pressed on the keyboard? I have already tried using data-backdrop="static" data-keyboard="false" but it still closes. Additionally, I have ensured that the form tag is no ...

Troubles with caching on Amazon S3

Just starting out in web development and I'm working on updating HTML and CSS files stored on Amazon S3. Our website's backend is hosted on Heroku, while the front-end is on Amazon S3. Whenever I make changes to a CSS file, I can immediately see ...

JavaScript and Responsive Design Techniques

I'm struggling to create a responsive page that starts with a mobile-first approach, but I keep running into the same issue. When I have my dropdown menu in the mobile version, it looks good. However, when I try to switch it to the non-mobile version ...

Tips for Resizing and Reviving Divs with jQuery

I have recently ventured into the world of web development to assist a family member with their website. My knowledge and experience are limited, but I am facing an interesting challenge. I am trying to manipulate certain divs as users scroll down the page ...

How can you give a custom icon to the 'startIcon' of a MaterialUI button?

Below is the current code I am using: import { Button, Grid, Typography, } from '@material-ui/core'; import EditOutlinedIcon from '@material-ui/icons/EditOutlined'; <Grid item> <Button variant="outlined" ...

Implementing the onEnded event handler for an HTML video element

I recently experimented with the video tag using flowplayer and I wanted to share my code below: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"> </script> <!-- 2. flowplayer --> <script src="http://releas ...

What could be causing the image not to be centered inside the <table><td>?

Currently, I am developing a web application with Google Script that generates a dashboard featuring multiple tables. To design the layout, I am utilizing Bootstrap (https://cdn.jsdelivr.net/npm/[email protected] /dist/css/bootstrap.min.css). Despite ...

AngularJS enables box to smoothly slide up and down when hovered over

I am attempting to create a div that will overlay an image on hover with a slide up and slide down effect. Can anyone provide guidance on how to achieve this without relying on jQuery? I would prefer to utilize only AngularJS in my application. Here is a ...

Exploring design techniques in React Native

I am currently part of a team working on a react native project with multiple contributors. My goal is to find an effective way to segregate the styling tasks from coding, allowing UI developers to work independently without relying on code developers. In ...

Struggling to float <aside> to the left of <article> for proper alignment?

tldr; I'm attempting to position the aside-section to the left of the article-section (similar to a sidebar/side column), but my "float" property doesn't seem to be working at all. Is there a way to achieve this without modifying the HTML code an ...

Unforeseen Issues Arising from Media Queries

Trying out some new styling for different screen sizes based on the bootstrap grid system dimensions. Facing an issue where only certain styles apply while others don't seem to take effect. Here's a snippet of the CSS: @media only screen and ( ...

Persistent vertical menu dropdown that remains expanded on sub menu pages

I am struggling to understand how to keep my menu sub items open when on the active page. Although I have tried similar solutions, I have not been successful in implementing them. I apologize if this question has been asked before. My approach involves usi ...

How can I convert a JSON template to HTML using AngularJS?

As someone who is just starting out with AngularJS, I am facing an issue where the HTML tags in a JSON file are not being encoded when rendered in HTML using AngularJS. Is there a specific method in AngularJS that can help with this? This is what my HTML ...

The second picture in the slider transitions smoothly into view over the visible area of the first image

My image slider is based on jQuery but I'm facing a problem. When the page loads or reloads, the second image of the slider appears in the visible area overlapping the first image like this: https://i.sstatic.net/osbfb.png However, if I click the ar ...

JQuery does not immediately update the input value

I'm working on a jQuery placeholder that mimics the behavior of default placeholders in Chrome and Firefox for browsers that don't support it. However, I'm facing an issue where the placeholder div's HTML doesn't change as quickly ...

What's up with portable devices requiring two taps to hover over one DIV and affect another DIV?

Upon implementing "hover one DIV - affecting another inner DIV," a strange issue arose on iPad/iPhones where it now requires two taps to click on the link within the DIV with the hover effect: Below is the HTML code: <div class="portfblock"> &l ...

The margin of HTML is larger than that of the body

Currently, I am in the process of designing my own personal website using Bootstrap. While working with Bootstrap4 and making some modifications, I noticed that the HTML margin seems to be larger than the body. Despite trying to zero the margin of various ...

Error occurred during download or resource does not meet image format requirements

Encountering an issue when attempting to utilize the icon specified in the Manifest: http://localhost:3000/logo192.png (Download error or invalid image resource) from the console? import React from 'react'; import Logo from '../Images/logo1 ...