Is there a way to use a media query on a div element to check for the presence of a scroll bar?

A div has been styled with overflow-y: auto, triggering the appearance of a vertical scroll bar when the content exceeds the height of the div. This causes the content to shift horizontally to accommodate the scroll bar, resulting in misalignment with the headers positioned above the div.

No Scroll Bar:

https://i.sstatic.net/jiN3y.png

With Scroll Bar:

https://i.sstatic.net/82ZN0.png

The query is whether CSS offers a method to detect the appearance of the scroll bar and incorporate padding/margin adjustments to the right of the headers accordingly.

An attempt was made using containment (reference: https://www.w3.org/TR/css-contain-3/) as follows:


                     .drivers-box {
                        padding: 5px;
                        border: 1px solid $ra-pam-control-border-grey;
                        width: 100%;
                        height: 65px;
                        overflow-y: auto;
                        container-type: inline-size;
                    }

                    .driver-header {
                        padding: 0 6px;
                    }
                    
                    @container (min-width: 280px) {
                        .driver-header {
                            padding: 0 15px 0 6px;
                        }
                    }
<div class="drivers">
    <div class="driver-row driver-header">
        <div style="font-weight: 100">{{ 'CEBM_DRIVERS_LABEL' | translate }}</div>
        <div class="driver-row-btns">
            <i class="fas fa-chart-scatter" style="cursor: auto;" title="show in scatter plot"></i>
            <i class="fas fa-chart-line" style="cursor: auto;" title="show in time series"></i>
            <i class="fas fa-fill" style="color: red; cursor: auto" title="select series color"></i>
            <i class="fas fa-trash" style="cursor: auto;" title="delete driver"></i>
        </div>
    </div>
    <div class="drivers-box">
        <div class="driver-row" ng-repeat="driver in drivers">
            <div>{{ driver.name }}</div>
            <div class="driver-row-btns">
                <input type="checkbox" title="show in scatter plot" ng-checked="driver.showInScatterPlot" ng-click="showInScatterPlotClicked(driver.id)" />
                <input type="checkbox" title="show in time series" ng-checked="driver.showInTimeSeries" ng-click="showInTimeSeriesClicked(driver.id)"/>
                <i ng-style="{ color: driver.color }" class="fas fa-circle" title="select series color" ng-click="seriesColorClicked(driver.id)"></i>
                <i class="fas fa-trash" title="delete driver" ng-click="deleteDriverClicked(driver.id)"></i>
            </div>
        </div>
    </div>
    <button class="mini-btn" style="margin-top: 5px;" ng-click="addDriverClicked()">{{ 'CEBM_ADD_DRIVER_BTN_TEXT' | translate }}</button>
</div>

However, this implementation yielded no results, suggesting that it does not recognize the terms container-type or @container (used within an SCSS file).

https://i.sstatic.net/HdnIy.png

The question now arises - is containment being utilized correctly? Is there another approach to address this issue effectively?

Your insights are greatly appreciated!

Answer №1

Various web browsers, as well as different CSS styles, can cause the scrollbar to appear with varying widths. Relying on a method that checks for the presence of a scrollbar would therefore be unreliable.

If your data is in a tabular format, here is an example of using an HTML table where the header remains fixed while the rest of the rows scroll.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="vieWport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Title</title>
  <style>
    .container {
      height: 5em;
      overflow: auto;
      width: 20em;
    }
    
    input:checked~.container {
      height: 20em;
    }
    
    table {
      width: 100%;
    }
    
    thead {
      position: sticky;
      top: 0;
      background: white;
    }
    
    th,
    td {
      text-align: center;
    }
  </style>
</head>

<body>
  <input type="checkbox" id="toggle"><label for="toggle">Click to make the container height bigger/smaller</label>
  <div class="container">
    <table>
      <thead>
        <tr>
          <th>A</th>
          <th>B</th>
          <th>C</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>A</td>
          <td>B</td>
          <td>C</td>
        </tr>
        <tr>
          <td>A</td>
          <td>B</td>
          <td>C</td>
        </tr>
        <tr>
          <td>A</td>
          <td>B</td>
          <td>C</td>
        </tr>
        <tr>
          <td>A</td>
          <td>B</td>
          <td>C</td>
        </tr>
      </tbody>
    </table>
  </div>

</body>

</html>

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

What is the direction of auto-filling in CSS grid?

Here's an unusual query that I've been pondering. Despite checking various CSS documentation, I haven't found a clear answer yet. Take a look at this: https://i.sstatic.net/SglOl.jpg It resembles something like this grid-template-columns: ...

What is the best way to use CSS to center two blocks with space in between them?

My goal is to achieve a specific design: I want two text blocks with some space in between them aligned around the midline of the page (refer to the image). I have experimented with the float property, as well as used margin and padding to create the gap ...

Display information on a web page based on user input using HTML

I've hidden other p tags and divs using display:none, How can I make them visible after inputting my name? I'd like to type in my name and reveal all the content within the previously hidden div <form method="post"> <p> < ...

The pseudo-class for invalid input triggers CSS invalidation, even when the input field is left blank

The input property for handling invalid input is currently malfunctioning. It triggers an error even when the input field is left empty. I would like the input border to be goldenrod when it's empty, turn red if the input doesn't match the specif ...

Making the text gradually disappear at the bottom of a section using a see-through overlay, while ensuring the height remains within the boundaries of the section even

Trying to achieve a sleek fade-out effect at the end of a text section for a 'read more' indicator. I've been studying various tutorials, including this, and my current code is as follows: html <section> <p>Lorem ipsum dol ...

Is it necessary for me to watch and compile sass into css before using it in Vue?

Recently I embarked on a Vue project and decided to incorporate Sass into it. Despite configuring Sass globally, Vue doesn't seem to be recognizing the sass files. Do I need to watch and compile sass to css for it to function properly? I added Node-s ...

Customizing the appearance of checkboxes in React Material-Table filtering feature

Is there a way to modify the style of checkboxes in filtering? For instance, if I wish to adjust the dimensions of the checkbox that appears for the Birth Place field, what steps should I take? https://i.stack.imgur.com/pZLqQ.png ...

Next.js in combination with Tailwind CSS

While attempting to customize the styling for my Next.js 13 app, I encountered an error message that states: Syntax error: C:\Users\User\Documents\webdev\TicketingApp\ticketing-app\app\globals.css The text-default- ...

CSS3: What is causing this issue in FireFox?

http://jsfiddle.net/tNg2B/ It's clear that there is a noticeable difference when viewing this in Chrome compared to Firefox. The ":before" and ":after" pseudo classes seem to be malfunctioning in Firefox. Is there a way to resolve this issue? Edit: ...

The functionality of the custom file upload button is experiencing issues on Microsoft Edge

I've been working on creating a unique custom image upload button that functions perfectly in Chrome, Firefox, and Opera based on my testing. However, I'm facing an issue where it doesn't work properly in Microsoft Edge. Feel free to check ...

Header element not keeping the navbar at the bottom

My goal is to attach this navigation bar to the bottom of a header, but it's sticking to the top instead. I want the navigation to remain at the bottom of the header even when the user scrolls down. Currently, both the header and navigation are set t ...

Adjust the background color of alternate elements

I am looking to customize the background colors of every other element within the structure provided below: <div class="dets"> <div>Simple Layout</div> <div>Few Pictures/Links</div> <div>Element Uniformity</div> ...

Combining the Power of Bootstrap with Yii Framework

As a newcomer to the Yii framework, I am looking to incorporate my Bootstrap design into this platform. One issue I have encountered is related to my custom CSS. In Bootstrap, we use navbar-default for styling the navbar. I customized my navbar color by ...

The text element does not line up with the icons

As I'm working on creating my first account page header, I've run into an issue with some of the icons not aligning perfectly with the text. While advanced developers may find this task simple, as a beginner, I could really use some advice on how ...

Fixed navbar at the top changes from absolute positioning to relative when scrolling

My goal is to dynamically change the position of the navbar from fixed to absolute as the user scrolls past 600px. Initially, I used the following code: if (scroll >= 700) { $(".navbar-fixed-top").addClass("navbar-scroll"); } else { $(".navbar ...

Unable to choose the option from the modal that functions similarly to a drop-down menu

I need to choose a field that functions similarly to a dropdown box, but does not actually display as one. The select field reveals its values in a pop-up with values and sub-values that I need to select. Challenges : I'm struggling to locate the ob ...

What is the best way to arrange div elements in this particular manner using CSS?

Wanting to create a specific layout for my website by positioning some divs in a unique way and adding content to them, I have been focusing on responsive design. I am wondering if it is possible to position these divs while ensuring responsiveness. The de ...

Tips for keeping Sub Menu visible at all times

Is there a way to keep the sub menu always visible on the homepage of my website's navigation? Thank you for your help! Check out my website here ...

Enhancing the SVG font size through custom CSS styling

Working with an SVG element and utilizing CSS to adjust the font-size of the text within the SVG. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720"> <rect class="vBoxRect" width="1280" height="720" fill="none" stroke="red"& ...

Setting a uniform width for all columns in a table using ReactJS

When working with variable amounts of data displayed as columns in a table, I struggled to maintain fixed widths for each column. Despite my efforts, the columns would stretch based on available space rather than obeying the specified widths. For example, ...