【问题标题】:Background-size:cover makes background image full-size instead of fitting to divBackground-size:cover 使背景图像全尺寸而不是适合 div
【发布时间】:2014-12-18 15:22:32
【问题描述】:

。你好,我想弄清楚为什么当我在通过javascript放置为div背景的图像上使用background-size: cover时,图像不会缩小到div的大小,而是保持全尺寸,只有一部分是在 div 中是可见的。

我曾考虑过在将图像设置为背景图像时使用 javascript 来设置图像属性,但这似乎是一个 hacky 解决方案,并没有解决我显然在某处制造的问题。

任何关于我哪里出错的帮助或见解将不胜感激!非常感谢!

CSS:

#module-background
    {
        text-align: center;
        height: 400px;
        width: 400px;
        margin: 0;
        padding: 0;
        font-size: 2em;
        color: #fff;
        background: #888;
        background-size: cover;

        background-repeat: no-repeat;
        background-attachment: fixed;
    }

    #module-background a 
    {
        color: #fff;
    }

    #module-background h1 
    {
        font-size: 2em;
        padding: 0 0.3em;
        line-height: 1em;
    }

    #module-background p 
    {
        padding: 0 1em;
    }

    #module-background section 
    {
      min-height: 100%;
      text-shadow:  -1px -1px 0 #000,
                    1px -1px 0 #000,
                    -1px 1px 0 #000,
                    1px 1px 0 #000;  
    }

HTML

<div>
    <section  id="module-background">
        <h1 id="city">Dummy Text</h1>
        <p>
            <span id="temperature">
            </span>
            <span id="weather">
                Current temperature and weather conditions
            </span>
        </p>
    </section>
</div>

【问题讨论】:

  • background-size:100% 100%

标签: javascript html css background background-image


【解决方案1】:

从您的 CSS 样式中删除这一行:

background-attachment: fixed;

它应该可以解决问题。

【讨论】:

  • "Theeeeerrrrre gooooesss mmyy hheeeerrrrooooo. 看着他走。"哈哈。谢谢,之前完全忘记了。等待 10 分钟后将接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2022-01-21
  • 1970-01-01
  • 2012-01-02
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多