简体   繁体   English

带标题的图像-如何伸展和居中?

[英]Image with caption — how to strech and center it?

When I had just an image I had a nice working solution -- I could center (in both axis) an image and also stretch it in such way, that it was no bigger than container and no smaller (it preserved the ratio). 当我只有一幅图像时,我有一个不错的解决方案-我可以将图像居中(在两个轴上)并以这种方式拉伸,使其不大于容器且不小于容器(保留比例)。

Now I would like to add a caption directly under the image in such way, that caption takes the space based on font size, and the image stretch to what is left from the container space. 现在,我想以这种方式直接在图像下方添加标题,该标题将根据字体大小占用空间,并且图像会拉伸到容器空间中剩余的位置。 And I am unable to do it. 而且我做不到。

My previous solution (I left constraint only on height) -- now, not working: 我以前的解决方案(我只限制了高度)–现在,不起作用:

<div style="border: 1px solid;box-sizing: border-box; width: 1000px; padding: 8px; height: 200px; text-align: center; " >
    <span style="display: inline-block; height: 100%; vertical-align: middle;"></span>
    <img style="display:inline-block; max-height:100%; " src="icons/speaking.png" ></img>
    <br/><span style="">TEST</span>
</div>

Other tries, but they work even worse, image is no stretched in any way. 其他尝试,但效果更差,图像丝毫没有拉伸。 table with table-cell tabletable-cell

<div style="border: 1px solid;box-sizing: border-box; width: 1000px; padding: 8px; height: 200px; text-align: center; " >
    <div style="display:table;width:100%;height:100%;max-height:100%;">
        <div style="vertical-align:middle;display:table-cell;max-height:100%;">
            <img style="display:inline;max-height:100%;" src="icons/speaking.png" ></img>
        </div>
    </div>
</div>

Or figure -- no difference really: figure -确实没有区别:

<div style="border: 1px solid;box-sizing: border-box; width: 1000px; padding: 8px; height: 200px; text-align: center; " >
    <div style="display:table;width:100%;height:100%;max-height:100%;">
        <figure style="vertical-align:middle;display:table-cell;max-height:100%;">
            <img style="max-height:100%" src="icons/speaking.png" ></img>
            <figcaption style="">TEST</figcaption>
        </figure>
    </div>
</div>

IMPORTANT: I don't have a working solution, any solution, but I try to solve this problem without any fixed values except for container. 重要说明:我没有可用的解决方案,没有任何解决方案,但是我尝试解决此问题,除了容器外,没有任何固定值。 The reason for this is, then when resize occurs, I have set the dimension of the container, and the content scale accordingly. 原因是,当发生调整大小时,我已经设置了容器的尺寸,并相应地调整了内容的比例。

Big container, the image occupies almost entire height, it cannot fit width to 100%, because the height would be too big (odd look may be the result of my Gimp skills): 大容器,图像几乎占据了整个高度,它不能适合100%的宽度,因为高度会太大(奇怪的外观可能是我的Gimp技能造成的):

容器更大

Small container, the image is squeezed. 小容器,图像被挤压。 Still there is room for text preserved. 仍然有保留文本的空间。

图像较小

The text may look like scaled up (by comparison), but the size of it is the same -- only the image is squeezed, and container is smaller. 文本看起来像是按比例放大(通过比较),但是其大小相同-仅压缩图像,并且容器较小。

If I got you right, you want to set a specific font size to the caption and the image re-size itself to the same width of the caption, maintaining it's aspect ration. 如果我说对了,您想为字幕设置特定的字体大小,然后将图像本身调整为与字幕相同的宽度,并保持其宽高比。

In this case I suggest you to use table a table with two TR and one TD. 在这种情况下,建议您使用带有两个TR和一个TD的表table。 Put image in first row (TR) and the caption in the second row. 将图像放在第一行(TR),将标题放在第二行。 Do not define the width of the TABLE, TD or TD. 不要定义TABLE,TD或TD的宽度。 So whatever font size you will give to the caption in the second row, the column will set its width to that size and the image with 100% width will set itself according to the TD size. 因此,无论您给第二行的标题提供什么字体大小,该列都将其宽度设置为该大小,而宽度为100%的图像将根据TD大小自行设置。

For example, 例如,

 <Table> <TR> <img style="display:inline-block; max-height:100%; " src="icons/speaking.png" ></img> <TD></TD> </TR> <TR> <TD> <br/><span style="">TEST</span> </TD> </TR> <Table> 

If the browsers you want to support allow you to use flexbox, I think you can achieve it in the following fashion. 如果您要支持的浏览器允许您使用flexbox,我认为您可以通过以下方式实现它。 Instead of using a <img> tag for the image I've opted to use a background-image together with background-position: center center and background-size: contain to center and stretch the image. 我没有对图像使用<img>标签,而是选择将background-imagebackground-position: center center一起使用background-position: center centerbackground-size: contain将图像居中和拉伸。 It might be worthwhile to see if you can implement this using a <img> tag. 值得一看的是,是否可以使用<img>标记实现此目标。

I've added display: flex and flex-direction: column on the container .media class. 我在容器.media类上添加了display: flexflex-direction: column If you add flex-grow: 1 on the .media__image class this will make the image container grow until the entire height is used. 如果在.media__image类上添加flex-grow: 1 ,这将使图像容器增大,直到使用了整个高度。

 .media { display: flex; flex-direction: column; width: 200px; height: 200px; } .media__image { flex-grow: 1; background: url('http://placehold.it/150x100') center center no-repeat; background-size: contain; background-color: #f99; } .media__caption { background-color: #9f9; text-align: center; font-family: Helvetica, Arial, sans-serif; padding: 0.5em; color: #333; } 
 <div class="media"> <div class="media__image"></div> <div class="media__caption"> Caption </div> </div> 

See this Codepen for three examples using different sized containers, images and fonts. 有关使用不同大小的容器,图像和字体的三个示例,请参见此Codepen

For background information on using flexbox: 有关使用flexbox的背景信息:

I am really confused.. 我真的很困惑

Just by adding a simple html tag I have achieved what I think you're talking about how ever your question is very unclear. 只需添加一个简单的html标记,我就可以实现我认为您正在谈论的问题还不清楚的地方。

<html>
<head>
<style>
span {
font-size: 100px;
}
</style>
</head>
<body>
<center><img src="http://i.imgur.com/S2E4MDu.png"/>
<br/><span style="">TEST</span></center>
</div>
</body>
</html>

This centers the content and the span for me so all that's left to do is add a style in css for span declaring it to the size that you're looking for.. 这将使内容和跨度居中,因此剩下要做的就是在CSS中添加一个样式,以跨度将其声明为所需的大小。

EDIT: I see that you are also having issues with it inside of a container so I went ahead and tested this as well. 编辑:我看到您在容器中也遇到了问题,所以我继续进行了测试。

.container {
position: relative;
width: 100%;
border: 1px solid red;
}
.container img {
width: 500px;
height: 500px;
}

Simply wrap the container around the image 只需将容器包装在图像周围

<div class="container">
<center><img src="http://i.imgur.com/S2E4MDu.png"/>
<br/><span style="">TEST</span></center>
</div>

And we should be set. 我们应该被设置。

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

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