What is the best way to ensure a div remains fixed in one spot on the screen at all times?

I am struggling with positioning a div containing a textarea and a submit button on my screen. I want the div to stay in a fixed location regardless of any actions that may occur.

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

Currently, the textarea moves up or down every time I send a message, causing it to shift from its original position.

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

My goal is to keep this textarea at the bottom of the screen without being affected by incoming messages.

HTML

  <div className='messageText'>
    <textarea  
      name="" 
      id="" 
      cols="30" 
      rows="10"  
      placeholder='Digite sua mensagem...'
      value={message}
      onChange={(e) => setMessage(e.target.value)}
     ></textarea>
    <button onClick={handleSendMessage}>Enviar</button>
  </div>

CSS

.messageText {
  align-items: center;
  text-align: center;
  justify-content: center;
  display: flex;
  margin-top: 4vh;
}

.messageText textarea {
 border-radius: 6px;
 border: none;
 outline: none;
 width: 50%;
 height: 10vh;
 background-color: #3d3939;
 color: #fff; 
}

.messageText button {
  height: 10vh;
  width: 10vh;
  background-color: #ff914d;
  color: #fff;
  outline: none;
  border: none;
  cursor: pointer;
  letter-spacing: 0.1rem;
}

Answer №1

By utilizing the CSS property position: fixed, you can accomplish your desired layout.

footer {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  width: 100%;
  bottom: 0;
  left: 0;
  height: 100px;
}

.messageText {
  align-items: center;
  text-align: center;
  justify-content: center;
  display: flex;
}

.messageText textarea {
  border-radius: 6px;
  border: none;
  outline: none;
  height: 10vh;
  background-color: #3d3939;
  color: #fff;
}

.messageText button {
  height: 10vh;
  width: 10vh;
  background-color: #ff914d;
  color: #fff;
  outline: none;
  border: none;
  cursor: pointer;
  letter-spacing: 0.1rem;
}
<footer>
  <div class="messageText">
    <textarea name="" id="" cols="30" rows="10" placeholder="Enter Your Message..."></textarea>
    <button>Send</button>
  </div>
</footer>

Answer №2

@Bernard Borg has already provided a response to the question ahead of me. Nevertheless, I will still share my answer for added clarity.

Following his advice to utilize position: fixed;, you can easily achieve the desired result.

Please disregard the demo text used for scrolling the div.

.TextAreaDiv{
    position: fixed;
    bottom: 0;
}
.MessageArea{
    overflow: scroll;
    height: 150px;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adcfc2c2d9ded9dfccdded98839f839e">[email protected]</a>/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="77151818030403051607374259455944">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
<div class="MessageArea">
<h3>quotes</h3>
 <blockquote>
   This is a lengthy quotation
   WWF, for half a century, has been devoted to preserving the future of our environment. The foremost conservation organization globally, WWF operates in 100 nations and receives backing from 1.2 million members in the U.S. and nearly 5 million worldwide.
   <br>
   <q>You can insert a short quote within</q>
</blockquote> 
<q>Here's a brief quote</q>
<hr>
<h3>preformatted</h3>
 <pre>
Text enclosed in the pre element
is exhibited using a fixed-width
font, maintaining both      spaces and
line breaks
</pre> 
<hr>
<h3>abbreviations</h3>
<p>
  The HTML &lt;abbr&gt; tag signifies an abbreviation and also offers an optional detailed explanation. <br>
  Beneath is a demonstration <br>
  Founded in 1948, <abbr title="World Health Organization">WHO</abbr> was created. </p>
<hr>
<h3>address</h3>
<p>A display address using the &lt;address&gt; tag
  <address>
    Crafted by <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="720517101f130106170032170a131f021e175c111d1f">[email protected]</a>">Jon Doe</a>.<br>
    Visit us at:<br>
    Example.com<br>
    Box 564, Disneyland<br>
    USA
  </address> 
<hr>
<h3>citation</h3>
 <p><cite>The Scream</cite> by Edward Munch. Created in 1893.</p> 
<hr>
</div>
<div class="TextAreaDiv">

<div class="input-group mb-3">
   <input type="text" class="form-control" placeholder="Send message" aria-label="Send message" aria-describedby="button-send">
      <div class="input-group-append">
          <button class="btn btn-primary" type="button" id="button-send">Send</button>
      </div>
 </div>
 </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

Tips on fetching data from a different webpage via ajax

Hello, I am new to programming. I am looking for a way to update the content of the current PHP page with content from another PHP page using AJAX without needing to refresh the page. The content that needs to be replaced is located within div elements. ...

Utilizing pop-up info boxes

After thoroughly reviewing the documentation on Imagemapster's website and browsing several posts, I am still struggling to get the tooltips working correctly. I attempted to share my HTML and JS code on jsfiddle.com without success, so I have provide ...

Tips for designing a navbar specific to a profile section

I am currently working on an angular application with a navigation bar composed of anchor tags. When the user logs in, I have an ngIf directive to display my profile icon with dropdown options. However, I am facing challenges in styling it correctly. I aim ...

Incorporate content upon button click using Javascript

Looking to create a unique Survey layout that includes: Question Name Options The question name is editable and can be changed with a click. Initially, there is one option with a checkbox to select it. This option should also update when clicked. Addit ...

Unable to access Bootstrap dropdown menu after initial ajax form submission

I am encountering an issue with a dropdown menu on my webpage, specifically within the manager.php file. Please excuse any formatting discrepancies as I am using Bootstrap: <!-- Bootstrap --> <script type="text/javascript" src="https://netdna ...

Having trouble getting Material Design Lite to function properly on my site

I'm having trouble with my HTML template that incorporates Google Material Design Lite. The header is working fine, but anything I add below it just moves to the top. Can someone take a look at the code and offer some assistance? .demo-layout-trans ...

IE11 retains radio button selection even after page reload

I am facing an issue on my webpage where I have implemented radio buttons for filtering a list. ALL (default selected with checked='checked') MATCHED REST <input type="radio" value="ALL" name="options" class="radioStyle" checked autocompl ...

The dynamic links in Knockout.js are unresponsive to clicks

I've been working on a new project that utilizes knockout js. The main issue I'm facing is with setting up a table to display images and information from a form, being stored in an observable array. Each image is wrapped in an anchor tag, and the ...

Circular image displaying as an elongated oval in the latest version of Bootstrap

I am currently working on a project that involves creating circular avatar images within a Node Web App. The issue I am facing is when I apply the Boostrap class to the image like so: class="rounded-circle" The image ends up looking like an oval instead ...

Tips for maintaining the position of items with absolute positioning while resizing:

Hello, I am seeking help with an issue in my code. My problem is as follows: I have two items and one frame. The frame has a position relative to the two items (children) which have positions set to absolute. Ideally, these children should always remain ...

Guide to incorporating a rectangular image into a circular frame with CSS

I have a rectangular image with dimensions of 300px by 300px. To display this image within a circle, I have created the following CSS code: img { width: 300px; height: 300px; background-color: white; border-radius: 50%; border: 2px solid black; ...

Template designed in the style of Spotify with HTML5 and responsive features

I am in the process of creating a one-page website with a similar effect to that on spotify.com. Essentially, I want the background image to change as you scroll down, while still keeping elements displayed over the previous backgrounds in a smooth and res ...

Adding a class to a Vue component using the $refs property

I am facing an issue where I need to add dynamic class names to various Vue components based on their reference names listed in a configuration file. Manually adding classes to each component is not feasible due to the large number of components. To addre ...

Can local storage be accessed within a function and utilized during the Windows.onload event?

I have an onclick function that dynamically returns 'x' and stores it in a div. However, after a page refresh, the dynamic div resets and the data is lost. To prevent this, I stored the data in local storage within the 'test' function a ...

What is the method used by flexbox to determine the width of a flex-item when neither flex-basis nor width is specified?

Can you explain how flexbox calculates the final width of flex-items when specific properties like flex-basis, width, min-width, or max-width are not set? I'm curious about how flexbox determines the width based solely on content. Check out this exam ...

Accessing the text area value from an HTML page and sending it to Python Flask using JavaScript

I'm struggling to figure out how to successfully send a post request from Javascript to my Python Flask server. Below is the key content in my html file: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js </script> < ...

Troubleshooting HTML Templates and HTML Import in Firefox: Script Inside Not Running

Currently, I am implementing webcomponents.js to enable Firefox support for web components. An issue arises when loading an HTML template through an HTML import – a script within the template fails to execute once the custom-element based on the templat ...

The carousel spun around, each section moving to the side on its own

One issue I'm facing is that on my page, I have multiple carousel rows. However, when I click on the "next" or "prev" button to navigate through the items in the carousel, it affects all carousels instead of just the one I clicked on. I've attem ...

Retrieve only the initial tag content using jquery

My goal is to extract the "22" from the following code... <div class="left"> <a class="count-link" href="http://url1.com"> <span>22</span> users </a> <a class="count-link" href="http://url2.com"> <span>10</span ...

why isn't bootstrap 4 pull-xs-right functioning properly?

I am utilizing Bootstrap 4 utilities to align buttons to the right of the screen in my HTML, and it is achieving the desired effect. However, I am facing an issue where the buttons are not pushing down the content below them as anticipated. Instead, they ...