我正在使用下面的CSS,但它将图像放在按钮的中心.使用左右对齐图标的任何方式<input type="button">
,以便文本和图像很好地匹配和对齐?
background: url('/common/assets/images/icons/16x16/add.png');
background-position:center;
background-repeat:no-repeat;
Run Code Online (Sandbox Code Playgroud)
Del*_*ani 139
如果你绝对必须使用input
,试试这个:
background-image: url(...);
background-repeat: no-repeat;
background-position: <left|right>;
padding-<left|right>: <width of image>px;
Run Code Online (Sandbox Code Playgroud)
它通常是一个比较容易使用button
与img
内:
<button type="submit"><img> Text</button>
Run Code Online (Sandbox Code Playgroud)
然而,button
用于提交的浏览器实现是不一致的,以及在button
使用时发送所有按钮值的事实- 这在多提交表单中杀死了"点击了什么按钮"检测.
ssh*_*how 74
假设您的按钮图像是16 x 16像素,这应该可以做你想要的.
<input type="button" value="Add a new row" class="button-add" />
Run Code Online (Sandbox Code Playgroud)
input.button-add {
background-image: url(/images/buttons/add.png); /* 16px x 16px */
background-color: transparent; /* make the button transparent */
background-repeat: no-repeat; /* make the background image appear only once */
background-position: 0px 0px; /* equivalent to 'top left' */
border: none; /* assuming we don't want any borders */
cursor: pointer; /* make the cursor like hovering over an <a> element */
height: 16px; /* make this the size of your image */
padding-left: 16px; /* make text start to the right of the image */
vertical-align: middle; /* align the text vertically centered */
}
Run Code Online (Sandbox Code Playgroud)
示例按钮:
更新
如果您碰巧使用Less,这个mixin可能会派上用场.
.icon-button(@icon-url, @icon-size: 16px, @icon-inset: 10px, @border-color: #000, @background-color: transparent) {
height: @icon-size * 2;
padding-left: @icon-size + @icon-inset * 2;
padding-right: @icon-inset;
border: 1px solid @border-color;
background: @background-color url(@icon-url) no-repeat @icon-inset center;
cursor: pointer;
}
input.button-add {
.icon-button("http://placehold.it/16x16", @background-color: #ff9900);
}
Run Code Online (Sandbox Code Playgroud)
以上编译成
input.button-add {
height: 32px;
padding-left: 36px;
padding-right: 10px;
border: 1px solid #000000;
background: #ff9900 url("http://placehold.it/16x16") no-repeat 10px center;
cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
小智 9
<button type="submit" style="background-color:transparent; border-color:transparent;">
<img src="images/button/masuk.png" height="35"/>
</button>
<button type="reset" style="background-color:transparent; border-color:transparent;">
<img src="images/button/reset.png" height="35"/>
</button>
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你.