html - 使用三 (3) 个背景图像 css 的按钮

标签 html css image sass background-image

我正在尝试使用三个背景图像制作一个按钮,以便我们可以为按钮的文本引入翻译并很好地展开。我们可能会为 IE8 添加一个基本样式,但我们的设计师希望我们使用此样式,而我们无法使用纯 CSS3 很好地重新创建它。

图片如下:
Button front end Tiny middle piece to repeat Button rounded end piece

这是 HTML(只是一个简单的按钮,但我认为我还是应该放它:

<button class="back clickable" aria-label="Back" onclick="javascript:history.back();">Back</button>

我已经尝试过一些东西;我将粘贴两次尝试的 CSS。

尝试 1:使用伪选择器
http://jsfiddle.net/c2B6X/

.back {
    background: url("images/back-middle.png") 14px 0 repeat-x;
    color: $white;
    height: 28px;
    padding: 5px;
    &:before {
        background: url("images/back-front.png") 0 0 no-repeat;
        width: 14px;
    }
    &:after {
        background: url("images/back-end.png") 100% 0 no-repeat;
        width: 8px;
    }
}

尝试 2:三个 background-image
http://jsfiddle.net/nPUQN/

.back {
    background: none;
    background-image: url("images/back-middle.png"), url("images/back-end.png"), url("images/back-front.png");
    background-position: 14px 0, 100% 0, 0 0;
    background-repeat: repeat-x, no-repeat, no-repeat;
    border-right: 8px transparent;
    border-left: 14px transparent;
    color: $white;
    height: 28px;
    padding: 5px;

如果它看起来像非典型的 CSS,那是因为我们使用的是 SASS。

是否有明显我遗漏或做错的事情?非常感谢任何关于如何使这项工作的建议。

编辑
由于我得到了太多“有效”的答案,我将标记为在 Chrome、FF 和 IE9 中最有效的答案。

编辑 2
我已经尝试了所有答案,但在 IE9 中没有任何效果。我们必须支持 IE9(和 IE8,但我暂时不会去那里)。我要开始赏金了。任何可以提供适用于 IE9、Firefox 和 Chrome 的答案的人都知道。

最佳答案

伪内容需要content ,因此您首先需要指定:

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

然后要定义任何布局,例如宽度和高度,您需要将伪元素显示为 blockinline-block。 block 布局将强制每个伪元素换行,inline-block 将位于行上,因此您必须使用 float 或绝对定位。

.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;
}

此处为您演示:http://codepen.io/anon/pen/yaJGI

关于html - 使用三 (3) 个背景图像 css 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17625401/

相关文章:

html - CSS - 背景剪辑属性 'Mismatched property value'

javascript - Bootstrap 选项卡标题元素中的复杂 html

HTML-如何让按钮在旁边的按钮被移除后保持在固定位置?

html - 如何将标题 h2 中的图像链接垂直对齐到底部?

image - 为什么我收到错误 : "Not a JPEG file: starts with 0x89 0x50"

python - 具有随机像素颜色的 100x100 图像

javascript - 使用正则表达式仅解析 id

html - 当我运行 Grunt 时,它正在删除我的 CSS 文件

jQuery fadeOut 将不透明度设置为 1

css - ul 列出溢出父 div 的元素