Styling a DIV with a Background Image using CSS

I've hit a roadblock trying to prevent a background image from extending beyond its DIV. I've had to resort to some less than ideal methods.

.animation span {
    position: absolute;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: -999;
    animation-name: fade;
    animation-iteration-count: infinite;
    animation-duration: 40s;
    background-size: cover cover;
    background-repeat: no-repeat;
    background-position: center center;
}

...

<body>
  <div id='banner'>
    <div class="animation">
      <span id="f4"></span>
      <span id="f3"></span>
      <span id="f2"></span>
      <span id="f1"></span>
    </div>
    <div id="title">
      <h1>Silly Webpage Banner</h1>
    </div>
  </div>
  <div id="content" style="background-color:white;">
    Content
  </div>
</body>

Here is a fiddle

I ended up applying width, height, and top properties to the animation class just to display the image properly. Without a z-index, the content DIV ends up behind. Ideally, I would like it to adhere to background-size and background-position, but I can't seem to make it work.

Answer №1

Is your design concept leaning more towards this approach? Where the background animation elements are all encapsulated within a defined banner container with specific width and height?

html,
body {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}
#banner {
  position: relative;
  display: block;
  height: 4rem;
  width: 100%;
  overflow: hidden;
}
#banner h1 {
  position: absolute;
  /* Position banner title */
  top:1rem;
  left:1rem;
  padding:0;
  margin:0;
}
#main {
  position: relative;
}
.animation div {
  position: absolute;
  width: 100%;
  height: 100%;
  animation-name: fade;
  animation-iteration-count: infinite;
  animation-duration: 40s;
  background-size: cover cover;
  background-repeat: no-repeat;
  background-position: center center;
}
#f1 {
  animation-delay: -0s;
  background-image: url('https://newevolutiondesigns.com/images/freebies/white-wallpaper-14.jpg');
}
#f2 {
  animation-delay: -10s;
  background-image: url('http://hdwallpaperbackgrounds.net/wp-content/uploads/2015/08/White-Background-Wallpapers-3D-Ball.jpg');
}
#f3 {
  animation-delay: -20s;
  background-image: url('http://dlc.middcreate.net/wp-content/uploads/2013/09/quality-photo-for-desktop-white-bubbles-widescreen-picture-and-image-background-wallpaper-with-high-resolution.jpg');
}
#f4 {
  animation-delay: -30s;
  background-image: url('http://hdpic.org/wp-content/uploads/2015/02/Pattern-Black-and-White-Amazing-Background-Cool-Background.jpg');
}
@keyframes fade {
  0% {
    opacity: 1;
  }
  17% {
    opacity: 1;
  }
  25% {
    opacity: 0;
  }
  92% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<!DOCTYPE HTML>

<body>
  <div id='banner'>
    <div class="animation">
      <div id="f4"></div>
      <div id="f3"></div>
      <div id="f2"></div>
      <div id="f1"></div>
    </div>
    <h1>Silly Webpage Banner</h1>
  </div>
  <div id="main">
    <div id="title">
    </div>
    <div id="content" style="background-color:white;">
      Content
    </div>
  </div>

</body>

Answer №2

According to the positioning explanation on MDN's CSS position guide:

When an element is positioned relatively, it remains part of the normal document flow. On the other hand, absolute positioning removes the element from the flow, causing it not to take up space in relation to other elements. The absolutely positioned element is placed relative to its closest positioned ancestor (non-static). If no such ancestor exists, the initial container is used as reference.

This means that a background div set to absolute positioning is placed above other elements, thereby concealing them - hence the necessity for utilizing the z-index property.

Furthermore, since an absolutely positioned element does not interact with its parent element, you must explicitly define its size and location.

Answer №3

Curious about your intentions with this - can you provide more context?

.motion-effect {
     height: custom_height_value;
     position: absolute;
     width: custom_width_value;
}

Offering a suggestion that could potentially clarify things.

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

Try utilizing querySelectorAll() to target the second item in the list

As I delve into the world of HTML and JS, I came across the document.querySelectorAll() API. It allows me to target document.querySelectorAll('#example-container li:first-child'); to select the first child within a list with the ID 'exampl ...

Creating a uniform mobile version for all smartphones on a website

My website, www.sauleezy.altervista.org, is experiencing an issue that I have been unable to resolve. I have set up two different .css files for desktop (style.css) and mobile (handheld.css). In my index file, I included the following code: <meta name= ...

Switch up the text inside a div using jQuery

I am developing a small grocery shopping application. My goal is to create a page where users can view the contents of their shopping cart by clicking a button. I have designed a template that displays the contents in a white space. The idea is to store ...

Adjusting the size of the post title's font

Looking to decrease the font size of the latest post's title and keep it centered on my Jekyll based blog. I attempted to adjust https://github.com/x0v/x0v.github.io/blob/master/_sass/atoms/_feature-title.scss by adding font-size: 45px !important; f ...

What could be causing the border-sizing property with a value of "border-box" to not

I am attempting to animate the width of the div, starting from 0. However, the padding I have set is preventing the div from being less than that padding. I have tried using border-sizing: border-box;, but it has not solved the issue. What could I be doin ...

Dash - Add personalized HTML component

I am currently developing a Dash application in Python to showcase the results of some Topic Analysis that I have conducted. In order to visualize the topics, I used a tool called pyLDAvis which provides a nice visualization output saved as an html file na ...

Function fails to execute after drop-down menu selection

I set up a double drop-down feature and implemented my function calling code. However, after choosing an option from the drop-down menus, the function is not getting executed. The purpose of the code is to trigger a specific function based on the selectio ...

Generating a pop-up window upon clicking a specific word within the text

I've been tasked with converting a textbook into a website for my teacher, and I've encountered an issue. Within a div element, there is text in a foreign language. Certain words need to be clickable, triggering a pop-up box with their translatio ...

jQuery and ajax method are failing to insert data into the database

I am having trouble inserting data into the database using jQuery and Ajax. Can someone please assist me in figuring out if I'm doing something wrong? Here is the code snippet: form.html <!DOCTYPE html> <html> <head> & ...

Trouble with displaying vertical scroll bar in React app when height is set to 100%

I am developing a React application where I need the content to always occupy at least 100% of the height to style the background accordingly. I have attempted to achieve this by setting the CSS height to 100% at the top level, which works fine. However, t ...

Combining cells for certain entries in an Angular data table

Just starting to learn angular, and here's the scenario I'm dealing with: I have a table consisting of 10 columns. Let's say column 4 contains different status categories like children, teen, young, adult, and senior. When displaying all ...

Is the image too tiny for the parallax effect?

It seems like the image at the bottom of the parallax theme is showing some spacing at full height. I initially thought the image was too small, but upon comparing it to the one in the top widget area, its height is actually larger. Should I look for a big ...

The outcome of the Python web crawling did not meet our expectations

After using Selenium to scrape web data, I encountered an issue where it only displayed 96 out of 346 products instead of the expected 346. Does anyone have suggestions on how to fix this problem? The crawling code is placed right after the loop for clicki ...

Tips on saving a form submit button data to localStorage

As a newcomer, I am on a quest to make this function properly. My goal is to create a form where the submit button saves data to the localStorage. Here's what I have tried thus far. <script> function storeRecipe() { localStorage.setItem($(" ...

What is the best way to transform one svg into a different svg using Anime.js?

I'm facing an issue where two of my SVGs have the same number of points, but when I try to play the animation, they seem to be too close together. The animation suddenly jumps and creates a weird shape before transitioning from the first SVG to the se ...

Are there any simpler ways in HTML to navigate up directories without using the ../ repetition?

Seeking advice on a simple issue I'm facing regarding the organization of my web images and CSS files. Currently, all my assets are located in the root directory. However, I also have a catalogue folder with several subcategories within it. Navigating ...

Is it possible to set the <li> background to full width and center it vertically?

Getting straight to the point: I have a vertical menu bar and I am looking to have an <li> element with a full background color and vertical alignment. I came across a suggestion to add vertical-align: middle;, but that doesn't seem to work wel ...

What are the steps to shift columns to the left within a table?

I need to adjust the alignment of the columns and also create a fixed size space between them. Currently, it appears like this: https://i.sstatic.net/F7Rqk.png My desired outcome is something similar to: https://i.sstatic.net/ALKa9.png CSS Code: .tabl ...

Sequence of HTML elements arranged in a stack

Recently, I came across a useful jQuery tutorial called "jQuery for Absolute Beginners: Day 8". In this tutorial, there is an interesting code snippet that caught my attention: $(function() { $('.wrap').hover(function() { $(this).childre ...

What could be causing my page's wrapper to shrink upon logging in?

After logging out, the background of my page wrapper expands to cover 100% of the page. However, once you log back in, it shrinks and adjusts to the width of the member bar... TO TEST USER ACCOUNT BELOW: USERNAME: TEST PASSWORD: TEST123 HTML FOR LOGGED ...