简体   繁体   English

在 CSS 网格区域之间对齐文本

[英]Align text between CSS grid areas

I have a CSS grid layout - for this question I will use a simple example -> 1 row, 4 columns (2 areas).我有一个 CSS 网格布局 - 对于这个问题,我将使用一个简单的例子 -> 1 行 4 列(2 个区域)。 In grid areas, I have a text with different font sizes.在网格区域,我有一个不同字体大小的文本。 I would like to align the text to each other but can't figure it out.我想将文本彼此对齐,但无法弄清楚。 I didn't find similar questions but maybe I searched for wrong phrases.我没有找到类似的问题,但也许我搜索了错误的短语。

EDIT: Both must be aligned to the bottom line.编辑:两者都必须与底线对齐。 Font-size is calculated.计算字体大小。 Code and example have been modified.代码和示例已被修改。

 .timer { display: grid; grid-template-rows: 1fr; grid-template-columns: 1fr auto auto 1fr; grid-template-areas: "status status status timer"; } .timer-label { grid-area: status; display: flex; font-size: calc(1em + 3vw); background-color: blue; align-items: flex-end; } .timer-value { grid-area: timer; font-size: calc(1em + 9vw); background-color: green; }
 <div class="timer"> <div class="timer-label"> <span>Processing...</span> </div> <div class="timer-value"> <span>00:55</span> </div> </div>

Result:结果:

在此处输入图片说明

Expected result: I would like to have the bottom of "Processing..." text aligned to the bottom of the timer so both are in the same line (red line on print screen).预期结果:我希望“正在处理...”文本的底部与计时器的底部对齐,因此两者都在同一行(打印屏幕上的红线)。

JS Fiddle: https://jsfiddle.net/sores/m1uhLewy/7/ JS小提琴: https : //jsfiddle.net/sores/m1uhLewy/7/

Make Following change in your CSS Code在您的 CSS 代码中进行以下更改

.timer {
    align-content: start;
    display: grid;
    grid-template-rows: auto;
    grid-template-columns: 1fr auto auto 1fr;
    grid-template-areas: "status status status timer";
}

.timer-label {
    grid-area: status;
    display: flex;
    font-size: 30px;
    background-color: blue;
    align-items: flex-end;
    line-height: 70px; /* Add this Line */
}

.timer-value {
    grid-area: timer;
    font-size: 75px;
    background-color:green;
    line-height: 70px; /* Add this Line */
}

Explanation: The problem is your font size is different for the Processing text and the time causing the issue in height, You can specify the line-height property and make the design consistent.说明:问题是处理文本的字体大小和导致高度问题的时间不同,您可以指定 line-height 属性并使设计保持一致。 You can adjust the value of line height based on your preference.您可以根据自己的喜好调整行高的值。

One trick is to bring the font-size of each section to the other one in order to get the same basline.一个技巧是将每个部分的字体大小带到另一个部分以获得相同的基线。 You can do it with by using pseudo element like below:您可以通过使用如下伪元素来做到这一点:

 .timer { display: grid; grid-template-rows: 1fr; grid-template-columns: 1fr auto auto 1fr; grid-template-areas: "status status status timer"; } .timer-label { grid-area: status; font-size: calc(1em + 3vw); background-color: blue; } .timer-value { grid-area: timer; font-size: calc(1em + 9vw); background-color: green; } /**/ .timer-value:before { content:""; font-size: calc(1em + 3vw); } .timer-label:before { content:""; font-size: calc(1em + 9vw); }
 <div class="timer"> <div class="timer-label"> <span>Processing...</span> </div> <div class="timer-value"> <span>00:55</span> </div> </div>

in class .timer-label just change align-items: center;在 .timer-label 类中只需更改 align-items: center; that's it就是这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM