如何使用CSS创建平铺的背景按钮

时间:2013-07-25 20:43:41

标签: html css

我正在尝试创建一个这样的按钮(输入类型=“提交”):Simple Button

使用需要平铺的图像来实现上述效果。 Image to be tiled

到目前为止,我尝试过这样做:

            border: none;
            background: url(myImage.png) no-repeat;
            background-size: 80px 40px;
            padding-left: 40px;
            padding-right: 40px;
            padding-top: 5px;
            padding-bottom: 5px;

但看起来很多玩像素数字来达到预期的效果。有没有更简单的平铺方法来实现这种效果。

6 个答案:

答案 0 :(得分:4)

如果您不想弄乱渐变,这是一种更简单的方法,因为我看到图像中实际上没有渐变,而是双色调按钮。

通过使用伪元素HTML添加50%高,透明的白色div,您可以在不改变:before的情况下实现此双色调效果。

如果你使用这种方法,你实际上对你的按钮有反射,那么它里面的文字和图标也会有“闪耀”(example)。

#login{
    background:#444;
    display:inline-block;
    color:white;
    padding:10px 60px;
    border-radius:8px;
    position:relative;
}

#login:before{
    content:'';
    width:100%;
    height:50%;
    background:rgba(255,255,255,0.2);
    position:absolute;
    top:0px;
    left:0px;
}

DEMO:http://jsfiddle.net/ySTbB/

答案 1 :(得分:1)

使用CSS3和边框半径

-webkit-border-radius: 11px;
-moz-border-radius: 11px;
border-radius: 11px;
background: rgb(76,76,76); /* Old browsers */
background: -moz-linear-gradient(top,  rgba(76,76,76,1) 0%, rgba(89,89,89,1) 12%, rgba(71,71,71,1) 39%, rgba(44,44,44,1) 50%, rgba(0,0,0,1) 51%, rgba(17,17,17,1) 60%, rgba(28,28,28,1) 91%, rgba(19,19,19,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(76,76,76,1)), color-stop(12%,rgba(89,89,89,1)), color-stop(39%,rgba(71,71,71,1)), color-stop(50%,rgba(44,44,44,1)), color-stop(51%,rgba(0,0,0,1)), color-stop(60%,rgba(17,17,17,1)), color-stop(91%,rgba(28,28,28,1)), color-stop(100%,rgba(19,19,19,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* IE10+ */
background: linear-gradient(to bottom,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4c4c4c', endColorstr='#131313',GradientType=0 ); /* IE6-9 */

SRC:http://www.colorzilla.com/gradient-editor/

答案 2 :(得分:1)

如果这是您想要用作背景的实际图像,那么使用CSS可能是更好的主意:

input[type="submit"]
{
    border: none;
    border-radius: 5px;
    background: linear-gradient(to bottom, #AAA 49%, #555 50%);
}

答案 3 :(得分:0)

如果button-slice.jpg是背景图块图案的40px高,1px宽的jpg“切片”,这将有效。

.button {
    display: inline-block;
    width: 170px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    background: url(button-slice.jpg) repeat-x;
    border: 0;
    border-radius: 8px;
    text-decoration: none;
}

您还可以完全放弃图像并使用CSS3渐变来创建背景效果,如果它仅适用于iOS,这可能是更好的解决方案。

答案 4 :(得分:0)

由于您可以使用CSS3,我强烈建议您使用它:

input[type="submit"] {
    background: #3e3e3e;
    background: -moz-linear-gradient(top,  #616161 0%, #777777 50%, #3b3b3b 51%, #393939 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#616161), color-stop(50%,#777777), color-stop(51%,#3b3b3b), color-stop(100%,#393939));
    background: -webkit-linear-gradient(top,  #616161 0%,#777777 50%,#3b3b3b 51%,#393939 100%);
    background: -o-linear-gradient(top,  #616161 0%,#777777 50%,#3b3b3b 51%,#393939 100%);
    background: -ms-linear-gradient(top,  #616161 0%,#777777 50%,#3b3b3b 51%,#393939 100%);
    background: linear-gradient(to bottom,  #616161 0%,#777777 50%,#3b3b3b 51%,#393939 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#616161', endColorstr='#393939',GradientType=0 );
    border-radius: 6px;
    color: white;
    min-width: 100px;
    padding: 10px 15px;
    text-align: center;
    text-decoration: none;
}

它可以加载更快,并在不同的DPI设备上渲染更好。

答案 5 :(得分:0)

我会使用带有图像的渐变背景作为不支持渐变的浏览器的备份。图像bg将自动平铺,边框半径将确保圆角。

DEMO http://jsfiddle.net/kevinPHPkevin/hSwJN/16/

#submit {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius:6px;
display: inline-block;
color: #000;
font-family:'Oswald';
font-size: 20px;
padding: 12px 24px;
border:none;
background:url('http://www.islandpoolnspa.com/More_info_button_background_rot_180.jpg');
background: rgb(76,76,76); /* Old browsers */
background: -moz-linear-gradient(top,  rgba(76,76,76,1) 0%, rgba(89,89,89,1) 12%, rgba(71,71,71,1) 39%, rgba(44,44,44,1) 50%, rgba(0,0,0,1) 51%, rgba(17,17,17,1) 60%, rgba(28,28,28,1) 91%, rgba(19,19,19,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(76,76,76,1)), color-stop(12%,rgba(89,89,89,1)), color-stop(39%,rgba(71,71,71,1)), color-stop(50%,rgba(44,44,44,1)), color-stop(51%,rgba(0,0,0,1)), color-stop(60%,rgba(17,17,17,1)), color-stop(91%,rgba(28,28,28,1)), color-stop(100%,rgba(19,19,19,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* IE10+ */
background: linear-gradient(to bottom,  rgba(76,76,76,1) 0%,rgba(89,89,89,1) 12%,rgba(71,71,71,1) 39%,rgba(44,44,44,1) 50%,rgba(0,0,0,1) 51%,rgba(17,17,17,1) 60%,rgba(28,28,28,1) 91%,rgba(19,19,19,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4c4c4c', endColorstr='#131313',GradientType=0 ); /* IE6-9 */
}