解决Bootstrap布局注册表单input标签前增加必填项*提示与input框不在同一行问题

时间:2022-10-12 04:33:45

注册表单部分代码如下:

 <form id="registForm" class="form-horizontal" action="${pageContext.request.contextPath }/register" method="post" style="margin-top: 5px;">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用户名</label> <div class="col-sm-6">
<em style="color: red;">*</em>
<input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
<div class="col-sm-6">
<em style="color: red;">*</em>
<input type="password" class="form-control" id="password" name="password" placeholder="请输入密码">
</div>
</div>

出现的问题是*与input框不在同一水平位置上,如下图示

解决Bootstrap布局注册表单input标签前增加必填项*提示与input框不在同一行问题

解决方法:覆盖input标签的class=“form-control”,修改display为“inline”,原来的block会让div前后带上换行符,将width设置为90%(width原来为100%,需在引入bootstrap的css文件后引入自己写的css样式)

display的两个取值

解决Bootstrap布局注册表单input标签前增加必填项*提示与input框不在同一行问题

在自己的css文件中修改如下

 .form-control {
display: inline;
width: 90%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
-webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
-o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
}

修改后的页面,*与input框在同一行,效果如下

解决Bootstrap布局注册表单input标签前增加必填项*提示与input框不在同一行问题