• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Centering Form on HTML page  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Spring MVC project using Thymeleaf. I have a submission given by the following file:



The css file is



I want to align the <label>s and <input>s and center them, so I modified the form to be a CSS table. I changed the
HTML file to enclose the <label>s and <input>s with <p></p> as follows:



Then I added the following to my CSS file:


This aligned the <label>s and <input>s, but does not center them. The before and after shots look like:


spittr.png
[Thumbnail for spittr.png]
Before modification
spittrleft.png
[Thumbnail for spittrleft.png]
After modification
 
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what's your question?

Also, it's best to show the client-side markup in this forum, rather than the server-side source.
 
Mike Tabak
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:So what's your question?

Also, it's best to show the client-side markup in this forum, rather than the server-side source.



My question is I was able to align the <input>s and <labels>s, but now the form shifted to left justification. I want
to keep the form centered as in the first image.
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, to be honest, I'd backtrack quite a bit -- there'a lot of unnecessary complexity both in your markup, and in your CSS.

Then, to center the form itself:
  • Make sure the form is a block element (it is by default, but if there's anything making it otherwise, you need to make sure it's a block element)
  • Give the form a width; it can be a percentage if you want.
  • Make the left and right margin auto.


  • That's it. That will center the form block on the page.

    Remove all the excess styling you have now.

    You can then style the interior as you see fit.
     
    Bear Bibeault
    Sheriff
    Posts: 67257
    170
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'd also avoid using the <p> tag here -- forms are not text paragraphs. You should strive to keep your HTML as semantic as possible.'

    Also, at this stage, all new pages should be HTML5; XHTML is long dead and buried.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!