html - 仅使用 css 输入后换行

标签 html css carriage-return

我得到了一个带有很多标签+输入的html

<label>Application</label>
<input id="ApplicationName" />
...
<label>Foo</label>
<input id="Foo" />
...
<label>Junk</label>
<input id="Junk" />

我强制宽度以增加一些一致性

label {
    display: inline-block;
    width: 10em;
}
input {
    width: 10em;
}

使用这种样式会好一些,但是根据容器的宽度,流程仍然会“随处”中断。如何使标签+输入成为一个 block ? (没有将它们都封装在一个容器中)

另一个可接受的解决方案是
add a virtual carriage return after each input或者
before each label .
没成功放after是因为input标签不支持after。
我也不能把它放在前面因为

label::before {
    content: "\A";
    white-space: pre;
}

不能很好地与标签{display:inline-block} 混合

label {
    display:inline-block;
    width:10em;
}

input {
    width:10em;
}
<div>
    <label>namespace</label>
    <input id="namespace" />
            
    <label>Application</label>
    <input id="application" />
            
    <label>Description</label>
    <input id="Description" />
            
    <label>Author</label>
    <input id="Author" />
</div>
resize the window to exhibit unwanted behaviour

最佳答案

你可以混合使用 float 和清除,有点老派但似乎对结构有效:

label {
  display: block;
  width: 10em;
  float: left; /* makes the element act like inline block */
  clear: left; /* clears any left floats so before so this should start on new line */
}

input {
  width: 10em;
  float: left;
}
<div>
  <label>namespace</label>
  <input id="namespace" />

  <label>Application</label>
  <input id="application" />

  <label>Description</label>
  <input id="Description" />

  <label>Author</label>
  <input id="Author" />
</div>

或者你可以给你的父容器一个宽度来强制内容到下一行:

div {
  width: 21em;
}

label {
  display: inline-block;
  width: 10em;
}

input {
  width: 10em;
}
<div>
  <label>namespace</label>
  <input id="namespace" />

  <label>Application</label>
  <input id="application" />

  <label>Description</label>
  <input id="Description" />

  <label>Author</label>
  <input id="Author" />
</div>

关于html - 仅使用 css 输入后换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513036/

相关文章:

javascript - 在 div 中使用图像而不使用 <img> 标签

html - 如何将 JPEG 切片成 HTML

在 Windows 中打印到控制台时需要回车吗?

java - 如何忽略 freemarker 中嵌入列表的换行符/回车符?

javascript - CSS & JS - 搜索时更新位置

html - rails 我在这个展示的开发环境中有一个背景图片,但在生产环境中没有

html - 在 css 的列中对齐图像的问题

html - SCSS 动态选择器

Javascript : is it possible to target only one version of iOs?

c# - 在 Visual Studio 中更改 CR/LF 输出类型