• 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Centering Form on HTML page

 
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
 
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
    Marshal
    Posts: 67289
    170
    Mac Mac OS X IntelliJ IDE jQuery Java
    • 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.
     
    Oh. Hi guys! Look at this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!