Div with a fixed button that can be resized and scrolled

In the HTML below, you will find a resizable and scrollable div containing a button and additional content.

.container {
  width: 100%;
  max-width: 100%;
  height: 150px;
  position: relative;
  overflow: auto;
  resize: both;
  white-space: nowrap;
  background: lightgray;
}

.container .fixed {
  position: absolute;
  right: 5px;
  top: 5px;
}
<div class="container">
  <button class="fixed">Test</button>
  <div class="content">
    texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
  </div>
</div>

The button is intended to remain fixed at the top right corner of the div. Although it stays in place while resizing, it moves when scrolling. How can this issue be addressed?

Answer №1

Initially, I had a different understanding of how to position the button. However, the solution below has proven effective. You can adjust the size of the div while keeping the button anchored in the top right corner. [Edit] - Since the two elements are not connected, I don't think CSS alone can achieve this. Therefore, I included a small JS snippet to complete the task. It may be simple, but it gets the job done, and you can build upon it.

const resizer = new ResizeObserver((items) => {
    items.forEach(item => {
        const btn = document.getElementById("content-button-id");
        btn.style["right"] = item.target.clientHeight < item.target.scrollHeight ? "25px" : "5px";
    })
});
window.addEventListener('DOMContentLoaded', () => {
    const el = document.getElementById("content-id");
    resizer.observe(el);
});
.container {
    position: relative;
    height: 150px;
    resize: both;
    overflow: auto;
    display: flex;
    flex-flow: column;
}

.container .content {
    position: relative;
    flex: 1 1 auto;
    white-space: nowrap;
    background: lightgray;
    overflow: auto;
    z-index: 5;
}

.container .content-button {
    position: absolute;
    right: 5px;
    top: 5px;
    z-index: 10;
}
<!DOCTYPE html>
<html lang="en>
<head>
    <title>Resize Component Example</title>
</head>
<body>
    <div class="container">
        <button id="content-button-id" class="content-button">Test</button>
        <div id="content-id" class="content>
            <p>
            texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext...
            </p>
            ...
        </div>
    </div>
</body>
</html>

See some screenshots demonstrating the functionality: https://i.sstatic.net/Tp3oIWtJ.png

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

Answer №2

You need to make a change in the content class by moving the style overflow: auto inside it.

Additionally, ensure that the content class has a height of 100% if you plan on scrolling within the container div. If not, you can remove the height: 100% and scroll within the content itself.

.container {
  width: 100%;
  max-width: 100%;
  height: 150px;
  position: relative;
  resize: both;
  white-space: nowrap;
  background: lightgray;
}

.container .fixed {
  position: absolute;
  right: 5px;
  top: 5px;
}

.container .content{
  overflow: auto;
  height: 100%;
}
<div class="container">
  <button class="fixed">Test</button>
  <div class="content">
    texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext
  </div>
</div>

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

Can SCSS be used in conjunction with Less in a backwards compatible manner?

Can scss be considered backwards compatible with less? Personally, I have my doubts. One key difference is that while less uses '@' to prefix variables, scss opts for '$'. Changing the variable prefix might not be enough, as there are l ...

What is the best way to display an alert box through AJAX technology?

My code snippet is as follows: <script> function updateQty(quantity) { $.ajax({ alert(quantity); }) } </script> Here is the corresponding HTML markup: <form name="quantityForm"> <select name="quantity" id="quantity" onChan ...

Is it possible to place an open div panel between two tweets?

I'm in the process of developing a UI with Twitter feeds. I want to create a feature where a panel can be opened between two tweets, causing the tweet below to shift downwards. This function is commonly seen in tweet clients like TweetBot; as each new ...

Guide on aligning all list items to the left using React Material-UI, incorporating fontAwesome icons, and styling with Tailwind.css

I am trying to align the text of my list items to the left. Here is what I have currently: https://i.stack.imgur.com/a5o5e.png Is there a way to left-align the text in this code snippet? import React from 'react'; import PropTypes from 'p ...

What is the best way to use hasClass in a conditional statement to display text based on the content of a different div element?

Let's say we have the following HTML code: <div>New York</div> Now, we want to add another div like this: <div>Free Delivery</div> How can we achieve this using JavaScript? ...

Troubleshooting the issue with the collapse feature of the Angular UI

I am struggling to ensure my navbar starts collapsed using the code provided below. I am utilizing Angular-ui-bootstrap: navbar.directive.html: <button type="button" ng-click="isCollapsed = !isCollapsed"> <span class="sr-only">Toggle naviga ...

What could be causing my Font Awesome 6.1.1 icons to not show up after loading?

I'm currently enrolled in Brad Traversy's 50 Projects 50 Days course. On Day 26, which focuses on the Double Vertical Slider project, I noticed that the Font Awesome CDN being used is version 5.15.1, an outdated version. I have updated it to vers ...

To center a div vertically within a Bootstrap 4.1 container-fluid > row > col, you can use the following method

I'm currently working on incorporating Bootstrap 4.1's align-middle into a col nested within a row. Despite my efforts, the content within is not aligning to the middle as expected. Take a look at my code below: <body class="d-flex"> < ...

Issue with z-index in Internet Explorer version 7

Currently, I have implemented a drop-down list using jQuery that activates when the user hovers over the main menu. This functionality is working appropriately in all browsers except for IE7 and earlier versions. In an attempt to ensure the drop-down menu ...

Tips for arranging the items within the slider according to the specified direction (rtl, ltr) in the Owl Carousel slider for Angular

Is there a way to dynamically change the direction of the owl-carousel-o angular slider based on the selected language? Currently, I have set rtl: true in the owl-carousel configuration during initialization. However, when the user switches to a different ...

You need to click the form submit image twice in order to make it work

Struggling to get a standard opt-in form to work with an image submit button. The script works fine with a regular submit button, but the image button requires two clicks initially and then one click after unless the page is refreshed. I believe there mig ...

Tips for applying multiple style properties to an element using JavaScript

I have been experimenting with different versions of this code in an attempt to modify a specific element for a coding challenge, but none of them seem to successfully change multiple styling properties of the element upon clicking on a button. Would great ...

Combine object attributes to create a new column in a table

Check out this plunker I created. What is the most efficient way to merge the contents of $scope.blacklist into $scope.friends when ng-click="showColumn('Blacklist');" is triggered, and also add a new column named Coming to the table. Ng-App &am ...

Is there a way to convert this JSON object into HTML table code?

I've been working on tweaking a code snippet I came across, but I'm struggling to get it to function the way I desire. Here is the current Javascript code: function JsonUtil() { /** * Given an object, * return its type as a string. ...

Tips on adjusting the label on a datetime-local button

While using mobile browsers, I noticed that the datetime local feature includes three buttons. One of these buttons is called the Set button, but I would like to change its name to Ok. Is there a way to do this? I tried looking for solutions but couldn&apo ...

Using PHP to narrow down SQL results by date

When it comes to populating an HTML table with data from an SQL table using PHP, I have a specific method in place. Here's how I go about it: <table class="posts"> <?php $respost = mysqli_query($link,"SELECT * FROM table WHE ...

Where will the user's input be stored?

Consider the following HTML code: <div class="form-group"> <label class="col-md-3 col-xs-3 col-sm-3 control-label">Phone</label> <div class="col-md-4 col-xs-4 col-sm-4"> <input id="input ...

javascript: window.open()

I am currently using VB.NET 2005 and I have a requirement to launch a new browser window using Process.Start(). The challenge is that I need to specify the size of the browser window, for example, height:300 and width:500. Process.Start("firefox.exe", "ab ...

What could be causing the space between float and div elements?

Could someone shed light on the reason behind the minor gap between the top navigation element and the content div underneath it in this jsfiddle? When I float the navigation list items, a slight gap appears between the top navigation element and the main ...

What could be causing my links to not appear in Internet Explorer 9?

Why are my links not showing up properly in Internet Explorer 9? If you prefer not to visit the website, you can directly access the style sheet at... This issue is specific to IE 9 and does not occur in other versions of Internet Explorer. ...