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

657
Vistas
Can't center absolute position (Tailwind.css)

Background & Problem:

I'm using Tailwind CSS and Alpine.js for a simple search bar that has a dropdown positioned using absolute

Codepen here: https://codepen.io/jdonline/pen/YzXpbyJ

When I position the dropdown using relative, it positions perfectly as I want it to (but stretches the rest of the page which I don't want). However, when I change this to absolute, although it no longer stretches the page, it extends the dropdown wider than expected.

Example:

You can see this by clicking the dropdown arrow on the right side of the search bar. You can also see the difference when changing absolute to relative on Line 26

Question:

How can I, using Tailwind.css, position the dropdown so it has absolute position, but doesn't extend wider than the search bar?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I found my answer in this proposal on the Tailwind Github profile. https://github.com/tailwindlabs/tailwindcss/discussions/1531

Adding this utility class it works perfectly:

.inset-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
over 4 years ago · Santiago Trujillo Denunciar

0

The answer is very simple, position:absolute should have a parent div with position:relative, in your case relative is for body I think, You need to give relative to the parent div which is n line number 25,

You can also refer Position CSS

<div x-show.transition.opacity.duration.700ms="open" class="relative" >
    <div class="absolute inset-x-0 shadow-xl bg-white w-3/4 md:w-2/5 mx-auto -mt-1 rounded-lg rounded-t-none">
over 4 years ago · Santiago Trujillo Denunciar

0

with tailwind only, you can use the following classes

absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2

so, it would be like

<div class="relative">
    <div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2"> </div>
<div>
over 4 years ago · Santiago Trujillo 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