简体   繁体   English

h1/h2 文本被截断并且不在其容器内

[英]h1/h2 text cutting off and not staying inside its container

When I resize my browser's window height while testing my website, there is a horizontal当我在测试我的网站时调整浏览器的 window 高度时,有一个水平在此处输入图像描述 invisible line cutting into my h1/h2 text.隐形线切入我的 h1/h2 文本。 From my research, it seems to be something due to line-height.根据我的研究,这似乎是由于线高造成的。

Thoughts I had:我的想法:

  1. When I don't set a line-height, my h1/h2 height renders to 0px.当我不设置行高时,我的 h1/h2 高度呈现为 0px。 The p tag in the middle seems to be fine, I don't understand why?中间的p标签好像没问题,不明白为什么?

  2. When it does have a line height, the text of the h1/h2 seems to be offset of the container.当它确实有行高时,h1/h2 的文本似乎是容器的偏移量。 When I highlight the h1 in devtools, the text is higher than the box, outside of the margins.当我在 devtools 中突出显示 h1 时,文本高于框,在边距之外。

  3. When I resize my window, the 'invisible line' cutting into my text seems to be in the same spot, while my text moves up and down with the page resizing.当我调整 window 的大小时,切入我的文本的“隐形线”似乎在同一个位置,而我的文本随着页面大小的调整而上下移动。 So when the invisible line is between lines of the h2/h1, the page looks normal.所以当隐形线在 h2/h1 的行之间时,页面看起来很正常。

  4. I have a script animating numbers that sets the innerHTML to the next number.我有一个动画数字的脚本,将 innerHTML 设置为下一个数字。 Maybe the use of.innerHTML is screwing something up?也许 .innerHTML 的使用搞砸了? There are multiple 'invisible lines' when the animation happens, and then when I resize the window, all but one of them disappears.当 animation 发生时,有多条“不可见的线”,然后当我调整 window 的大小时,除了一条之外,它们都消失了。

Sorry for the bad description, but I did my best.抱歉描述不好,但我尽力了。 Could someone point me in the right direction?有人能指出我正确的方向吗?

在此处输入图像描述

<section class="experiences-section-two">
        <div class = "redcliff-metrics-1">
            <h1>We Make Redcliff City RP</h1>
            <p>
                Redcliff City RP is a Roblox game in which players can own lavish homes, cruise around in fancy cars,
                decorate their own avatar with free accessories,
                and create their own unique experiences with the plethora of in game activities.
            </p>
            <p>
                Redcliff City RP has been growing since 2021 with constant updates and new features.
                Our thanks to the community's support is the hard work we put into our games.
            </p>
            <div id = "metrics-holder">
                <div class = "metrics-holder-child">
                    <h2 class = "animated" data-target-number = 191>0</h2>
                    <p><em>MILLION</em><br />Total Visits</p>
                </div>
                <div class = "metrics-holder-child">
                    <h2 class = "animated" data-target-number = 31>0</h2>
                    <p><em>THOUSAND</em><br />Peak CCU</p>
                </div>
                <div class = "metrics-holder-child">
                    <h2 class = "animated" data-target-number = 47.4>0</h2>
                    <p><em>MILLION</em><br />Hours Played</p>
                </div>
            </div>
        </div>
    </section>
{
    color: rgb(40,41,42);
    background-color:#f8b543;
    display:flex;
    flex-direction: column;
    align-items: center;
    padding: 4rem 1rem 4rem;
}

.redcliff-metrics-1 {
    padding: 1.5rem;
    max-width: 615px;
    text-align: center;
    min-height: 500px;
}

.redcliff-metrics-1 > * {
    margin:10px;
    line-height: 100%;
}

#metrics-holder {
    display:flex;
    flex-wrap: wrap;
    flex-direction: row;
    justify-content: space-evenly;
    font-weight:700;
    margin:50px 0;
}

.metrics-holder-cihld {
    padding:10px;
}

.metrics span {
    display:block;
}

.animated {
    font-size: 3.5rem;
    font-weight: 900;
}

em {
    font-style:normal;
    font-size:24px;
}

.experiences-section-two h1 {
    font-size: 55px;
    line-height: 100%;
}

.experiences-section-two h2 {
    font-size: 48px;
    line-height: 100%;
}

.experiences-section-two p {
    font-size: 18px;
}
const holder = document.getElementById("metrics-holder");
const objs = document.getElementsByClassName("animated");
const overlay = document.getElementById("hamburger-overlay");

function animateValue(obj, start, end, duration) {
  let startTimestamp = null;
  const step = (timestamp) => {
    if (!startTimestamp) startTimestamp = timestamp;
    const progress = Math.min((timestamp - startTimestamp) / duration, 1);
    obj.innerHTML = Math.floor(progress * (end - start) + start)+ '+';
    if (progress < 1) {
      window.requestAnimationFrame(step);
    }
  };
  window.requestAnimationFrame(step);
}

function isScrolledIntoView(el) {
    var rect = el.getBoundingClientRect();
    var elemTop = rect.top;
    var elemBottom = rect.bottom;
    return elemTop < window.innerHeight && elemBottom >= 0;
    
}

function metricsAreInView(){
    if(isScrolledIntoView(holder))
    {
        for(let i = 0; i<objs.length; i++)
        {
            let obj = objs[i];
            animateValue(obj, 0, obj.dataset.targetNumber, 3000);
        }

        removeEventListener('scroll', metricsAreInView);
    }
}

addEventListener('scroll', metricsAreInView);

I can't add a comment so adding an answer, can you not use box-sizing to resolve this?我无法添加评论,所以添加答案,你不能使用 box-sizing 来解决这个问题吗? https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

This should then force the h1/h2 to remain within the box rather than cutting part of it.这应该会迫使 h1/h2 留在盒子内,而不是切割它的一部分。

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

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