Complete alignment of several elements in a horizontal scrolling container

In the table-like container, I have elements with horizontal scrolling. Each element has the same width but potentially different heights. I want to add a button in the top-right corner of each element without it disappearing when scrolling.

I came across this solution that addresses a similar issue for a single item in a scrollable div. However, this method positions the button relative to the container, which doesn't work for multiple elements.

My current approach is as follows:

.container {
  width: 400px;
  position: relative;
  overflow-x: auto;
}
.table {
  display: table;
}
.row {
  /* 
  If the row isn't relative, all buttons stick to the
  container top.
  
  However, the buttons are now also out of view 
  due to the overflow.
  */
  position: relative;
  
  /* Just to make the content overflow */
  white-space: nowrap;
  
  /* For a clearer distinction between rows */
  padding: 0.5rem;
  border-bottom: 1px solid black;
}
.button {
  /* The button needs to be in line with the parent row */
  top: 0;
  
  right: 0;
  position: absolute;
}
<div class="container">
  <div class="table">
    <div class="row">
      <div>
        Some content so long that it for sure overflows the container, and thus a horizontal scroll is needed.
      </div>
      <button class="button">1</button>
    </div>
    <div class="row">
      <div>
        Some content so long that it for sure overflows the container, and thus a horizontal scroll is needed.
        <br>
        This one is however a bit taller than the other items.
      </div>
      <button class="button">2</button>
    </div>
    <div class="row">
      <div>
        Some content so long that it for sure overflows the container, and thus a horizontal scroll is needed.
      </div>
      <button class="button">3</button>
    </div>
  </div>
</div>

Answer ā„–1

To tackle this issue, you can utilize the CSS property position:sticky

.container {
  width: 400px;
  position: relative;
  overflow-x: auto;
}

.table {
  display: table;
}

.row {
  position: relative;
  white-space: nowrap;
  padding: 0.5rem;
  border-bottom: 1px solid black;
  display:flex;
}

.button {
  right: 0;
  margin-top:-5px;
  position: sticky;
  align-self:flex-start;
  margin-left:auto;
}
<div class="container">
  <div class="table">
    <div class="row">
      <div>
        Insert long content here that overflows the container, requiring a horizontal scroll.
      </div>
      <button class="button">1</button>
    </div>
    <div class="row">
      <div>
        Insert more lengthy content here that requires a horizontal scroll. 
        <br> This one is taller than the others.
      </div>
      <button class="button">2</button>
    </div>
    <div class="row">
      <div>
        Add text that overflows the container and needs a horizontal scroll.
      </div>
      <button class="button">3</button>
    </div>
    <div class="row">
      <div>
        Insert some content here
      </div>
      <button class="button">4</button>
    </div>
  </div>
</div>

Answer ā„–2

The issue will be resolved using the CSS property position:sticky.

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

Refresh Form (Reactive Forms)

This is the HTML code snippet: <div class="container"> <ng-template [ngIf]="userIsAuthenticated"> <form [formGroup]='form' name="test"> <div class="form-group"> <input type="text" class="form-contr ...

Issues with hover functionality in Javascript, CSS, and HTML

Seeking assistance with my JavaScript, HTML, and CSS development, I ran into an issue while trying to create a hovering function for my webpage. Despite my efforts, the links are not displaying anything when hovered over, and the divs meant for specific ho ...

Using PHP's modulus operator, you can dynamically apply CSS classes to a grid of images based on alternating patterns

Exploring the use of modulus to dynamically apply a CSS class to images displayed in a grid under different scenarios: Situation 1 In this situation, the goal is to target and apply a CSS class to middle images marked with *. Since the number of images ...

I am currently seeking a way to validate if a variable corresponds to the choice made in the dropdown menu. Any suggestions on how to accomplish this task?

I have put together a simple drop down menu. My goal is to grab the currently selected value from the drop down list, store it in a variable, and display it in the console. The ultimate objective is to compare that variable with another one to determine if ...

Creating a new dynamic page can be achieved by clicking on a dynamically generated link. Would you like to learn how to do that?

Recently, I developed a custom API using Node.js to retrieve information about blogs from Medium.com. The API currently provides: The author/main picture of the article Title A link to the article on medium.com (redundant) The entire article text in the ...

how to dynamically update a button's text using Vue.js

I have a challenge where I need to dynamically change the text of a button based on whether a value is true or false. Below is the code snippet that I have been working on: <button class="btn btn-primary table-button" type="button&quo ...

Ensure the placeholder remains front and center, growing in size as it moves vertically

.inpsearch{ border:2px solid #bbb; border-radius:14px; height:29px; padding:0 9px; } .inpsearch::-webkit-input-placeholder { color: #ccc; font-family:arial; font-size:20px; } <input type='search' class='inpsea ...

JavaScript function unable to access static file for image

I need assistance with dynamically displaying an image (a checkmark or "X") based on a variable. When I insert the image using a script directly in the HTML file, it functions correctly. However, when attempting to set the image from within the createEasyD ...

Adjusting the navigation image as it passes through various div elements during scrolling

Is it possible to dynamically change an image in the navigation bar based on the user's scroll position? For example, I want pic1 to be displayed when the page content is at the top, then switch to pic2 once the user reaches the footer, and then back ...

Numerous categories housed within various div containers

I am working with an HTML code that contains 6 different div elements: <div class="hisclass1 hisclass2 myclass hisclass3"> <div class="herclass1 herclass2"> <!-- 2nd div --> </div> </di ...

What is the correct way to nest multiple ng-if statements?

I'm currently grappling with a problem involving multiple nested ngIf directives applied to ng-template elements in Angular.js, and I've yet to find the ideal solution. While I am aware of workarounds, they are not as efficient as I would like th ...

Are you curious about the array of elements in React's carousel?

I'm currently in the process of constructing a website using React, and I have a specific challenge related to the "news" section. Within this section, I have a list of three components that represent different news items. These components are housed ...

What is the best way to ensure that the child element of a Material UI TableCell Component occupies the full height of the cell?

I am currently using MUI version 5 to create a table. In this table, there are two columns with cells that contain multiline TextFields. My goal is to have the TextFields completely fill the cell area. However, I am encountering an issue where when I start ...

Unable to modify jwplayer css styles to customize the chromecast icon

Is there a way to customize the chromecast icon within the JWPlayer skin to have a specific color? I've identified that the styles I need to change are --connected-color and disconnected-color when inspecting the element. Despite my attempts through ...

Exploring the Play Framework: Building basic HTML hyperlinks

I've been working on a website consisting of multiple HTML files and a CSS file, but now I'm looking to transition to the Play! Scala Framework. However, Iā€™m facing difficulties in understanding how to connect one HTML page to another. Current ...

``CSS: Styling a Rounded Div with a Clipped Scrollbar

I am attempting to contain a scrollbar within a div so that it remains within the rounded corners without overflowing. Take a look at the code snippet below to see the problem in action. Is there a way for the scrollbar to be fixed in its position, while ...

Is it possible to update the text within a button when hovering over it in Angular?

I'm looking to update the text displayed inside a button when hovering over it, similar to the examples in these images. I have already implemented the active state, but now I just need to change the text content. <button type="submit" ...

Incorporating a navigation bar at the top and a sidebar on the left side of the lower half of an HTML webpage

Currently utilizing materializecss and bootstrap, I am aiming to create a design that resembles: --------------------------------- --------------------------------- | | | | The objective is to split the page into two horizontal sections. The ...

Is it possible to add and delete DIVs simply by clicking on a link?

By utilizing a select dropdown list and appending the HTML code stored in the variable "code" (resembling <div>...</div>) right before the end of the div with ID mydiv using $(code).appendTo('#mydiv');. Within these dynamically added ...

Image transformed by hovering effect

I've been attempting to add a hover effect to the images in my WordPress theme. The images are displayed in a grid format, created by the featured image on the posts. The grid layout is controlled within content.php <?php /** * controls main gri ...