When an image with the property "position: absolute" is placed inside a container with the property "position:

I am aiming to have my mail icon centered at the bottom of my page. I initially used <div class=section4> with a position: relative and applied position:absolute to my #mailicon. This successfully placed the icon at the bottom of my div, but it now consistently shifts to the right without being centered.

How can I reposition it back to the center while still keeping it at the bottom of my div? Additionally, as I am developing a responsive website with a mobile-first approach, the most responsive solution is preferred! Thank you!

Below is my HTML:

<div class="section section4">
        <img src="icons/ML-network.gif" alt="icon3">
        <h1>...</h1>
        <p class="ultralight">...</p>
        <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05666a6b71646671456864626c666c6601636826676e67616c7d36">[email protected]</a>"><img id="mailicon" src="icons/email-icon%20color.png" alt="mail icon"></a>
</div>

and the corresponding CSS:

.section {
    height: 100vh;
    width: 100%;
    text-align: center;
}
.section4 {
    position: relative;
}
#mailicon {
    width: 15%;
    position: absolute;
    bottom: 0; 
}

Answer №1

To center an element with the CSS property position: absolute, you can achieve this by setting left: 0; right: 0; margin: auto; as shown below:

#logo {
    width: 20%;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    margin: auto;
}

JSFiddle Link

Alternatively, you can also use translate like so:

#logo {
    width: 20%;
    position: absolute;
    top: 0;
    left: 50%;
    transform: translate(-50%,0);
}

JSFiddle Link

Answer №2

Consider updating your css code with the following adjustments:

#mailicon {
  width: 15%;
  margin:  auto;
  position: absolute;
  bottom: 0; 
  right: 0;
  left: 0;
}

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 preventing the button inside my div element from responding?

I created a button inside of a moving div with a background image and added an animation to the div for movement. However, I encountered an issue where the button did not react as expected. To test this, I added another stationary button which reacted acco ...

Display an alert message through jQuery script if the input type file is not an image

When working with an input type file, if an image is selected it will show a preview. If something other than an image, like a PDF or DOCX file, is selected I want to show an alert as invalid. However, I am encountering an error: Cannot read property &apos ...

Bootstrap breakpoint activated on mobile window

Is there a way I could insert a breakpoint for when the HTML content is displayed in a small window, like xs? <br class="show-when-small-window"> ...

Identify the index of a list item using a custom list created from buttons

When dealing with a dynamically built list like this: <ul id="shortcuts"> <li><input type="checkbox" value="false"/><button>foo</button><button>-</button></li> <li><input type="checkbox" value ...

I am puzzled as to why the frames in my code are failing to display

I'm attempting to create a simple two-frame menu and content system, but I've been struggling to display the frames properly. Here's the snippet of code that I'm working with. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http: ...

What is the process of turning a picture from a menu file into a clickable link?

I have created a menu with some images included, and I want them to be clickable links. However, currently only the text on top of the images acts as a link when hovered over. I would like the entire image to be clickable, not just the text. I believe I ...

What is the best way to position a title and subheading alongside an image?

I am currently working on developing a blog using php and html to enhance my coding skills. On my website, I have a special category where I display posts, the number of which can vary. Each post is composed of a title, a subheading, and a placeholder div ...

Launch a PowerPoint presentation in a separate tab on your web browser

If you have a link that leads to a .pdf file, it will open in a new tab like this: <a target="_blank" href="http://somePDF.pdf">Download</a> But what about a Powerpoint file, such as a .ppt? Is there a way to make it open in a new browser tab ...

Obtaining the width of an object in Three.js using CSSObject3D

In my process, I am dynamically generating HTML Dom Elements from HTML text using a specific function: String.prototype.toDomElement = function () { var wrapper = document.createElement('div'); wrapper.className = "toDomWrapper"; wrapper.i ...

Align list item span to the center

I have encountered an issue with creating a match list using li and ul tags. Despite applying the center span to a fixed width and the team spans to the remaining space, the widths still appear different. How can I ensure that the team spans have the same ...

Is there a way to adjust the SVG level so that it aligns with the center of the polygon instead

Looking to align a simple svg icon for a dropdown button with the text in the button so that the centers match up. Currently, the icon is aligned with the bottom of the text instead. Here's the code for the svg: <svg xmlns='http://www.w3.org ...

What is the method for styling the third list item in a CSS hierarchy?

I'm struggling to understand the hierarchy in CSS for creating this specific layout. I've searched for explanations, but haven't found a clear one that breaks down how each level works. This is my first time working with CSS so it's bee ...

Generate a link that can easily be shared after the content has loaded using

One feature on my website involves a content container that displays different information based on which list item is clicked (such as news, videos, blogs, etc.). This functionality is achieved by using jQuery's load method to bring in html snippets ...

Creating a dynamic pulse effect using jQuery to manipulate CSS box-shadow

My goal is to create a pulsating effect using CSS box-shadow through jQuery. Despite my best efforts, the code I attempted failed to produce the desired smooth pulse effect with box-shadow. The code snippet I experimented with: <div class="one"> &l ...

Turn on/off the button click functionality on a jQuery smart wizard control

I am currently utilizing this particular control, which happens to be version 3 of jQuery Smart Wizard. Here is the code snippet for the smart wizard: $('#wizard').smartWizard({transitionEffect:'slide'}); These are some helper functi ...

Simple method to restrict duration of video uploads using HTML5's capture attribute

I'm working on a website that allows users to take photos and videos with their smartphones. Everything is running smoothly with HTML5 and some javascript, but I want to set a maximum time limit for the videos without requiring users to start recordin ...

The website code lacks a dynamically generated <div> element

When using jQuery to dynamically add content to a "div" element, the content is visible in the DOM but not in the view page source. For example: <div id="pdf"></div> $("#btn").click(function(){ $("#pdf").html("ffff"); }); How can I ensur ...

As my character slides off the moving platform in this exciting Javascript canvas game, my heart

Can anyone help me figure out how to keep my player on the moving platform? I'm not sure if I need to add gravity or something else. I'm still learning the ropes. export function checkTopCollision({ item1, item2 }) { return ( item1.y + item ...

What steps are needed to adjust a modal window so that it perfectly fits the size of the image it contains?

I am currently utilizing Bootstrap 4.2 to create a popup window (modal) featuring an image displayed at its real size (100%) in both width and height, provided that the browser window size permits. If the image surpasses the window dimensions, it should au ...

Tips for sending data to CSS in Angular

I have an Angular application where I need to calculate the width of an element and store it in a variable called finalposition. Then, I want to move this element to the left by (finalposition)px when hovering over it. How can I achieve this styling effect ...