CSS: How can I make a sticky element cling to a specific position on the page rather than the top?

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

Is there a way to make the "+01" element follow the image div while scrolling, but have it start moving downwards before reaching the top of the page? I am looking for a simple solution to achieve this effect.

Answer №1

If I have grasped your question correctly, it seems like you are interested in achieving a sticky position. Find out more here

I've created an example inspired by your image. Take a look:

body {margin:0; padding:0; background-color:orange;}
.container {
  position:relative;
  margin:0 auto;
  width:400px;
  margin-top:20px;
  margin-bottom:40px;
}
img {
  width:100%;
  display:block;
  margin-top:-40px;
}
.number {
  position:sticky;
  top:30px;
  margin-left:-40px;
  font-size:40px;
  color:#fff;
  text-shadow: 2px 2px 3px rgba(0,0,0,0.8);
  font-weight:bold;
  
}
<div class="container">
  <div class="number">
      +01
    </div>
    <img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">

</div>
<div class="container">
  <div class="number">
      +02
    </div>
    <img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">

</div>
<div class="container">
  <div class="number">
      +03
    </div>
    <img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">

</div>
<div class="container">
  <div class="number">
      +04
    </div>
    <img src="https://image.shutterstock.com/image-vector/greece-parthenon-sketch-600w-78255484.jpg" alt="">

</div>

Answer №2

Here is an example similar to the one provided earlier, which I have personally worked on and would like to share.

body{
margin:0;
padding:0;
}

.container{
margin:0 auto;
height:250px;
width:100px;
margin-top:10px;
position:relative;
padding:0.05px;
}

.img{
width:100%;
height:100%;
position:absolute;
background-color:red;
}

h1{
position:sticky;
top:100px;
margin-top:50px;
}
<body>
<div class="container">
  <div class="img"></div>
  <h1>01<h1>
</div>
<div class="container">
  <div class="img"></div>
  <h1>02<h1>
</div>
<div class="container">
  <div class="img"></div>
  <h1>03<h1>
</div>
<div class="container">
  <div class="img"></div>
  <h1>04<h1>
</div>
<div class="container">
  <div class="img"></div>
  <h1>05<h1>
</div>
</body>

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

Change classes of individual divs as one scrolls, with each div having its own distinctive class

I came across this article: Add/remove class with jquery based on vertical scroll? However, I am struggling to implement it for multiple divs, each with their own unique classes that require adding and removing 1 class at different distances from the top. ...

Incorporate a new attribute into objects within a designated level of a tree structure in

I am working with an array of objects structured as a tree. I have a requirement to add a new property called "dateType" to the objects at the 3rd level. let tree = [ { id: 1, name: "parent1", children: [ { id: 2, ...

connecting to a particular segment within the current html page

One issue I'm facing is with a large fixed header that covers the content when navigating to HTML id links. Is there a way to adjust the position slightly higher within an id section using CSS? ...

Is there a way to recover a deleted element from an array?

I have a list of user cards, and my task is: Clicking the "undo" button: will restore the last card that was deleted (using an array) Question 1: How can I create an array from the displayed cards list? Question 2: How do I restore the last deleted card? ...

The HTML Style for implementing HighChart title text does not work when exporting files

I have inserted the <br/> and &nbsp; HTML tags into the HighChart titles. The style changes successfully appear in the chart view, but unfortunately, when exported as PNG or JPEG images, the text style fails to apply in the resulting images. To s ...

The beginning of an HTML document and the viewport (screen view) location

Absolute positioning is relative to a containing block that provides a positioning context, which defaults to the document. a) If absolute positioning is relative to the document, can we visualize the starting point of a document as a two-dimensional coor ...

Transferring information from an external JavaScript file to a Vue component

I am currently facing an issue with passing data from an external file called data.js to the component for its usage. The error I'm encountering states that the data is not defined. Below is the content of my data.js file: const data = [ { cha ...

The caching of Document.write is impacting the website's performance

I have a script that uses document.write before the page loads to inject html into my page. However, this content gets heavily cached and only updates with a hard refresh. I know I shouldn't be using document.write, but for now, I need a quick soluti ...

Customize cards in Bootstrap 4 using jQuery to apply filters

I am in the process of designing a webpage that will be filled with multiple cards, utilizing the new card component in bootstrap 4. My goal is to incorporate a search bar that filters out cards based on their titles when a search query is entered. Check ...

adjusting the arrangement of flex elements

I have a container with multiple divs and am utilizing the flexbox model. My goal is to achieve a specific layout using CSS without altering the order of the div tags. https://i.sstatic.net/jdAK6.png The desired outcome is to have the div with the value ...

Sort products by the subcategory in Firebase

I am currently facing a challenge in filtering products based on their sub child nodes within Firebase. This is how my data is structured: products/ product1 /author: 12345 /title: "Awesome" /description: "more awesome" ...

Does the Loopback Model have an "exists" method that includes a where clause?

I am interested in querying the data using filters to check for existence. Does loopback support this method of querying? If so, could you provide some guidance? myModel.exists({where: {and: [{city: "London"}, {gender: "F"}]}}, function(err, bool){ if(bo ...

When attempting to establish a socket connection to a node server from an HTTPS protocol, a mixed

I have recently enhanced my website by adding a nodejs+socket.io based conversation server to it. This server is hosted on a separate AWS instance while the main website runs on HTTPS. However, I encountered an issue when trying to establish a socket conn ...

The relationship between the diffuse map and color in three.js materials

Considering the example below: var mat = new THREE.MeshLambertMaterial({'color': ..., 'map': ...}); How does changing the color affect the final appearance in situations where the color is set to the average pixel value of the map, wh ...

Modify the background of a div and an image

My goal is to add a background to both the image and the div itself, similar to what I've seen on Amazon. However, I'm struggling to achieve this effect where the white background of the image doesn't show when applied onto the div: Image w ...

How can I create a smaller box with text inside using Bootstrap button?

For my current project, I am trying to add a bootstrap button with a small white box next to the main text containing additional text. If you need a visual reference, check out this image: Button example I have searched extensively for a solution but have ...

Getting the value of a pin code in React Native using Haskkor

I am currently encountering an issue with my react native app. I have implemented a Pin code feature using @haskkor/react-native-pincode - npm. However, I am struggling to retrieve the value of the registered pin code from this npm package. My goal is to s ...

Exploring mongodb with limited keywords to locate information

Currently, I am utilizing node js, mongoose, express, and mongodb for my project. I have developed a simple form where users can input their full name to search for additional details in the database. I am curious if it is feasible to conduct a search us ...

Avoiding cached responses when using Astro on Vercel

I'm facing an issue while fetching data on the client side in Astro - I keep receiving a cached response. From the endpoint located at src/pages/api/spotify.ts, this is what I'm sending: export const prerender = false; ... export const GET: APIR ...

Resolving the issue of data transmission within Yii2's framework: Page-to-page data

I'm currently using Yii2-advanced-app and I have encountered an issue. I want to pass the selected value from a dropdown menu to a PHP code that displays a list with checkboxes on the same page. Here is an image for reference on what I am trying to ac ...