【问题标题】:How to set a background inside a div如何在 div 中设置背景
【发布时间】:2013-04-17 03:10:56
【问题描述】:

我需要的最终结果背景图片:

我使用过的背景图片:

但我有这个Fiddle

::提琴总结::

HTML...

<div id="top-part">
<div id="topmost">
    <div id="top-most" class="wrapper">

    </div>
</div>
<div id="topmenu" class="wrapper">

</div>

CSS...

.wrapper{
      position: relative;
      width: 943px;
      margin: 0 auto;
}
#top-part{
      background: url(img/bg-header-effects.png) no-repeat top center;
}
#topmost{
      background: #900;
      opacity: 0.8;
}
#top-most{
      height: 139px;
}
#topmenu{
      background: #900;
      opacity: 0.8;
      height: 51px;
      border-radius: 0 0 20px 20px;
}

【问题讨论】:

  • 在顶部(topmost 和 topmenu)内设置背景图片
  • @Boaz- 嘿!这不是您的链接重复
  • 查看我更新的问题可能会让你明白。

标签: html css


【解决方案1】:

更新 - 涵盖您最近的编辑

#header{

    background: #f00 url('http://i.stack.imgur.com/GWVfL.jpg');
    opacity: .6;
    width: 100%;
    height: 189px;
}

Working Fiddle


您可以尝试在 CSS 中使用 background 属性:

div{
    background: url('path_to_your_image.jpg') no-repeat;
}

Learn more about using the background-image property here

注意:

backgroundbackground-image 之间存在差异。在这个答案中,我使用了 background 属性,它基本上采用 CSS 中背景图像的所有可能选项,并让它们在单个调用中使用。

例如,您可以将上面的内容分成两个选择器:

div{
    background-image: url('path_to_your_image.jpg') no-repeat; 
    background-repeat: no-repeat;
}

【讨论】:

  • 由于 w3schools 在 SO 中已被弃用,这里有另一个链接会更好:developer.mozilla.org/en-US/docs/CSS/background-image
  • @Passerby 它不允许我发布该链接任何想法为什么?
  • 我无法实现我想要的,因为我认为我的问题不清楚?请查看更新后的问题。
  • 我认为没有人理解我的问题,所以我用小提琴进行了更新。请仔细阅读我的问题。
  • @user2253835 我认为最简单的方法(如果您不想走下面的路线)是使用 Photoshop 将底部部分变白,然后创建一个图像:) 然后你不必使用两个图像来创建这个大工作。
【解决方案2】:

你可以这样做 this fiddle

html...

<div id="top-part">
  <div id="topmost">
  </div>
</div>
<div id="top-menu" class="wrapper">
  <div id="topmenu">
  </div>
</div>

css...

.wrapper{
    position: relative; 
    width: 943px; 
    margin: 0 auto;
}

#top-part{
   background: url(img/bg-header-effects.png) no-repeat top center;
}
#topmost{
   background: #900; 
   opacity: 0.8; 
   height: 139px;
}
#top-menu{
   background: url(img/bg-header-effects.png) no-repeat 50% 45%; 
   border-radius: 0 0 20px 20px;
}
#topmenu{
   background: #900; 
   opacity: 0.8; 
   height: 51px; 
   border-radius: 0 0 20px 20px;
}

【讨论】:

    【解决方案3】:

    我正在考虑的简单方法是在 div 中包含一个覆盖整个页面的图片。代码将非常简单,但唯一的缺点是图像可能会变形或者可以单击,除非你有这个。

    HTML:

    <div id="backgroundcolor">
                <div id="backgroundimage">
                    </div>
            </div>
    

    CSS:

    #backgroundcolor {
        background-color: #000;
        z-index: 1;
    }
    #backgroundimage {
        background: ("http://cdn.theatlantic.com/static/infocus/election110712/s_e01_37923312.jpg");
        resize: none;
        object-position: center;
        object-fit: initial;
    }
    

    【讨论】:

    • 我无法理解您的回答。请给我演示一下。
    猜你喜欢
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2020-11-17
    相关资源
    最近更新 更多