简体   繁体   English

即使字体大小发生变化,也能垂直对齐容器内的文本

[英]Vertical Align Text Inside Container even when font size changes

I'm trying to vertical-align: middle a text inside a container with different font sizes.我正在尝试垂直对齐:将具有不同字体大小的容器内的文本居中。 This has been asked before, but I can't get any of the solutions to work for me, What I'm missing?之前有人问过这个问题,但我无法获得任何适合我的解决方案,我错过了什么?

I want the text with different font sizes to align vertically in the middle of the container.我希望具有不同字体大小的文本在容器中间垂直对齐。

Here is the code: .这是代码:。

 let upperGuideText = document.getElementById("upperGuideText"); let guide = "Some text here" setTimeout(function(){ upperGuideText.style.fontSize = "3vw"; upperGuideText.innerHTML = `${guide}`; }, 500); setTimeout(function(){ upperGuideText.style.fontSize = "5vw"; upperGuideText.innerHTML = `${guide}`; }, 2500);
 .upperGuideContainer { position: absolute; overflow: hidden; left: 10vw; top: 51vh; height: 26vh; width: 88vw; display: flex; justify-content: center; align-items: center; outline: 0.1vw dashed orange; } .upperGuide { position: absolute; font-family: 'Open Sans', sans-serif; font-weight: bold; color: rgb(128, 128, 128); left: 0.5vw; top: -11.4vh; opacity: 1; vertical-align: middle; }
 <div class = "upperGuideContainer"> <p id="upperGuideText" class="upperGuide"></p> </div>

You're asking your CSS to do two different things, which is causing your problem.你要求你的 CSS 做两件不同的事情,这导致了你的问题。

the below flexbox properties that you included in the parent container are enough to achieve what you want.您包含在父容器中的以下 flexbox 属性足以实现您想要的。

.upperGuideContainer {
    display: flex;
    justify-content: center;
    align-items: center;
}

When you tell the child <p> to take an absolute position, you're breaking the flexbox properties of the parent当您告诉孩子<p>采取绝对位置时,您正在破坏父级的 flexbox 属性

.upperGuide {
    /* position: absolute; */
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    color: rgb(128, 128, 128);
    /* left: 0.5vw; */
    /* top: -11.4vh; */
    opacity: 1;
    /* vertical-align: middle; */
}

EDIT: If you want to learn more about the magical properties of flexbox, I highly recommend this article .编辑:如果你想了解更多关于 flexbox 的神奇属性,我强烈推荐这篇文章 I reference it constantly.我不断地参考它。

I think you are using position absolute on the child when you don't really need to do it.我认为当您真的不需要这样做时,您正在对孩子使用绝对位置。 once your parent container has "display: flex" as a property you can align things with "align-items" and "justify-content", there is no need for the child to have position absolute unless you need it to for other purposes.一旦您的父容器具有“display: flex”作为属性,您就可以将事物与“align-items”和“justify-content”对齐,除非您出于其他目的需要它,否则子容器无需具有绝对位置。

 .upperGuideContainer { position: absolute; overflow: hidden; left: 10vw; top: 51vh; height: 26vh; width: 88vw; display: flex; justify-content: center; align-items: center; outline: 0.1vw dashed orange; } .upperGuide { /*position: absolute;*/ font-family: 'Open Sans', sans-serif; font-weight: bold; color: rgb(128, 128, 128); /*left: 0.5vw;*/ /*top: -11.4vh;*/ opacity: 1; /*vertical-align: middle;*/ }

As a side note, if you are using position absolute in a container inside of an absolute container, you should avoid using vh (viewport height) and vw (viewport width) as these are values coming from the viewport.作为旁注,如果您在绝对容器内的容器中使用绝对位置,则应避免使用vh (视口高度)和vw (视口宽度),因为这些值来自视口。 I recommend using % or px instead as these are more accurate.我建议改用 % 或 px ,因为它们更准确。

Remove position:absolute , and top from .upperGrid ..upperGrid删除position:absolutetop Because when you use absolute it is positioned according to the nearest containing block that is not absolute.因为当您使用绝对时,它根据最近的非绝对包含块定位。 Also the element is taken out of the flex display.该元素也从 flex 显示中取出。

.upperGuideContainer {
    position: absolute;
    overflow: hidden;
    left: 10vw;
    top: 51vh;
    height: 26vh;
    width: 88vw;   
    display: flex;
    justify-content: center;
    align-items: center;
    outline: 0.1vw dashed orange;
}

.upperGuide {
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    color: rgb(128, 128, 128);
    left: 0.5vw;
    opacity: 1;
    vertical-align: middle;
}

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

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