html - :before and :after elements on an img element

标签 html css sass

是否可以在 img 元素上使用前后伪选择器?认为它是但没有任何运气,将 css 切换到 div 并且它工作正常。 .page-overhang 类是 img 元素。

// page overhang mixin
@mixin overhang {
    &:before,
    &:after {
        position: absolute;
        left: 0;
        display: block;
        width: 10px;
        height: 18px;
        background: url(../images/sprite.png) no-repeat;
        content: "";
    }

    &:before {
        top: -18px;
        background-position: 0 -100px;
    }

    &:after {
        bottom: -18px;
        background-position: 0 -119px;
    }
}

/* scss file */
.page-overhang {
    position: relative;
    display: block;
    @include overhang();
}

最佳答案

:before:after 伪元素不能用于 void elements ,因为它们指定要添加到元素实际内容之前或之后的内容,而 void 元素可以没有内容。

关于html - :before and :after elements on an img element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13870635/

相关文章:

javascript - Canvas HTML5 数据点到数组

javascript - 错过中间部分的一页点导航

html - 使用 CSS 动画更改文本的一部分

css - 旋转图像 CSS 动画

javascript - 每次刷新随机交换三种颜色

css - Bootstrap 网格中列之间的间隙

javascript - 定义 Javascript 和 HTML 使用的 DIV/SPAN ID?

html - 如何在 html/css 中淡入淡出动画图像图标

angularjs - 键盘显示时 ion-content 未正确调整

css - 编译 SCSS 生成超过 3 MB 的 CSS 文件