【问题标题】:Formating input and label tags with css vertically使用 css 垂直格式化输入和标签标签
【发布时间】:2018-06-11 16:01:55
【问题描述】:

我有这个 HTML 和 css 代码,它是一个表单...

我尝试了在其他问题中找到的另一种解决方案,但我无法将inputlabels“垂直”对齐...

谁能帮帮我?

PS.:在我的浏览器 (Firefox) 中看起来像这张图片...


<style type="text/css">.modal {
  background-color: rgba(0, 0, 0, 0.4);
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  position: fixed;
  color: #B1EBF9;
}

.close {
  color: #111;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

#divMain,
.post-menu {
  filter: blur(5px);
}

.close:hover,
.close:focus {
  color: red;
  text-decoration: none;
  cursor: pointer;
}

.modal-content {
  background-color: #3B474C;
  float: center;
  /*        border: 1px solid #000;
*/
  width: 35%;
  height: 35%;
  padding: 1%;
  opacity: 1;
  margin-left: 40%;
  margin-right: 40%;
  margin-top: 20%;
  margin-bottom: 20%;
  text-align: center;
  border-radius: 15px;
}

#up input {
  /*background-color: #546E7A;*/
  text-align: center;
  appearance: none;
}

input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type=number] {
  -moz-appearance: textfield;
  min: 0;
}

input {
  background-color: #3B474C;
  color: #B1EBF9;
  border: 2px #28C953 solid;
  border-radius: 50px;
  padding-top: 2%;
  padding-bottom: 2%;
  margin-top: 1%;
}

#buttonUpdate {
  background-color: #28C953;
  margin-top: 3%;
  border: 2px #28C953 solid;
  border-radius: 50px;
  padding-top: 2%;
  padding-bottom: 2%;
  padding-left: 6%;
  padding-right: 6%;
  color: #3B474C;
  font-weight: bold;
}

</style>
<div id="popup" class="modal" onclick="if (event.target == document.getElementById('popup')){window.location = '/dashboard/clientBackup/';}">
  <div class="modal-content">
    <form action="" method="post">
      <div id="up">
        <input name="csrfmiddlewaretoken" value="S90LWPAUTykZXspFHHUQ1JXCBRnzXHK2PfgDvTO2mShTEzxI1csOWLQRQbQQgt5q" type="hidden">
        <p>
          <label for="id_identificador">Identificador:</label>
          <input name="identificador" value="devserver-fd" required="" id="id_identificador" maxlength="200" type="text">
        </p>
        <p>
          <label for="id_email">Email:</label>
          <input name="email" value="example@example.com" required="" id="id_email" maxlength="200" type="text">
        </p>
        <p>
          <label for="id_n_jobs">N jobs:</label>
          <input name="n_jobs" value="1" required="" id="id_n_jobs" type="number">
        </p>
      </div>
      <input id="buttonUpdate" value="Update" type="submit"><br>
    </form>
  </div>
</div>

【问题讨论】:

  • 在你的 sn-p 中看起来是垂直的
  • 可能取决于你的分辨率,但在图像中看到......我只想让它垂直格式化,而不是全部居中且不对齐
  • 哦,是的,抱歉,不是全屏

标签: html css input label


【解决方案1】:

不知道为什么你有&lt;p&gt;标签作为输入+标签的容器而不是&lt;div&gt;,但我只是使用display: flex;flex-direction: column;来垂直定位它。

所以form p 的最后一个样式是我唯一改变的。

例如,如果您希望标签左对齐,只需添加:

form p label {
  align-self: flex-start;
}

<style type="text/css">.modal {
  background-color: rgba(0, 0, 0, 0.4);
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  position: fixed;
  color: #B1EBF9;
}

.close {
  color: #111;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

#divMain,
.post-menu {
  filter: blur(5px);
}

.close:hover,
.close:focus {
  color: red;
  text-decoration: none;
  cursor: pointer;
}

.modal-content {
  background-color: #3B474C;
  float: center;
  /*        border: 1px solid #000;
*/
  width: 35%;
  height: 35%;
  padding: 1%;
  opacity: 1;
  margin-left: 40%;
  margin-right: 40%;
  margin-top: 20%;
  margin-bottom: 20%;
  text-align: center;
  border-radius: 15px;
}

#up input {
  /*background-color: #546E7A;*/
  text-align: center;
  appearance: none;
}

input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type=number] {
  -moz-appearance: textfield;
  min: 0;
}

input {
  background-color: #3B474C;
  color: #B1EBF9;
  border: 2px #28C953 solid;
  border-radius: 50px;
  padding-top: 2%;
  padding-bottom: 2%;
  margin-top: 1%;
}

#buttonUpdate {
  background-color: #28C953;
  margin-top: 3%;
  border: 2px #28C953 solid;
  border-radius: 50px;
  padding-top: 2%;
  padding-bottom: 2%;
  padding-left: 6%;
  padding-right: 6%;
  color: #3B474C;
  font-weight: bold;
}

form p {
display: flex;
flex-direction: column;
}

</style>
<div id="popup" class="modal" onclick="if (event.target == document.getElementById('popup')){window.location = '/dashboard/clientBackup/';}">
  <div class="modal-content">
    <form action="" method="post">
      <div id="up">
        <input name="csrfmiddlewaretoken" value="S90LWPAUTykZXspFHHUQ1JXCBRnzXHK2PfgDvTO2mShTEzxI1csOWLQRQbQQgt5q" type="hidden">
        <p>
          <label for="id_identificador">Identificador:</label>
          <input name="identificador" value="devserver-fd" required="" id="id_identificador" maxlength="200" type="text">
        </p>
        <p>
          <label for="id_email">Email:</label>
          <input name="email" value="example@example.com" required="" id="id_email" maxlength="200" type="text">
        </p>
        <p>
          <label for="id_n_jobs">N jobs:</label>
          <input name="n_jobs" value="1" required="" id="id_n_jobs" type="number">
        </p>
      </div>
      <input id="buttonUpdate" value="Update" type="submit"><br>
    </form>
  </div>
</div>

【讨论】:

  • 那是因为我正在使用 django 为我提供的更新表单,它使用更新视图(通用类)。
  • 现在,我的表单在此打印中看起来像 i.prntscr.com/NFBu8sVrTiOmsntbjyFgUQ.png :( 您的代码正在运行,但我可能有一些问题......
  • 您可以为输入添加固定宽度。像宽度:300px。然后添加 align-items: center;到形式 p
  • 完美运行!!
猜你喜欢
  • 1970-01-01
  • 2012-01-23
  • 2011-05-26
  • 2011-11-16
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
相关资源
最近更新 更多