html - 居中联系表单内容

标签 html css position

我建立了一个相对简单的联系表单,似乎无法将实际内容居中(它目前位于左侧,我希望将其移至死点),我的逻辑是边距自动类应该有效但没有运气好,提前感谢任何能看出我的逻辑错误之处的人。

表单对我来说有点新,也许我应该针对不同的元素?

<section id="contact">
      <div class="contact-title">
        <h2>Lets talk about building your new site</h2>
        <h3>Contact me today and ill be in touch soon</h3>
      </div>

      <div class="contact-form">
        <form id="contact-form" method="post" action="">



          <input name="name" type="text" class="form-control" placeholder="Your Name" required>
          <br>

          <input name="email" type="email" class="form-control" placeholder="Your Email" required>
          <br>


           <input name="phone" type="text" class="form-control" placeholder="Your Phone" required>
          <br>

          <textarea name="message" class="form-control"  cols="60" rows="10" placeholder="Message goes here"></textarea>
          <br>

          <input type="submit" class="form-control submit" value="SEND MESSAGE">

        </form>
      </div>
   </section>

CSS

#contact {
margin:0;
padding: 0;
text-align: center;
background: linear-gradient(rgba(0,0,50,0.5),rgba(0,0,50,0.5)),url('https://images.unsplash.com/photo-1491986926302-149ec463b90a?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f34dc245ab45d60718efaa50bcdecee1&auto=format&fit=crop&w=1351&q=80');
background-size: cover;
background-position: center;
}

.contact-title {
  padding-top: 3rem;
  margin-top:50px;
  text-transform: uppercase;
  color: #fff;
  transition: all 4s ease-in-out;
}

.contact-title h2 {
 font-size: 4rem;

}


form {
  margin-top: 5rem;
  transition: all 4s ease-in-out;
  text-align: center;
}

.form-control {
  width:600px;
  background:transparent; 
  border:none;
  outline: none;
  border-bottom: 1px solid grey;
  color:#fff;
}


.form-control:hover {
  width:600px;
  background:transparent; 
  border:none;
  outline: none;
  border-bottom: 1px solid grey;
  color:#fff;
}

input {
  height:45px;
}

form .submit {
  background: #ff5722;
  border-color: transparent;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  letter-spacing: 2px;
  height: 50px;
  margin-top: 20px;
  text-align: center;
}

form .submit:hover {
  background: #ff5722;
  cursor: pointer;
}

.contact-form {
  width: 100%;
  margin: 0 auto;
}

最佳答案

问题是您将表单控件的宽度固定为 600 像素,而联系表单宽度为 100%。它填满了屏幕,您实际上看不到它是否居中。在下面的代码片段中,我将表单控件大小减小到 200 像素,将 div 宽度减小到 50%,您可以看到表单居中。

#contact {
margin:0;
padding: 0;
text-align: center;
background: linear-gradient(rgba(0,0,50,0.5),rgba(0,0,50,0.5)),url('https://images.unsplash.com/photo-1491986926302-149ec463b90a?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f34dc245ab45d60718efaa50bcdecee1&auto=format&fit=crop&w=1351&q=80');
background-size: cover;
background-position: center;
}

.contact-title {
  padding-top: 3rem;
  margin-top:50px;
  text-transform: uppercase;
  color: #fff;
  transition: all 4s ease-in-out;
}

.contact-title h2 {
 font-size: 4rem;

}


form {
  margin-top: 5rem;
  transition: all 4s ease-in-out;
  text-align: center;
}

.form-control {
  width:200px;
  background:transparent; 
  border:none;
  outline: none;
  border-bottom: 1px solid grey;
  color:#fff;
}


.form-control:hover {
  width:200px;
  background:transparent; 
  border:none;
  outline: none;
  border-bottom: 1px solid grey;
  color:#fff;
}

input {
  height:45px;
}

form .submit {
  background: #ff5722;
  border-color: transparent;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  letter-spacing: 2px;
  height: 50px;
  margin-top: 20px;
  text-align: center;
}

form .submit:hover {
  background: #ff5722;
  cursor: pointer;
}

.contact-form {
  width: 50%;
  margin: 0 auto;
}
<section id="contact">
  <div class="contact-form">
    <form id="contact-form" method="post" action="">
      <input name="name" type="text" class="form-control" placeholder="Your Name" required>
      <br>
      <input name="email" type="email" class="form-control" placeholder="Your Email" required>
      <br>
       <input name="phone" type="text" class="form-control" placeholder="Your Phone" required>
      <br>
      <textarea name="message" class="form-control"  cols="60" rows="10" placeholder="Message goes here"></textarea>
      <br>

      <input type="submit" class="form-control submit" value="SEND MESSAGE">

    </form>
  </div>
</section>

关于html - 居中联系表单内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548856/

相关文章:

html - 固定列宽,列数/行数取决于屏幕宽度

javascript - 使用 jQuery 代码创建 jQuery 插件

javascript - 我们什么时候赋值调用jQuery事件?

css - 网络安全字体在国际上也安全吗?

ios - 如何指向和定位 arkit 文本

javascript - 有没有办法以某种方式同时拥有绝对位置(底部)和同一 div 的多个并排?

html - CSS 多列 div 格式问题

html - 使用对象时div区域不想显示

html - 绝对定位的子 div 宽度,脱离相对定位的容器

html - 似乎 IE 动画/处理我的 loading.gif 即使我设置了显示 :none?