CSS: The element's relative size causes the background image to vanish

I am facing an issue with displaying a background-image that is 800x480 pixels. The background image appears when the element has a fixed size, but not when the element has a relative size or a max-width.

Functional CSS code:

.audio-container, .settings-container {
  max-width:800px;
  height:480px;
  position:absolute;
  background-image:url("../../public/images/Audio/I_Audio_BGK.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

CSS code causing background image to not appear:

.audio-container, .settings-container {
  width:100%;
  /* Same result with max-width */
  height:100%;
  position:absolute;
  background-image:url("../../public/images/Audio/I_Audio_BGK.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

How can I make sure the background-image shows while still having the element sizes relative to the browser window?

As requested, here are the parent DIVs:

<div ng-controller="MainController" class="main-guy">
    <div class="screen-inside">
        <div class="audio-container" ng-controller="AudioController">
        </div>
    </div>
</div>

And here are the parent DIV CSS styles:

.main-guy { 
  position:absolute;
/* Same result if width and height are set to a fixed number */
  width: 100%;
  height: 100%;
  margin: auto;
} 
.screen-inside {
  margin:auto;
  position:relative;
  height:60%;
  width:66.66%;
}

Answer №1

To make sure that your image behaves correctly within the .settings-container, you need to update the CSS from position:absolute to position:relative. This adjustment is necessary because the image is considered a child element of the .settings-container, and it should follow the positioning rules of its parent. Using position:absolute will not achieve the desired effect.

Here's a closer look at the CSS code:

.main-guy { 
  position:absolute;
  width: 100%;
  height: 100%;
  margin: auto;
  background:#999;
} 
.screen-inside {
  margin:auto;
  position:relative;
  height:60%;
  width:66.66%;
  background-color:blue;
}

.audio-container, .settings-container {
  width:100%;
  /* Same result with max-width */
  height:100%;
  background-image:url(http://reservations.life/css/images/bg-01.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  position:absolute;
}
<div ng-controller="MainController" class="main-guy">
    <div class="screen-inside">
        <div class="audio-container" ng-controller="AudioController">
        </div>
    </div>
</div>

Answer №2

When using the code below:

<div class="settings-container"></div>

Along with this CSS:

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

.settings-container {
  width: 100%;
  height: 100%;
  text-align: center;
  background-image: URL("your-image-here");
  background-repeat: no-repeat;
  background-size: cover;
}

The result will be a background that fills the entire viewport both in width and height. In order to address any issues you may encounter, it's important to consider the context of your overall layout and potentially apply height elsewhere in your HTML document.

Be cautious when using position: absolute, as its effectiveness will depend on how it aligns within your website or application.

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

Having trouble with GSAP CDN not functioning properly in your JavaScript or HTML document?

After watching a tutorial on YouTube by Gary Simon, I attempted to animate text using GSAP. Despite following the instructions meticulously and copying the CDN for GSAP and CSSRulePlugin just like him, nothing seems to be happening. Even setting my border ...

A guide on using jQuery to cycle through every row of every table on an HTML page

I am trying to figure out the correct syntax for nesting two loops within each other in jQuery. The first loop should iterate over each table in an HTML page that does not have any IDs or classes, while the second loop should go through each table row of t ...

What is the best way to implement a JSON object within an <img> src attribute?

Currently, I am in the process of creating a dynamic Vuetify navigation drawer and I have the intention of storing images in a JSON array. My main inquiry revolves around figuring out if it's feasible to extract the image attribute and incorporate it ...

The positioning of the second wrapper in absolute does not match the behavior of the first wrapper

Working with CSS can be tricky, especially when dealing with nested navigation elements. I am facing an issue where the styles that work perfectly for one dropdown menu do not function as expected for subsequent nested navigations. My objective is to disp ...

Unable to apply CSS styles to a form input using Ajax

Alright, so I've got this Ajax form that successfully fetches the data and displays it in #register_msg. However, I'm also trying to apply some styles to the input forms. Here's my Ajax code, along with the form. But for some reason, the st ...

The problem with the Bootstrap Navbar Dropdown is that it opens up within the confines of

I have created a navigation bar using Bootstrap 4 and included the .navbar-bottom class to enable scrollbar functionality when there are more menu items than the total width of the page. However, an issue has arisen where the Bootstrap 4 navbar dropdown o ...

The transparency of the navigation menu is perplexing to me. I am unclear as to the reasoning behind it. I desire a

I am currently using a pre-made WordPress theme. Recently, I incorporated particles.js as the background for my website. The issue I am facing now is that my navigation menu is blending into the same color as the background. I have tried defining a separat ...

What could be causing the .text method to not retrieve text even when it is present?

Currently experimenting with web scraping using Selenium to extract the color of a particular dress. Despite finding the text content under the 'screen-reader-text' class when inspecting the website, my attempts at fetching it result in an empty ...

Harnessing the power of lazysizes with WebP

I've been working on optimizing our site through the Google Lighthouse audit, and it's clear that images are a major focus. Right now, my main goal is to address the issue of 'Deter offscreen images' highlighted in the audit. To tackle ...

Update the color of a span in JQuery when its value equals 0

Currently, my goal is to update the color of a span tag only if its value is 0. I attempted to achieve this with the following code: $('span.number:contains("0")').css('color', '#C1C1C1'); However, this code also changes the ...

Employing CSS animations to elevate div elements

Currently, I am working on animating a table of divs and trying to achieve an effect where a new div enters and "bumps up" the existing ones. In my current setup, Message i3 is overlapping Message 2 instead of bumping it up. How can I make Messages 1 and 2 ...

CSS - Dynamic triangle separator

I've been working on creating a triangle-shaped CSS divider. Using the code snippet below, I was able to achieve the desired result: border-width: 4vw 75vw 0 23vw; However, this method feels a bit unconventional. Since you can't use percentages ...

Display information retrieved from database query in HTML

As someone who is new to PHP, I am determined to learn and improve my programming skills through it. Currently, I have incorporated the following PHP code into my webpage to fetch data from my database: <?php //code missing to retrieve my ...

How can I create a parent div with cursor-pointer and apply inline styling to make all child elements

Is there a way to apply the cursor-pointer style to an entire div with inline CSS without it affecting just the white space around the child elements? I have tried using position and z-index without success. I am unable to use an external stylesheet for t ...

Is it better to apply the font-family to the body or to the html element?

Is it more appropriate to specify the font-family CSS property on the html element or the body element? html { font-family: sans-serif; } or body { font-family: sans-serif; } I've looked into this issue online, but there doesn't seem to ...

Ways to unlock all the information windows in Google Maps

Is it possible to automatically display all info windows on the map when it is first opened, eliminating the need for users to click on markers? In other words, I would like all info windows for all markers to be shown by default when the map is opened. ...

Content within a div that is floated left

The scenario at hand is: HTML Structure: <div id="main"> <div id="a"></div> <div id="b">Some Text</div> </div> CSS Styles: #a{ float:left; width:800px; height:150px; background-color:#CCC; } ...

Using gradients in the app bar with ReactJS and Material UI is not currently supported

Recently, I decided to create my own custom appbar for a personal project and opted to use the Erica One font. As it is still in its initial stages, there isn't much code written yet. However, I've encountered two issues related to linear and ra ...

What is the best method for creating a distinctive identifier in HTML and jQuery?

Coding in HTML: @foreach($permission->childs as $subMenu) <tr> <td>{{$subMenu -> id}}</td> <td> &nbsp; &nbsp;{{$subMenu -> display_name}}</td> <td ...

Position the scroll down arrow at the center bottom of a full-screen div using WPBakery Visual Composer

I have a series of full-screen sections in Visual Composer and I am trying to add an arrow at the bottom of each one to indicate to users that they should scroll for more content. However, my attempts with absolute positioning on the divs containing the ic ...