确保文本不会在按钮中溢出(使用Bootstrap v4)

huangapple go评论70阅读模式
英文:

Make sure text doesn't overflow in button (using Bootstrap v4)

问题

这是我的当前代码。

<div class="col-xl-2 col-lg-12">
    <button class="btn btn-secondary w-100" value=1>But.</button>
</div>
<div class="col-xl-4 col-lg-12">
    <button class="btn btn-secondary w-100" value="1">Button Down</button>
</div>
<div class="col-xl-4 col-lg-12">
    <button class="btn btn-secondary w-100" value="1">Button Up</button>
</div>
<div class="col-xl-2 col-lg-12">
    <button class="btn btn-secondary w-100">But.</button>
</div>

这是它通常的外观。

每个“col”都有自己的规则,以填充列,取决于窗口的大小。但是,有一个分辨率的小窗口,按钮会这样做:

在采用正确的布局之前:

“But.”按钮的内部文本溢出,而“Button down”按钮的文本分成两行。我该如何防止“But.”按钮的文本溢出,并使“Button down”按钮的文本分成两行?我曾经考虑重新编译Bootstrap以重新定义xl网格断点,但这似乎太多了(而且是一种不好的做法,为了解决这样一个小问题而修改Bootstrap)。有没有办法获得我想要的行为?

英文:

Here's my current code.

&lt;div class=&quot;col-xl-2 col-lg-12&quot;&gt;
    &lt;button class=&quot;btn btn-secondary w-100&quot; value=1&gt;But.&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;col-xl-4 col-lg-12&quot;&gt;
    &lt;button class=&quot;btn btn-secondary w-100&quot; value=&quot;1&quot;&gt;Button Down&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;col-xl-4 col-lg-12&quot;&gt;
    &lt;button class=&quot;btn btn-secondary w-100&quot; value=&quot;1&quot;&gt;Button Up&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;col-xl-2 col-lg-12&quot;&gt;
    &lt;button class=&quot;btn btn-secondary w-100&quot;&gt;But.&lt;/button&gt;
&lt;/div&gt;

And this is how it normally looks.
确保文本不会在按钮中溢出(使用Bootstrap v4)

Each col has its own rules so they fill the column depending on the window's size. However, there is a small window of resolution where the buttons do this:

确保文本不会在按钮中溢出(使用Bootstrap v4)

Before adopting the right layout:

确保文本不会在按钮中溢出(使用Bootstrap v4)

The "But." buttons' inner text overflows, while the "Button down" button text splits in two lines. How can I keep the text from the "But." button from overflowing and keep the text from the "Button down" button to be separated into two lines?
I thought I could re-compile Bootstrap to redefine the xl grid breakpoint, but that fills like too much (and a bad practice, to modify Bootstrap like that for such a small issue). Isn't there a way to get the behaviour I want?

答案1

得分: 1

主要问题是我试图使用w-100使按钮的宽度达到100%。问题在于,当屏幕尺寸为手机大小时,行为是符合预期的(按钮水平填满列)。然而,在大屏幕上,在某些分辨率下,w-100会导致按钮比内部文本所需的空间小(因为按钮的宽度是包含它的列的100%)。

为了解决这个问题,我删除了这两个"But."按钮的w-100,并在我的CSS中添加了自己的规则。

@media (max-width: 1199.98px) {
    .btn-full-width {
        width: 100%;
    }
}

因此,默认情况下,按钮占据与内部文本所需的空间一样多,而在较小的屏幕上(XL分界点以下的任何分辨率),它会占据整个列。

关于文本被分成两行的另一个问题,通过在另外两个按钮上使用text-nowrap类来解决了。

<div class="row mb-2">
    <div class="col-xl-2 col-lg-12">
        <button class="btn btn-secondary btn-full-width">But.</button>
    </div>
    <div class="col-xl-4 col-lg-12">
        <button class="btn btn-secondary text-nowrap w-100">Button up</button>
    </div>
    <div class="col-xl-4 col-lg-12">
        <button class="btn btn-secondary text-nowrap w-100" type="button">Button down</button>
    </div>
    <div class="col-xl-2 col-lg-12">
        <button class="btn btn-secondary btn-full-width invert-button" type="button">But.</button>
    </div>
</div>
英文:

The main problem is that I was trying to make the buttons' width 100% with w-100. The thing is that when the screen is phone size, the behaviour is the expected one (the button fills the column horizontally). However, on big screens, there is a small window of resolutions during which w-100 forces the button to be smaller than the space that the inner text needs (since the button width is 100% of the column containing it).

To fix this, I removed the w-100 of those two "But." buttons and add my own rule to my css.

@media (max-width: 1199.98px) {
	.btn-full-width {
	  width: 100%;
	}
  }

So, by default, the button takes as much space as the inner text requires, and on smaller screens (any resolution below the XL breakpoint), it takes the whole column.

The other issue, about the text getting divided on two lines was fixed with the class text-nowrap in the other two buttons.

&lt;div class=&quot;row mb-2&quot;&gt;
    &lt;div class=&quot;col-xl-2 col-lg-12&quot;&gt;
        &lt;button class=&quot;btn btn-secondary btn-full-width&quot;&gt;But.&lt;/button&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-xl-4 col-lg-12&quot;&gt;
        &lt;button class=&quot;btn btn-secondary text-nowrap w-100&quot;&gt;Button up&lt;/button&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-xl-4 col-lg-12&quot;&gt;
        &lt;button class=&quot;btn btn-secondary text-nowrap w-100&quot; type=&quot;button&quot;&gt;Button down&lt;/button&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-xl-2 col-lg-12&quot;&gt;
        &lt;button class=&quot;btn btn-secondary btn-full-width invert-button&quot; type=&quot;button&quot;&gt;But.&lt;/button&gt;
    &lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月27日 19:24:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定