"Maintain element in a fixed position at the top of the page while scrolling

From the static screenshots, it's evident that an <audio> element is fixed at the top of the page upon scrolling. However, the preference is to have the element visible even before any scrolling occurs.

The current solution involves using cumbersome JavaScript to remove and append the element as a child during scrolling. Are there any alternative suggestions?

http://jsfiddle.net/bobbyrne01/772yerga/1/

html ..

<div class="header">
    <audio controls>
        Your browser does not support the audio element.
    </audio>
</div>
<div class="outer">
    <span class="banner">LOGO</span>
    <div class="header">Header</div>
</div>
<div class="content">Content</div>

css ..

.header {
    background-color: #ccc;
    width: 100%;
    position: fixed;
    top: 0;
    bottom: auto;
}
.outer {
    background-color: #fff;
    position: relative;
    height: 100px;
}
.outer .banner {
    font-size: 46px;
}
.outer .header {
    position: absolute;
    bottom: 0;
    z-index: 2;
    top: auto;
}
.content {
    height: 1500px;
    color: blue;
    background-color: #fff;
    margin-top: 100px;
}

Before scroll ..

After scroll ..

Answer №1

To ensure that the header remains on top, I adjusted the z-index to 99 while allowing the content to scroll underneath. Additionally, I included top: 50px; in the outer section to start a bit lower.

Check out the demo here: http://jsfiddle.net/v2oyjpkg/

   .header {
        background-color: #ccc;
        width: 100%;
        position: fixed;
        z-index: 99;
    }
    .outer {
        background-color: #fff;
        position: relative;
        height: 100px;
        top: 50px;
    }
    .outer .banner {
        font-size: 46px;
    }
    .outer .header {
        position: absolute;
        bottom: 0;
        z-index: 2;
        top: auto;
    }
    .content {
        height: 1500px;
        color: blue;
        background-color: #fff;
        margin-top: 100px;
    }

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

Attach to dynamically generated elements by event listeners that bind on mouseover or hover

I am looking to implement a hover effect for dynamically generated items in JavaScript. Specifically, when I hover over an icon element, I want a unique text to appear related to that icon: for (var i = 0; i < items.length; i++) { $('.items&ap ...

Make sure to include !important for the hidden property when applying inline styles in React jsx

Is there a way to include !important in the hidden property within React JSX inline style? I'm attempting to conceal the scroll bar in an Ag Grid table component as it is displayed by default. I've already attempted: ref={(nod ...

What is the best way to position images and text on a webpage using CSS?

Is there a way to properly align images and text in my code? Below is the code I am currently using. Any suggestions on how to achieve the alignment? <ul> <li> <a href=""><i class="icon-profile"></i> </a> < ...

Unveiling the Art of Styling a Reactjs Component with CSS

Recently delving into the world of Reactjs, I've created a component that I'm eager to enhance with CSS :) This component consists of a few buttons and boxes that are revealed when a button is clicked. class Days extends React.Component { con ...

Develop a custom script function for every instance of a @foreach loop

I have a challenge where I need to style automatically generated table rows by clicking on a button. Currently, my code appears as follows: @foreach($tours as $tour) <tr id="{{$tour->id}}"> <td> {{$tour->tournr}} &l ...

Adding a background image in javascript using data from a MySQL database

My current tech stack includes CodeIgniter, vanilla JavaScript, AJAX, CSS, and MySQL. I am trying to figure out how to set the background of an image that is stored in a MySQL database. While the following code is error-free and working perfectly, my cha ...

What could be causing some videos not to load on a Chrome page?

Make sure to click on the link using a webkit browser. Upon loading, you should see a 4x4 grid of videos, but in Chrome, only 1-3 videos tend to load. Interestingly, it works perfectly fine on Safari. Why is this discrepancy happening even though they a ...

Activate the function for items that are overlapping

Here is a scenario I am working on: HTML <body> <div> <p>Text</p> </div> <body> JQuery $('div').click(function(){ $(this).find('p').fadeToggle('fast'); }); $('bo ...

Exploring the Differences in CSS Styling between Web and Mobile Platforms

Recently, I launched my new website and encountered a peculiar issue. Upon clicking on Pickup Date & Time, a bootstrap datepicker pops up like it should. However, the problem arises when viewing the website on mobile devices - the datepicker appears offsc ...

The append method is not available for a div element

this is my unique code snippet for (i = 0 ; i< number ; i++){ var iDiv = document.createElement('div'); iDiv.innerHTML = "<label for=\"timeslot\" class=\"p-label-required\">Time Slot</label>" iDiv.i ...

Enhancing the appearance of the Switcher by framing it with

I've been working with the component package https://www.npmjs.com/package/react-switch-selector to create a design similar to this https://i.stack.imgur.com/voHqm.png Although I have made some progress, I'm struggling to add a border outline. H ...

What is the best way to create a fully clickable navbar item for the Bootstrap dropdown feature?

I'm struggling to make a button in a navbar fully clickable for the dropdown to open. Even when I try adding margin instead of padding, it only makes things worse. Can someone help me figure out what mistake I'm making here? Essentially, my goal ...

It seems that FireFox does not fully support IFrame Data URIs

Whenever I attempt to load a PDF Data URI into an iframe (for example, src="data:application/pdf;base64,...") in FireFox (version 18.0.2 [latest release], on OSX), it ends up triggering a download window instead. Just take a look at the website JSPDF.com ...

`Addressing issues with table cell layout for improved horizontal scrolling`

I am currently facing a challenge involving fixing the first and last column (.headcol, .lastcol) in a horizontally scrolling table, ensuring they always align to the left & right of the visible panel. My understanding was that position: absolute elements ...

Issue with line height using Material-UI grid alignment: Ensure line heights are unitless for proper alignment

Encountering a common error in ReactJs projects: Error: Material-UI: unsupported non-unitless line height with grid alignment. Use unitless line heights instead. A snippet of the code where the error occurs: const breakpoints = createBreakpoints({}); le ...

Ways to prompt a specific text value to generate varied responses

Whenever I try to input the letter "h", I expect a specific value in return but for some reason, it's not working as intended. Despite my best efforts to troubleshoot the issue, I have been unsuccessful in finding a solution. It's frustrating bec ...

Achieving proper HTML element order?

Here is the HTML page I am working with: Click here to view the code on jsfiddle <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Extended UI</title> <style type="text/css"> .header{ padding-rig ...

Determined Worth of Chosen <Datalist> Option

Currently, I am utilizing an <input type='text'> Element in conjunction with a <datalist> to suggest user names for a form. The functionality is working correctly and all my users are displayed. However, upon form submission, I need ...

Reply to changes in the window size *prior to* adjusting the layout

Take a look at the "pixel pipeline" concept illustrated with a vibrant diagram on this page. I am currently working on resizing an element (let's say, a span) dynamically when the browser window is resized. I have implemented this using window.onresi ...

Converting a table-based layout to Bootstrap using the "row" and "col-*-*" classes

My current project is built on a table-layout structure, but now the requirements have changed and we need to make it responsive. To achieve this, we have decided to use Bootstrap. How can I convert the existing tables into a Bootstrap grid layout without ...