What is the best way to ensure that my HTML element remains fixed to the bottom of its parent element while utilizing overflow:hidden and explicitly setting the height of the parent element?

I am currently developing a Keeper App with React. In this application, users can add notes that are displayed on the screen. The specific requirement I have is to set the note's height to be implicit at 150px and enable overflow:auto so that users can scroll to view the note content. Additionally, I have included a delete button in the bottom-right corner of the note and I want it to remain fixed in that position even when the user scrolls through the note.
You can refer to the image linked below for reference:

Note: The issue arises when the user starts scrolling - the delete button moves along with the text instead of maintaining its position in the bottom-right corner.
Below is the HTML structure for the Notes:

<div className="note">
      <h1>{props.title}</h1>
      <p>{props.content}</p>
      <button onClick={deleteNoteInNote}>
        {" "}
        <DeleteIcon />{" "}
      </button>
    </div>

The CSS styling for the notes is as follows:

.note {
  background: #fff;
  border-radius: 7px;
  box-shadow: 0 2px 5px #ccc;
  padding: 10px;
  width: 240px;
  height: 140px;
  margin: 16px;
  float: left;
  overflow: auto;
  position: relative;
}
.note h1 {
  font-size: 1.1em;
  margin-bottom: 6px;
}
.note p {
  font-size: 1.1em;
  margin-bottom: 10px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.note button {
  color: #f5ba13;
  border: none;
  width: 36px;
  height: 36px;
  cursor: pointer;
  outline: none;
  position: absolute;
  right: 0px;
  bottom: 0px;
}

This issue specifically pertains to desktop viewport only, as below a width of 726px the height attribute changes to max-content instead of 150px.
To address this, I prefer using Bootstrap CDN exclusively and not the ReactBootstrap npm package.
Thank you in advance for your assistance! :D

Answer №1

To fix the issue, enclose the note div within another div. Assign a relative positioning to the new parent div. Set the button inside the child div with an absolute position.

Following these steps should resolve the problem you are experiencing.

To better visualize this solution, I have created a JSFiddle example: http://jsfiddle.net/koder613/18dt3yhL/8/

Here is the HTML(JSX) code snippet:

<div className="noteParent">
  <div className="note">
        <h1>{props.title}</h1>
        <p>{props.content}</p>
        <button onClick={deleteNoteInNote}>
          {" "}
          <DeleteIcon />{" "}
        </button>
  </div>
</div>

And here is the corresponding CSS code:

.noteParent {
    width:200px;
    height: 150px;
    position:relative;
}
.note {
    height:100%;
    overflow: auto;
    
}
.note button{
   position:absolute;
    bottom:0;
    right:1rem;
}

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

Creating a Striking Multi-Layered CSS Header

Can someone help me figure out how to add horizontal lines behind the words in this header? Here is what I have so far: https://i.sstatic.net/dN7s4.jpg. However, I want to achieve something similar to this design: https://i.sstatic.net/FZoSF.jpg. You can v ...

The pagination in React using React Query will only trigger a re-render when the window is in

Currently, I am utilizing React-Query with React and have encountered an issue with pagination. The component only renders when the window gains focus. This behavior is demonstrated in the video link below, https://i.sstatic.net/hIkFp.gif The video showc ...

Is there a way to make the content overlap the video when I scroll down?

My goal is to create a scrolling effect where the content in the div underneath slowly overlaps the video as I scroll down, similar to the design on the Minecraft homepage. ...

Unable to utilize the useState hook in TypeScript (Error: 'useState' is not recognized)

Can you identify the issue with the following code? I am receiving a warning from TypeScript when using useState import * as React, { useState } from 'react' const useForm = (callback: any | undefined) => { const [inputs, setInputs] = useS ...

"React Spring's useTransition function unexpectedly displays the initial item thrice upon rendering

Encountering an issue with useTransition, where the console log displays transitions as Menu three times instead of Menu1, Menu2, and Menu3 Ttransition.map is also showing Menu1 repeated three times const [items, setItems] = useState([{ text: 'Menu1 ...

Despite the correct value being displayed in the console.log, the Textfield is not responding to the Reducer

I am currently working on a project to create a tool that can iterate through the pupils of a school class. In order to achieve this, I have implemented a text field in a react component that displays a value: <input className="form-control" onChange={ ...

React create-react-app with Content Security Policy: Blocked execution of inline script

I recently launched a new website using the Material UI Create React Template found on GitHub. After adding a Content Security Policy, building successfully, and deploying the site, I encountered an issue where the page wouldn't display in the browse ...

JavaScript date selector allowing the selection of multiple dates

As a beginner in JavaScript, I am struggling to create a datepicker input field that allows multiple selections. Despite researching for solutions, none seem to fit my specific case. Can anyone offer guidance on how to achieve this? Your help is greatly ap ...

Set the default radio selection to be checked when it is assigned an object as the ng-value

Within my controller, I have a scoped object that initializes default values for my app as shown below: //set default values for form $scope.appText = { 'size':'normal', 'colour':'green', 'alignme ...

Achieving consistent alignment for text on a curved path

I am working on a unique "flow-builder" project and I need to achieve this specific result: https://i.sstatic.net/6TTuS.png At the moment, my current edge looks like this: https://i.sstatic.net/9ydzx.png The text on the edge is currently aligned with the ...

Next JS not triggering rerender on Mobx observer change

I've been grappling with this issue for quite some time now and I'm unsure of what steps to take next I have two separate components: Navbar.tsx import CoreButton from "../../core/CoreButton"; import {observer} from "mobx-react-lite"; impo ...

Troubleshooting extra whitespace issue while transforming Bootstrap Columns into a table layout with CSS

* { box-sizing: border-box; } .row-table { width: 100%; margin: 0!important; table-layout: fixed; } .row-t { display: table!important; } .row-tr { display: table-row!important; } [class*="col-"] { display: table-cell!important; ...

Dynamic Vimeo button integration

Is there a way to create a button over-video design that is responsive, without using JavaScript and only inline styles? <div class="video-container"> <div class="overlay"> <div class="button-co ...

Selenium test passes in Firefox but fails when run headless

I am currently using Selenium automated tests to navigate through a wizard. Each step in the wizard has a stage counter that helps me keep track of the current step. I use assertEquals to compare the hidden stage element on the screen to ensure that I am o ...

Auto Suggest: How can I display all the attributes from a JSON object in the options list using material-ui (@mui) and emphasize the corresponding text in the selected option?

Currently, I am facing a requirement where I need to display both the name and email address in the options list. However, at the moment, I am only able to render one parameter. How can I modify my code to render all the options with both name and email? ...

Exploring creative methods for incorporating images in checkboxes with CSS or potentially JavaScript

Although it may seem like a basic question, I have never encountered this particular task before. My designer is requesting something similar to this design for checkboxes (positioned on the left side with grey for checked boxes and white for unchecked). ...

Modify the color of the text to be white

How can I modify the code below to change the text color to white? The original code is inspired by this question. I am unsure about how the text color was originally set to blue. .footer-background { padding-top: 20px; padding-bottom: 20px; bac ...

Ensuring that objects remain fixed in place regardless of window resizing is a common task in web development, often achieved through a

I've been attempting to create range sliders with boxes that display the slider's current value, but I'm having trouble getting them positioned correctly when the window size changes. Despite trying various solutions found online, I resorted ...

Is it allowed to modify <li> tag only when it contains a <strong> child element?

Is it possible to use SCSS to change the background color of a li element based on whether its child is a strong tag or not? li { margin-left: 3px; margin-bottom: 3px; border: solid transparent; background-color: #fff; border-top-left-radius: ...

React button click event causes continuous rerendering when used in conjunction with mapping technique

Just starting out with React and could use some guidance! I have a table of users fetched from an API that I've displayed using mapping. To enable editing, I included an Edit button which is supposed to open a separate form for editing the user detail ...