Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

finchy70's avatar

Can't centre Text with mx-auto (Tailwindcss)

I've started using Tailwind in my laravel projects and I really like it. I have got stuck.

I have two divs

<div class="mx-auto text-xs font-bold italic">{{App\Paginatable::pagination_count($posts)}}</div>
<div class="my-4">{{$posts->links()}}</div>

the mx-auto does not centre the return from the pagination count method. I have tried putting it in another div. Changing the dicv to p and h5. I cant centre the output.

Imgur

Showing x to x of x records.

0 likes
8 replies
finchy70's avatar

I have got it to center by putting it inside a div with class of flex.

<div class="flex">
            <span class="mx-auto text-xs font-bold italic">{{App\Paginatable::pagination_count($posts)}}</span>
        </div>
Sinnbeck's avatar

For mx auto to work it needs to be inside a full width div. Flex shouldnt be needed

finchy70's avatar

No, if I remove the flex class it aligns to the left edge.

Sinnbeck's avatar

Oh sorry forgot to mention that it cannot be full width as well (default)

Try this (change w to how wide you want it). If the text should just be center aligned then drop mx-auto and just do text-center

<div>
    <div class="mx-auto w-1/2" >lala</div>
</div>
finchy70's avatar
<div>
    <span class="mx-auto w-1/2 text-xs font-bold italic">{{App\Paginatable::pagination_count($posts)}}</span>
</div>

doesn't work

<div>
    <span class="text-center w-3/4 text-xs font-bold italic">{{App\Paginatable::pagination_count($posts)}}</span>
</div>

doesn't work

<div class="flex">
    <span class="mx-auto text-xs font-bold italic">{{App\Paginatable::pagination_count($posts)}}</span>
</div>

works

<div>
    <div class="text-center text-xs font-bold italic">{{App\Paginatable::pagination_count($posts)}}</div>
</div>

also works. Strange?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You cannot set width on a span. :)

And again. If you just want to center a text, you should just use text-center

<div class="text-center">
    <span class=" text-xs font-bold italic">{{App\Paginatable::pagination_count($posts)}}</span>
</div>
Snapey's avatar

you can center text in a span by applying a width to it and setting it to inline-block

eg

<span class="text-center w-full inline-block text-xs font-bold italic ">{{App\Paginatable::pagination_count($posts)}}</span>

Please or to participate in this conversation.