【问题标题】:centering contact form content居中联系表格内容
【发布时间】:2018-11-06 00:19:33
【问题描述】:

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

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

<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;
}

【问题讨论】:

  • 左边是什么?我运行了你的代码,发现表单在页面的中心位置。

标签: html css position


【解决方案1】:

问题是表单控件的固定宽度为 600 像素,而联系表单宽度为 100%。它填满了屏幕,你实际上看不到它是否居中。在下面的 sn-p 中,我将表单控件的大小减小到 200px,将 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>

【讨论】:

  • 谢谢,当我运行它时,它在你的代码 sn-p 中看起来非常好..但是当我使用这是我的真实站点时,它仍然不居中,非常令人沮丧
  • 也许你的代码中有另一种风格覆盖了这种风格?
  • 感谢 Kavindra,如果您将 HTML/CSS 复制到普通文本编辑器并在 chrome 中打开,您能否复制此内容?
  • 是的,使用正确的&lt;html&gt;,&lt;body&gt; 等标签,它可以工作。@Ravi21
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 2020-07-13
  • 2012-10-13
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
相关资源
最近更新 更多