How come the background image is not being applied using ::before?

My goal is to have a background image for the body with content placed on top of it. I am using CSS to set the background image through the body selector with the ::before pseudo-element. However, the image does not show up in the background without setting z-index: -1. This approach allows me to fix the image to the viewport without affecting scrolling behavior on the page.

body::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  background-image: url(...);
}

form {
  background: purple;
  border-radius: 5px;
  padding: 50px;
  width: 40%;
  margin: 0 auto;
}
<html>

<body>

  <form></form>

</body>

</html>

It raises the question: Why is z-index: -1 needed for the image to be visible in the background? Shouldn't the ::before pseudo-element place it automatically behind all content as intended?

Answer №1

It gets a bit tricky here. The use of ::before doesn't necessarily mean that the pseudo element will be placed directly before the form element. Instead, it indicates that the pseudo element will be created before any children of the form element. The same concept applies to ::after. Additionally, setting position: fixed; for ::before establishes a new stacking context, causing it to overlap the content of the form element (you can learn more about this here: Why does position:relative; appear to change the z-index?)

UPDATE: You may consider using background-attachment: fixed; on the body element as an alternative solution. More details can be found here: https://www.w3schools.com/cssref/pr_background-attachment.asp

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

JS use regex to set input field value

Is there a way to automatically format the input value in an HTML time picker field using regex and the replace function? For example, when a user enters numbers, the input value should be formatted accordingly based on a specific pattern. I attempted to ...

Prevent the overlap of text by controlling the positioning of a div element

Below is a simplified version of the layout I'm working with: <div style="position: relative; width:600px;"> <p>Content with unknown length, but very lengthy</p> <div>Content with unknown height</div&g ...

SassError: Variable not defined

I have a file containing variables with different themes that I need to override: _vars.scss body { $bg-color: #fff } body.theme-dark { $bg-color: #333 } In my Angular button component, I am referencing these variables: button.scss @import '_va ...

Customizing input tags

Greetings everyone, I have encountered a dilemma : <input type ="file" name ="fileUpload"> I created a file input that shows "choose a file" by default. I'm not too fond of it and would like to make it look more like a button. However, when I ...

Nested ordered lists with varied initial counter positions

In order to create an html ordered list, I am using a Nested counter. This is the code snippet that I am using: http://jsfiddle.net/Katalhama/YpgfF/ The expected output should be as follows: 0. zero 0.1 zero.one 1. one 1.1. one.one 1.2. one.two ...

How to ensure scrollbar adjusts to increasing height when using scroll:auto?

My <div> element has the style property scroll:auto. Whenever text is added to the <div> and the content height exceeds the default height, I find myself having to scroll down in order to see the latest lines of text that have been added. What ...

bootstrap 4 appears to encounter some responsiveness issues when it comes to rendering on iPhone

https://i.sstatic.net/rdOtX.png While inspecting my website on mobile using developer tools, everything looks perfect. However, when I view it on my actual iPhone 6, the layout seems off. Could this be a bug specific to the iPhone 6? Here are my meta tag ...

Unable to apply 100% height to flex child element

When it comes to creating a layout for my website, I have a simple idea in mind. I want the body content to take up at least 100% of the height of the page. This setup works perfectly on desktop screens, but when I switch to a mobile view (which changes th ...

Steps for importing a JavaScript file from Landkit into a Vue project

Currently, I am working on an interesting project and utilizing Vue with Landkit Bootstrap. However, I am encountering issues with the JavaScript behavior of the Landkit component. I usually import the following links in my index.html file, but in the new ...

reason behind text styling: thick 12 pixels height with line spacing of 25 pixels using "Arial" font

Question about CSS Size: Understanding font sizes in CSS {font: 14px/24px Arial, Helvetica, sans-serif} I've noticed some CSS code like this on the website .selector {font: bold 12px/25px "Arial";} I know that 'bold' refers to font w ...

Unique CSS Styles for Border Radius

Is there a way to achieve rounded corners on only the top left and top right, but not on the bottom two corners of a div using a specific value for -webkit-border-radius? ...

DIV will not show the Real Ajax Uploader interface

I've hit a roadblock. I recently purchased the Real Ajax Uploader Source codes and it functions perfectly on its own. However, when I attempt to display it in a DIV, things go haywire - only showing one DIV with non-functional JS code inside. There ar ...

Using webapp2 to serve a stylesheet in a non-Google App Engine environment

After successfully deploying an app using webapp2/jinja2 and a Paste server, I encountered difficulties serving static stylesheets. I managed to access static files by following this method. Additionally, I implemented a StaticFileHandler that I discovere ...

What is the best way to incorporate padding into an Angular mat tooltip?

I've been attempting to enhance the appearance of the tooltip dialog window by adding padding. While adjusting the width and background color was successful, I'm encountering difficulties when it comes to styling the padding: https://i.sstatic.ne ...

Blog Format Featuring Side-by-Side Columns

Is there a way to achieve horizontal columns that don't cut off articles and flow seamlessly into the next column? I want the columns to have varying heights, such as Post A at 60% height, followed by Post B at 40% height, then Post C at 30%, and fina ...

Tips for making a rounded text box

How can I design a circular textarea where the text conforms to the shape? Here is my attempt, but I'm unsure how to keep the text inside the circle without relying on javascript. ...

Can you explain the meanings of <div class="masthead pdng-stn1"> and <div class="phone-box wrap push" id="home"> in more detail?

While working on styling my web pages with CSS and Bootstrap, I came across a few classes like "masthead pdng-stn1" and "phone-box" in the code. Despite searching through the bootstrap.css file and all other CSS files in my folders, I couldn't find a ...

div positioned on top of additional div elements

My HTML code consists of simple div tags <div class="left">Project Name: </div> <div class="right">should have a name</div> <div>Shouldn't this be on a new line?</div> These classes are defined in a stylesheet as ...

Labels appear to float and overlap with input fields that already contain data

Having trouble with input values overlapping the label when pre-filled? https://i.sstatic.net/ECuwr.jpg If you want to ensure that the label does not overlap when the input is pre-filled and also have a smooth transition effect when the user removes the ...

The issue persists where Tailwind Inline color decorators are unable to function properly alongside CSS variables

In my current project using Tailwind CSS with CSS variables for styling, I have noticed a peculiar issue. The color previewers/decorators that usually appear when defining a color are not showing up in my class names. These previewers are quite helpful in ...