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

Altering the text and functionality of buttons with jQuery

A JavaScript method I have is designed to change based on the state of a button, which is determined by using indexOf("..some text.."). $('#add1').click(function(){ if($(this).text().indexOf("Add Me!")) { $.ajax({ type: & ...

Getting the text value from a table in JavaScript is a straightforward process. By using

I am working with a table displaying available hotel rooms and want to change the text color to green if the room is marked as "available." Is there a way to check the innerHTML of a td element to see if it contains the word "available"? var status = do ...

When a class and ID are dynamically applied, the click event may not fire and the CSS may not be applied

Hey everyone, I am facing an issue with declaring id and class when creating a table dynamically. I have tried multiple functions to make my click event work but without success. Can anyone help me with this? Below is the code for my dynamic table where I ...

Using ngModel to retrieve and display only the month, year, and date

Currently, I am working with an interface named Person which includes fields such as name, last name, birthday, and others. However, I am facing a confusion when it comes to the person's birthday format, as it contains some additional letters at the e ...

When scrolling, apply a CSS class to a div element once it becomes visible on the

I'm in the process of developing a timeline feature for my website, and I am facing an issue where the addClass function is being applied to all sections, even those that are not currently visible on the screen during scrolling. If you would like to ...

Move the footer to the bottom of the content

I'm struggling to position my footer directly after the last div on my page, as it's appearing lower than I'd like. Can anyone assist with this issue? Here is the code snippet: <div id="container"> <div id="header"> ...

Inserting Multiple Inputs

I have a unique application interface that is structured in the following manner: The rows within the application consist of either group headings (displayed as rows with single inputs) or ingredient forms (comprising a small input field, followed by a se ...

Is there a way to modify the background color of a button once it has been clicked, specifically using JavaScript?

I am looking to change the background color of a button that I have clicked on in my code. Do I need to use loops and conditions for this task? I attempted to access the first index, but I am unsure how to change the background color of other buttons. l ...

Mastering the art of creating the origami illusion using advanced 3D transformation techniques

I'm looking to create an origami-inspired effect on a div element, but I'm not sure where to start. To help illustrate what I mean, I will be sharing two images of a paper sheet: and . How can I achieve a similar origami effect on a div like th ...

What's the preferred formatting for a Jade Layout?

I've been struggling to convert this HTML code into Jade: <div class="textItem"> <div class="avatar"> <img src="http://wowthemes.net/demo/biscaya/img/demo/avatar.jpg" alt="avatar"> </div> "How many times ha ...

What is the proper way to utilize CSS animation delays in order to design a collapsible HTML container?

I'm trying to implement responsive CSS animation delay to create an accordion effect when a user clicks on an arrow associated with a table, all without using JavaScript and only utilizing HTML/CSS. Check out the mobile view in action here: https://w ...

What is the best way to find information in a multi-page table?

I've implemented a table with pagination and search functionality to look up data within the table. However, currently the search only works within the current page of the table rather than searching the entire dataset. Is there a way to modify the se ...

Tips for dividing the AJAX response HTML using jQuery

I have a piece of code that is functioning properly. However, I am having trouble splitting the HTML response using AJAX jQuery. I need to split it into #div1 and #div2 by using "|". Could you please provide a complete code example and explanation, as I am ...

Revise the cascading style sheets for HTML content loaded via AJAX

I have a situation where I am dynamically loading HTML content using AJAX with JQuery. After the content is loaded, I am triggering a click event on specific elements within the newly added HTML. However, I am encountering an issue when trying to set the ...

The hamburger menu in Bootstrap collapses along with the navigation items when viewed on a mobile device

How can I resolve the issue of the hamburger menu icon collapsing with the nav-items when clicked? Navbar <header> <div class="container-fluid"> <div class="row"> <div class="col-12 col-sm-12"> ...

Linking to a Different Tab without Tab Mutation with Bootstrap 3.3.5

I am facing a similar issue to the mentioned questions. I am trying to use a link to change tabs, but the problem is that the link only changes the tab content and not the active tab. The most similar question can be found at: Bootstrap linking to a tab w ...

The background image is being cropped outside the item. What steps should I take to display it properly?

I am facing an issue with positioning a background image just outside the lime container. When I position it correctly, any part of the image that extends beyond the container gets cut off. How can I display the whole image even if it is outside the conta ...

Positioning of bottom navigation bars

Is there a way to ensure that the bottom navigation bar remains fixed at the bottom of the screen, regardless of the device's screen size? position: absolute; bottom: 0; width: 100%; While this solution works, it has been observed that when th ...

Executing PHP code from a list item

On one of my website pages, I have a list that functions as a delete button. However, I am interested in making it so that when a user clicks on the delete option, a php script is triggered - similar to how a submit button works. Below is the list structu ...

Discovering the background color of see-through HTML elements using Selenium

Looking to count the characters with a specific background color. I am currently traversing through all the HTML nodes using this method: Counting inner text letters of HTML element Example HTML Page: <span style="background-color: #ffff00;"> ...