Tips for preventing an element from scrolling horizontally along with the page body

This is the structure of my HTML:

<div class='header-wrapper'>
    <div class='title'>Title</div>
</div>


<div class='some-wide-content'>
</div>

Let's imagine a scenario where the browser window is 500px wide. In this case, I want the header-wrapper to also be 500px wide with a colored background. The title should be horizontally centered within this 500px width. On the other hand, some-wide-content consists of a large table with a width of 1000px. Due to the size of the table, the body of the page extends beyond the browser window, allowing users to scroll horizontally. However, I do not want the header-wrapper to move when scrolling horizontally. Using position: fixed for header-wrapper is not feasible since it needs to move vertically along with the page.

So, how can I create an element that remains fixed horizontally even when the rest of the page scrolls horizontally?

EDIT: Currently, my workaround involves making only the some-wide-content section scrollable so that users aren't scrolling the entire page horizontally. However, this solution is not ideal as there are specific reasons why I need the entire page to scroll horizontally.

Answer №1

If you want to prevent the whole page from scrolling but still allow for scrolling within a wide content wrapper, you can try something like this:

body{
    overflow-x:hidden;
}
.header-wrapper{
    width:500px;
    background:#f00;
    color:#fff;
    text-align:center;
}
.some-wide-content{
    width: 1000px
}
.wide-wrapper{
    width:500px;
    overflow:scroll
}
<div class='header-wrapper'>
    <div class='title'>Title</div>
</div>
<div class="wide-wrapper">
    <div class='some-wide-content'>
        As an example, let's say that you have a list that contains ten items. You check off the first item. Most JavaScript frameworks would rebuild the entire list. That's ten times more work than necessary! Only one item changed, but the remaining nine get rebuilt exactly how they were before.

        Rebuilding a list is no big deal to a web browser, but modern websites can use huge amounts of DOM manipulation. Inefficient updating has become a serious problem.
    </div>
</div>

Answer №2

Introducing a scroll bar for better navigation through the table. Customize the width of sections and the entire table to suit your needs.

HTML

<html>
<head>
<style> 
div {
    width: 500px;
    height: 110px;
    border: thin solid black;
    overflow-x: overflow;
    overflow-y: hidden;
}
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>

<div>
<table width=1500px>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>Contact</th>
    <th>Contact</th>
    <th>Country</th>
    <th>Country</th>
    <th>Country</th>
    <th>Country</th>
    <th>Country</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
</table>
</div>
</body>
</html>

Hope this solution works well for your requirements.

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

When PHP displays values from the $_POST array, it appears as "1" instead of the actual element values

Why isn't PHP reading my values correctly? One Color <input type="radio" name="NumberOfColors" id="oneColor" value="oneColor" checked/><br> Two Colors <input type="radio" name="NumberOfColors" id="twoColor" value="twoColor"/><br& ...

How can I center two divs within a wrapper div without also centering all the text inside?

Is there a method other than using display: inline-block and text-align:center that allows us to center two divs inside a wrapper div? I prefer not to have my text centered. ...

Set meanmenu to a fixed position

I am currently utilizing the meanmenu plugin to create a responsive menu for mobile devices. My goal is to fix the position of the menu at the bottom of the page so that users can easily access it. However, when I set the position of the meancontainer to ...

iFrame initially loads the page and subsequently reloads it continuously in a separate tab

Encountering a strange issue with IE11 regarding a series of links: <a href="page1.html" target="iframe_name">Page #1</a> <a href="page2.html" target="iframe_name">Page #2</a> <a href="page3.html" target="iframe_name">Page #3 ...

Is there a way to fetch a random record from a MongoDb collection and exhibit all the fields of that haphazardly chosen document in HTML?

In my current project, I am fetching a random document from a MongoDB Collection and attempting to showcase all the fields of that document in HTML. Although I can successfully retrieve a random document, the issue arises when trying to display its fields ...

error being triggered due to relative positioning

On my webpage, I have a set of buttons located at the bottom. When I first open the page, all the buttons are displayed properly. However, sometimes the first three buttons become hidden mysteriously and the layout changes. I am puzzled as to what could be ...

What is the best way to include a select HTML element as an argument in an onSubmit form function call?

I am currently facing an issue where I am attempting to pass HTML elements of a form through the submit function as parameters. I have been able to successfully retrieve the nameInput element using #nameInput, but when trying to access the select element ( ...

Boundaries on Maps: A guide to verifying addresses within a boundary

User provides address on the website. If the address falls within the defined boundary, it is marked as "Eligible". If outside the boundary, labeled as "Ineligible". Are there any existing widgets or code snippets available to achieve this functio ...

Different placeholder text rendered in two text styles

Can an input box placeholder have two different styles? Here's the design I have in mind: https://i.stack.imgur.com/7OH9A.png ...

Retrieving HTML content from an AJAX request

Is it possible to query HTML data returned from an AJAX request? I attempted the following code: $.post("gethtml.php", { url: $url.val() }, function(data) { var $html = $(data), $links = $("link, script, style", $html), $body ...

Looking for assistance with a CSS selector in jQuery

I am working with a WordPress blog that has multiple pages with dropdown subpages on hover. However, I only want the main pages to have links, not the subpages. To achieve this, I am using some basic JavaScript. jQuery(document).ready(function(){ jQ ...

Need to delete the product page link on WooCommerce?

I am currently working on fixing the one-page checkout process and I need to remove a single product link from a product picture. The goal is to have only the checkout link displayed on this page, specifically within a quickview view. After trying out var ...

Show a webpage depending on specific JavaScript criteria

I have a condition in my JavaScript code that determines whether or not a user should be granted access to a specific page. However, I don't want users to be able to directly access this page even if they know the URL. This page contains both HTML and ...

Has anyone come across an XSLT that can effectively convert Apple Pages files into high-quality HTML?

When Apple’s word processor/DTP app Pages saves its files, it does so as zipped folders with an XML file containing the content and attachments like images stored as separate files. I am interested in converting this content into HTML format, using < ...

Adding class names dynamically to div elements using jQuery can enhance the interactivity and styling of a webpage

I need to dynamically add a class name to multiple divs using jQuery. Specifically, I have 10 divs in mind. <div class="sec"></div> <div class="sec"></div> <div class="sec"></div> <div class="sec"></di ...

When dealing with lengthy product names, Python Selenium may encounter difficulty retrieving the product's name

I need to extract information about all products (such as name, image, price, and link) from . However, I encountered a problem where the product card cannot display the full name of the product if it is too long, resulting in the name and price being retu ...

Centering content within a <th> tag

Hey there, I'm currently facing an issue with aligning a div inside another one. To illustrate the problem, I've set up a small fiddle which you can check out here: https://jsfiddle.net/jpq7xzoz/ The challenge arises when using the jQuery table ...

What should be done in Android when the app crashes due to incoming HTML content?

If I register using an email and password, the process is successful if the email exists within the domain. However, if a malformed email address like [email protected] is used, it returns HTML and crashes my app. How can I handle this issue? publi ...

Create engaging text animation by transitioning from horizontally displayed text to vertically displayed text with the letters standing upright using An

I'm trying to create an animation where an acronym is displayed horizontally in the middle of the screen and then flips vertically. The challenge I'm facing is rotating individual letters once they are vertical so that the text reads from top to ...

Interactive Video Effect

Seeking to create a unique video hover effect on an image where, when the mouse hovers over it, a video pops up similar to examples found on this website. I have been grappling with finding a solution for this issue over the past few months. Does anyone ...