html - 在 CSS 中设置图像周围的背景

标签 html css image background

我插入一张图片并对其进行分类:

<div class="first-img"><img src="images/image1.png" alt="image01" /></div>

现在我想用 CSS 为这张图片设置背景:

.slide .first-img{
  text-align: left;
  width: 5%;
  height: 5%;
  bottom: 0;
  left: 307px;
  top: 119px;
  right: 0;
  position: absolute;
  opacity: 1;
  background: rgba(255,255,255,0.2);
  padding-left: 5px;
  padding-right: 5px;
  padding-top: 30px;
  padding-bottom: 30px;
  margin-bottom: auto;
  margin-left: auto;
  margin-right: auto;
  margin-top: auto;
}

但它所做的是将图像放在背景的正下方。无论我为填充设置什么,图像只会在背景结束后才开始。 如何同步背景和图像的 x 和 y 位置?

最佳答案

你是这个意思吗?

有一个 fiddle - Fiddle Link!

HTML

<div class="first-img"><img src="images/image1.png" alt="image01" /></div>

CSS

.first-img { 
    padding: 30px;
    width: 500px;
    background: url('http://www.placehold.it/500/FF0000');
}

关于html - 在 CSS 中设置图像周围的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23874403/

相关文章:

网站上的 CSS Url 属性?

jquery - 取消隐藏左列中的元素时,相对定位的右列向下移动

css - 将西装字体转换为网络字体

asp.net - 如何让 ASP.net 对象出现在同一行?

java - 为什么我的 JLabel 图标没有出现?

javascript - 在html中更改子元素onclick父元素的颜色

html - 在网站上对齐元素时遇到问题,div 卡在左上角,按钮未对齐

python-3.x - MIMEImage 未显示在电子邮件正文中/尝试在电子邮件中嵌入图像

php - 无法使用 CSS 将 div 水平对齐

javascript - 如何减慢褪色效果?