当前位置: 首页 > 知识库问答 >
问题:

有一个与兄弟图像相同高度的div

江天宇
2023-03-14

我试图做一个面板的响应性设计,由一个图像和另一个容器组成,内部有自己的元素,两个并排。

我有一个父div,里面有一个img和另一个div标签。这两个子标记是浮动块,宽度为%,可以工作。

问题是,当我收缩页面时,img会收缩以遵守%width规则,其高度也会收缩,并且应该保持这种方式,但是侧面的div不会将其高度更改为与同级图像相同。

我将它们包裹在另一个div中,并将内部div放在高处:100%希望这样做能起到作用,但什么也没有。

我想让右边的div根据左边的图像改变它的高度。

.img-container {
    display: block;
    width: 59%;
    float: left;
}
    
img {
    width: 100%;
}

.product-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 41%;
    float: left;
    background-color: #e4d233;
}
      
.product-img {
    width: 45%
}
<div class="imgProductContainer">
	<div class="img-container">
		<img src="http://pngimg.com/uploads/running_shoes/running_shoes_PNG5815.png"/>
	</div>
	<div class="product-container">
		<img id="product2Img" class="product-img" src="http://pluspng.com/img-png/png-gym-shoes-shoe-away-hunger-is-a-partnering-program-where-footwear-is-turned-around-to-provide-an-eco-friendly-means-of-support-for-our-feeding-the-futures-programs-520.png">
		<p><span id="product2Name"></span><br>
		<span>2.00 €</span></p>
	</div>
</div>

共有2个答案

耿玄裳
2023-03-14

您可以使用CSS Grid强制容器的高度相等:

.img-container {
    display: flex;
    width: 100%;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
    
img {
    width: 100%;
}
.imgProductContainer {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.product-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    background-color: #e4d233;
}
      
.product-img {
    width: 100%;
}
<div class="imgProductContainer">
	<div class="img-container">
		<img src="http://pngimg.com/uploads/running_shoes/running_shoes_PNG5815.png"/>
	</div>
	<div class="product-container">
		<img id="product2Img" class="productImg" src="http://pluspng.com/img-png/png-gym-shoes-shoe-away-hunger-is-a-partnering-program-where-footwear-is-turned-around-to-provide-an-eco-friendly-means-of-support-for-our-feeding-the-futures-programs-520.png">
		<p><span id="product2Name"></span><br>
		<span>2.00 €</span></p>
	</div>
</div>
吕衡
2023-03-14
.imgProductContainer {
  display: flex;
  flex-direction: row;
  /*
    justify-content: center;
    align-items: center;
  */
}

.img-container {
  width: 59%;
}

img {
  width: 100%;
}

.product-container {
  width: 41%;
  background-color: #e4d233;
}

.product-img {
  width: 45%
}
<div class="imgProductContainer">
  <div class="img-container">
    <img src="http://pngimg.com/uploads/running_shoes/running_shoes_PNG5815.png" />
  </div>
  <div class="product-container">
    <img id="product2Img" class="product-img" src="http://pluspng.com/img-png/png-gym-shoes-shoe-away-hunger-is-a-partnering-program-where-footwear-is-turned-around-to-provide-an-eco-friendly-means-of-support-for-our-feeding-the-futures-programs-520.png">
    <p><span id="product2Name"></span><br>
      <span>2.00 €</span></p>
  </div>
</div>
 类似资料:
  • 我知道这个问题已经问过很多次了,但都没有解决我的问题。我有一个滑块(与ACF pro)和我只是希望所有我的图像是相同的高度(这意味着:所有的肖像img有相同的高度(并改变他们的宽度),作为一个景观img)。 这里是我当前的代码:HTML CSS JS Slick网站给出的例子是: 在此输入图像说明

  • 问题内容: 加号()用于下一个兄弟姐妹。 以前的兄弟姐妹有同等学历吗? 问题答案: 不,没有“先前的兄弟”选择器。 与此相关的是,它用于一般后继兄弟(这意味着该元素在此之后,但不一定紧随其后),并且是CSS3选择器。是下一兄弟姐妹,是CSS2.1。 看到相邻的兄弟组合子从选择器3级和5.7相邻同胞选择从层叠样式表级别2版本1(CSS2.1)规范]。

  • 问题内容: 我正在尝试创建由产品照片组成的图像墙。不幸的是,它们都具有不同的高度和宽度。如何使用CSS使所有图像看起来都一样大小?最好是100 x 100。 我正在考虑做一个高度和宽度为100px的div,然后再进行一些填充。不知道该怎么做。 我该怎么做? 问题答案:

  • 问题内容: 我本质上是在尝试创建一个“ figure”元素的版本(HTML5中即将推出),在该图像的下面有一个简短说明的图像。 但是,我想将整个元素的宽度限制为图像的宽度,以使文本不比图像宽(必要时可以换成多行)。 基本HTML: 我精通使用CSS,但我想不出任何纯CSS解决方案,而无需添加到相匹配的图像宽度。 更新 :经过一番搜索和思考,最好的方法似乎是在div上使用内联宽度。如果我希望div比

  • 问题内容: 在下面的代码中,我希望带有“ y”的div将div的高度与3个“ x”相匹配。 需要注意的是内部div是浮动的。 问题答案: 如果您不反对使用jQuery,可以使用EqualHeight,它应该做您想要的

  • 问题内容: Safari和Chrome以及Opera和Firefox可以处理伪类和相邻兄弟选择器: 这可行。 但是,当添加另一个相邻的兄弟姐妹时: Webkit分崩离析。 但是,如果您先将鼠标悬停在该样式上, 然后将 鼠标悬停在该样式上,则会按照应有的方式应用样式。 我进一步感到困惑,因为如果添加: 无论是否声明了样式,它都会按预期方式开始工作。 我在以下位置看到此问题: 谷歌浏览器15.0.87