【问题标题】:multiple background colors for an image图像的多种背景颜色
【发布时间】:2016-10-26 01:43:05
【问题描述】:

我希望实现的是显示具有透明背景层的图像,该图像将位于背景上,该背景将具有白色边框和灰色框,位于图像区域的中心。

基本上给图像一个部分灰色背景,然后将其余部分留白,让图像的外观“浮动”在灰色背景上,并允许我进行简单的 css 更改以更改背景诗句必须重做图像才能改变外观。

这是我尝试过的css:

.borderlist img {
    text-align:center;
    vertical-align:middle;  
    background:
        linear-gradient
          (255,255,255, 0.9),
        url('../images/gray.png') no-repeat;
    max-width: 100%;
    height:auto;

}


And the html:

 <a href="http://www.domain.com/bounty.html"><span class="borderlist"><img src="images/bounty.png" alt="BOUNTY" title=" BOUNTY " width="225" height="155"></span><br>BOUNTY </a>

【问题讨论】:

  • 你有代码吗?
  • 您可以将代码添加到问题中吗?这样,它看起来会更好(具有适当的线端等)。无论如何,答案可能是使用渐变。
  • 你的 html 坏了,你能修一下吗?
  • 已修复并添加到原始问题中...
  • @Peter 不要忘记将最有帮助的回复标记为答案

标签: html css image


【解决方案1】:

我从&lt;a&gt; 中删除了下划线,因为它在&lt;br&gt; 上创建了一个奇怪的下划线。如果您希望文本带有下划线,您可以将其放入&lt;span&gt; 中,并带有一个告诉它拥有它的类。但这就是我得到的。如果您需要它来做一些不同的事情,请告诉我。

.overflowing-img {
  display: inline-block;
  text-align: center;
  text-decoration: none;
}
.undrline {
  text-decoration: underline;
}
.borderlist {
  text-align: center;
}
.borderlist img {
    text-align:center;
    vertical-align:middle;
  background-image: linear-gradient(rgba(160,160,160, 0.5), rgba(160,160,160, 0.5));
  background-repeat: no-repeat;
  background-size: 80% auto;
  background-position: center center;
}
<a href="http://www.domain.com/bounty.html" class="overflowing-img">
  <span class="borderlist">
    <img src="http://pngimg.com/upload/gift_PNG5950.png" alt="BOUNTY" title=" BOUNTY " width="225" height="155">
  </span>
  <br>
  <span class="undrline">BOUNTY<span>
</a>

【讨论】:

  • 三个很好的答案,谢谢大家,我知道我需要做什么,感谢大家提出的所有帮助:)
【解决方案2】:

我使用了不同的图像,但这是您想要实现的目标吗?

.borderlist img {
  width: 200;
  height: 100;
}

.borderlist {
  width: 225px;
  height: 125px;
  text-align: center;
  vertical-align: middle;
  background: linear-gradient(to bottom, #c8c8c8, #ffffff);
  margin: auto;
}

.whiteBorder {
  width: 255px;
  height: 155px;
  background-color: #ffffff;
  text-align: center;
}
<div class="whiteBorder">
  <div class="borderlist">
  <a href="http://www.domain.com/bounty.html">
    <img src="http://en.wikipedia.org/static/images/project-logos/enwiki.png" alt="BOUNTY" title="BOUNTY">
    <br>BOUNTY</a>
  </div>
</div>

【讨论】:

  • 试过了,但没有运气。
  • 谢谢,你的灰色背景比图片大,我需要一个带白色边框的小灰色背景(如上所示),你的背景比维基图片大...跨度>
  • 我明白了。关键是您可以在背景中使用分层 div 来获得“白色边框”(大小为最大的 div),然后使用一个较小的以较大的 div 为中心,并使用灰色渐变来保存图像。根据需要调整大小,但您应该能够看到方法。
【解决方案3】:

尝试在您的 css 中使用 :before 将图像覆盖在 div 的顶部。

div{
  width: 200px; height: 200px;
  background-color: lightgray;
  box-sizing: border-box;
  border: 20px solid white;
  position: relative;
}
div:before{
  content: "";
  display: block;
  margin: -20px;
  width: 200px;
  height: 200px;
  background-image: url('https://i.stack.imgur.com/eLzG5.png');
  background-size: contain;
  background-repeat: no-repeat;
}
&lt;div&gt;&lt;/div&gt;

【讨论】:

  • 三个很好的答案,谢谢大家,我知道我需要做什么了,感谢大家提出的所有帮助:)
【解决方案4】:

让我们先来非常简单:

您不能通过更改 css 将图像向左移动

.borderlist { 
    background: grey;
    border: 60px solid white;
    box-sizing: border-box;
    width: 260px;
}
<div class="borderlist">
<img src="https://s9.postimg.org/d0odjmlcv/dwa.png" height="100px" width="150px" />
</div>

你也可以这样做,创建一个容器 div,在里面,创建灰色 div,然后将图像浮动到灰色 div 上方,像这样(我认为这是最好的):

.borderlist {
    padding: 5%;
    background: white;
    width: 160px;
    height: 120px;
    position: relative;
}
.grey {
  position: absolute;
  background: grey;
  width: 130px;
  height: 90px;  
  margin: 10px;
}

.float {
  position: absolute;
}
<div class="borderlist">
<div class="grey">
</div>
<img class="float" src="https://s9.postimg.org/d0odjmlcv/dwa.png" height="100px" width="150px" />
</div>

随意更改和玩耍以正确理解它

【讨论】:

  • 三个很好的答案,谢谢大家,我知道我需要做什么,感谢大家提出的所有帮助:)
【解决方案5】:

如果您可以使用遮罩(覆盖边缘的白色背景),您可以使用多个背景加上背景颜色。 (感谢用户 Dave Cripps 的演示图像,我无耻地从他的演示中窃取了我的演示图像。)

a {
  text-align:center;
  display:inline-block;
}

.borderlist {
  display:inline-block;
  text-align:center;
  vertical-align:middle;  
  background:
    linear-gradient(to right, white 15%, transparent 15%, transparent 85%, white 85%), linear-gradient(to bottom, white 15%, transparent 15%, transparent 85%, white 85%);
  background-color: #eee;
  transition: background-color 0.4s;

}

a:hover .borderlist {
  background-color: #5C5;
}

.borderlist img {
  height:auto;

}
&lt;a href="http://www.domain.com/bounty.html"&gt;&lt;span class="borderlist"&gt;&lt;img src="http://en.wikipedia.org/static/images/project-logos/enwiki.png " alt="BOUNTY" title=" BOUNTY " width="225" height="155"&gt;&lt;/span&gt;&lt;br&gt;BOUNTY &lt;/a&gt;

【讨论】:

  • 我厚颜无耻地从维基百科上窃取了图片,所以一切都很好! :)
猜你喜欢
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2015-03-29
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多