如何在没有固定容器的情况下填充剩余空间

huangapple go评论53阅读模式
英文:

css, how to fill remaining space without fixed container

问题

I try many similar post like https://stackoverflow.com/questions/90178/make-a-div-fill-the-height-of-the-remaining-screen-space without any success since it's required a fixed height container and i don't have it

我尝试了许多类似的帖子,如https://stackoverflow.com/questions/90178/make-a-div-fill-the-height-of-the-remaining-screen-space,但都没有成功,因为它需要一个固定高度的容器,而我没有它。

I have similar structure

我有类似的结构

#content {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
}

#remaining {
  background-color: red;
}
<body>
  <header>
    可变内容
  </header>
  <main>
    可变内容
    <div id="pagina-id">
      <h1>标题</h1>
      可变内容
      <div class="tabs">
        <ul>
          <li>标签1</li>
          <li>标签2</li>
        </ul>
        <div id="content">
          <div>
            可变内容
          </div>
          <div id="remaining">
            应占据整个页面的剩余空间
          </div>
        </div>
      </div>
    </div>
  </main>
</body>

where #remaining should take all page remaining space.

其中#remaining 应占据整个页面的剩余空间。

all outer containers are not in my total control since are dynamically generated

所有外部容器都不在我的完全控制之下,因为它们是动态生成的。

英文:

I try many similar post like <https://stackoverflow.com/questions/90178/make-a-div-fill-the-height-of-the-remaining-screen-space> without any success since it's required a fixed height container and i don't have it

I have similar structure

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#content {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
}

#remaining {
  background-color: red;
}

<!-- language: lang-html -->

&lt;body&gt;
  &lt;header&gt;
    variable content
  &lt;/header&gt;
  &lt;main&gt;
    variable content
    &lt;div id=&quot;pagina-id&quot;&gt;
      &lt;h1&gt;Title&lt;/h1&gt;
      variable content
      &lt;div class=&quot;tabs&quot;&gt;
        &lt;ul&gt;
          &lt;li&gt;tab1&lt;/li&gt;
          &lt;li&gt;tab2&lt;/li&gt;
        &lt;/ul&gt;
        &lt;div id=&quot;content&quot;&gt;
          &lt;div&gt;
            variable content
          &lt;/div&gt;
          &lt;div id=&quot;remaining&quot;&gt;
            content that should take all page&#39;s remaining space
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/main&gt;
&lt;/body&gt;

<!-- end snippet -->

where #remaining should take all page remaining space.
all outer containers are not in my total control since are dynamically generated

i try to set flex or grid to #content div and set it to height 100% without success

答案1

得分: 2

以下是您要翻译的内容:

"It's really not easy if the structure changes. But as described like you did, it should be possible to use several times flexbox in a column mode as you know almost where the content changes.

It might break some of the rest of the layout of your page because you may have some float or other CSS rules. You'll probably have to adapt a bit.

I added a pagina class to your <div id="pagina-id"> as I expect that the id changes. But you could replace .pagina by [id^="pagina-"] if you cannot change the generated HTML.


/* Change box sizing to simplify matters. */
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}

html {
font: 16px/1.4 Arial, sans-serif;
/* You have to set a height to if not you cannot get min-height working on . */
height: 100%;
background: #eee;
}

body {
margin: 1em;
padding: 1em;
min-height: calc(100% - 2em);
display: flex;
flex-direction: column;
background: white;
box-shadow: 0 0 .5em rgba(0, 0, 0, 0.2);
}

/* Several childs will have to have the same way of growing vertically with a flex in column mode. /
main,
.pagina,
.tabs,
#content {
flex: 1; /
To make it grow vertically */

/* We also want it to be displayed as flex so that we can do the same for the child items. */
display: flex;
flex-direction: column;
}

#remaining {
/* Make it the only cell to grow inside #content. /
flex: 1;
/
Visually show that this cell grows vertically. */
background-color: #f8f8f8;
background-image: linear-gradient(
45deg,
white 25%,
#f8f8f8 25%,
#f8f8f8 50%,
white 50%,
white 75%,
#f8f8f8 75%,
#f8f8f8 100%);
background-size: 3em 3em;
}


variable content


variable content

Title

variable content

  • tab1
  • tab2
variable content
content that should take all page's remaining space



If the structure isn't at all handled by you, then I would switch to some JavaScript code. You'll have to react to all window resize events to re-run the calculation of the height to set to your #remaining element in case the height of the page is lower than the window height. You could use the resize observer in order to detect changes and do this calculation. It's not very clean and sexy but I don't know what else you could do."

英文:

It's really not easy if the structure changes. But as described like
you did, it should be possible to use several times
flexbox
in a column mode as you know almost where the content changes.

It might break some of the rest of the layout of your page because you
may have some float or other CSS rules. You'll probably have to adapt a
bit.

I added a pagina class to your &lt;div id=&quot;pagina-id&quot;&gt; as I expect
that the id changes. But you could replace .pagina by
[id^=&quot;pagina-&quot;] if you cannot change the generated HTML.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

/* Change box sizing to simplify matters. */
html {
  box-sizing: border-box;
}
*, *::before, *::after {
  box-sizing: inherit;
}

html {
  font: 16px/1.4 Arial, sans-serif;
  /* You have to set a height to &lt;html&gt; if not
     you cannot get min-height working on &lt;body&gt;. */
  height: 100%;
  background: #eee;
}

body {
  margin: 1em;
  padding: 1em;
  min-height: calc(100% - 2em);
  display: flex;
  flex-direction: column;
  background: white;
  box-shadow: 0 0 .5em rgba(0, 0, 0, 0.2);
}

/* Several childs will have to have the same
   way of growing vertically with a flex in
   column mode. */
main,
.pagina,
.tabs,
#content {
  flex: 1; /* To make it grow vertically */

  /* We also want it to be displayed as flex so
     that we can do the same for the child items. */
  display: flex;
  flex-direction: column;
}

#remaining {
  /* Make it the only cell to grow inside #content. */
  flex: 1;
  /* Visually show that this cell grows vertically. */
  background-color: #f8f8f8;
  background-image: linear-gradient(
    45deg,
    white 25%,
    #f8f8f8 25%,
    #f8f8f8 50%,
    white 50%,
    white 75%,
    #f8f8f8 75%,
    #f8f8f8 100%);
  background-size: 3em 3em;
}

<!-- language: lang-html -->

&lt;body&gt;
  &lt;header&gt;
    variable content
  &lt;/header&gt;
  &lt;main&gt;
    variable content
    &lt;div id=&quot;pagina-id&quot; class=&quot;pagina&quot;&gt;
      &lt;h1&gt;Title&lt;/h1&gt;
      variable content
      &lt;div class=&quot;tabs&quot;&gt;
        &lt;ul&gt;
          &lt;li&gt;tab1&lt;/li&gt;
          &lt;li&gt;tab2&lt;/li&gt;
        &lt;/ul&gt;
        &lt;div id=&quot;content&quot;&gt;
          &lt;div&gt;
            variable content
          &lt;/div&gt;
          &lt;div id=&quot;remaining&quot;&gt;
            content that should take all page&#39;s remaining space
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/main&gt;
&lt;/body&gt;

<!-- end snippet -->

If the structure isn't at all handled by you, then I would switch
to some JavaScript code. You'll have to react to all window resize
events to re-run the calculation of the height to set to your
#remaining element in case the height of the page is lower than
the window height. You could use the
resize observer in order to detect changes and do this calculation.
It's not very clean and sexy but I don't know what else you could do.

huangapple
  • 本文由 发表于 2023年4月17日 17:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定