Aligning a div in relation to the background image

I am seeking guidance on how to center a Div Box and make it responsive relative to the background image. The div box should be contained within the white box, similar to the example shown here:

body{
  font-family: 'Roboto Mono', monospace;
  background-image: url("assets/handheld_bg_white.png");
  background-size: cover;
  background-repeat: no-repeat;
}

.overlay-box {
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%, -50%);
  z-index: 1000;
}

Appreciate any help. :)

Answer №1

It is worth noting that you have not specified whether the solution must be reliant solely on CSS. Nevertheless, if it is permissible to leverage Bootstrap 4, then achieving the desired outcome can be accomplished in the following manner:

<div class="d-flex justify-content-center align-items-center">
    <div>
        Greetings Earthlings
    </div>
</div>

Answer №2

To achieve this layout in CSS, you can utilize the properties "display: flex", "justify-content: center", and "align-items: center". Here is a demo on JSFiddle with some example code:

body{
  font-family: 'Roboto Mono', monospace;
  background-image: url("assets/handheld_bg_white.png");
  background-size: cover;
  background-repeat: no-repeat;
}

.background {
  display: flex;           
  justify-content: center; 
  align-items: center;     
  height: 50vw;
  width: 80vw;
  padding: 20px;
  margin: 20px;
  background-color: blue;
  position:relative;
  z-index: 100;
  
}

.overlay-box {
  display: flex;          
  justify-content: center; 
  align-items: center;    
  margin: 0 auto;
  height: 25vw;
  width: 25vw;
  background-color: white;
  position:relative;
  z-index: 1000;
  position:relative;
}
<div class = "background">
  <div class = "overlay-box">
  </div>
</div>

You can apply these properties to the parent div (in this case "background") to center its content - the child div ("overlay-box"). You may also include the "flex" and "center" attributes in the child for additional centering, but they are optional.

If you have a background image, you can insert it inside the parent div like so:

<div class = "background">
  <img src="/image.png" class="responsive">
  <div class = "overlay-box">
  </div>
</div>

Simply add a responsive class in your CSS to make the image adapt to the size of the parent container:

.responsive {
  width: 100%;
  height: 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

Transforming website content into HTML code

I am in need of a solution to convert a webpage into an HTML page programmatically. I have researched several websites, but the information provided only covers converting pages into PDF formats. Currently, my program involves saving a page as .html and th ...

Highlight all the written content within the text box

I'm struggling with a piece of code that is supposed to select all the text inside an input field: <input id="userName" class="form-control" type="text" name="enteredUserName" data-ng-show="vm.userNameDisplayed()" data-ng-model="vm.enteredUs ...

How can you implement a transform transition toggle when a user clicks on an element

Check out this CodePen example for a cool overlay animation: https://codepen.io/pen/MoWLrZ Experience the unique animation of two overlays as you click for the first time. However, on the second click, watch as both overlays seamlessly return to their ori ...

How to Enhance Your MUI DataGrid Rows with Borders

Trying to customize the borders of Material UI DataGrid rows to achieve a design similar to this screenshot (with some info censored): https://i.stack.imgur.com/0xRFd.png The issue I'm facing is that I'm unable to display the right border correc ...

Creating an indentation on one side of a div using CSS for a visually appealing effect

https://i.stack.imgur.com/io6m0.jpg I'm in the process of trying to create two shapes using CSS. I've been able to almost achieve the first shape on the left, but it extends out too far. As for the second shape, I'm having difficulties cre ...

The html input box's ability to handle overflow situations

Currently, I am working on creating a form that allows users to update data. <dl> <dt>Medical Needs:</dt> <dd class="med_needs" style="height:60px;overflow:auto"> <input type = "text" id="med_needs"name ="med_nee ...

add text nodes with unnecessary quotation marks around my selection list

Looking to incorporate a select list into my website using a button. I must utilize nodes for access within the DOM to retrieve its value later, so innerHTML isn't an option. Experiencing difficulty as createTextNode seems to encase my list in quota ...

What is the best way to horizontally center a div while ensuring the content within the div stays aligned?

I'm attempting to align a div in the center while maintaining the position of its contents. Check out my code snippet: <style> .wrap{ position: relative; } .character{ position: absolute; bottom: -10%; left: 0; heigh ...

I am attempting to integrate a datetimepicker onto my website, but I keep encountering an error. Can

Once upon a time, my website had a perfectly functioning datetimepicker. However, one day it suddenly stopped working. After some investigation, I realized that the JavaScript file I was referencing had been taken down. Determined to fix the issue, I visit ...

How to send parameters to Bootstrap modal using a hyperlink

I need help confirming the deletion of a datatable row using a Bootstrap modal dialog. When a user clicks on the delete link, I want a modal to appear asking for confirmation. Here is my code: <c:forEach items="${equipes}" var="equ"> ...

Tips for Keeping Sub Menu Visible Without Having to Click

I am working on an HTML navigation menu that opens submenus on click like this... $("#nav_evnavmenu > li > a").click(function () { // bind onclick if ($(this).parent().hasClass('selected')) { $("#nav_evnavmenu .selected div div ...

Changing the Appearance of the RStudio Editor

I am exploring options to customize an RStudio Editor Theme to personalize the colors. My current setup includes RStudio version 0.99.473 on Windows 10. After reviewing Any way to change colors in Rstudio to something other than default options?, which wa ...

Is it possible for me to utilize code blocks within the head content and link tags to make reference to specific themes

I am currently in the process of developing an ASP.NET application using themes. I have successfully assigned a theme to the application via the web config file. One issue I am facing is trying to reference a bookmark icon located within the themes direct ...

The margins within the div of the new tab are not applying as expected

Greetings! Currently, I am in the process of generating a dynamic form within a new tab using JavaScript. For some reason, the margin effects on the div element are not being applied correctly. Despite my attempts to set the margins using both classes and ...

What considerations should I keep in mind when changing the DOCTYPE from html 4.01 transitional to 'html'?

Firefox is telling me that my pages are being rendered in 'standards compliance mode' based on the doctype... <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> After changing it to < ...

Font consistency varies between different operating systems or machines

I have been experiencing a strange issue with the "Populaire" font that I am using. On my laptop, the font appears perfectly without any distortion on all browsers. However, when viewed on other systems (not all, but some), it becomes distorted across all ...

Utilizing Regular Expressions in Vb.net for extracting telephone numbers

I have developed a code to extract mobile numbers from a web link. I have three links in a list box and I am extracting their source code using the following code. However, when I try to use RegEx to Extract Phone Numbers, I keep getting the same number re ...

Distinction between style and styleClass on a JSF webpage

I recently began navigating a java code base that utilizes the style and styleClass keywords to customize elements within the jsf page. This particular project is built on jsf 2.2. The 'style' keyword is used for applying html attributes like: ...

Struggling to integrate an audio player specifically for mp3 files and feeling quite perplexed by the process

I'm struggling with implementing an audio player for a file sharing platform, specifically only for .mp3 files. I have successfully implemented a function for images (.jpeg and others), but I am unsure how to do the same for mp3 files. function is_au ...

What is the best way to retrieve the data submitted in the Input field by using Ajax?

One feature I'm working on involves an input field within a form where users can update their name by clicking a button. The goal is to capture this change in the input field using Ajax and update it accordingly. Here's the HTML code snippet: & ...