Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
Center element horizontally in visible area in a scroll view

I want to center an element horizontally in the visible area of a scrollable grid. So the text in the last row here should be centered horizontally inside the blue line.

This is what I want:

enter image description here

When scrolling, it should stay centered and visible:

enter image description here

This is what I've got so far:

JSFiddle: https://jsfiddle.net/6v0ybnd2/24/

<div class="wrapper">
<div class="otherContent"></div>
  <div class="grid" >
  <div class="row">
   Row - this is a long text in row 1
  </div>
  <div class="row">
   Row - this is a long text in row 2
  </div>
  <div class="row">
   Row - this is a long text in row 3
  </div>
  <div class="row-element" id="element">
   this row should always be centered in the visible area
  </div>
</div>
</div>
.wrapper {
  display: grid;
  grid-template-columns: auto auto;
}

.otherContent {
  width: 200px;
  background: orange;
}

.grid {
  display: grid;
  gap: 10px;
  margin: 10px;
}

.row {
  height: 20px;
  width: 1400px;
  background: lightBlue;
}

.row-element {
  height: 20px;
  width: 100vw; /* This width should be set to the visible  */
  position: sticky;
  text-align: center;
  left: 0;
  background: lightBlue;
}
const el = document.getElementById("element");
console.log(el.clientWidth);
el.style.maxWidth = el.clientWidth;

It works somewhat using width: 100vw, but only when the grid spans the full width. As far as I understand clientWidth gives the visible width of an element, however setting the width to that doesn't really work (presumably because it changes clientWidth again?

Any ideas how to solve this? Ideally only in css, but I'm assuming that's not possible. I'd also prefer not setting the position via javaScript, since that results in visible lag, so I think setting the width and using sticky is the best approach.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Change position: sticky; to position: fixed;, doing it this way though you will need to move it more since it will cover other elements. I moved it below the other rows with margin-top: 100px;, but it depends on how far you want it from the other rows and .otherContent

about 4 years ago · Juan Pablo Isaza Denunciar

0

<div class="wrapper">
    <div class="otherContent"></div>
    <div class="grid">
        <div class="row">Row - this is a long text in row 1</div>
        <div class="row">Row - this is a long text in row 2</div>
        <div class="row">Row - this is a long text in row 3</div>
        <div class="row-element" id="element">
            <span style="position: fixed"
                >this row should always be centered in the visible area</span
            >
        </div>
    </div>
</div>

Check this out,

about 4 years ago · Juan Pablo Isaza Denunciar

0

I believe this is what you're after.
Wrap the text in its own html element (e.g <span>), make that element fixed, and center it horizontally like below. The translateX repositions the element horizontally, and using % inside of it means percentage of the element's own width.

<div class="row-element" id="element">
  <span class="fixed-centered">
     This row should always be centered in the visible area 
  </span>
</div>
.fixed-centered {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda