html - 将两个 div 并排放置,当两个 div 的宽度未知时,这应该填满空间

标签 html css

考虑以下 HTML 标记:

 <div class='wrapper'>
    <div class='left'> Text text text </div>
    <div class='right'>
         <button></button>
    </div>
 </div>

假设按钮的宽度可变(因为按钮上显示的文本)。是否可以在纯 css 中将 .left.right div 彼此相邻放置,并且 .left div 应该占用计算完 button 元素的宽度后剩余的空间?

最佳答案

您可以使用 flexbox .

flex: <positive-number>

Makes the flex item flexible and sets the flex basis to zero, resulting in an item that receives the specified proportion of the free space in the flex container. If all items in the flex container use this pattern, their sizes will be proportional to the specified flex factor.Equivalent to flex: 1 0.

Source

.wrapper {
  display: flex;
}

.left {
  flex: 1 1;
}
<div class='wrapper'>
  <div class='left'> Text text text </div>
  <div class='right'>
    <button>button</button>
  </div>
</div>

关于html - 将两个 div 并排放置,当两个 div 的宽度未知时,这应该填满空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45592624/

相关文章:

html - 在 HTML CSS 方面需要帮助

html - 通过 css 设置标签的大小属性?

html - 将高度从一个 div 继承到另一个 div

html - 输入旁边的按钮

html - 如何用CSS去除内联元素底部和Div内边距之间的空白?

html - <div> 作为背景

html - Bootstrap 响应式导航栏折叠错位

html - 在移动页面中将按钮放置在正方形中

HTML 适当的 div 大小

javascript - 菜单性能问题