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

How can I show/hide a div based on checkbox click on my website? It works in jsFiddle, but not on my actual site. Any suggestions?

Is there a way to show or hide a div based on a checkbox click? I've got it working in jsFiddle, but for some reason, it's not functioning properly on my website. Any thoughts on how to fix this? My goal is to offer multiple payment methods (cre ...

Where can I locate htmlWebpackPlugin.options.title in a Vue CLI 3 project or how can I configure it?

After creating my webpage using vue cli 3, I decided to add a title. Upon examining the public/index.html file, I discovered the code snippet <title><%= htmlWebpackPlugin.options.title %></title>. Can you guide me on how to change and cu ...

CSS/JS Label Positioner using Mootools, perhaps?

I have been tasked with incorporating a form into our website. It seems simple at first, but this particular form has some interesting JavaScript code in place to ensure that the label for each input field sits inside it. This is a clever feature, but unfo ...

exploring the seamless transfer of values between Thymeleaf and Spring controllers, and vice versa

As a newcomer to thymeleaf, I'm seeking guidance on how values are passed between the thymeleaf HTML and Spring controllers. Could someone please recommend some helpful tutorials for thymeleaf-spring-mvc? In the following example, I would like to und ...

Style line breaks using CSS

Can CSS(3) be used to visually or textually emphasize line breaks that were automatically added by browsers? For example, displaying a symbol like ↻ at the end of each wrapped line. It is crucial to identify where lines were wrapped in source code, as n ...

Refresh React component after form submissionIn this guide, learn how to automatically refresh a

Every time I submit the page, it keeps loading indefinitely and I have to manually refresh the page to see the input in my table. The table.js and submit.js are located in two different components. Is there a way to automatically refresh the page after sub ...

Switch the background color alternately from red to green every second

Need help with a webpage that changes the background color every second using JavaScript. The issue lies in figuring out how to correctly change the variable within the function. Here's an example of the code: <!DOCTYPE html> <html> ...

What is the best way to display the output after retrieving an array?

Database : --> product table P_id P_name P_uploadKey 1 Cemera 7365 2 Notebook 7222 3 Monitor 7355 4 Printer 7242 --> buy table B_id P_id B_nam ...

Slide the menu off to the right

Check out these images to see my progress: Main Image. When you click on the menu, everything shifts right. I've managed to set up the push menu, toggle switch menu, and main divs. Now I need help with centering the 3 lines that are clicked within th ...

How to Eliminate Lower Borders from DataGrid Component in Material UI (mui)

I've been trying to customize the spacing between rows in a MUI Data Grid Component by overriding the default bottom border, but haven't had much success. I've experimented with different approaches such as using a theme override, adding a c ...

Add a .handlebars or .hbs file to an HTML document

When visiting the emberjs.com homepage, you will find an example of a todo list using Ember.js and Handlebars.js. Within the todo list, there is an extension for a .hbs file. I am curious - what exactly is a .hbs file? And how can I include a .hbs script ...

My Testimonial Slider seems stuck in its current position and won't budge to the left

As an aspiring web designer, I took a template from CodePen and crafted a testimonial slider for my website. To seamlessly merge it with the background, I've been trying to shift the entire testimonial to the left. Despite attempting the float-left p ...

Tips for accurately aligning a relative p tag within a td

Description: I am trying to prevent text overflow in a table cell and position the text underneath a header without wrapping it under an image. I have tried setting a static width for the image and adjusting the left property, but it is not working as expe ...

Recalling a hidden div in a flexbox arrangement

I am facing a challenge with hiding and displaying two aside divs to enable a fullscreen view of my central div main. The layout is created using flexbox to assign proportions, but when I try to redisplay the elements, it disrupts the original design. Belo ...

What could be causing the loading issues with my three.js examples?

After downloading Mr. Doob's three.js project, I noticed that the examples in the folder work fine if they don't involve a model or texture. However, the ones with models or textures appear blank for some reason. This issue perplexes me as I am a ...

What is the fastest way to add an email subscription form to my website without needing to configure a database?

Would it be possible to integrate a sign-up text box on our site for users interested in receiving launch notifications? Any suggestions on the most effective method to implement this feature? EDIT: Our current use of Prefinery has limitations as we cann ...

Mobile devices not displaying background images correctly

My Wordpress website has a section with the code below, but the image is not adjusting properly on mobile phones. Can you provide some advice? .one_half_bg { width: 50%; float: left; background-position: 50% 50% !important; backgroun ...

A distinctive web page featuring an engaging image backdrop: captivating content effortlessly scrolling beneath

On my website, I have implemented a background image with a fixed transparent header. When scrolling down, I want the main content to be hidden beneath the header. To achieve this effect, I used the -webkit-mask-image property. However, this approach affec ...

The content inside a Textbox cannot be clicked on. I am seeking help with implementing JavaScript to enable it to be

As a newcomer in the world of programming, I am sharing a snippet of my JavaScript and HTML code with you. I am facing an issue where I want to enable users to start typing in a text box upon clicking on the text "User Name", without deleting any existing ...

Showing a diverse range of data types with null values in the Django admin list_display

My explanation of the title might have been unclear. I have a simple chat application where users can send text, audio, video, or images. To render these messages, I have a method that checks the message type and renders it accordingly. If the message is t ...