Adjusting the positioning of elements in a constantly changing scenario

Utilizing Bootstrap, I aim to keep the top-right corner of the red background fixed to the upper right corner of the blue section within the visible area regardless of window resizing due to its responsive design.

The red element should be position fixed so it remains stationary while scrolling, always staying on the top-right side of the visible blue background.

An illustration of the desired outcome: https://i.sstatic.net/WKInA.png Even when scrolling (visualized using Windows Paint)

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

To see the implementation, visit the link or refer to the code snippet below:

.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}

.left {
  background-color: blue;
  width: 100%;
  height: 4000px;
}

.right {
  background-color: red;
  width: 100%;
  position: fixed;
  right: 18%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col-11 left">left

    </div>
    <div class="col-1 right">right
    </div>
  </div>
</div>

Answer №1

To position an element at the top right corner of the page, outside of any other elements without scrolling, you can employ the position: absolute CSS property. Give this code snippet a try:

.right{
  background-color: red;
  width: '100%';
  position: absolute;
  right: 15%;
  top: 30px;
}

Answer №2

For achieving a sticky positioning effect, you can use the position: sticky property. Here are the steps to implement this feature:

  1. Ensure that the parent element (in this case, the .row) has position: relative set.
  2. Assign a value to the top property of the sticky element so it knows where to stick when scrolling takes place.
  3. To avoid CSS specificity issues with Bootstrap classes, add an additional class .sticky to the .right element for applying the necessary styling.

.row {
  background: #f8f9fa;
  margin-top: 20px;
  align-items: flex-start;
  position: relative;
}


.left {
  background-color: blue;
  width: 100%;
  height: 4000px;
}

.right.sticky {
  background-color: red;
  width: 100%;
  position: sticky;
  right: 18%;
  top: 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col-11 left">left

    </div>
    <div class="col-1 right sticky">right
    </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

What methods can I use to maintain alignment across different screen sizes?

html, body { height: 100%; width: 100%; margin: 0; padding: 0; overflow: hidden; } .container { position: absolute; width: 100%; height: 100%; ...

IE11 not running Angular code inline as expected

Currently in the process of developing an AngularJS application, I came across a strange issue that I have never encountered before. It may be a simple fix, but I am unsure of the exact terminology to search for the right solution, so I apologize in advanc ...

PHPWord setup complication

I am struggling to run a PHPWord script. When attempting to execute the sample example Text.php, it does not display anything. I have verified that the class is loading successfully. You can find more information in the documentation. If anyone has encou ...

The HTML retrieved cannot be accessed by external JavaScript code

I've encountered a major issue. I'm using ajax to retrieve content in HTML format, but my main JavaScript file is unable to manipulate any retrieved elements - tags, classes, IDs, you name it. Is this normal? How can I work with the data once it& ...

Centering "Label - Value" without tables in web design

It's common to have multiple labels (such as name, age, color) and a corresponding value for each one. One way to ensure that the values (for example Steve, 19, Red) all start in the same horizontal position is by organizing them in a 2 column, 3 row ...

Enable users to upload pictures along with text descriptions

I want to enhance the user experience on my website by enabling them to upload images along with captions. Currently, users can upload images but they don't have the option to add a caption. I know I need to implement a database for this functionality ...

Tips for displaying specific information using Javascript depending on the input value of an HTML form

I am working on an HTML form that includes a dropdown list with four different names to choose from. window.onload = function(){ document.getElementById("submit").onclick = displaySelectedStudent; } function displaySelectedStu ...

Comparing the name and URL of a link using Selenium webdriver in C#

I am currently looking for ways to automate the testing of links on a Sharepoint site, ensuring they are connected to the correct URL. However, I have hit a roadblock when it comes to performing the comparison. After locating the links and adding them to ...

Tips for fixing the position without affecting the width

I'm trying to figure out how to keep my #header fixed in position without it expanding in width. Despite checking my code multiple times, I can't seem to identify the factor causing my header to become wider and shift downwards unexpectedly. I in ...

Issues with displaying ngAnimate animations

For the past 10 hours, I've been attempting to get my animations to function properly. It seems that they only work when I include the animate.css stylesheet and use the "animated classname". However, when I try to create custom entrance and exit anim ...

What makes CSS Overflow: hidden toggle from working initially to suddenly not working?

There seems to be an issue with the code below - it works fine initially, but as soon as I tweak the height values for .Image, it stops functioning as expected. The test image starts overflowing instead of staying hidden, and no matter how many times I a ...

Is there a way for me to insert a new column into a table using jinja?

As a beginner in the final project of CS50x, I am working on creating a webpage that allows users to input weights into a database table and then view those weights along with weight gain/loss information. I am using jinja and python to render a query resu ...

Show and hide HTML div elements dynamically using jQuery based on the selection from a dropdown

I am attempting to display or hide various divs based on user input from a select drop-down box. In fact, I initially tried to directly use the code from jQuery dropdown hide show div based on value, but I seem to be missing something simple that is preven ...

I'm feeling a bit lost on how to pull out HTML tag elements using Python

I have been attempting to extract the html tags for alternative purposes, but due to the structure of the html file, I am facing challenges using conventional methods to locate the elements. The section containing the elements is quite long (consisting of ...

How can I display just the time portion of a date in AngularJS?

Hey everyone, I need assistance with displaying only the time value in AngularJS. Check out my Plunker I have fetched the time value using ng-repeat, but I want to display only the time value on my portal. I have tried to display the time value lik ...

Shifting the position of personalized tabs using Jquery

I have created some basic HTML/CSS tabs and I want to move to the next tab by clicking a link at the bottom of every tab. HTML <ul class="tabs"> <li class="active"> <a href="#">Explainer (2mins)</a> <div class=" ...

When using equal-width columns in Bootstrap, the columns will be stacked one on top of the other

When I utilize Bootstrap 5, I am encountering an issue where equal width columns are being displayed one underneath the other. To illustrate, here is a simple example directly from the Bootstrap website: <link rel="stylesheet" href="https://cdn.jsd ...

What is the method for connecting a page with a submit button?

<button class="w-100 btn btn-lg btn-primary" type="submit">Sign in</button> I have a code snippet from bootstrap that creates a button. How can I modify this code to add a link to another HTML file while maintaining data val ...

Element appearing on screen but not found in the document structure

While using Selenium for HCI tests, I encountered a situation where Selenium was unable to locate a visible element. After some investigation, it seems that the element is visible but not present in the DOM. I could be wrong, but that’s how it appea ...

jQuery id selector issue: not selecting elements properly

It seems like I am overlooking a very fundamental aspect here. Essentially, there are 2 div elements in the HTML markup. When one is clicked (#x), the other div element (#block1) gains a new class (.change), resulting in a change in its color. The issue ...