Center a div that contains a contact form

可紊 提交于 2019-12-12 01:26:38

问题


How do I center a contact form which is inside a div. I have tried margin:0 auto; and it didn't work.

<div id="new_div_3">
        <form class="contact_form" action="#" method="post" name="contact_form">
            <ul>
                <li>
                    <h2>Contact Us</h2>
                    <span class="required_notification">* Denotes Required Field</span>
                </li>
                <li>
                    <label for="name">Name:</label>
                    <input type="text"  placeholder="John Doe" required />
                </li>
                <li>
                    <label for="email">Email:</label>
                    <input type="email" name="email" placeholder="john_doe@example.com" required />
                    <span class="form_hint">Proper format "name@something.com"</span>
                </li>
                <li>
                    <label for="website">Website:</label>
                    <input type="url" name="website" placeholder="http://johndoe.com" required pattern="(http|https)://.+"/>
                    <span class="form_hint">Proper format "http://someaddress.com"</span>
                </li>
                <li>
                    <label for="message">Message:</label>
                    <textarea name="message" cols="40" rows="6" required ></textarea>
                </li>
                <li>
                    <button class="submit" type="submit">Submit Form</button>
                </li>
            </ul>
        </form>
    </div>

I have tried using various methods and none of them seem to work to center the div. I even tried centering the contact form itself and it just moved the input fields to the center of the contact form, rather than the div.


回答1:


In order to get margin: 0 auto; work you need to set width.

#new_div_3 {
    width: 500px;
    margin: 0 auto;
}

Check the fiddle: http://jsfiddle.net/9cMAC/




回答2:


#new_div_3 {
    width:100%;
    display:block;
    margin:auto;
}

Should be all you need.




回答3:


Here you go:

#new_div_3 {
margin: 0 auto;
position: relative;
width: 60%;

}

check fiddle



来源:https://stackoverflow.com/questions/18683863/center-a-div-that-contains-a-contact-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!