Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda