简体   繁体   English

将固定宽度设置为父级的 100% 高度

[英]setting fixed width with 100% height of the parent

In the following HTML, I want to set the height of left and right 100% of the parent element.在下面的HTML中,我想设置parent元素left 100% right高度。 In addition, the left div has fixed width.此外, left div 具有固定宽度。 The right should use all of the remaining width. right应该使用所有剩余的宽度。

I think because of using display: flex;我认为是因为使用了display: flex; in the parents div, the width of the left div doesn't stay constant.在父 div 中, left div 的宽度不会保持不变。 How can I set fixed width for it and allocate all of the remaining space to the right.如何为其设置固定宽度并将所有剩余空间分配到右侧。

Edit: the calc(100-52px) is the height of the parent .编辑: calc(100-52px)parent的高度。 The question is only about setting fixed width of 100px to the left so that it doesn't change on resizing the window.问题只是关于将固定宽度设置为左侧 100 像素,以便在调整 window 的大小时不会改变。

在此处输入图像描述

Here's what I'm trying:这是我正在尝试的:

 body { padding: 0; margin: 0; }.parent { background: red; display: flex; height: calc(100vh - 52px); }.left { width: 100px; background: yellow; }.right { background: orange; width: 100%; }
 <div class="parent"> <div class="left">the width should be fixed, not flexible</div> <div class="right">width should be all of the remaining</div> </div>

parent { display: -webkit-box;display:-webkit-flex;display: -ms-flexbox;display:flex;flex-wrap: wrap;父{显示:-webkit-box;显示:-webkit-flex;显示:-ms-flexbox;显示:flex;flex-wrap:包装; } }

.parent > [class*='col-'] { display: flex; .parent > [class*='col-'] { display: flex; flex-direction: column;弹性方向:列; } }

You can use width: calc(100% - 100px) or flex: 1 for the right div.您可以使用width: calc(100% - 100px)flex: 1作为正确的 div。

Percentage values are calculated from the parent element, therefore you need to extract static values from 100% to get the remaining area.百分比值是从父元素计算的,因此您需要从 100% 中提取 static 值以获得剩余面积。

But as you are already using a flex container here, you can just set flex: 1, which is the shorthand for flex-grow: 1, that will allow your container to take all the extra space in the parent container, since no other items are available.但是由于您已经在这里使用了 flex 容器,您可以设置 flex: 1,它是 flex-grow: 1 的简写,这将允许您的容器占用父容器中的所有额外空间,因为没有其他项目可用。

Add a flex declaration to the .left selector:.left选择器添加flex声明:

flex: 0 0 100px;

flex syntax: flex语法:

none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]

So this declaration is stating: "don't grow, don't shrink, define the initial size as 100px"所以这个声明是说:“不要增长,不要收缩,将初始大小定义为 100px”

Read more: flex (MDN)阅读更多: flex (MDN)

If right is to stand 52px away from the far right, then a margin will do.如果右边距离最右边 52 像素,则可以使用边距。 Please clarify your question.请澄清你的问题。

 body { padding: 0; margin: 0; }.parent { background: red; display: flex; height: calc(100vh - 52px); }.left { width: 100px; background: yellow; }.right { flex-grow: 1; background: orange; margin-right: 52px; }
 <div class="parent"> <div class="left">the width should be fixed, not flexible</div> <div class="right">width should be all of the remaining</div> </div>

Set flex: 0 0 100px;设置flex: 0 0 100px; on your .left div (you can remove width: 100px ) if you want it to be a constant 100px - so no growing or shrinking.在您的.left div 上(您可以删除width: 100px ),如果您希望它是一个恒定的 100px - 所以不会增长或缩小。

 body { padding: 0; margin: 0; }.parent { background: red; display: flex; height: calc(100vh - 52px); }.left { flex: 0 0 100px; background: yellow; }.right { background: orange; width: 100%; }
 <div class="parent"> <div class="left">the width should be fixed, not flexible</div> <div class="right">width should be all of the remaining</div> </div>

see if this helps you, comment if you need any changes看看这是否对您有帮助,如果您需要任何更改,请发表评论

stackblits link for 1 fixed width, 1 relative column 1个固定宽度,1个相对列的stackblits链接

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

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