DC1
DC1

Reputation: 371

how do I horizontally center a div that only has a max-width and has fixed positioning?

I have been trying to figure out a solution for this and have been searching forums but thy only talk about percentages.

I have a div that contains text only. I need it to have a max-width of 700px, be positioned fixed and be centered horizontally on the screen.

In a perfect world, this is what I want to work but of course it won't:

<div style="max-width: 700px; text-align: center; position: fixed; bottom: 40px; margin: auto;">sample text sample text sample text sample text sample text sample text sample text sample text sample text sample text</div>

Does anyone have a solution?

Upvotes: 1

Views: 64

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115202

set left:0;right:0;

<div style="max-width: 700px; text-align: center; position: fixed; bottom: 40px; margin: auto;left:0;right:0;">sample text</div>

Fiddle Demo

Upvotes: 5

Related Questions