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:

With Scroll Bar:

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).

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 best way to transfer a table with multiple rows in a single column to an Excel spreadsheet while maintaining the headers on a horizontal plane

Currently, I am in the process of gathering data. The data is being sent to me in a word format and organized in a vertical columned table with different headers and alternating data. I am looking for a way to export this data from Word into an Excel spre ...

What is the best way to create a border that surrounds a collection of pictures?

I'm currently facing a challenge in getting a border to perfectly fit around a collection of images. The issue can be observed in this Jsfiddle I shared, where the border aligns with the top and left edges, but not with the bottom and right edges. Her ...

When utilizing the tab key on Chrome, the iFrame fails to scroll

We are currently facing an issue with our web application that is embedded inside an iFrame. On one of our HTML pages, there are numerous textboxes with a large amount of content, requiring users to scroll down to navigate through it all. When using the ...

Enhancing User Experience with Animated jQuery Progress Bar

I came across a cool tutorial on animating progress bars: The only issue was that the progress bar didn't utilize jquery, and there wasn't information on linking multiple buttons to it. After some searching, I found another tutorial that address ...

Get rid of the .php extension in the URL completely

Lately, I've been experimenting a lot with the .php extension. I successfully used mod_rewrite (via .htaccess) to redirect from www.example.com/example.php to www.exmaple.com/example. Everything is running smoothly. However, I noticed that even though ...

Is there a way to spin the picture but keep the container still?

Is there a way to animate the rotation of the gradient color without rotating the rhombus? I attempted using transform: rotate, but it ended up rotating the entire shape. Any suggestions on how to achieve this effect? .box { position: absolute; top ...

What is the best way to ensure the header spans the entire width of a webpage using the display: flex; property and flex-direction: column styling within the body element

Click here for the CSS code snippet Hello everyone, this is my very first query on Stack Overflow. I know it might be a simple question but it's my first time posting here so kindly bear with me. Below is the CSS code snippet provided: *{ ...

Adjusting the content of mat-cards to fill in blank spaces

I have encountered an issue with the alignment in a list using mat-card. Here is my current layout: https://i.stack.imgur.com/VKSw4.jpg Here is the desired layout: https://i.stack.imgur.com/8jsiX.jpg The problem arises when the size of content inside a ...

Tips on choosing a button and applying custom styles with emotion styles in MUI

Looking to apply a margin-right to my button. Currently utilizing mui 5 with Button variant='contained'. I created a custom CSS style using the styled component in mui and targeted the Box. const Wrapper = styled(Box)({ display: 'flex&ap ...

Position a div to float in the top right corner without overlapping its sibling header

Is there a way to position a div in the top right corner of a section without overlapping the text of a h1? This is the HTML code: <section> <h1>some long long long long header, a whole line, 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6</h1> < ...

Align the checkbox input in the center of a flexbox container

I am in the process of creating a side menu with filters, and I need to design a row that includes an input checkbox along with some accompanying text. However, I am facing an issue where the checkbox does not center properly within the flex container when ...

Creating your own unique CSS animation or transition timing function is a great way to add a

When it comes to CSS timing functions, they often appear to exist between two specific points: https://i.stack.imgur.com/qyvON.png Even with the option of using custom bezier curves, the curve created can only transition from one point to another. https ...

Is it possible to disable the pointer event on the parent element only in CSS

Here's the structure I'm working with: <div class='offline'>some text <button class='delete'>Delete</button></div> I want to disable the div using pointer-events: none, but still keep the button activ ...

Utilizing the empty space to the right of an image in HTML

What could be causing the empty space to the right of my image? This is how the HTML file appears: ... <body> <div class="image"> <img src="image.jpg" alt="Picture here" id="image" align="middle"></img> </div> ...

What is the best way to swap out an icon based on the chosen option?

Here is my code: $('#mySelect').on('change', function() { var value = $(this).val(); $(".title").html(value); }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href=" ...

Encountering an issue with multi ./src/styles.scss during the deployment of my Angular application on Heroku

My attempt to deploy my Angular app on Heroku is resulting in an unusual error. I've experimented with changing the styles path in angular.json, but it hasn't resolved the issue. I suspect it's a path problem, yet I can't seem to corre ...

Outlook for Windows automatically sets the default font for HTML emails to Times New Roman

My email design looks great in Mac Outlook, but Windows Outlook is changing the font to Times New Roman. Below is the code for the email template. Can someone help me figure out what I'm missing? <style style="-ms-text-size-adjust: 100%;&q ...

attempting to display an element using jQuery that belongs to the identical class

I have multiple buttons with the class .modif, each with a different title attribute such as title="one". All these buttons are associated with boxes that share the class .box_slide. However, I am trying to display only the box that also has the additional ...

Is there a way to make my red div switch its background color from red to green when I press the swap button?

How can I make the red div change its background color to toggle between red and green when I click on the swap button in the following code? $(document).ready(onReady); var numberOfClicks = 0; function onReady() { console.log('inside on ready ...

I'm searching for a universal guidebook on creating web page layouts

After 5 years of creating webpages, I have realized that my sites tend to have a nostalgic 1995 internet vibe. Despite being a C# programmer with knowledge in HTML, JavaScript, and CSS, my design skills could use some improvement. Is there a quick referenc ...