【问题标题】:There is a gap, and I want my image to go there有一个差距,我希望我的形象去那里
【发布时间】:2016-08-04 18:28:43
【问题描述】:

我想把我的图像放在一个空白处,但我失败了,这里是jsfiddle。如您所见,正确的图像和叠加层没有按应有的方式放置。我希望他们正好在上面的空隙中。

HTML 代码:

<div class = "leftpart fadeInBlock">    
  <a href="http://www.google.com">
    <img src="images/test.jpg"/>
    <div class="flex-caption hvr-fade">Adventurer Lemon</div>
  </a>
  <div class = "sleft1">
    <a href="http://www.google.com">
      <img src="images/test.jpg">
      <div class="flex-caption hvr-fade">Adventurer Lemon</div>
    </a>
    <div class = "sleft2">
      <a href="http://www.google.com">
        <img src="images/test.jpg">
        <div class="flex-caption hvr-fade">Adventurer Lemon</div>
      </a>
    </div>
  </div>
</div>

【问题讨论】:

  • “如您所见,正确的图像和叠加层没有按应有的方式放置。”它应该如何放置?能给我们看看照片吗?
  • 我不确定我理解你的意思。你能画出预期的结果吗?
  • 你有 jsfiddle,你应该阅读下一句

标签: html css image position overlay


【解决方案1】:

见sn-p

.leftpart {
  width: 43%;
  margin-top: 0.7%;
  float: right;
}

.sleft1,.sleft2 {
  float:left;
  width:50%;
}

.leftpart a img {
  width: 100%;
  height: 130px;
  display:block;
}

.leftpart .sleft1 a img {
  
  display:block;
  background-color:blue;
  margin-top:4.5%;
}

.sleft2 a img {
  display:block;

  margin-top:4.5%;
  background-color:red
}


.flex-caption {
  width: 96%;
  padding: 2%;
  top: 0px;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .5);
  color: #fff;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, .3);
  position: relative;
  line-height: 18px;
  box-sizing: border-box;
}

.flex-caption {
  width: 100%;
  padding: 2%;
  top: 0px;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .5);
  color: #fff;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, .3);
  position: relative;
  line-height: 18px;
}
<div class = "leftpart fadeInBlock">	
					<a href="http://www.google.com">
						<img src="images/test.jpg"/>
						<div class="flex-caption hvr-fade">Adventurer Lemon</div>
					</a>
					<div class = "sleft1">
						<a href="http://www.google.com">
							<img src="images/test.jpg">
							<div class="flex-caption hvr-fade">Adventurer Lemon</div>
						</a>
            </div>
						<div class = "sleft2">
							<a href="http://www.google.com">
								<img src="images/test.jpg">
								<div class="flex-caption hvr-fade">Adventurer Lemon</div>
							</a>
						</div>
					</div>
				</div>

你的htmlcss 都有很多问题

HTML

如果您希望 2 个 divsfloat 彼此相邻,则需要将它们作为兄弟姐妹放置在 html 中。你的 .sleft2.sleft1 的孩子。你忘了为.sleft1关闭&lt;/div&gt;

CSS

无需为.flex-caption div 编写两种不同但相同的样式。您只能为两者编写一种样式(如果您希望它们相同)

重要你需要float这两个容器.sleft1.sleft2而不是它们里面的元素!所以从其他元素中删除所有floats 并添加

 .sleft1,.sleft2 {
   float:left;
   width:50%;
 }

.flex-caption 上添加box-sizing:border-box,因为如果不这样做,使用padding:2%.flex-caption 的宽度会超过100%

+一些小的 CSS 更改

如果有帮助请告诉我

【讨论】:

    【解决方案2】:

    .leftpart {
      width: 43%;
      margin-top: 0.7%;
      float: right;
    }
    
    .leftpart a img {
      width: 100%;
      height: 130px;
      display:block;
    }
    .sleft1{
      float: left;
      width: 45%;
    }
    .sleft2{
      float: right;
      width: 45%;
    }
    .leftpart .sleft1 a img, .leftpart .sleft2 a img {
      display:block;
      background-color:blue;
      margin-top:4.5%;
    }
    
    
    
    .flex-caption {
      box-sizing: border-box;
      padding: 2%;
      top: 0px;
      left: 0;
      bottom: 0;
      background: rgba(0, 0, 0, .5);
      color: #fff;
      text-shadow: 0 -1px 0 rgba(0, 0, 0, .3);
      position: relative;
      line-height: 18px;
    }
    <div class = "leftpart fadeInBlock">	
    				<a href="http://www.google.com">
    					<img src="images/test.jpg"/>
    					<div class="flex-caption hvr-fade">Adventurer Lemon</div>
    				</a>
    					<div class = "sleft2">
    						<a href="http://www.google.com">
    							<img src="images/test.jpg">
    							<div class="flex-caption hvr-fade">Adventurer Lemon</div>
    						</a>
    					</div>
    				<div class = "sleft1">
    					<a href="http://www.google.com">
    						<img src="images/test.jpg">
    						<div class="flex-caption hvr-fade">Adventurer Lemon</div>
    					</a>
    				</div>
    			</div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      相关资源
      最近更新 更多