html - 在 div 中垂直对齐具有响应字体大小的文本 div

标签 html css vertical-alignment font-size

我有这个 div :

enter image description here

我尝试使用此代码使文本的字体大小响应:

font-size:calc(xx + 1.5vw);

如果我将屏幕变小,标题将不会垂直对齐:

enter image description here

HTML:

<div id="topsection">
<div id="header1">HEADER 1</div>
<div id="header2">Header 2</div>
</div>

CSS:

#header1 {
margin-left:220px;
font-size:calc(20px + 1.5vw);
color:#fff;
}

#header2 {
color: #fff;
margin-left:220px;
font-size:calc(9px + 1.5vw);
}

#topsection {
background-color:#222939;
width:100%;
height:75px;
z-index:1;
position:absolute;
top:10px;
left:0;
color:white;
}

我试过像这样更改标题的 CSS:

#header1 {
display: inline-block;
vertical-align: middle;
margin-left:220px;
font-size:calc(20px + 1.5vw);
color:#fff;
}

#header2 {
display: inline-block;
vertical-align: middle;
margin-left:220px;
font-size:calc(9px + 1.3vw);
color: #fff;
}

但现在看起来像这样:

enter image description here

最佳答案

您需要一个内容 div 居中。请参见下面的示例。仅在 Chrome 中对此进行了测试。

.toolbar{
background-color:#222939;
width:100%;
height:195px;
line-height:195px;
z-index:1;
position:absolute;
top:0;
left:0;
color:white;


}
.toolbar .content{
display:inline-block;
vertical-align: middle;
}
.toolbar .title {


line-height:1;

font-size:calc(20px + 1.5vw);
color:#fff;
}

.toolbar .subtitle {


line-height:1;
font-size:calc(9px + 1.5vw);
color: #fff;
}
 <div class="toolbar">
<div class="content">
<div class="title">HEADER 1</div>
<div class="subtitle">Header 2</div>
</div>
</div>

最终代码

<!DOCTYPE html>
<html>
<head>
    <style>
        .toolbar{
background-color:#222939;
width:100%;
height:195px;
line-height:195px;
z-index:1;
position:absolute;
top:0;
left:0;
color:white;
}
.toolbar .content{
display:inline-block;
vertical-align: middle;
}
.toolbar .title {
line-height:1;
font-size:calc(20px + 1.5vw);
color:#fff;
}

.toolbar .subtitle {
line-height:1;
font-size:calc(9px + 1.5vw);
color: #fff;
}
    </style>

    </head>
<body>
     <div class="toolbar">
<div class="content">
<div class="title">HEADER 1</div>
<div class="subtitle">Header 2</div>
</div>
</div>
</body>
</html>

关于html - 在 div 中垂直对齐具有响应字体大小的文本 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38635932/

相关文章:

javascript - 如何修复 JavaScript HTML 元素未显示错误?

html - 如何将 100% 高度设置为动画 div

jquery - 无法实现加载栏

html - 文本的垂直对齐 - Bootstrap

javascript - 使用 jquery 滚动时替换 html 类

javascript - Canvas native 方法占用大量 RAM

javascript - 如何修复 <LI> 中用户可以使用大写字母或小写字母的搜索?

html - 在站点标题中的 3 个图标后显示文本

html - 在 div 内垂直对齐图像和跨度

jquery - 使用jquery调整div大小并向上移动