html - 如何让 child 不扩展 flex 容器 parent 的宽度?

标签 html css flexbox

我在容器上使用 display:flex 并有一个子标题定义它的宽度。我想要它下面的一些段落文本,但我不希望该段落拉伸(stretch) flex 容器的宽度。我怎样才能做到这一点?

.main {
	display:flex;
	position:relative;
	width:100%;
}

.left {
	position:relative;
	width:100%;
}

.right {
	flex:0 0 auto;
	position:relative;
}
<div class='main'>
  <div class='left'>
    <h1>SOME TITLE</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla commodo pretium dolor, non vestibulum erat ullamcorper a. Cras ac bibendum nibh. Sed eget lectus quis lorem tempus pulvinar.</p>
  </div>
  <div class='right'>
    <h1>ANOTHER TITLE</h1>
    <p>This paragraph should break into lines and expand vertically instead of stretching the parent div horizontally.</p>
  </div>
</div>

最佳答案

将夸张的 flex-shrink 值 100 放在 .right 上,并将 white-space:nowrap 放在标题上就可以了。感谢Michael_B .

.main {
	display:flex;
	position:relative;
	width:100%;
}

.left {
	position:relative;
	width:100%;
}

.right {
	flex:0 100 auto;
	position:relative;
}

.right h1 {
  white-space:nowrap;
}
<div class='main'>
  <div class='left'>
    <h1>SOME TITLE</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla commodo pretium dolor, non vestibulum erat ullamcorper a. Cras ac bibendum nibh. Sed eget lectus quis lorem tempus pulvinar.</p>
  </div>
  <div class='right'>
    <h1>ANOTHER TITLE</h1>
    <p>This paragraph should break lines and expand vertically instead of stretching the parent div horizontally.</p>
  </div>
</div>

关于html - 如何让 child 不扩展 flex 容器 parent 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42335662/

相关文章:

html - IE 的 CSS 调试工具

html - 在 flexbox 列中水平居中文本

html - CSS - 在图像周围堆叠 DIV

javascript - 选择多个下拉选项后运行函数

html - 使用 CSS 以 270 度垂直书写文本

html - 使用 flexbox 在 "card"中垂直居中文本 block 不起作用

html - 显示没有 float 的内联 block

java - 从 html 获取链接返回重复链接的方法

css - 是否可以将 div 对齐到位置为 : fixed? 的 div 的底部

css - flex 盒 : Align last row to grid