Sta*_*tan 119 html css alignment
我有非常基本和已知的形式场景,我需要正确对齐输入旁边的标签.但是我不知道怎么做.
我的目标是将标签与右侧的输入对齐.这是期望结果的图片示例.
我为你的方便做了一个小提琴,并澄清了我现在拥有的东西 - http://jsfiddle.net/WX58z/
片段:
<div class="block">
<label>Simple label</label>
<input type="text" />
</div>
<div class="block">
<label>Label with more text</label>
<input type="text" />
</div>
<div class="block">
<label>Short</label>
<input type="text" />
</div>
Run Code Online (Sandbox Code Playgroud)
bfa*_*tto 175
一种可能的方案:
display: inline-block
;那是:
label {
display: inline-block;
width: 140px;
text-align: right;
}?
Run Code Online (Sandbox Code Playgroud)
<div class="block">
<label>Simple label</label>
<input type="text" />
</div>
<div class="block">
<label>Label with more text</label>
<input type="text" />
</div>
<div class="block">
<label>Short</label>
<input type="text" />
</div>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ord 17
虽然这里的解决方案是可行的,但最近的技术已经成为我认为更好的解决方案.CSS网格布局允许我们构建更优雅的解决方案.
下面的CSS提供了一个2列"设置"结构,其中第一列预期是右对齐标签,后面是第二列中的一些内容.通过将其包装在<div>中,可以在第二列中呈现更复杂的内容.
[作为旁注:我使用CSS添加跟踪每个标签的':',因为这是一个风格元素 - 我的偏好.
/* CSS */
div.settings {
display:grid;
grid-template-columns: max-content max-content;
grid-gap:5px;
}
div.settings label { text-align:right; }
div.settings label:after { content: ":"; }
Run Code Online (Sandbox Code Playgroud)
<!-- HTML -->
<div class="settings">
<label>Label #1</label>
<input type="text" />
<label>Long Label #2</label>
<span>Display content</span>
<label>Label #3</label>
<input type="text" />
</div>
Run Code Online (Sandbox Code Playgroud)
And*_*ich 12
之前回答过这样的问题,你可以看一下这里的结果:
创建表单以使字段和文本彼此相邻 - 这样做的语义方式是什么?
因此,要将相同的规则应用于您的小提琴,您可以使用display:inline-block
它们并排显示您的标签和输入组,如下所示:
CSS
input {
margin-top: 5px;
margin-bottom: 5px;
display:inline-block;
*display: inline; /* for IE7*/
zoom:1; /* for IE7*/
vertical-align:middle;
margin-left:20px
}
label {
display:inline-block;
*display: inline; /* for IE7*/
zoom:1; /* for IE7*/
float: left;
padding-top: 5px;
text-align: right;
width: 140px;
}
Run Code Online (Sandbox Code Playgroud)
我使用类似的东西:
<div class="form-element">
<label for="foo">Long Label</label>
<input type="text" name="foo" id="foo" />
</div>
Run Code Online (Sandbox Code Playgroud)
样式:
.form-element label {
display: inline-block;
width: 150px;
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个旧线程,但更简单的解决方案是将输入嵌入到标签中,如下所示:
<label>Label one: <input id="input1" type="text"></label>
Run Code Online (Sandbox Code Playgroud)
小智 5
您也可以尝试使用 flex-box
<head><style>
body {
color:white;
font-family:arial;
font-size:1.2em;
}
form {
margin:0 auto;
padding:20px;
background:#444;
}
.input-group {
margin-top:10px;
width:60%;
display:flex;
justify-content:space-between;
flex-wrap:wrap;
}
label, input {
flex-basis:100px;
}
</style></head>
<body>
<form>
<div class="wrapper">
<div class="input-group">
<label for="user_name">name:</label>
<input type="text" id="user_name">
</div>
<div class="input-group">
<label for="user_pass">Password:</label>
<input type="password" id="user_pass">
</div>
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)