使 flex 容器采用内容的宽度,而不是宽度 100% [英] Make flex container take width of content, not width 100%

查看:35
本文介绍了使 flex 容器采用内容的宽度,而不是宽度 100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我有一个具有以下样式的按钮...

.button-flexbox-approach {/* 其他按钮样式 */显示:弹性;对齐内容:居中;对齐项目:居中;填充:1.5rem 2rem;}

http://codepen.io/3stacks/pen/JWErZJ

但它会自动缩放到 100% 而不是内容的宽度.您可以设置明确的宽度,但这样就不允许您的文本自然换行.

如何制作一个按钮,其内部文本以 flexbox 居中,但不会增长以适应容器的大小?

.container {宽度:100%;高度:100%;}.按钮 {/* 简单地弯曲按钮使它增长到容器的大小...我们如何使它只成为其中内容的大小?*/显示:弹性;对齐内容:居中;对齐项目:居中;-webkit 外观:无;边界半径:0;边框样式:实心;边框宽度:0;光标:指针;字体粗细:正常;行高:正常;边距:0;位置:相对;文本对齐:居中;文字装饰:无;填充:1rem 2rem 1.0625rem 2rem;字体大小:1rem;背景颜色:#D60C8B;边框颜色:#ab0a6f;颜色:#fff;}.一 {边距底部:1rem;}.二 {/* 你可以在上面设置一个明确的宽度,但是你会失去里面内容的灵活性 */宽度:250px;}

<p>按钮 1 没有宽度弯曲,它会增长到其容器的大小</p><p><a class="button one" href="#">Button</a></p><p>按钮 2 弯曲了一个明确的宽度,使其更小,但我们没有改变内容的灵活性</p><p><a class="button two" href="#">Button 2</a></p>

解决方案

在容器上使用 display: inline-flex 代替 display: flex.

这会将容器从块级(占用其父级的整个宽度)切换到行内级(占用其内容的宽度).

这种大小调整行为类似于 display:blockdisplay: inline-block.

有关相关问题和解决方案,请参阅:

In the below example, I have a button with the following styles...

.button-flexbox-approach {
    /* other button styles */
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1.5rem 2rem;
}

http://codepen.io/3stacks/pen/JWErZJ

But it automatically scales to 100% instead of the width of the content. You can set an explicit width, but then that doesn't allow your text to wrap naturally.

How can I make a button that has the inner text centered with flexbox, but doesn't grow to fit the size of the container?

.container {
  width: 100%;
  height: 100%;
}

.button {
/*   Simply flexing the button makes it grow to the size of the container... How do we make it only the size of the content inside it? */
  display: flex;
  justify-content: center;
  align-items: center;
  -webkit-appearance: none;
  border-radius: 0;
  border-style: solid;
  border-width: 0;
  cursor: pointer;
  font-weight: normal;
  line-height: normal;
  margin: 0;
  position: relative;
  text-align: center;
  text-decoration: none;
  padding: 1rem 2rem 1.0625rem 2rem;
  font-size: 1rem;
  background-color: #D60C8B;
  border-color: #ab0a6f;
  color: #fff;
}

.one {
  margin-bottom: 1rem;
}

.two {
/* You can put an explicit width on that, but then you lose the flexibility with the content inside */
  width: 250px;
}

<div class="container">
  <p>
    Button one is flexed with no width and it grows to the size of its container
  </p>
  <p>
    <a class="button one" href="#">Button</a>
  </p>
  <p>
    Button two is flexed with an explicit width which makes it smaller, but we have no flexibility for changing the content
  </p>
  <p>
    <a class="button two" href="#">Button 2</a>
  </p>
</div>

解决方案

Instead of display: flex on the container, use display: inline-flex.

This switches the container from block-level (which takes the full width of its parent) to inline-level (which takes the width of its content).

This sizing behavior is similar to display: block vs. display: inline-block.

For a related problem and solution see:

这篇关于使 flex 容器采用内容的宽度,而不是宽度 100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆