Ensure that the top of a div stays in place as the user scrolls, and spans the entire width of the div

My goal is to create a sticky or fixed position header inside a div with a list, but I'm facing an issue where it only sticks to the width of the text. If I expand the width to 100%, it takes up the size of the entire page:

import styled from 'styled-components';

const ScrollableContainer = styled.div`
overflow-y: visible;
overflow-x: hidden;
height: 700px;
text-align: left;
justify-content: space-between;
cursor: auto;
`;

const TableHeader = styled.header`
position: fixed;
width: 100%;
height: 40px;
background-color: white;
z-index: 9;
line-height: 40px;
`;
export const TestView= () => {
     return (       
<ScrollableContainer>
            <TableHeader>
                Text here
            </TableHeader>
// some list goes 
        </ScrollableContainer >
)
};

I am unable to dynamically set the width value since the div needs to adjust according to the screen size.

Answer №1

When you use the 'position: fixed' property, the element's position is relative to the viewport. By setting its width to 100%, it will span the entire page.

You can specify 'position: inherit;' to make the element inherit its parent's width.

const TableHeader = styled.header`
position: fixed;
width: inherit;
height: 40px;
background-color: white;
z-index: 9;
line-height: 40px;
`;

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

Height of the image is being boosted while the width remains consistent

When working on a responsive page layout, I encountered an issue with increasing the height of an image while keeping the width constant. Modifying the width of the image reflects changes, but adjusting the height does not seem to make any difference. I t ...

What methods can be used to keep divs from breaking onto separate lines?

My approach involves using divs to mimic a table structure. Below is the header section of this table: <div id="table-header"> <div id="header-row"> <div class="rate-rule-column s-boarder"> ...

Is it possible to update a RSC with a stored value?

In my React Server Component, I can easily access the initial values and dispatch actions to the Redux store. The initial value is currently set to 10. // store/counterSlice.ts const initialState: CounterState = { value: 10, }; // app/serverComponent.t ...

How can I adjust the appearance of an HTML tag within an Angular component?

I have created an Angular component with the following definition: import { Component, Input, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'rdc-dynamic-icon-button', templateUrl: './dynamic-icon-button. ...

Automatically populating additional fields in JavaScript/React by taking information from the initial field

1. I am working with an array called "listOfPaysIndexes", which contains 12 indexes. My goal is to use this array to iterate through and display 12 DateInput fields. 2. Each of these fields can be clicked on to select a date. 3. Once a date is chosen in ...

Ways to align text on the left within a centered format

It seems like there is something missing here. I am struggling to align text to the left in a centered div. Below is an example of my final work: <div class="grid-row"> <img src="http://www.fununlimitedsports.com/wp-content/images/istock ...

Clicking on the element will not cause the body to scroll

For some reason, I need to keep the onclick function, but I'm struggling to make it work as intended. What I want is for the body to slide to the next logo when the previous logo is clicked. This is a simplified version of what I have: <div class ...

Designing HR components using Bootstrap and CSS

Trying to integrate Bootstrap into my portfolio website, but struggling with styling the hr elements. They keep coming out like this: https://i.sstatic.net/IApoi.png They also refuse to center align, always sticking to the left. No idea why this is happe ...

The slider components have an endless loop feature that only runs once before coming to a

I'm working on creating an infinite slider that loops continuously without the need for navigation buttons. Instead, I plan to allow users to control the slider using touch gestures (although this functionality is not yet implemented in the code snipp ...

Is it possible to integrate HTML into PHP code without using echo statement?

On my index page, I wanted PHP to verify if the user is logged in when it loads. If the user is logged in, the webpage content would be displayed. If not, it would show the login page for the user. Here is the PHP code for the check: <?php if ($_SESSI ...

What is causing the extra whitespace on the right side with container-fluid?

Could someone assist me with an issue I'm experiencing when using the container-fluid in Bootstrap? It seems to be leaving some space on the right side of my web page, and as a newcomer to Bootstrap, any help would be greatly appreciated. <link ...

Issue with background image positioning in IE 7 while scrolling

I am struggling with creating nested divs where the middle one has a fixed background image that remains static while the rest of the content scrolls. My solution works in Firefox, Chrome, and Opera but I'm encountering issues in IE7. Despite knowing ...

I can view the data retrieved in the console, but I am having difficulty displaying it on the screen

I have successfully retrieved data from the API, as indicated by the output in my console. However, I am facing issues in displaying this data on the webpage due to a potential error in my code structure. Despite this, I can confirm that the fetched data ...

Use of image tag inside the title attribute

After coming across the question on how to add an image tag inside the title attribute of an anchor tag and finding only one answer claiming it's impossible, I stumbled upon a page where it was actually done: I decided to view the source of the page ...

Looking to tilt text at an angle inside a box? CSS can help achieve that effect!

Hey there, is it possible to achieve this with CSS or JavaScript? I require it for a Birt report. ...

Altering a specific attribute of an object in Redux and React

Hey there, I've been working on updating the value of my "duration" property within my reducer function. It seems like everything is working fine when I console.log the individual "collab", but I'm having trouble correctly updating my newState va ...

What is the best way to hide certain buttons from specific users in Angular using the If condition?

I am facing an issue with my array of blocked_users, where I need to hide certain buttons if a user is in the blocked_users list. Below is the code snippet from my angualr.component.html: <div *ngIf="!(userId in event.blocked_users)"> ...

Is it possible to rotate an image with a random angle when hovering in Angular?

I'm currently working on a photo gallery project and my goal is to have the images rotate when hovered over. However, I am experiencing difficulties in passing values from TypeScript into the CSS. HTML <div class="back"> <div cl ...

Internet Explorer does not support the use of a:focus

Unfortunately, the issue with a:focus not working in IE is still unresolved. I have been searching for a solution, but have not been able to find one yet. <div class="city-selection"> <ul> ...

Creating a layout of <video> components in vue.js, complete with the ability to rearrange and resize them through drag and drop functionality

Despite trying numerous libraries and Vue.js plugins, I have yet to find one that meets all of my requirements. I am in need of creating a grid of video HTML elements that are draggable and resizable with a consistent aspect ratio of 16:9. Additionally, I ...