How to match width of text to width of dynamically sized image/title?(如何将文本的宽度与动态大小的图像/标题的宽度相匹配?)
问题描述
Please see this codepen: https://codepen.io/allen-houng/pen/XGMjMr?editors=1100#0
<div>
<img src="https://res.cloudinary.com/djcpf0lmv/image/upload/v1549372650/Templates/yoga.jpg" data-radium="true" style=";">
<div class="description">
Fusce consequat. Nulla nisl. Nunc nisl. Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.
</div>
</div>
I have a parent div with two child divs - an image and a description. I'm sizing the image according to viewport height which means the width would be dynamic and responsive. How would I size the corresponding sibling div .description
to match the image's width without javascript?
In other words, how do I turn this:
into this:
Make the container inline-block
(or any shrink-to-fit configuration like table
,inline-grid
,inline-flex
, float
,absolute
etc) then force the width of text to be 0
so the width of the container is defined by the image (the text doesn't contribute to the width) then force the width again to be 100%
using min-width
.parent {
background: pink;
display:inline-block;
}
img {
display: block;
max-height: 70vh;
}
.description {
width:0;
min-width:100%;
}
<div class="parent">
<img src="https://picsum.photos/id/1004/900/600">
<div class="description">
Fusce consequat. Nulla nisl. Nunc nisl. Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.
</div>
</div>
The same trick work even without image where you need any element to be sized based on another one.
Example with text defining the size of another text:
.parent {
background: pink;
display:inline-block;
}
.description {
width:0;
min-width:100%;
}
<div class="parent">
<h2>a title that will define the width</h2>
<div class="description">
Fusce consequat. Nulla nisl. Nunc nisl. Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.
</div>
</div>
Example with text defining the size of the image (the opposite of the first snippet)
.parent {
background: pink;
display:inline-block;
}
img {
width:0;
min-width:100%;
}
<div class="parent">
<img src="https://picsum.photos/id/1004/900/600">
<h2>a title that will define the width</h2>
</div>
<div class="parent">
<img src="https://picsum.photos/id/1004/900/600">
<h2>define the width</h2>
</div>
<div class="parent">
<img src="https://picsum.photos/id/1004/900/600">
<h2>very small</h2>
</div>
It can also work with complex structure.
Example using CSS grid:
.container {
display: inline-grid;
border: 1px solid;
grid-template-columns: auto auto;
}
.container > div {
padding:2px;
border:1px solid green;
}
.auto {
grid-column:1/-1;
width:0;
min-width:100%;
}
<div class="container">
<div>some text here</div>
<div>and here</div>
<img src="https://picsum.photos/id/1004/900/600" class="auto">
</div>
<br>
<div class="container">
<div>some text here</div>
<div>and a long one here</div>
<p class="auto">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ornare lacus at nisi laoreet, a fermentum augue vestibulum. Cras convallis ultrices quam, ac fermentum nibh posuere eu. Cras in pellentesque lorem. In et condimentum justo. Phasellus scelerisque nisi vitae vestibulum volutpat. Duis sit amet augue </p>
</div>
another one with table:
table {
display: table;
border:1px solid green;
}
td {
padding: 5px;
text-align: center;
border:1px solid green;
}
.auto {
width: 0;
min-width: 100%;
display: block;
}
<table>
<tr>
<td>text </td>
<td>more text</td>
<td>A</td>
</tr>
<tr>
<td colspan="3"><img src="https://picsum.photos/id/1004/900/600" class="auto"></td>
</tr>
</table>
<table>
<tr>
<td>long text here</td>
<td>more text</td>
<td>AAAAAAAA</td>
</tr>
<tr>
<td colspan="3"><p class="auto">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ornare lacus at nisi laoreet, a fermentum augue vestibulum. Cras convallis ultrices quam, ac fermentum nibh posuere eu. Cras in pellentesque lorem. In et condimentum justo. Phasellus scelerisque nisi vitae vestibulum volutpat. Duis sit amet augue </p></td>
</tr>
</table>
这篇关于如何将文本的宽度与动态大小的图像/标题的宽度相匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将文本的宽度与动态大小的图像/标题的宽度相匹配?
基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01