【问题标题】:spritesheet image used as background fill the container用作背景的 spritesheet 图像填充容器
【发布时间】:2017-09-20 07:12:58
【问题描述】:

我正在尝试使从 spritesheet 中获取的背景图像的选定部分填充其容器中的整个空间。

在这支笔中,背景图像应始终填充字符。同样在调整大小时,这是主要问题。 https://codepen.io/timsim/pen/wdWZWv

如果你使用背景位置,你不知道图像应该有多大,只知道在哪里剪切它。此外,设置高度和宽度假定它需要切割具有高度和宽度尺寸的图像。

#image{
    background-repeat: no-repeat;
    background: url(../assets/spritesheet.jpg);
    background-position: -220px -220px;
    width: 90%;
    height: 90%;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    您想根据容器的大小和要使用的精灵部分来使用background-size

    #image {
        width: 10%;
        padding-bottom: 10%;
        background-color: red;
        background-image: url(https://cdn.codeandweb.com/blog/2016/05/10/how-to-create-a-sprite-sheet/spritestrip.png);
        background-size: 650%;
        background-position: 21% 146%;
        background-repeat: no-repeat;
    }
    <div id="container">
      <div id=image></div>
    </div>

    【讨论】:

    • 谢谢。我在实际使用与第一个不同的图像时遇到了一些问题。您如何从示例中选择第二个?
    • @sanjihan 使用background-position
    • 它的行为真的很奇怪
    • 如果你设置了背景位置,那么整个调整大小都会中断
    • 如果您尝试选择第二个,请更新我的答案。
    猜你喜欢
    • 2016-02-12
    • 2019-01-28
    • 1970-01-01
    • 2012-04-28
    • 2014-08-09
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    相关资源
    最近更新 更多