Element with position:absolute is failing to center within parent div with position:relative

Just to clarify, I am utilizing Bootstrap 3 for my project. Moving on to this specific section of the page (with more content below), I have set the viewport height to 100%. Within this section, there are two rows, each occupying 50% of the viewport height. The first row contains an image that I'd like to center both horizontally and vertically. The second row has an image that I want to center horizontally and fix to the bottom. Here's the HTML structure:

<div class='row vh-100'>
    <div class='col-xs-12 col-sm-12 col-md-12 col-lg-12 height-100'>

        <!-- Logo -->
        <div class='row height-50'>
            <div class='col-xs-12 col-sm-12 col-md-12 col-lg-12 relative height-100'>
                <img src='images/logo.png' 
                     class='img-responsive block absolute center middle max-height-100'>
            </div>
        </div>

        <!-- Silhouette -->
        <div class='row height-50'>
            <div class='col-xs-12 col-sm-12 col-md-12 col-lg-12 relative height-100'>
                <img src='images/silhouette.png' 
                     class='img-responsive block absolute center bottom max-height-100'>
            </div>
        </div>

    </div>
</div>

Here is the corresponding CSS:

.block{
    display: block;
}

.absolute{
    position: absolute;
}

.relative{
    position: relative;
}

.vh-100{
    height: 100vh;
}

.vh-50{
    height: 50vh;
}

.height-100{
    height: 100%;
}

.height-50{
    height: 50%;
}

.center{
    margin-left: auto;
    margin-right: auto;
}

.top{
    top: 0%;
}

.middle{
    top: 50%;
}

.bottom{
    bottom: 0%;
}

.max-height-100{
    max-height: 100%;
}

I'm encountering an issue where neither image centers horizontally unless their position is changed to relative. Furthermore, the first image aligns vertically within the entire page, rather than in the containing div. Any suggestions on how to resolve this?

Answer №1

When positioning elements absolutely, you can center them by using the following CSS:

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

Make sure to include vendor prefixes for the transform property (-webkit-transform, etc).

If you know the width of the absolute element, you can also use the following method:

margin-left: -halfelemwidth;
margin-top: -halfelemheight;

This can provide support for older browsers that do not fully support CSS transforms.

Note: It was pointed out that in the previous version, margin-top and margin-left should be half instead of full.

Another approach: To vertically center an image within a parent element with height, you can use the following CSS:

display: table-cell;
vertical-align: middle;
text-align: center;

This method can center images without the need for relative and absolute positioning of elements.

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

Creating a new row with a dropdown list upon clicking a button

I want to include a Textbox and dropdown list in a new row every time I click a button. However, I seem to be having trouble with this process. Can someone assist me in solving this issue? Thank you in advance. HTML <table> <tr> ...

What is the method for retrieving the input element's value when it loses focus?

I have an input box along with a label. My goal is to apply a specific style to the label whenever there is content in the input box. To achieve this, I need to retrieve the value entered in the input field during the blur event so that I can compare it. & ...

When utilizing SVG 'use' icons with external references, Chrome may fail to display icons when viewing an HTML file locally

I'm currently experimenting with using SVG icons, following the guide found at https://css-tricks.com/svg-use-with-external-reference-take-2/ Here's how it appears: <!-- EXTERNAL reference --> <svg> <use xlink:href="sprite.svg# ...

Exploring ways to extract and process scope fields that incorporate HTML code

Here's a straightforward example of AngularJS with a scope field called title. I want to display the title, which is "Hello world," in subscript format like this: https://i.sstatic.net/AtgZs.png The issue is that AngularJS doesn't correctly int ...

How about creating a fresh variable in Assemble or merging two comparison helpers together?

Is it possible to create a new variable within a partial in Assemble (assemble.io) or combine two comparison helpers? For example: {#is somevar "yes" || anothervar "no"} In my partial, I have HTML that should only display if one of two different variable ...

Adjusting the height of the Iframe dynamically every 2 seconds

One issue I am facing is with the iframe height. When I load the iframe, it sets the height correctly and adjusts when content is loaded dynamically. However, if I reduce or delete content from the loaded page, the iframe height does not decrease (e.g., in ...

Creating a button in HTML that performs a POST request without redirecting involves using specific code techniques

Looking for a way to create an HTML button that triggers a POST request to a quick route with parameters passed through req.params. The challenge is preventing the button from redirecting me to the route when clicked, but instead staying on the same page w ...

The pictures are not appearing on my local website

After completing the following steps: Node.js was successfully installed In the command prompt, I entered: npm install http-server -g Navigated to my website files located in cd user/me/local In Google Chrome, I typed: localhost:8080 Upon opening my HTML ...

Include HTML tag and class inside a JavaScript code block

A dynamic button is loaded when a certain condition in the JavaScript is met. Upon loading, I want to trigger a function (ClickMe) by clicking the button. However, I'm facing difficulty connecting the function with the button in my code. In my scrip ...

Unable to modify the .top attribute style in elements within an arraylist using JavaScript

I need to align multiple images in DIVs on the same line by setting their Top value to match the first image in the array. Here is the code I am struggling with: (addBorder function is called when divs are clicked) <div class="DRAGGABLE ui-widget-cont ...

"Can someone provide step-by-step instructions on how to mount a Vue 3 CLI project similar to

I have successfully created a pen that works here. Now, I am attempting to recreate the same application within a vue-cli project. Everything is set to default settings (vue3 preview), except for these 2 specific files: main.js import { createApp } from ...

Implementing the bootstrap dynamic modal in Laravel Blade for delete confirmation is an effective way to enhance user

I'm aiming to utilize a bootstrap modal for delete confirmation. However, I'm facing an issue where I need to pass a dynamic variable (containing the item ID) value to the bootstrap modal in order to submit a post request to delete that specific ...

Tips on extracting text from an HTML table

I have created a code that generates an HTML table when a button is pressed: function createTable($columns, $division, $rows) { $employees = 0; $progressive = 0; echo "<table border='1' width='50%' height='25%' ...

Adjusting the appearance of dropdown option borders in HTML/CSS - customize or eliminate borders as

select { width: 300px; font-family: inherit; font-size: 18px; padding-top: 10px; padding-bottom: 10px; padding-right: 30px; display:block; color: #999; border: none; border-bottom: 1px solid #757575; } *:focus { outline: #757575; } ...

Styling select input forms with Bootstrap 3

I'm looking to create a select dropdown menu that matches the style of other inputs in a bootstrap 3 form. You can view an example of what I want to achieve on this plunker or see the image below: This CSS code is included in the header: <!-- Boo ...

Django informing me that the template is not found

Why am I getting a "template does not exist" error in Django? I'm trying to navigate from film_detail.html to film_report.html by clicking on the "report" button... views.py class FilmReport(LoginRequiredMixin, UpdateView): model = Report fi ...

Why is my Feed2JS RSS feed functional locally but not operational after deployment on GitHub Pages?

I've been using feedtojs.org to display my Medium blog posts on my GitHub Pages website. Strangely enough, it works perfectly fine on my local server but not on the actual domain. The RSS feed is valid. Here's how it appears on my local server: ...

Full height background image

One issue I'm encountering is that the background image randomly disappears when scrolling down, revealing the background color instead. To better illustrate, here's a gif demonstrating the problem: https://gyazo.com/d2d742a1ff1225babe04faa0a597a ...

"Implementing responsive design with Bootstrap's carousel through media queries

I'm having some trouble creating a basic carousel using bootstrap because the images are not displaying full screen. Do I need to use media queries to achieve this and if so, how can I implement them? <div id="myCarousel" class="carousel slide" da ...

Exploring HTML5 canvas hit detection using isPointInPath() method

When it comes to hit testing with HTML5 canvas, here's an idea I've been considering: 1) Store the coordinates of a shape (e.g. a rectangle) - x, y, width, height 2) When the mouse is moved or clicked, redraw the rectangle on the screen canvas w ...