html - H2 文本宽度低于 h1 文本

标签 html css alignment

所以我有这个 html 代码:

<div>
    <h1>My h1 title</h1>
    <h2>This is my h2 text which needs to be center aligned and in the same time not to pass the h1 title width</h2>
</div>

我希望这个 block 看起来像这个只使用 css 的 block :

h2 width should not pass h1 width

说明:我的 h1 字体大小较大,而我的 h2 字体较小。我希望我的 h2 在 div 中居中对齐并且 div 应该具有我的 h1 文本的宽度。 (h1 应该留在 1 行而不是很多)

我怎样才能做到这一点?谢谢

最佳答案

您可以尝试一个名为 FitText.js 的 jQuery 插件.顾名思义,它适合填充容器的文本,如图所示 here .将您的两个 header 放在同一个 div 中并运行 $("h1,h2").fitText(); 应该给它们相同的宽度。查看他们的 Github page更多示例。

关于html - H2 文本宽度低于 h1 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30593506/

相关文章:

html - 在 chrome 中对齐下拉列表

html - 使用边距和 float 水平对齐 div

html - 为什么我的 UL 背景色没有覆盖 LI 元素?

javascript - 我需要为计算机科学制作石头剪刀布,但我需要添加蜥蜴和史波克

javascript - 在 HTML 或 Javascript 中,如何为每个人存储变量?

javascript - 如何正确切换图标按钮?

html - 基于第n层的动态css

CSS/HTML 水平对齐 div

javascript - 如何使 iframe 不保存到 Chrome 中的历史记录?

html - 如何在html中获取书签的页码