如何设置与父元素相同的高度

时间:2019-02-10 18:06:14

标签: html css vertical-alignment katex

我想知道将p元素中的文本对齐为垂直居中。

这是我的代码:

p.task{font-family: Arial,Helvetica Neue,Helvetica,sans-serif; font-weight: 700;  display: table-cell; vertical-align: middle;}
span.label {float: left; width: 60px; color: red}
span.text {float: left; width: calc(100% - 60px);}
<link href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/katex.css" rel="stylesheet"/>

<p class="task">
  <span class="label">2</span>
  <span class="text">Vypočtěte jednu třetinu z \( 3^{3k+3} \) , kde \( k \space \epsilon \space Z \).</span>
</p>

<script src="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/katex.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/contrib/auto-render.min.js" 
    onload="renderMathInElement(document.body);"></script>

1 个答案:

答案 0 :(得分:2)

vertical-center类添加到 p

.vertical-center {
    display: flex;
    align-items: center;
}

或可以在 task 类中添加以上两个样式规则。

.task {
    font-family: Arial,Helvetica Neue,Helvetica,sans-serif; 
    font-weight: 700;
    display: flex;
    align-items: center;
}