+ 1

Why when I write margin-left:50%; my block does not aligned to center?

I want to align registration form to center, but it doesn't work when i write div{margin-left:50%;}.

4th Jun 2019, 6:01 PM
Anisimov David
Anisimov David - avatar
3 Antworten
+ 4
margin acts from the top left of the element not from the center of the element. setting it to 50% means that your giving it am offset of 50% from its top left corner. if you know the height and width of the element then you can do something like this align it to the center of the screen. position: absolute; top: 50%; left: 50%; margin-left: -half_width; margin-top: -half_height; if you want to only horizontally align it to center, you can do this: (width should not be 100%) margin: 0 auto;
4th Jun 2019, 6:22 PM
Farry
Farry - avatar
+ 2
Try this Form{ Margin: 0 auto; Width:50%; } Hope it helps
4th Jun 2019, 6:23 PM
Trinity
+ 2
forget about trying to align that way as it is archaic and error prone try this form{ position:relative; margin:auto; } it would also be in your best interest to learn about grid and flexbox. Once learnt your code will never look the same.
4th Jun 2019, 8:47 PM
Logomonic Learning
Logomonic Learning - avatar