• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Centering Form on HTML page

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic