简体   繁体   English

如何防止我的图像溢出内联块容器

[英]How do I keep my image from overflowing an inline-block container

I have a website I am putting together for a client.我有一个网站,我正在为客户建立一个网站。 Everything was going swimmingly until I ran into what seems like a simple problem, but one that I have never had to deal with before and can't find a solution to.一切都进展顺利,直到我遇到了一个看似简单的问题,但我以前从未遇到过并且找不到解决方案。

I'm trying to set up a 2 column deal with a heading and text on one side and an image on the other.我正在尝试设置一个 2 列处理一侧的标题和文本,另一侧处理图像。 I have my image and my text inside respective container divs.我在各自的容器 div 中有我的图像和文本。 The divs have their display set to "inline-block" and thus, the image won't scale down to fit inside it's div. div 的显示设置为“inline-block”,因此,图像不会缩小以适应它的 div。 Is there a simple solution to this?有没有简单的解决方案?

Here is the code:这是代码:

HTML HTML

<div class="col">
    <h1>Header</h1>
    <p>Text</p>
</div>
<div class="col">
    <img src="images/img1.jpg" alt="Image">
</div>

CSS CSS

.col {
    display: inline-block;
    width: 45%;
    vertical-align: top;
    margin-bottom: 20px;
}

Again, I know this is probably super easy (or completely impossible for some reason) but any help would be appreciated.同样,我知道这可能非常容易(或由于某种原因完全不可能),但任何帮助将不胜感激。

Simply add width: 100% to the CSS.只需将width: 100%添加到 CSS。

.col img {
  width: 100%;
}

Or you can put it in the <img> tag:或者你可以把它放在<img>标签中:

<div class="col">
    <img src="images/img1.jpg" alt="Image" width="100%">
</div>

The image will grow proportionally to the maximum width of the parent element.图像将与父元素的最大宽度成比例增长。 If you don't explicitly set the width, it will be presented in its original size.如果您没有明确设置宽度,它将以其原始大小显示。

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

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