假设我有一个默认的文本输入框,没有任何CSS样式
<input type="text" />
我想将其边框颜色改为红色,因此我添加了一个样式
<input type="text" style="border-color:red" />
之后边框将变为红色,但其大小为2px。如果我将边框更改为1px,则输入框的高度将小于默认高度。
我如何将边框更改为1px并确保输入框的实际高度(包括边框)与默认高度相同?
假设我有一个默认的文本输入框,没有任何CSS样式
<input type="text" />
我想将其边框颜色改为红色,因此我添加了一个样式
<input type="text" style="border-color:red" />
之后边框将变为红色,但其大小为2px。如果我将边框更改为1px,则输入框的高度将小于默认高度。
我如何将边框更改为1px并确保输入框的实际高度(包括边框)与默认高度相同?
使用此方法,不会影响高度:
<input type="text" style="border:1px solid #ff0000" />
试一试
<input type="text"/>
它将在所有跨浏览器中显示相同,如 Mozilla、Chrome 和 Internet Explorer。
<style>
input{
border:2px solid #FF0000;
}
</style>
不要添加内联样式,因为它不是良好的实践,使用类来为您的输入框添加样式。
设置一个透明的边框,然后再改变它:
.default{
border: 2px solid transparent;
}
.new{
border: 2px solid red;
}
$("input.address_field").on('click', function(){
$(this).css('border', '2px solid red');
});
input:active,
input:focus {
border: 1px solid #red
}
input:active,
input:focus {
padding: 2px solid #red /*for firefox and chrome*/
}
/* .ie is a class you would need to set at the html root level */
.ie input:active,
.ie input:focus {
padding: 3px solid #red /* IE needs 1px extra padding*/
}
我知道在Firefox和Chrome上不需要,但是IE需要。而且有些情况下你会需要它。
在类似的情况下,这对我有用:
input:focus {
outline-offset: -2px; // negative value to 'squeeze' it inside
outline-color: orangered; // new desired color
}