Aligning a DIV both horizontally and vertically in the center of a background

My current challenge involves coding a layout using CSS and Bootstrap 4:

https://i.sstatic.net/1uCwG.png

In this layout, DIV1 and DIV2 are integral parts of the background and need to remain fixed in their positions - DIV1 in the upper right corner, and DIV2 in the bottom right corner.

For DIV1, I utilized the following CSS:

#DIV1 {
    position: relative;
}
#DIV1 img {
    position: absolute;
    top: 50px;
    right: 25px;
    width: 20%;
    height: auto;
}

#DIV2 {
    position: relative;
}

#DIV2 img {
    position: fixed;
    bottom:-150px;
    right:-50px;
    width: 40%;
    height: auto;
}

However, my challenge lies with DIV3, which is not part of the background and needs to overlap DIV1 and DIV2 when necessary. DIV3 should be centered both vertically and horizontally, and be responsive to different screen sizes. I'm unsure of the best approach to tackle this - should I leverage the Bootstrap grid system? Would it be beneficial to place DIV1, DIV2, and DIV3 within a grid structure?

Answer №1

Does this proposed solution meet your requirements?

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
}

img {
    width: 100%;
    height: 100%;
}

#DIV1 {
    position: absolute;
    top: 0;
    right: 0;
    width: 80px;
    height: 40%;
}

#DIV2 {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 120px;
    height: 40%;
}

#DIV3 {
    position: absolute;
    left: 0;
    right: 140px;
    top: 0;
    bottom: 0;
    margin: auto;
    width: 35%;
    height: 50%;
}
<div id="DIV1">
    <img src="https://picsum.photos/300/300/?random">
</div>
<div id="DIV2">
    <img src="https://picsum.photos/300/300/?random">
</div>
<div id="DIV3">
    <img src="https://picsum.photos/300/300/?random">
</div>

Answer №2

implement

position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);

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

I'm perplexed as to why my webpage displays differently across various browsers

Currently, I am utilizing the Yahoo CSS reset and all my CSS is based on pixel values rather than relative units like ems. Interestingly, there seems to be a discrepancy in the position of the bottom right corner of the form-containing div between Chrome a ...

Switch Button Hyperlink in HTML/CSS

I have the following code: document.addEventListener("DOMContentLoaded", function(event) { (function() { requestAnimationFrame(function() { var banner; banner = document.querySelector('.exponea-banner3'); banner.classList ...

What is the best way to erase information displayed when hovering over an element using mouseout?

Whenever the user hovers over an image, an information box appears regarding that specific image. The information inside the box changes as I move over another image, but when not hovering over any images, the information box remains visible. Unfortunately ...

How can AJAX be used to execute a PHP script that deletes a record from a database table?

Yesterday, I asked for help on how to save user-generated blog posts and I successfully tackled the database aspect of it. Now, my next goal is to be able to delete a blog post upon clicking a button with an onclick event. After researching extensively onl ...

jQuery: Revealing or concealing several divs upon selection alteration

When I populate a form, I want to display or hide multiple divs based on the OPTION selected in a DROPDOWN. Currently, my code works but the issue is that one div can be hidden or shown by multiple OPTIONS. As a result, these divs keep toggling between hi ...

Increase the size of the grid system column upon clicking on a Bootstrap icon

I am using Google Maps in a tab with Bootstrap, but the map is too small. I would like to change the Bootstrap grid system when I click on a FontAwesome icon: the first column should turn into col-md-8, and the second column should become col-md-4. Here i ...

Has the HTML attribute 'step' stopped functioning properly?

I'm currently working on an Angularjs project that primarily uses material design. I am trying to set up a timepicker using the input control with the type=time attribute. However, I want to restrict the user to select times only within a 30-minute ra ...

How can the object currently being dragged be chosen using JQueryUI Draggable?

Working with JQueryUI draggable, I am interested in applying styling to the draggable element while it is being dragged. Various attempts have been made using code like this: $(".ui-widget-content").draggable({ drag: function(event, ui) { $(this).cs ...

Extract hidden form variables using a function in PHP

Here is the JavaScript function that I have written: function GetCellValues() { var rows = document.getElementsByTagName('tr'); var str = ''; for (var c = 1 ; c < rows.length ; c++) { str += '\n&apo ...

Is it possible for you to generate an array containing only one element?

I've encountered an issue with my code. It functions correctly when the JSON data for "Customers" is in array form. However, if there is only one customer present, the code erroneously creates 11 table columns (corresponding to the number of keys in t ...

Internet Explorer having trouble with onload function

Here is a snippet of code that I am working with: <html> <head> <style> #visibilitycontent{ visibility:hidden; } </style> </head> <body onload="visibilitychange()"> <div id="header"> This is the h ...

What is the best way to create a div that horizontally scrolls and contains multiple other div elements?

I've set up a div that contains other divs, including images and text. My goal is to make the main div scrollable horizontally while aligning the inner divs using inline-block. However, when I apply: overflow-x: scroll; The main div ends up with tw ...

The intricate procedure behind applying a blur effect using CSS3 filters

MDN documentation explains that the filter blur function applies a Gaussian blur to the input image. After comparing it with OpenCV's GaussianBlur, I noticed that their effects are not identical. I am looking to achieve a similar effect using CSS3&ap ...

JQuery script fails to load in the head section while dynamically generating an HTML page with JavaScript

Using JavaScript, I have created a new window dynamically and added some HTML code to it. However, when I try to insert a script link into the HTML head, it fails to load when the window is open. <script type="text/javascript"> function newWindo ...

Calculation error causing incorrect top value display in Chrome [bug alert!]

After dynamically creating an element in jQuery and adding it to the page, I applied a class that sets its position to top:50%. Everything appeared fine at first glance, with the element positioned correctly at 50%. However, when attempting to retrieve the ...

Input field that adjusts its width based on screen size, accompanied by two buttons displayed side

I need a solution to display a text input and two buttons side by side. The buttons should take up only the space they require, while the text input should expand to fill the remaining space. It's important to have small margins between these elements ...

Is there a way to conceal the horizontal divider in the login form of the ionic HTML mobile user interface?

While designing this login form on the HTML ionic mobile UI, everything looks good except for one thing - I want to hide the horizontal line that appears between the "Forgot Password" label and the LOGIN button. Is there a way to do this? Login.html: < ...

Tips for keeping your content at the forefront of the page while incorporating fluid design elements and background hover effects

I'm facing some issues with the responsiveness of my layout. 1) I am noticing height discrepancies in my boxes when adjusting the screen width. Is there a way to resolve this using heights? 2) I have implemented a hover state using Jquery that darke ...

Upon removing an element, the button click event fails to trigger

I recently created a div that looks like this <div id="hidden"> <img src="photos/Close-2-icon.png" width="16" height="16"> <input id="add" value="Add field" type="button" > <input type='button' id=&a ...

After a single click, the functionality of jquery.nav.js seems to be malfunctioning

Encountering an error message: Uncaught TypeError: Cannot read property 'top' of undefined(…) jquery.nav.js:183 In an effort to convert my web app into a Single Page Application (SPA) using the jquery.nav.js library (available at https://githu ...