John Cliven
John Cliven

Reputation: 1231

Can't center absolute position

Background & Problem:

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

Codepen link is dead and removed.

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?

Upvotes: 91

Views: 193739

Answers (7)

Tyrell Curry
Tyrell Curry

Reputation: 381

The solution can also possibly be using a span tag. This causes issues in some browsers. Try using a div tag instead if you see you're using spans.

Upvotes: 0

Ivan Zakharchuk
Ivan Zakharchuk

Reputation: 161

Shortest and functional version:

absolute inset-0 m-auto

Upvotes: 6

Alarih
Alarih

Reputation: 485

for Tailwind try this :

absolute m-auto left-0 right-0

Upvotes: 46

Adharsh M
Adharsh M

Reputation: 3832

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>

Upvotes: 273

Davide Casiraghi
Davide Casiraghi

Reputation: 18087

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%);
}

Upvotes: 9

Reagan
Reagan

Reputation: 2377

You can also use the grid and the place-items-center to place item in the middle e.g.

<div class="relative">
  <div class="absolute bottom-0 left-0 right-0 top-0 grid place-items-center">
   <!-- Add your content here -->
  </div>
</div>

If you want to place the item in the center you can also do it with the following classes absolute left-0 right-0 grid place-items-center

<div class="relative">
  <div class="absolute left-0 right-0 grid place-items-center">
   <!-- Add your content here -->
  </div>
</div>

I created an example on the playground https://play.tailwindcss.com/FPLV9gLqBD

Upvotes: 3

Akhil Aravind
Akhil Aravind

Reputation: 6130

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">

Upvotes: 19

Related Questions