Button using three (3) background images css

半城伤御伤魂 提交于 2019-12-06 12:34:11

Pseudo-content requires content, so you'll first need to specify that:

.selector::before {
  content: ' ';
}

Then to define any layout such as width and height you'll need to display the pseudo elements as a block or inline-block. Block layout will force each pseudo element to wrap and inline-block will sit on the line so you'll either have to use floats or absolute positioning.

.selector {
  position: relative;
  height: 28px;

  /* allow for the pseudo-elements which do not have layout due to absolute positioning */
  margin: 0 15px;
}
.selector::before,
.selector::after {
  content: ' ';
  position: absolute;
  top: 0;
  width: 15px;
  height: 28px;
}
.selector::before {
  left: -15px;
}
.selector::after {
  right: -15px;
}

Demo here for you: http://codepen.io/anon/pen/yaJGI

You'll need to add content for :before and :after to show. After that, you can position them absolutely and by giving them right: 100% and left: 100% respectively, you can position them in front of and behind the button.

button {
  background:transparent;
  border: none;
  padding: 0;
  margin: 0;
  line-height: 1;
  font-size: 12px;
  cursor: pointer;
  margin-left: 14px; /* width of :before */
}
.back {
    background: url("http://i.stack.imgur.com/DaQcG.png") 14px 0 repeat-x;
    color: white;
    height: 28px;
    padding: 5px;
    position: relative;
}
.back:before {
        position: absolute;
        content: "";
        height: 28px;
        top: 0;
        right: 100%;
        background: url("http://i.stack.imgur.com/6m2HC.png") 0 0 no-repeat;
        width: 14px;
    }
.back:after {
        position: absolute;
        content: "";
        height: 28px;
        top: 0;
        left: 100%;
        background: url("http://i.stack.imgur.com/2WA5B.png") 100% 0 no-repeat;
        width: 8px;
    }

The definitions of before and after are slightly the same, so you could write it down more compactly, but you need to re-sass it anyway. ;)

http://jsfiddle.net/c2B6X/

Tip: Note that downloading three images is less efficient. You can create one image that contains the start and end at the top, and the middle part at the bottom. By positioning the background, you can show the right part inside the elements. This technique is called sprites and it decreases the number of requests to make.

I came up with a little something that you can take a look at. You can modify it to best fit your needs.

http://jsfiddle.net/Xy7Hv/1/

HTML:

<button class="back">Back</button>

CSS:

.back {
    border: none;
    height: 28px;
    padding-right: 8px;
    padding-left: 14px;
    background-image: url("http://i.stack.imgur.com/DaQcG.png"),             
        url("http://i.stack.imgur.com/6m2HC.png"), 
        url("http://i.stack.imgur.com/2WA5B.png");
    background-position: 14px 0px, left, right;
    background-size: 30px 100%, 14px 28px, 8px 28px;
    background-repeat:  no-repeat,no-repeat,no-repeat;
}

("background-size: 30px" is the width of the button, so if all your buttons are the same size it shouldn't be a problem)

with your multiple background version, you could add gradient or white image to build your button bg , keeping some space with padding. http://jsfiddle.net/nPUQN/1/

.back {
    background:
        url("http://i.stack.imgur.com/2WA5B.png") 100% 0 no-repeat ,
        url("http://i.stack.imgur.com/6m2HC.png") 0 0 no-repeat,
       -webkit-linear-gradient(0deg, white 0, white 14px , transparent 14px ,transparent) 0 0 no-repeat ,
        -webkit-linear-gradient(180deg, white 0, white 8px , transparent 8px ,transparent) 0 0 no-repeat ,
        url("http://i.stack.imgur.com/DaQcG.png") 14px 0 repeat 
        ;
    color: $white;
    height: 28px;
    padding: 5px 8px 5px 14px;
}

prefixed for chrome, add other prefix needed or use a prefix js :)

I add this answer because i like to keep the other as it is. This one is to be tested in IE8/9 with pseudo and position: http://codepen.io/gcyrillus/full/lBpaI or to edit : http://codepen.io/gcyrillus/pen/lBpaI

.back {
    background:
        url("http://i.stack.imgur.com/DaQcG.png") 14px 0 repeat 
        ;
    color: white;
    height: 28px;
    padding: 5px;
  position:relative;
  overflow:visible;
}
.back:before {
  content:url(http://i.stack.imgur.com/6m2HC.png);
  top:0;
  left:-14px;
  position:absolute;
}
.back:after {
  content:url(http://i.stack.imgur.com/2WA5B.png);
  position:absolute;
  right:-8px;
  top:0;
}

I used this code today. It's similar to your 2nd example, but uses the background shortcut property and a mixture of position strings.

background: url("../images/img01.png") 0px 0px no-repeat, url("../images/img02.png") 53px 0px repeat-x, url("../images/img03.png") right top no-repeat;

img01 = left image (53px wide)

img02 = fill image

img03 = right image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!