Struggling with positioning a div behind other divs - padding and margin seem to have no effect

Is there a way to perfectly center #home-slider behind all other divs within #home? I've tried using margin and padding, but it stays positioned at the top left corner of its parent div. What am I missing?

View jsfiddle here

Visit live site

HTML:

<!-- language: lang-html -->

    <div id="home">
                <div id="home-slider">              
                     <img src="http://lorempixel.com/g/840/420" alt="home-slider" />
                 </div>
            <div class="logo">
                <h1><a href="#"><img src="http://lorempixel.com/232/232" alt="logo" id="logo" /></a></h1>
            </div><!-- end logo -->

            <div id="slider_mask">
                <div class="slide_container">
                    <div class="slide"><p>is where creative <i>je ne sais quoi</i> + business savvy collide.</p></div>
                    <div class="slide"><p>is the maker + doer for makers + doers</p></div>
                    <div class="slide">
                        <ul>
                        <li>No items.</li>                  </ul>
                    </div>
                </div>
                <div class="left_button"><a href="#" class="left-arrow" title="left arrow"></a></div>
                <div class="right_button"><a href="#" class="right-arrow" title="right arrow"></a></div>
            </div>

        </div><!-- end home -->

CSS:

#home-slider {
    top:0;
    height:100%;
    left:0;
    position:fixed;
    z-index:-9999;
}

#home .logo {
    padding-top: 215px;
    margin: 0 auto;
    width: 232px;
}

#home #slider_mask {
    width: 600px;
    margin: 0 auto;
    padding-top: 60px;
    position: relative;
    overflow: hidden;
}

#home #slider_mask .left_button {
    float: left;
    display: block;
    width: 23px;
    height: 25px;
    background: url(img/left-arrow.png);
    text-indent: -99999px;
}

#home #slider_mask .left_button:hover {
    background: url(img/left-arrow-hover.png);
    background-position: 0 0;
}

#home #slider_mask .slide_container {
    float: left;
    font-size: 120%;
    text-align: center;
    width: 300px;
}

#home #slider_mask .right_button {
    float: right;
    display: block;
    width: 23px;
    height: 25px;
    background: url(img/right-arrow.png);
    text-indent: -99999px;
}

#home #slider_mask .right_button:hover {
    background: url(img/right-arrow-hover.png);
    background-position: 0 0;
}

#home #slider_mask .slide {
    float: left;
    display: block;
    text-align: center;
}

Answer №1

It has come to my understanding that you desire for the #home-slider to be positioned behind all other elements, with the others centered. As adaam mentioned, there was a discrepancy between the inline styling in the HTML element defining the #home-slider div and its absolute positioning in the CSS. The inline styling took precedence over the external stylesheet.

I have also included some additional CSS attributes which can be found here.

The correct structure for the HTML would look like this:

<div id="home-slider">
    <img src="http://lorempixel.com/g/840/420" alt="home-slider" />
</div>

Answer №2

The reason for the issue was that the HTML element had inline styles defining the #home-slider div with a position of relative, while in the CSS it was set to absolute. The inline styling took precedence over the external stylesheet.

You can view an updated version here: http://jsfiddle.net/dsZSE/10/

Here is the corrected code for your #home-slider:

#home-slider{
top:0;
height:100%;
left:0;
background-image:url('http://lorempixel.com/g/840/420');
background-repeat:no-repeat;
background-size:cover;
position:fixed;
z-index:-9999;
}

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

Issue with click events not registering on iOS devices when clicking on the document body

I recently added a side navigation bar to one of my websites and encountered an issue with hiding it on body click. I attempted to resolve the problem by using the following code snippet: jQuery(document.body).on('click', function (e) { ...

Troubleshooting CSS3 @keyframes animation issues within Firefox's shadow DOM

I am currently working on creating a custom shimmer loader using CSS3 keyframes animation within the shadow DOM. While the shimmer effect displays perfectly in Chrome and Safari, it seems to be malfunctioning in Firefox. Below is a concise snippet that de ...

Can a Chrome App access a user's files on their hard drive with proper permissions?

I'm currently working on a Chrome Packaged App that includes video playback functionality. My main goal is to enable users to stream online media (such as MP4 videos) while also giving them the option to save the video to a location of their choice. ...

What is the proper way to align text based on the text above using CSS?

I have a container with a width of 50% of the viewport width, which contains some text. I want to align some other text below it to the right side. You can find an example here: https://jsfiddle.net/hadr4ytt/1/ Here is the current CSS for this: .containe ...

Creating a unique geometric shape using div elements and adding personalized touches to enhance its appearance

Currently, I am working on a project where I need to create a pointer on top of a div, similar to a tooltip. My initial approach was to try the following: https://stackblitz.com/edit/angular-ivy-fa72tz My goal is to achieve the following design: I aim ...

The model name should not be overridden if it is already being used in different fields

I have a model that I am binding on two sides. The first side is inside an html p tag, and the second side is a textarea. When I make changes to the textarea, the content inside the p tag also changes. How can I ensure that only the content in the textar ...

Mastering CSS selectors with jQuery

My desire to delve into the world of jQuery has ignited. From my perspective, jQuery simply selects the desired element and performs actions on it. However, I find the selection process closely resembling CSS selectors, a realm with which I am not well-ver ...

Disappearing PHP Session following a redirection

I'm encountering an issue with the LDAP Authentication redirection in my system. When I access Index.php, it correctly redirects me to the login page (login.php). However, after entering my credentials, it fails to redirect me back to the Index Page. ...

Viewing underlined text in iPhone with Bootstrap 5

Recently, I completed a project using Bootstrap version 5.1.3. Upon checking the mobile version on my iPhone XS, I noticed that all anchor tags (<a href="#">Something</a>) have underlined text. To address this issue, I attempted to re ...

Saving JSON data as a file on server

Currently, I am running a localhost website on my Raspberry Pi using Apache and I am seeking advice on how to export a JSON string to a file on the Raspberry Pi itself. While I do know how to export a file, I am unsure of how to send it to the Raspberry Pi ...

Looking to exclude CSS inheritance (or reset it) for specific text fields in a jQuery.mobile mobile layout

I must admit, I find jQuery Mobile to be quite impressive. However, there is a layout issue that has been bothering me. I prefer not to have my text fields styled in the default way by jQuery Mobile - with rounded corners, full width, and other changes. ...

What is the best way to style only textboxes with CSS without affecting other input types such as checkboxes?

If attribute selectors were universally supported by all browsers, we could effortlessly implement the following styling: input[type='text'] { font:bold 0.8em 'courier new',courier,monospace; } input[type='radio'] { margin:0 ...

Transform the webpage's CSS into a JavaScript npm package designed specifically for rendering HTML in React Native

Within my JavaScript code, I have a CSS string stored in a variable. @import url('https://themes.googleusercontent.com/fonts/css?kit=OPeqXG-QxW3ZD8BtmPikfA'); o { margin: 0; padding: 0 } table td, table th { padding: 0 } .c2 { ...

What is causing the background image to not display within my <div>?

I want to add a background image to only a specific part of my website, using the image as an id called "fruit." However, the image is not showing up. Here is the code I have: <div class="p-1 text-center bg-light" id="fruit"> &l ...

Maintain the jquery menu in an open state after it has been

Encountering a common issue, I am having difficulty adapting existing solutions to my unique situation. I am working with a jquery vertical accordion menu intended for navigation across two websites: home.html and research.html. Below is the relevant excer ...

Customize the appearance of the v-autocomplete component in Vuetify

How can I style the autocomplete component to only show words that begin with the letters I'm typing, rather than all words containing those letters? Also, is there a way to display the typed letters in bold without highlighting them? https://i.sstat ...

Applying CSS gradient hover effect on images of various sizes

I have a grid with 20 different pictures of various sizes created using bootstrap. Currently, I am displaying only 3 grid elements on my example page. I would like to implement a hover effect on these images similar to the gradient effect shown in this exa ...

What is the best way to create a clickable image with a box-shadow when a z-index of -1 is applied?

Is there a way to make an image clickable while still retaining the box shadow effect created using CSS3? Currently, I have set the z-index of -1 for the img tag to apply the box shadow. However, this makes the image not clickable when an href tag is added ...

How to position footer at the bottom of pages in asp.net MVC without displaying a scrollbar for shorter pages

Thanks to a useful tip I found on Stack Overflow, I was able to position the footer at the bottom of all my pages in ASP MVC. I created a custom stylesheet called "mystyles" where I defined classes for .wrapper, .footer, and .push. In the _layout file, wit ...

CSS for decorating an image with a border and transparent background

Struggling to add a border while converting a PSD to HTML? Is it possible to apply a border to an image with a transparent background? For instance, let's consider the following image: Imagine wanting to place a border around the caret in the image l ...