简体   繁体   English

使ap与放置在另一个div中的图像具有相同的宽度/高度

[英]Make a p have the same width/height as an image that is placed in another div

I am trying to make ap always have the same width and height as an image that is placed next to it. 我试图使ap始终与旁边放置的图像具有相同的宽度和高度。

I set specific dimensions to both of the divs containing those elements, and then set the img width and height to 100% (as mentioned here ). 余设置特定尺寸两者都含有这些元素的div的,然后设置在img宽度和高度为100%(如所提到的在这里 )。 Unfortunately that led to images getting distorted even if I used the dimensions of the actual images being provided. 不幸的是,即使我使用提供的实际图像的尺寸,也会导致图像失真。 Forgot to mention that I use Picturefill by the way, perhaps it has something to do with that.. 忘了提我顺便使用Picturefill,也许与它有关。

I should mention that it is possible to somewhat control the amount of distortion, by adjusting the values in the sizes attribute of the picturefill HTML. 我应该提到,可以通过调整picturefill HTML的sizes属性中的值来稍微控制变形量。 However it turned out that I would have to add thousands of different values to that attribute to actually make it work this way. 但是事实证明,我必须向该属性添加成千上万个不同的值,才能使其以这种方式实际工作。 It just didn't really feel like the correct approach to something like this. 只是真的不觉得这是正确的方法。

HTML 的HTML

<div> 
  <img 
     sizes="(min-width: ...px) ...vw, (min-width: ...px) ...vw etc"
     srcset="/images/img1.jpg 280w,
             /images/img2.jpg 350w"
     alt="...">
</div>

<div> 
 <p> ... </p> 
</div>

Set the image width to 100% and height to auto . 将图像width设置为100% ,将heightauto That way, the image will preserve its aspect ratio while still being resized. 这样,图像将保持其宽高比,同时仍可调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM