高度在容器上的CSS设置

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

css Height on container

问题

我需要一些关于我的网站模板的帮助,我对CSS的高度和它们是如何制作的感到困惑。

为了更好地理解我的问题,我创建了一个CodePen,在这里可以简单地查看我的问题 链接

* {
  margin: 0;
}

.container {
  height: 100vh;
  background: red;
  width: 100wh;
  over-flow: hidden;
  padding: 1.25rem;
  box-sizing: border-box;
}

.container2 {
  border: 2px solid black;
  height: 100%;
  background-color: blue;
}

.boxtop {
  with: auto;
  height: 20px;
  background-color: pink;
}

.gridcontainer {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  height: 100%;
}

.box {
  grid-column: span 4 / span 4;
  background-color: yellow;
  height: 100%;
}
<div class="container">
  <div class="container2">
    <div class="boxtop"></div>
    <div class="gridcontainer">
      <div class="box">d</div>
      <div class="box">d</div>
    </div>
  </div>
</div>

在这个链接中,你会看到黄色部分超出了范围,我希望它停在蓝色部分的末尾。

每个盒子的高度应该是 container2 - boxtop

感谢您的帮助。

英文:

I will need some help for the template of my website I'm lost with CSS heights and how they are made.

To understand my issue better I've created an codepen where its possible to visualise my issue in a simple way link

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

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

* {
  margin: 0;
}

.container {
  height: 100vh;
  background: red;
  width: 100wh;
  over-flow: hidden;
  padding: 1.25rem;
  box-sizing: border-box;
}

.container2 {
  border: 2px solid black;
  height: 100%;
  background-color: blue;
}

.boxtop {
  with: auto;
  height: 20px;
  background-color: pink;
}

.gridcontainer {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  height: 100%;
}

.box {
  grid-column: span 4 / span 4;
  background-color: yellow;
  height: 100%;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;container2&quot;&gt;
    &lt;div class=&quot;boxtop&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;gridcontainer&quot;&gt;
      &lt;div class=&quot;box&quot;&gt;d&lt;/div&gt;
      &lt;div class=&quot;box&quot;&gt;d&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

In this link you will see that the yellow part goes way over and I want it to stop at the end of the blue part.

Each box should have the height of container2 - boxtop

Thanks for your help.

答案1

得分: 0

要修复这个问题,请将 box 的高度更新为 height: calc(100% - 20px);。此外,withover-flow 是无效的 CSS 属性。wh 是无效的 CSS 单位。请将其更新为 overflow: hidden;

英文:

To fix this update box height to this height: calc(100% - 20px);. Also, with and over-flow are invalid CSS properties. wh is invalid CSS unit. Update it to overflow: hidden;

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

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

* {
  margin: 0;
}

.container {
  height: 100vh;
  background: red;
  width: 100vw;
  overflow: hidden;
  padding: 1.25rem;
  box-sizing: border-box;
}

.container2 {
  border: 2px solid black;
  height: 100%;
  background-color: blue;
}

.boxtop {
  height: 20px;
  background-color: pink;
}

.gridcontainer {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  height: 100%;
}

.box {
  grid-column: span 4 / span 4;
  background-color: yellow;
  height: calc(100% - 20px);
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;container2&quot;&gt;
    &lt;div class=&quot;boxtop&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;gridcontainer&quot;&gt;
      &lt;div class=&quot;box&quot;&gt;d&lt;/div&gt;
      &lt;div class=&quot;box&quot;&gt;d&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

首先,一些拼写错误:wh 是无效的单位,with 不是 widthover-flow 应该是 overflow

  • 使用 CSS flex
  • 如果 .boxtop 中有内容,你不一定需要 20px height。让 flex 为你计算必要的空间。
  • 使用 dvh 单位来表示动态视口高度。
* { margin: 0; box-sizing: border-box; }

.container {
  height: 100dvh;
  display: flex;
  background: red;
}

.container2 {
  flex: 1;
  display: flex;
  flex-direction: column;
  margin: 1.25rem;
  border: 2px solid black;
  background-color: blue;
}

.boxtop {
  background-color: pink;
}

.gridcontainer {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
}

.box {
  grid-column: span 4 / span 4;
  background-color: yellow;
}
<div class="container">
  <div class="container2">
    <div class="boxtop">top</div>
    <div class="gridcontainer">
      <div class="box">box 1</div>
      <div class="box">box 2</div>
    </div>
  </div>
</div>
英文:

First of, some typos: wh is an invalid unit, with is not width, over-flow should be overflow

  • use CSS flex
  • you don't necessarily need the 20px height if you'll have content in the .boxtop. Let flex calculate the necessary spaces for you
  • use the dvh unit for dynamic viewport height

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

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

* { margin: 0; box-sizing: border-box; }

.container {
  height: 100dvh;
  display: flex;
  background: red;
}

.container2 {
  flex: 1;
  display: flex;
  flex-direction: column;
  margin: 1.25rem;
  border: 2px solid black;
  background-color: blue;
}

.boxtop {
  background-color: pink;
}

.gridcontainer {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
}

.box {
  grid-column: span 4 / span 4;
  background-color: yellow;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;container2&quot;&gt;
    &lt;div class=&quot;boxtop&quot;&gt;top&lt;/div&gt;
    &lt;div class=&quot;gridcontainer&quot;&gt;
      &lt;div class=&quot;box&quot;&gt;box 1&lt;/div&gt;
      &lt;div class=&quot;box&quot;&gt;box 2&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

你需要正确选择单位,使它们彼此兼容。例如,在你的原始代码中,你将容器高度设置为vh单位,内边距为rem单位,boxtop为px单位,盒子高度为**%单位。这并没有帮助你进行可视化元素大小所需的计算。如果你将上述所有内容都用vh**单位来测量,你可以这样做:

*{
  margin:0;
}
.container{
  height: 100vh;
  background:red;
  width:100%;
  overflow:hidden;
  padding:5vh;
  box-sizing:border-box;
}
.container2{
  border:2px solid black;
  height:100%;
  background-color:blue;
}
.boxtop{
  width:auto;
  height:5vh;
  background-color:pink;
}
.gridcontainer{
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  height:100%;
}
.box{
  grid-column: span 4 / span 4;
  background-color:yellow;
  height: 85vh;
}

在这里,我将总高度保持为100vh,上下内边距分别为5vh,而盒子顶部高度为5vh。因此,为了适应网格,盒子高度应为**(100-5-5-5)vh85vh**。如果你调整窗口大小,这也应该是响应式的,因为所有元素都使用相同的垂直高度单位。希望这有所帮助。

英文:

You will need to choose the units properly so that they are compatible with each other. For example, in your original code, you have set the container height in vh units, padding in rem units, boxtop in px units, and box height in % units. That is not helping you make the calculations needed to visualize the element sizes. If instead you measured all of the above in vh units, you could do something like this:

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

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

*{
  margin:0;
}
.container{
  height: 100vh;
  background:red;
  width:100%;
  overflow:hidden;
  padding:5vh;
  box-sizing:border-box;
}
.container2{
  border:2px solid black;
  height:100%;
  background-color:blue;
}
.boxtop{
  width:auto;
  height:5vh;
  background-color:pink;
}
.gridcontainer{
  display: grid;
   grid-template-columns: repeat(12, minmax(0, 1fr));
  height:100%;
}
.box{
  grid-column: span 4 / span 4;
  background-color:yellow;
  height: 85vh;
  
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;container2&quot;&gt;
    &lt;div class=&quot;boxtop&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;gridcontainer&quot;&gt;
      &lt;div class=&quot;box&quot;&gt;d&lt;/div&gt;
      &lt;div class=&quot;box&quot;&gt;d&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Here, I have kept the total height as 100vh, padding top and bottom I have applied 5vh each, and the boxtop height is 5vh. So the box height, in order to fit the grid, should have to be (100-5-5-5)vh or 85vh. This should also be responsive if you resize the window because all the elements use the same vertical height unit. Hope this helps.

huangapple
  • 本文由 发表于 2023年7月7日 03:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632053.html
匿名

发表评论

匿名网友

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

确定