【问题标题】:How Do I Center Background Color To Fits With It's Div Contents如何将背景颜色居中以适合其 Div 内容
【发布时间】:2019-03-25 19:36:10
【问题描述】:

下面是我正在创建的联系我们表单的代码,我给第二个 div 中的一个提供了类名 contact-form,背景为:rgba(0, 0, 0, 0.8),但我想要背景颜色居中并适合其他 div 的内容,而不是 100% 显示在屏幕上。

我尝试过使用 margin-left:50px 和 margin-right:50px 但它只发生了一点变化,但并没有产生预期的结果。

codepen.io link

body {
  background: linear-gradient(rgb(0, 0, 150, 0.5),rgb(0, 0, 0, 0.5)),url(https://backgroundcheckall.com/wp-content/uploads/2017/12/contact-background-image-8.jpg);
  background-size: cover;
  background-position: center;
  font-family: sans-serif;
}

.contact-title h2 {
  font-size: 16px;
  margin: 50px;
}

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

 .contact-form {
   background: rgba(0, 0, 0, 0.8);
   margin-left: 50px;
   margin-right: 50px;
   border-radius: 5px;
   text-align: center;
   opacity: 0.5;
   box-shadow: 0 0 10px rgba(0, 0, 0, 0.13);
 }

 form {
   margin-top: 50px;
   transition: all 4s ease-in-out;
 }

 .form-control {
   width: 50%;
   background: transparent;
   border: none;
   outline: none;
   border-bottom: 1px solid grey;
   color: #fff!important;
   font-size: 18px;
   margin-bottom: 16px;
 }

 input {
   height: 45px;
 }

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

 form .submit:hover {
   background-color: #f44336;
   cursor: pointer;
 }

.contact-menu {
  background-color: black;
  margin: 8px 8px 8px 8px;
  opacity: 0.8;
  overflow: hidden;
  height: 50px;
}

.contact-menu a {
  color: #f2f2f2;
  margin: 0px 0 0 0;
  padding: 14px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 17px;
}

.contact-menu a:hover {
  background-color: #ddd;
  color: #000;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="UX-Compatible" content="ie=edge">
    <title>BENDEVI-Contact </title>
    <link rel="stylesheet" href="css/main.css">
  </head>
  <body>

    <div class="contact-menu">
      <a href="index.html">Home</a>
      <a href="#">Foreign</a>
      <a href="#">AFrica</a>
      <a href="#">Unity</a>
    </div>

    <div class="contact-title">
      <h2>Contact Us</h2>
    </div>

     <div class="contact-form">

       <form id="contact-form" action="" method="post">
         <input type="text" name="name" class="form-control" placeholder="Your Name" value="" required><br>

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

         <input type="text" name="Subject" class="form-control" placeholder="Subject" value="" required><br>


         <textarea name="message" class="form-control" placeholder="Message" rows="4" required></textarea><br>

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

       </form>
     </div>
  </body>
</html>

【问题讨论】:

  • 您只需要该层用于表单部分或整页吗? @Stanleynd
  • 我编辑了您的问题以使 sn-p 正常工作。请更具体地说明您想要的结果应该是什么样的。联系表格应该有多大?
  • 您是否尝试过background: rgba(0, 0, 0, 0.8) no-repeat center center; .contact-menu
  • 仅适用于表单部分@Monkey D. Luffy
  • 您能否更具体地说明您要达到的目标?\

标签: html css


【解决方案1】:

根据您的描述,您希望表单元素占屏幕的 50%,但外部 div 不会比表单本身更宽。

您需要在单位% 中添加表单宽度的裁减量。在我的示例中为 25%。并让表单元素在表单内具有 100% 的宽度,因此它们占据了被截断的外部元素的整个宽度。通过使用%,它会随着每个屏幕宽度流动,达到所需的结果。 (玩弄边距的% 以获得所需的结果)。

body {
  background: linear-gradient(rgb(0, 0, 150, 0.5), rgb(0, 0, 0, 0.5)), url(https://backgroundcheckall.com/wp-content/uploads/2017/12/contact-background-image-8.jpg);
  background-size: cover;
  background-position: center;
  font-family: sans-serif;
}

.contact-title h2 {
  font-size: 16px;
  margin: 50px;
}

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

.contact-form {
  background: rgba(0, 0, 0, 0.8);
  margin-left: 25%;
  margin-right: 25%;
  border-radius: 5px;
  text-align: center;
  opacity: 0.5;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.13);
}

form {
  margin-top: 50px;
  padding: 5%;
  transition: all 4s ease-in-out;
}

.form-control {
  width: 100%;
  background: transparent;
  border: none;
  outline: none;
  border-bottom: 1px solid grey;
  color: #fff !important;
  font-size: 18px;
  margin-bottom: 16px;
}

input {
  height: 45px;
}

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

form .submit:hover {
  background-color: #f44336;
  cursor: pointer;
}

.contact-menu {
  background-color: black;
  margin: 8px 8px 8px 8px;
  opacity: 0.8;
  overflow: hidden;
  height: 50px;
}

.contact-menu a {
  color: #f2f2f2;
  margin: 0px 0 0 0;
  padding: 14px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 17px;
}

.contact-menu a:hover {
  background-color: #ddd;
  color: #000;
}
<div class="contact-menu">
  <a href="index.html">Home</a>
  <a href="#">Foreign</a>
  <a href="#">AFrica</a>
  <a href="#">Unity</a>
</div>

<div class="contact-title">
  <h2>Contact Us</h2>
</div>

<div class="contact-form">

  <form id="contact-form" action="" method="post">
    <input type="text" name="name" class="form-control" placeholder="Your Name" value="" required><br>

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

    <input type="text" name="Subject" class="form-control" placeholder="Subject" value="" required><br>


    <textarea name="message" class="form-control" placeholder="Message" rows="4" required></textarea><br>

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

  </form>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2021-11-10
    • 2014-05-27
    相关资源
    最近更新 更多