Struggling to properly align a div on top of an image

view image description herei need assistance with centering the div over the background image. I attempted to use relative positioning for the container and absolute positioning for the elements, but it did not yield the desired result. The code snippet below showcases my current progress. The background image fills the entire space and the container should be perfectly centered on top of it.

.circle123{
    position: relative;
    float: none;
    width: 600px;
    height: 200px;
    top: 0;
    margin-right:auto;
    margin-left:auto;
    text-align: center;
    border: 1px solid black;

}
#circle1{
    position: absolute;
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color: rgba(50,205,50, 0.75);
}
#circle2{
    position: absolute;
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color:rgba(135,206,235, 0.75);
}
#circle3{
    position:absolute ;
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color: rgba(220,20,60, 0.55);
}
.back-bar{
    display: inline-block;
    width: 100%;
    height: 100%;
    background: url(image.jpg);
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;       
    background-position: center center;   

}
<div class="back-bar"></div>
<div class="circle123">
<div id="circle1"><h2>1<h2></div>
<div id="circle2"><h2>2<h2></div>
<div id="circle3"><h2>3<h2></div>
</div>
</div>

Answer №1

One issue lies within your html code - the last </div> does not close anything. In order to center a container using position: absolute, the container containing the element must have a position of relative. Following this, you can center your element by using position: absolute with the following properties:


position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;

Answer №2

I'm having a hard time grasping your request, but it seems like you're looking to center content both horizontally and vertically within a div that has an image as its background. Here's a solution for you:

.image {
width: 300px;
height: 300px;
position: relative;
background: url('https://placehold.it/300x300/?text=This is your image');
}

.image > .centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="image">
<div class="centered">This is at center</div>
</div>

Answer №3

One of the initial steps is to eliminate the 'Position' attribute from the CSS.

The next action involves applying float:right to circle1 and float:left to circle3.

To complete the styling, include the following code within the CSS for div h { }:

#circle1{
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color: rgba(50,205,50, 0.75);
    float: right;
}
#circle2{
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color:rgba(135,206,235, 0.75);
    float: none;
}
#circle3{
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color: rgba(220,20,60, 0.55);
    float: left;
}

div h { 
        text-align: center;
        vertical-align: middle;
}

Answer №4

Your webpage is experiencing some HTML errors, specifically with unclosed <h2> tags and an extra </div> at the end.

If you want to center each circle within the container, include the following CSS code for each circle:

  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;

You can view a demonstration on jsfiddle here: https://jsfiddle.net/jLodgoc7/

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

Event not tracking properly due to missing label in GA event firing

Seeking assistance with a project I'm currently engaged in. I have created an HTML5 video containing a playlist and encountering difficulties setting up multiple labels in GA to track each individual video play. While I found code online, adapting it ...

Encircling a particular group of cells with a border

I have completed the component with a table layout. My current challenge is figuring out how to surround a range of selected cells with a border, similar to the first cell in the group shown below: https://i.stack.imgur.com/5Euht.png I attempted using d ...

Retrieve the text content within HTML tags that come after a specific paragraph using the DOM

I am looking to extract content from HTML documents. Specifically, I need the contents of all 'p' tags that come after 'h2' tags. Here is an example of the HTML to be parsed: <h1>Lorem ipsum</h1> <p> Lorem ipsum ...

Verifying the current password and updating the hashed password

I have been struggling with creating a PHP script for updating user hashed passwords. Unfortunately, the script is not functioning as expected and no error messages are being displayed. Every time, it only shows "Your old password is incorrect" message. I ...

If every single item in an array satisfies a specific condition

I am working with a structure that looks like this: { documentGroup: { Id: 000 Children: [ { Id: 000 Status: 1 }, { Id: 000 Status: 2 ...

Resize the div blocks by scaling the button placed beneath them

I decided to modify the g-recaptcha widget to fit my specific requirements, and it is working flawlessly. However, I noticed that the ng-click button directly beneath it is no longer responsive. The reason behind this issue eludes me. Here is a snippet of ...

How do I get my navbar to change color using a JavaScript scroll function?

I've been struggling with changing the color of my navbar when it's scrolled. I have some JavaScript code at the bottom that should handle this, but for some reason, it's not working as expected. I've double-checked that my bootstrap CD ...

Removing the dividing line between columns in an Antd table: A simple step-by-step guide

Do you notice the lines dividing each table column heading in the image? I want to get rid of these lines from the table. The table component I am using is Antd table. https://ant.design/components/table#examples https://i.stack.imgur.com/Npx5G.png ...

Is it feasible to capture a screenshot of a URL by using html2canvas?

Is it possible to take a screenshot of a specific URL using html2canvas? For example, if I have the following URLs: mydomain.com/home mydomain.com/home?id=2 mydomain.com/home/2 How can I capture and display the screenshot image on another page? window ...

I require the ability to access a reference parameter with a dynamically changing name within a Vue template

<div v-for="x in 4" :class="(images[x] === null) ? 'not-removable' : 'removable'" class="img-container"> <input style="display: none" type="file" ...

Tips for preserving the contents of a list with HTML and Javascript

My latest project involves creating a website with a To-Do list feature. Users should be able to add and delete items from the list, which I achieved using JavaScript. Now, my next goal is to ensure that the current items on the list are saved when the pag ...

What is the best way to conceal a parent element with jquery?

Let's say we have the following HTML structure: <li class="fooli"> <a class="foo" href="javascript:foo(this);">anchor</a> </li> <li class="fooli"> <a class="foo" href="javascript:foo(this);">anchor</a> ...

Drop down selection triggering text change is malfunctioning

I am facing an issue with a script I wrote that is supposed to change the label name based on the selection made from a dropdown menu. The problem I am encountering is that the label does not appear when I select an option from the dropdown. I would appre ...

What is the reason for background images not loading when using `display: none`?

I'm really struggling to understand this puzzle: When the page loads, will mypic.jpg be downloaded by the browser? #test1 { display: none; } #test2 { background-image: url('http://www.dumpaday.com/wp-content/uploads/2017/01/random-pic ...

Unable to retrieve all form properties when using enctype='multipart/form-data'

Recently, my NodeJS server has been successfully uploading files to Amazon using a secret form. However, in an effort to enhance security measures, I decided to introduce a password field to the form. Unfortunately, upon doing so, I encountered an issue wh ...

Position the spinner in the center of the user's screen

I created my own spinner: '''' #spinner-bg-loading{ position: absolute; left: 50%; top: 25%; width: 80px; height: 80px; margin: -75px 0 0 -75px; border: 16px solid #FFFFFF; border-radius: 50%; border-top: 16px solid #1 ...

Identifying when a page-loading or reloading event has been canceled

When the Cancel button is pressed during page load/reload, the content of the page loads partially. Is there a more effective way to address this issue rather than displaying incomplete content? For instance, consider showing a message such as "Page load ...

The integration of single page applications and <form> elements is a powerful combination in web development

Is there still value in using a <form> element over a <div> element when developing single page applications? I understand the purpose of the <form> element for traditional form submissions with a full page reload, but in the context of ...

Steps to create a clickable drop-down arrow with a label

I've designed a vertical navigation accordion menu that allows users to hover and click on any title (label) to expand a sub-menu. However, I'm facing an issue where my drop-down arrow is not clickable, unlike the rest of the label which works f ...

Importing styles from an external URL using Angular-cli

How can I load CSS styles from an external URL? For instance, my domain is domain.eu but my site is located at sub.domain.eu. I want to use styles that are stored on the main domain (common for all sites). The example below does not work: "styles&qu ...