Ensure that the absolutely positioned element disturbs the flow of scrollable text

My webpage has a text box that displays text content. The text box is set with a maximum height and overflow property of auto for scrollability. Now, I want to add a checkbox at the bottom right corner of this text box. To achieve this, I have positioned the checkbox absolutely at the bottom right, which works as intended:

https://i.sstatic.net/8gsSU.png

Here's my question: Can I make the text content wrap around the checkbox so that it doesn't get hidden by the checkbox?

This needs to be achieved without the checkbox moving when the text is scrolled, and it should visually stay within the boundaries of the text box.

I've tried using flex layout and grid layout techniques, but they either resulted in the checkbox moving during scrolling or taking up additional space outside of the text box, which is not desirable.

There is a related question that provides a solution for non-scrollable text, suggesting the use of an invisible spacer to break the text. However, this approach doesn't work here because the spacer location would need to vary based on scrolling position.

You can find my current version where the text extends below the checkbox (undesired behavior) in this fiddle:

.text {
  white-space: pre-wrap;
  max-height: 8em;
  overflow-y: auto;
}

.read {
  display: inline;
  padding: 8px;
  position: absolute;
  bottom: -2px;
  right: 36px;
  background: white;
  border: 1px solid black;
}

.wrapper {
  position: relative;
}
<div class="wrapper">
  <div class="text">
    Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello WorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World HelloWorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World HelloWorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World HelloWorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World
  </div>
  <div class="read" id="read">
    <input type="checkbox">
    <label for="read">Read</label>
  </div>
</div>

Answer №1

This solution is a close match to the specified requirements. It utilizes a float: right property on the checkbox and employs a scroll event handler on the wrapper to adjust the checkbox position dynamically.

In order for the text to wrap around the checkbox properly and eliminate any empty space above it from view, the checkbox has been positioned at the top in this implementation.

One consideration to note is the user experience aspect of having constantly shifting text as users interact with the content.

document.querySelector('.wrapper').addEventListener('scroll', (e) => {
    e.target.querySelector('.read').style.marginTop = `${e.target.scrollTop + 10}px`;
});
.read {
  float: right;
  margin: 10px 20px 10px;
  padding: 8px;
  background: white;
  border: 1px solid black;
}

.wrapper {
  max-height: 8em;
  overflow-y: auto;
}
<div class="wrapper">
  <div class="read" id="read">
    <input type="checkbox">
    <label for="read">Read</label>
  </div>
    Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello WorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World HelloWorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World HelloWorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello World HelloWorldHello WorldHello WorldHello WorldHello World Hello World Hello
    World
</div>

Answer №2

It's a bit challenging for me to achieve this because the text needs to constantly move due to scrolling. Perhaps implementing a JavaScript script could solve it, but it seems impossible while the div remains in absolute position.

One creative workaround could be hiding the checkbox when not hovered over:

*I highlighted the checkbox red to make it more visible when inactive

const read = document.querySelector('.read');
    
    read.addEventListener('mouseover', function() {
      read.style.transition = "opacity 0.5s";
      read.style.opacity = 1;
    });
    
    read.addEventListener('mouseout', function() {
      read.style.transition = "opacity 0.5s";
      read.style.opacity = 0.15;
    });
    
    read.style.opacity = 0.1;
    read.style.transition = "opacity 0.5s";
.text {
      max-height: 10em;
      overflow-y: auto;
    }
    
    .read {
      display: inline;
      padding: 8px;
      position: absolute;
      bottom: 5px;
      right: 40px;
      background: red;
    color:#fff;
      border: 1px solid red;
    }
    
    .wrapper {
      position: relative;
    }
<div class="wrapper">
    <div class="text"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu efficitur nunc. Suspendisse commodo condimentum felis et volutpat. Phasellus ultricies justo dolor, at porttitor ligula ornare vel. In eu nulla ut justo feugiat tincidunt. Vestibulum dignissim interdum purus, pellentesque placerat magna malesuada nec. Morbi malesuada orci non purus volutpat, ut accumsan nunc accumsan. Suspendisse posuere imperdiet diam blandit interdum. Mauris at ipsum purus. Quisque a augue nec enim mattis sagittis vel quis dui. Morbi condimentum nibh tellus, nec fermentum massa pretium suscipit. Duis dapibus ultrices dolor. Vivamus condimentum nulla nec fermentum vulputate. Cras aliquet diam in nulla euismod fringilla. Ut ultricies venenatis maximus.
    Sed luctus egestas elit ut venenatis. Sed at viverra mi. Etiam nec magna vel odio dignissim consequat. Pellentesque tempor, eros a semper accumsan, ipsum odio fringilla risus, in lacinia lorem felis quis nulla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus tristique diam ...
    
    <div class="read" id="read">
        <input type="checkbox">
        <label for="read">Read</label>
    </div>
</div>

A simpler alternative could be leaving a blank space (not ideal, I know). Just adjust the CSS like this:

.text {max-height: 10em;overflow-y: auto;padding-right: 120px;}

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

The website does not display properly on larger screens, while showing a scrollbar on smaller screens

I'm encountering an issue with my website where I can't seem to find a solution no matter what I try. My goal is to ensure that my website looks consistent across all computer screen sizes. However, when viewed on larger screens, there's a ...

Struggling to stack two forms on top of each other within a Bootstrap row class

I am trying to incorporate 2 forms on a web page, one below the other. Here is what I have: <body id="Signup"> <form class="row col-lg-6"> <legend>Légende</legend> <div class="form-group"> ...

Could we customize the appearance of the saved input field data dropdown?

I'm currently working on styling a form that includes an input field. The challenge I'm facing is that the input field needs to be completely transparent. Everything works fine with the input field until I start typing, which triggers the browser ...

Enhancing the mobile menu with a feature that allows users to easily close the

I am currently designing a mobile menu for a website that I created using Wordpress with the Divi Theme. When the user clicks on a "hamburger icon", it triggers a fullscreen menu to open: mobile menu If you tap on "Termine", it will reveal a submenu: mo ...

Fixing the height of the body padding with JS or JQuery for a

I have a rather straightforward question that pertains to an issue I am facing while building a website using BS4. Specifically, my problem revolves around a fixed-top navbar with the class "fixed-top". The challenge stems from not knowing the actual heig ...

Dynamically using Jquery to input a Maximum Number not functioning as expected

After creating a basic Input and attempting to set the Max value using Jquery, the initial value was set to 77 and then decreased to 50. The example below demonstrates that it works! $(document).ready(function () { minMaxAge("#test-input",0, ...

Using Polymer and D3: A guide to transferring JSON data from element attributes to templates

As a newcomer to polymer, I wanted to integrate D3 with it. After modifying an existing Gist, I encountered some challenges. My goal is to pass JSON from a Polymer element attribute (barData) in HTML to the polymer template. The JSON is passed as a String ...

Font family not appearing correctly in Firefox due to CSS issue

Hi there, I've been struggling with this CSS code in a scss file: @font-face { font-family: 'tr-icon'; src:url('../fonts/tr-icon.eot?88d087'); src:url('../fonts/tr-icon.eot?#iefix88d087') format('embedde ...

Troubleshoot: Why Equal Height Columns in Bootstrap 4 are not functioning

My design includes two columns placed side by side, each containing different content. I assumed this was a default feature in Bootstrap 4 or at least in one of its alpha versions. I have attempted the following: .equal { display: -webkit-box; di ...

The Bug in Microsoft Edge Blocking Scope Functionality

While this code functions flawlessly in Chrome and Firefox, it encounters difficulties performing in Edge: HTML: <ul> <li class="listItems">one</li> <li class="listItems">two</li> <li class="listItems">thre ...

Mobile responsiveness malfunctioning

I recently launched my first responsive design website, which you can view here After testing the responsiveness on Chrome and FF by resizing the window, everything seemed to be working fine. However, when I checked it on my phone, the layout appeared ide ...

Guide to creating a fully clickable Card component with Material UI and React JS

Currently in my React project, I am utilizing Material UI Next. Within this framework, I have implemented the Card component that contains both an image (Card Media) and text (Card Text), with a button located below the text. My inquiry pertains to makin ...

The CSS code isn't functioning properly on both desktop and mobile views

Here is what I desire to achieve: https://i.sstatic.net/8H4fv.png The CSS code I have is: And the HTML code looks like this: <body> <div class="contanier"> <div id="rightcol">Right Section</div> <div id="content">Cont ...

FTP upload is failing to transfer the audio file programmatically

I am having an issue with uploading an .mp3 file using FTP code in C#. The file is uploaded successfully to the server, but when I try to bind it to a simple audio control or view it directly in a browser, it does not function as expected. Interestingly, m ...

AngularJS confirmation directive for deleting items

I am currently utilizing this directive for a confirmation prompt when deleting an app. However, regardless of whether I click cancel or yes, the app still gets deleted. <small class="btn" ng-click="delete_app(app.app_id)" ng-show="app.app_id" ng-con ...

Embrace the words enveloped within large quotation marks

I am seeking a way to format paragraphs with large quotation marks surrounding them. I have attempted several methods, but my line-height seems to be affected, as shown in the following image: Can anyone suggest a proper method for achieving this? (Addit ...

I am having trouble styling a pre-existing class in bootstrap using my custom CSS file

html <section id="title"> <div class="container-fluid"> <!-- Nav Bar --> <nav class="navbar navbar-expand-lg navbar-dark"> <a class=" navbar-brand">tindog</a> ...

Transforming a radio button into a checkbox while successfully saving data to a database (toggling between checked and unchecked)

I have limited experience in web development, but I recently created a webpage that allows users to input data into an SQL database. While my code is functional, I believe there's room for optimization. I pieced it together from various online resourc ...

What is the best way to ensure the parent element adjusts to fit its floated children, without exceeding the width of the window?

I am attempting to center a group of floating elements, but due to them being displayed in masonry style, I cannot set them as inline. It is clear that a container is necessary, but I have been unable to find information on how to achieve this: Please dis ...

Auto language-switch on page load

Wondering if using jQuery is the best approach to create a single French page on my predominantly English website. I want it to work across multiple browsers on pageload, currently using HTML replace. jQuery(function($) { $("body").children().each(funct ...