Centering HTML form
Im trying to display my login form in the middle of the page instead of the top, but i cant figure out how to. I have tried giving the form an id, a class n applying css to those. I have also tried wrapping the form in a div but it dint work.
login.html code:
16 Replies
hard to tell without seeing the rest of the code; can you make a minimal reproduceable example in a codepen please? #how-to-ask-good-questions
https://codepen.io/StuckCoder50/pen/dyEGEvG
I put all the relevant stuff in this. Excluded the code for the navbar thats on top of each page.
To vertically centre an element within the page the first thing that it needs is that it's parent has to have the full height of the page (viewport).
One method to do this is using min-height and grid on the body:
This can be done with flex in a very similar way.
However bear in mind that that will affect ALL the contents. If you have other elements on the page (nav, footer etc) they will also be moved.
Note - there are other methods to achieve the same thing. The key is that the parent container needs to have the full height. There is also a very clever way to achieve the same thing, regardless of other content but this is not fully supported yet. That is to use a dialog. This is not the intended use of dialog but if it works.... This amazing trick/hack was shared by Temani Afif onTwitter X https://x.com/ChallengesCss/status/1790705680241819992
Note - there are other methods to achieve the same thing. The key is that the parent container needs to have the full height. There is also a very clever way to achieve the same thing, regardless of other content but this is not fully supported yet. That is to use a dialog. This is not the intended use of dialog but if it works.... This amazing trick/hack was shared by Temani Afif on
Ok thx for replying ill try it out
problem solvedđź‘Ť i wrapped the whole form in a div with class login_form n added margin-top to it in css.
i was also wondering how i cld add a bg image to the page that covers the complete page n not just the form area? cos whatever i tried only did the form area so i went back to without image.
you need to position the image relative to the body. Ensure that the image element is not placed within the form (as that has it's own positon defined).
Are you using a background image or an inline image?
I would probably do this with a background image placed within a psuedo element applied to the body. This would allow you to then apply opacity and filters etc. to the image without affecting the body element itself.
well i tried putting the image in a div element above the form's div
I would add a background image like this:
what is this doing? cld u explain pls
this put the same image for all the different pages of the web app, but i want to use 2 diff imgs for only 2 pages(login n registration). Any way to do that instead? thx for this tho definitely better
just change the image according to the page
but i put this in my styles.css file
ok ig i can do it
you could define the image url as a custom property and define it in the body tag.
<body style="--bg-img:url(IMG_URL)">
In the CSS:
background-image: var(--bg-img:);
Of course how you actually do this will depend on how you are creating your pages.
Another option would be to apply a class to the body, eg "login", "registration" and then define the background pseudo element just for that selector in the style sheet.
sorry, I missed that question - do you understand it now?i dont rlly understand whats inset and z-index
also body::before{content: ""
inset:0;
is the same as setting
This ensures that the pseudo element is the same size as the parent (body in this case)
z-index: -1
ensures that the image is behind any content.oh ok
All pseudo elements require a
content
property to be able to show up in the page.
As there is no actual content, we set the value to empty.ah that makes so much more sense, tysm
alr i did it, thx for all ur help