position Fixed only horizontal

Asked

Viewed 90 times

3

I’m having a question, I was wondering if it’s possible to leave my div with position: fixed only horizontally and vertically...

It’s possible to do that?

.body{
  height: 1500px;
  width: 1500px;
  background-color: blue;
}

.square {
  width: 100px;
  height: 100px;
  position: fixed;
  top: 25px;
  left: 25px;
  background-color: black;
  z-index: 10;
}
<div class="body">
  <div class="square"></div>
</div>

1 answer

2


Just use position:sticky with the left as a reference, and leave without top

inserir a descrição da imagem aqui

Follow the image code above.

.body{
  height: 1500px;
  width: 1500px;
  background-color: blue;
}

.square {
  width: 100px;
  height: 100px;
  position: sticky;
  /* top: 25px; */
  left: 25px;
  background-color: black;
  z-index: 10;
}
<div class="body">
  <div class="square"></div>
</div>

  • Saved my life kk. Thank you

  • @Otaviocapel without problems :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.