In CSS sometimes we complicate our lives with simple things that are less complicated than we think, and that when we see the solution we almost laugh at observing such anecdotes.
More than one person has asked me how to center an absolutely positioned div, and the answer is very simple:
- We give the div a fixed width. Example: 500px
- We absolutely position the div with 50% left. Example: position: absolute; left: 50%;
- We subtract half of what it measures with the margin. Example: margin-left: -250px;
The result is a perfectly centered, absolutely positioned div.
Thank you very much .. as you say something so simple and that despite the passage of time is still the author of many headaches.
Your post was very useful to me, thank you very much.
Thank you very much !!!
Good contribution helped me a lot, thanks
What a great crazy he served me: D
Excellent thanks…
Very good Carlos.
Excellent info
Thank you, it served me
You saved me!!! Thank you :)
Thank you uncle, you helped me a lot !!, that lack of imagination of mine.
thank you very much !!!!! this trick always helps me haha
thank you very much excellent friend
This is not centered, it all depends on the original size of the base div element that we make it grow depending on what is put inside it, for me it was more useful to use this than visually if I see it even more centered and reponsive:
absolutely;
left: 50%
margin-left: -100px;
that would be half.
The contribution is appreciated, it really helped me, greetings!
Very good contribution helped me a lot, a thousand grace!