【问题标题】:Problem while scrolling an article element in HTML5在 HTML5 中滚动文章元素时出现问题
【发布时间】:2021-12-25 23:10:26
【问题描述】:

我正在尝试使用 html5 在文章元素中实现垂直滚动,但它没有按我的意愿工作。如果我添加

,而不是保持固定尺寸

元素增加文章的高度。 我在这里发布代码和结果。

/*Especificidad 003*/

body section article {
  display: grid;
  grid-template-columns: auto auto auto auto auto;
  grid-column-start: 1;
  grid-column-end: 6;
  overflow-y: scroll;
  padding: 5%;
  margin: 5%;
  background-color: white;
  border: 0.5em;
}


/*Especificidad 004*/

body section article p {
  display: grid;
  grid-template-columns: auto auto auto auto auto;
  grid-column-start: 1;
  grid-column-end: 6;
}
<section>
  <h1>Calculadora RPN</h1>
  <article>
    <h2>Contenido de la pila</h2>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
    <p>Prueba</p>
  </article>
</section>

结果是这个,高度应该小得多,滚动应该可以工作,但它不能:

例如,如果我只添加 3 个元素,那就会发生:

【问题讨论】:

    标签: html css scroll


    【解决方案1】:

    为了做到这一点,您的文章需要一个固定的高度。

    body section article{
        display: grid;
        grid-template-columns:auto auto auto auto auto;
        grid-column-start: 1;
        grid-column-end: 6;
    
        overflow-y: scroll;
        padding:5%;
        margin:5%;
        /* for example */
        height: 200px;
        background-color: white;
        border: 0.5em;
    }

    【讨论】:

    • 非常感谢!!!多么愚蠢的事情!!! :D
    • 很高兴,如果您希望滚动条只显示隐藏内容,而不是一直显示,您可以将 overflow-y: scroll; 替换为 overflow-y: auto;
    • 在语义上你是不正确的。为了使其工作,容器需要小于计算出的内容高度。容器不一定需要固定高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2022-11-30
    • 2015-11-04
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 2016-10-10
    相关资源
    最近更新 更多