How to Align Image and Overlay Div in Bootstrap 4

I'm trying to center an image and a div layer within a parent div, but I'm having trouble getting it to shift from the left side of the column. I've experimented with several methods, but nothing seems to work. Even using the margin auto technique doesn't seem to have any effect, probably due to Bootstrap 4's flexbox system.

HTML:

#user-avatar {
    background: #ff4e00;
    border-radius: 50%;
    width: 250px;
    margin-bottom: 25px;
    position: relative;
}

#user img {
    border-radius: 50%;
    width: 250px;
    transition: .5s ease;
    backface-visibility: hidden;
}

#user-avatar:hover img {
    cursor: pointer;
    opacity: 0.3;
}

.middle {
    transition: .5s ease;
    opacity: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
}

.middle i {
    font-size: 34px;
}

#user-avatar:hover .middle {
    opacity: 1;
    cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div id="user" class="col-12">
    <div class="col-12" id="user-avatar">
        <img class="mx-auto d-block" src="assets/users/av-lg/liane.png" />
        <div class="middle">
            <i class="fas fa-camera-alt"></i>
            <p>Edit Avatar</p>
        </div>
    </div>
    <p id="username" class="text-center"><span>Howdy </span>Liane</p>
    <div id="xp-bar" class="text-left">
        <div id="xp"><p><i class="fas fa-stars"></i> XP</p></div>
    </div>
    <p id="stats" class="text-right">Level 1</p>
</div>

I am looking to center both the image and the 'underlay' (the 'middle' div) in the middle.

Answer №1

For #user-avatar, consider applying margin-left: 50% and transform: translateX(-50%). Further details can be found by following this link: http://jsfiddle.net/x1hphsvb/10994/

Answer №2

Include the class mx-auto on the element with id="user-avatar"

<div class="col-12 mx-auto" id="user-avatar">
        <img class="d-block" src="assets/users/av-lg/liane.png" />
        <div class="middle">
            <i class="fas fa-camera-alt"></i>
            <p>Edit Avatar</p>
        </div>
    </div>

https://jsfiddle.net/lalji1051/v06j8orn/9/

Answer №3

Just incorporate the following style - centrd now

#user-avatar {
    margin: 0 auto;
}

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 challenge of incorporating mod_rewrite with CSS files

I am new to mod_rewrite. I have a page profiles.php?page=XXX and I tried to change its URL to something more friendly like /cars/XXX/ RewriteEngine on RewriteRule ^cars/([^/]+)/?$ profiles.php?page=$1 [L] The issue arises when including styles: <li ...

Is there a way to eliminate a CSS class from a div element within the outcome of an ajax response?

My ajax response is functioning properly and displaying the content of the result, but I am always getting the CSS effects which I do not want. I need to eliminate the class="container body-content" from the following div in the ajax call's result. ...

The inclusion of a content editable feature within a carousel is leading to unexpected event propagation

I am dynamically creating an editable div using the code snippet below. <div class='reflection-field' contenteditable="true" data-number="${index}"></div> Expected Outcome: When I click on the generated div, I anticipate that the c ...

Tips for appending the id of an active element to a URL

My goal was to include the id value of the active element in a URL and then redirect to that URL with a button click. HTML <div class="icon" tabindex="0" id="company"> <img src="company.png"> </div> <button type="submit" onclick= ...

What is the process for generating an auto-incrementing button with a *different input field*?

After searching everywhere for a code snippet to add another input bootstrap input group, I finally decided to take matters into my own hands and create one from scratch. And would you believe it worked like a charm! ...

Navigating and Organizing in Ionic Service Factory

Apologies for the beginner question. I am looking to incorporate filtering and sorting by name on my webpage. However, I have encountered two issues after attempting to implement this functionality using a factory in services.js: When typing a search ter ...

Developing an interactive web interface with HTML

I've been attempting to design a flexible HTML page that replicates the layout of a Java applet clock, but I'm unsure if my current approach is correct. Below is an example of the three-clock image set that I am currently working on. My method i ...

Improve the parallax effect in your React component

Do you have any tips on smoothing out the scrolling animation for a React component with a simple parallax effect? I tried using requestAnimationFrame() in vanilla JS, but it doesn't seem to work well within the React component's rendering cycle. ...

Implementing border-right leads to alignment problems

I'm currently working on creating a list with bootstrap-Accordion using react-bootstrap. I'm applying a background color and border left to the selected item. The problem I'm facing is: Whenever I activate a bar, the text shifts to the rig ...

Issues with CSS and JavaScript functionality persist within the Flask framework

Having trouble with CSS and JavaScript on my website hosted via Flask framework on PythonAnywhere. Here is the directory structure: home/dubspher/mysite/ - README.md - app.py - index.html Static/ - css - fonts - images - js - sass Original HTM ...

The $_GET variable disappears once the form is submitted

Here's the issue I'm facing: On my homepage, there are tables with rows generated from the database using while loops. Each row includes a cell where an event can be added to that specific row. To achieve this, I pass the row ID as a $_GET variab ...

IE9: Enforce the browser and document modes of IE9

Currently in IE9, my webpage is rendering with the Browser Mode set to 'IE9 Compat View' and Document Mode as 'IE standards', causing issues with all canvas elements on the page. I have already ensured that I have included '' ...

angularjs issue with displaying data in ui-grid

Seeking assistance on implementing data display using ui-grid. Currently facing an issue where the grid is not showing any data, although when using a table with <tr ng-repeat="transaction in savingaccountdetails.transactions>, the data is displayed ...

Enhance Your Images with Hovering Icons

Is there a way to display an icon checkmark over the main image when a user hovers over it? The icon should appear in the top right corner of the image. <div> <img class="result-image" src="{{$property->photo}}"> <! ...

What could be the reason for the absence of margin on the top and bottom

.separator { border: 1px solid #000000; margin: 10px; } <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>Separator</title> </head> <body> &l ...

What is the most effective method to override parent styles while utilizing CSS Modules?

I'm currently using NextJS along with CSS Modules for styling purposes. One of my goals is to adjust the appearance of a component in different scenarios: When it's rendered normally on a page, utilizing props to specify className modifiers. W ...

`Make adjustments to the visual representation of crispy forms`

When using crispy forms, the labels for CheckboxSelectMultiple() fields are populated from the default string representation of objects defined in a list. To change this behavior, I am seeking assistance. Is it possible to create a custom string represent ...

When using WYSIWYG editors, be on the lookout for empty paragraphs and incorrect code in CSS, JavaScript, and

I am currently in the process of redesigning a website for a client. The site is already built using Joomla 1.7, but I am facing an issue with the articles section that was created by the client using a WYSIWYG editor. The problem lies in the messy code st ...

How can you incorporate a horizontal line beneath your text, similar to the one shown in the

https://i.sstatic.net/OzMVr.jpg I have a code snippet that needs a horizontal border similar to the one shown in the image <section class="about"> <div class="container"> <h1 class="text-center"> ...

Unable to create the complete PDF file using html-pdf in a NodeJS environment

When trying to create a PDF from an HTML template, I am encountering some difficulties. While it works with static entries, I want to generate the PDF for multiple items that can vary each time. I feel like I might be overlooking something, so any suggesti ...