html - 自动换行所有文本并使 div 适合 child 的大小?

标签 html css

我有以下 html 代码:

<div class="container">
    This is just some text. This is just some text. This is just some text.<br><br>
    <label>Input: <input type="text"></label>
</div>

我需要 div 是 <label> 的宽度以及要换行的文字。

没有文本,我可以制作 .container 内联 block ,使其适合标签,但否则它会随文本一起拉伸(stretch)。

How it should loook

这可能吗?

最佳答案

以下示例使用 CSS 来完成此操作:display: table-caption;div 上,以及 white-space: nowrap;标签上。

单击下面的“运行代码片段”按钮以查看其实际呈现方式。 (它只是带有嵌入式 CSS 的 HTML;没有 JavaScript。)

<div class="container" style="display: table-caption;">
	This is just some text. This is just some text. This is just some text.<br><br>
	<label style="white-space: nowrap;">Input: <input type="text"></label>
</div>

关于html - 自动换行所有文本并使 div 适合 child 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26263514/

相关文章:

html - 如何在 CKEditor 中创建自定义 html 模板?

html - 修复页脚表格/div 问题

调整元素大小的 CSS 变换

html - 分页前 : avoid; Why is there still a page break?

html - 如何在 bootstrap 中设置边距?

html - 有什么简单的方法可以确定影响 HTML 元素大小的因素吗?

css - 设置显示 : inline for 2 divs doesn't work

html - 是否可以使用 gulp 将带有特定变量的 scss 提取到 css 中?

html - 侧边栏滚动结束时防止内容 div 滚动

javascript - 列表样式在 IE 中未设置为无,但适用于 IE