How to horizontally center a DIV

后端 未结 3 1457
余生分开走
余生分开走 2021-01-25 06:09

How can I horizontally center the DIV with the textbox and button on this URL: http://tinyurl.com/d4lpyh5?

Regards, Kevin

3条回答
  •  生来不讨喜
    2021-01-25 06:37

    margin-left: auto; and margin-right: auto divide the remaining width in half between them with the result that the element is centered. That gets you much closer, but it isn't the everything. (And unfortunately it doesn't change appearance even a little bit although it's right.)

    The reason the element doesn't look centered yet is the element is by default the full width of the container/screen. So to make horizontal centering work, you need to not only specify left and right auto margins, but go further and explicitly set the width of the element.

    One would like to be able to specify something like "width: minimum-needed;" so it would adjust automatically to whatever it contained ...but there's no such thing in CSS. The only way I know is to explicitly specify the width of the element in a way that makes sense for what you know are its contents.

    So here below for example is your code modified so it looks like my understanding of what you desire (there's probably an extra

    in here, and you probably want to encapsulate this information in your stylesheet ...but you get the idea):

    
    
    Page
    
    
    
    
    

提交回复
热议问题