英文:
CSS - two row flexbox layout - having one row's width be determined by the second row's
问题
我有一个相当具体的CSS问题。
请查看codepen,因为几乎所有内容都在那里解释了... 但这里有更多的解释:
有两行(在一个容器内),它们都有相同的基本flexbox实现 -
{
width: auto;
display: flex;
align-items: flex-start;
}
顶部行包含页面标题,右侧有一个标题,右侧有一张图片。
底部行还包含两个div - 左侧的div具有固定宽度,但右侧的div是流动的,取决于其中的文本内容(从页面加载到页面加载会更改 - 实际上这将是一个长度各异的名称列表)。
目标是通过使用CSS来确定顶行的宽度,由底行决定。但我不知道如何在flexbox中做到这一点。
目前,display:flex的容器div的宽度都是浏览器的100%。
底部行的布局和实现已经确定/需要使用flexbox,但顶行在如何完成它的方式上是灵活的。
我熟悉CSS,知道通常不是这样的,一般来说,宽度是从父容器传递下来的... 所以我甚至不确定这是否可能?如果需要的话,我可以分解并在JS中完成它,但我总是更喜欢纯CSS的解决方案!
任何想法将不胜感激!
英文:
I have a fairly specific CSS issue.
Please look at the codepen, as pretty much everything is explained in there... but here is a bit more explanation:
There are two rows (inside a container) that both have the same basic flexbox implementation -
{
width: auto;
display: flex;
align-items: flex-start;
}
The top row houses a page header, with a title on the right, and an image flush right.
The bottom row also houses two divs - the left div has a static width, but the right is fluid, depending on whatever text is inside there (which changes from pageload to pageload - in reality this will be a list of names with varying lengths).
The goal is for the top row's width to be determined by the bottom row, via using CSS. But I have no idea how to do this in flexbox.
Currently the display:flex container divs' widths are both 100% of the browser.
The bottom row's layout and implementation is set in stone / needs to use flexbox, but the top row is flexible in how it is accomplished.
I am familiar with CSS and know this is not usually how it goes, in general width is passed down from the parent container... so I'm not even sure if this is possible? I can break down and do it in JS if needed, but I always prefer a CSS-only solution!
Any thoughts would be greatly appreciated!
答案1
得分: 1
您可以像这样操作。我认为您在代码中只需要在container
和两个弹性框之间再添加一层包装层。
<div class="page">
<div class="container">
<div class="top">
在这里放入任何您喜欢的内容
</div>
<div class="bottom">
<div class="bottomLeft">
这是静态尺寸
</div>
<div class="bottomRight">
<p>
一些文字和内容<br>
一些文字和内容<br>
一些文字和内容<br>
一些文字和内容<br>
一些文字和内容<br>
一些文字和内容<br>
一些文字和内容<br>
</p>
</div>
</div>
</div>
</div>
这是您的代码的HTML部分的翻译。
英文:
You can do something like this. I think all you would need to do in your code is add one more wrapper layer between your container
and your two flex boxes.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.page{
display: flex;
justify-content: center;
border: 1px solid black;
}
.container{
display: flex;
flex-direction: column;
gap: 20px;
}
.top{
width: 100%;
height: 200px;
background-color: lightgreen;
}
.bottom{
display: flex;
gap: 20px;
}
.bottomLeft{
width: 250px;
height: 100px;
background-color: lightblue;
}
.bottomRight{
min-width: 200px;
background-color: pink;
}
<!-- language: lang-html -->
<div class="page">
<div class="container">
<div class="top">
Put whatever you like in here
</div>
<div class="bottom">
<div class="bottomLeft">
This is statically sized
</div>
<div class="bottomRight">
<p>
Some words and stuff<br>
Some words and stuff<br>
Some words and stuff<br>
Some words and stuff<br>
Some words and stuff<br>
Some words and stuff<br>
Some words and stuff<br>
</p>
</div>
</div>
</div>
</div>
<!-- end snippet -->
答案2
得分: 0
以下是您要翻译的代码部分:
.container {
width: auto;
max-width: 1440px;
margin: 0 auto;
}
.basicFlex {
width: auto;
display: flex;
align-items: flex-start;
padding: 0 16px;
border: 1px grey solid;
}
.pageHeader {
flex-direction: row;
}
.pageHeader--title {
flex-grow: 2;
}
img {
height: 100px;
margin-left: 20px;
}
.pageContent {
justify-content: center;
}
.pageContent--staticLeft {
background: red;
width: 100%;
min-width: 220px;
min-height: 200px;
}
.pageContent--fluidRight {
background: DeepSkyBlue;
margin-left: 20px;
max-width: 822px;
}
<div class="container">
<div class="pageHeader basicFlex">
<div class="pageHeader--title">
这是页面标题!我希望它与红色框的左侧对齐。
</div>
<img src="https://rfbi.com.au/wp-content/uploads/Stay-Active-web-icon-100px-x-100px-300x300.jpg"/>
</div>
<div class="pageContent basicFlex">
<div class="pageContent--staticLeft">
<p>目标是使.pageHeader的宽度与.pageContent内部元素的宽度相同(即822px(红色div)+ 20px(间距)+ ???(黄色div的宽度)。</p>
</div>
<div class="pageContent--fluidRight">
<p>根据文本的长度不同(从页面加载到页面加载变化),此div将更宽或更窄。随时进行编辑。文本长度的不同会影响这个div的宽度。</p>
</div>
</div>
</div>
英文:
Not super certain if this is what you really want to happen. First you forgot to have closing paragraph. You can contain your 2 elements in a max-width so they can align the way you want them to be. On your pageContent--fluidRight you can try to put a max width on it so the text will wrap on its own. See sample below if this is what you mean and let me know.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.container {
width: auto;
max-width:1440px;
margin: 0 auto;
}
.basicFlex {
width: auto;
display: flex;
align-items: flex-start;
padding: 0 16px;
border: 1px grey solid;
}
.pageHeader {
flex-direction: row;
}
.pageHeader--title {
flex-grow: 2;
}
img {
height: 100px;
margin-left: 20px;
}
.pageContent {
justify-content: center;
}
.pageContent--staticLeft {
background: red;
width: 100%;
min-width: 220px;
min-height:200px
}
.pageContent--fluidRight {
background: DeepSkyBlue;
margin-left: 20px;
max-width: 822px;
}
<!-- language: lang-html -->
<div class="container">
<div class="pageHeader basicFlex">
<div class="pageHeader--title">
Here is the Page Title! I would like for this to be aligned with the left side of the red box.
</div>
<img src="https://rfbi.com.au/wp-content/uploads/Stay-Active-web-icon-100px-x-100px-300x300.jpg"/>
</div>
<div class="pageContent basicFlex">
<div class="pageContent--staticLeft">
<p>The goal is for the width of .pageHeader to be the same as the width of only the elements inside .pageContent (ie - 822px (the red div) + 20px (margin between) + ??? (the width of the yellow div).</p>
</div>
<div class="pageContent--fluidRight">
<p>Depending on how long this text is<br>
(which varies from pageload to pageload),<br> this div will be wider or more narrow.<br>Feel free to edit. Depending on how long this text isDepending on how long this text isDepending on how long this text isDepending on how long this text isDepending on how long this text isDepending on how long this text is Depending on how long this text isDepending on how long this text isDepending on how long this text is
</p>
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论