英文:
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.
<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>
And this is how it normally looks.
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:
Before adopting the right layout:
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.
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论