降低div顶部和底部的高度(不仅仅是底部)

时间:2017-02-19 12:49:40

标签: html css

我的h1 = {"a" => 1, "b" => 2, "c" => 3} h2 = {"a" => 2, "b" => 2, "d" => 3} 内部有一个h1.each do |key1, value1| h2.each do |key2, value2| if ((h2.include? key1) && (h2.include? value1)) puts "matching h2 key #{h2[key2]}and h1 key #{h1[key1]}" else puts " don not match h2 key #{h2[key2]}and h1 key #{h1[key1]}" end end end ,并且正在使图像的高度减小到div的大小。

当我减小<div>的大小时,它会移除图像的一些底部,因为它位于具有固定高度的容器内,并且溢出被隐藏,这没关系 - 因为这就是我想要的去做。但是,我还想删除一些图像的顶部。

<img>

如果你运行代码片段,你可以看到我的意思的一个例子。

JsFiddle:https://jsfiddle.net/6rqzy5pL/

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
div {
  position: relative;
}

div img {
  position: absolute;
  top: 50%;
  transform: translatey(-50%);
}
&#13;
<p>What the image first looks like:</p>
<img src="https://upload.wikimedia.org/wikipedia/en/6/69/Stop_sign(standard).svg" width="140" height="150">
<p>What the image looks like after removing some of it by putting it inside the div:</p>
<div style="overflow: hidden; padding: 0px; width: 140px; height: 120px;">
  <img src="https://upload.wikimedia.org/wikipedia/en/6/69/Stop_sign(standard).svg" width="140" height="150">
</div>

<p>But... I would like to remove some of the top of the stop sign also - not just the bottom!</p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试:

CSS:

.target_img {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}