Set same button size despite buttons containing different amounts of text.

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

Set same button size despite buttons containing different amounts of text

问题

我正在尝试重新创建下面的布局:

Set same button size despite buttons containing different amounts of text.

按钮的尺寸都相同(除一个外),不论按钮内的文本(包括换行)数量如何。我正在使用 Bootstrap 5 并且已经接近这个布局。我已经使所有按钮的宽度相同,但高度不同。以下是 CSS 代码,以及这里的示例:https://jsfiddle.net/mfen723/g94tr1k7/9/

.btn-warning {
    background: #e6deca;
    color: #000;
    border: #e6deca;
    padding: 2rem;
    border-radius: 50px;
    width: 280px;
}

当我为按钮添加特定的高度时,链接文本就会偏离中心,所以我不知道如何在保持布局响应的情况下实现与上面图片相同的统一按钮大小。

非常感谢任何帮助。

英文:

I am trying to recreate the layout below:

Set same button size despite buttons containing different amounts of text.

The buttons sizes are the same (except one) regardless of the amount of text (including line breaks) inside the button. I am using Bootstrap 5 and have got close to this layout. I have made the buttons all the same width but the height differs. CSS below and fiddle here: https://jsfiddle.net/mfen723/g94tr1k7/9/

.btn-warning {
    background: #e6deca;
    color: #000;
    border: #e6deca;
    padding: 2rem;
    border-radius: 50px;
    width: 280px;
}

When I add a specific height to the button the link text is thrown off centre, so I don't know how I can achieve the uniform button size as per the above image while keeping the layout responsive.

Any help much appreciated.

答案1

得分: 2

Add height to make them same size and use flexbox to center horizontally and vertically with align-items and justify-content.

像这样:

.btn-warning {
  background: #e6deca;
  color: #000;
  border: #e6deca;
  padding: 2rem;
  border-radius: 90px;
  width: 280px;
  height: 160px;

  display: flex;
  align-items: center;
  justify-content: center;
}
英文:

Add height to make them same size and use flexbox to center horizontally and vertically with align-items and justify-content.

Like this:

<!-- language: css -->

.btn-warning {
  background: #e6deca;
  color: #000;
  border: #e6deca;
  padding: 2rem;
  border-radius: 90px;
  width: 280px;
  height: 160px;
  
  display: flex;
  align-items: center;
  justify-content: center;
}

答案2

得分: 1

响应式设计并不意味着每个按钮的大小都相同,而是根据文本内容的不同来调整按钮的大小。可能的方法包括:

  1. 如果你知道某种方式来获取任何按钮的最大高度,那么可以使用min-height CSS属性为所有按钮设置固定高度,从而使所有按钮具有相同的大小。

  2. 如果你不知道最大高度,那么你需要为文本留出足够的空间,并且绝对不能使用overflow: hidden来隐藏文本。

  3. 较好的方法是为按钮设置固定宽度,然后使用min-height来设置至少覆盖大多数按钮的某个值的高度。现在,设计页面,例如以你的图像示例为例,为容器添加display: flex,在桌面上每行设置3个按钮。这样,即使文本增加,高度会增加,但每行仍然包含3个按钮,保持了页面布局的良好性能。对于平板屏幕,设置每行2个按钮。对于移动屏幕,则设置每行单个按钮

当然,你可以根据需要调整页面,但关键是要为文本留出空间,同时保持页面的响应性。希望这对你有帮助。

英文:

Responsive design doesn't mean that each button having same size regardless of the text. Definitely for different text, the button size change. What are the possiblities?

  1. If you know somehow the maximum height of any button, then set that fixed height for all buttons with min-height css property. So, that all buttons have same size.
  2. If you don't know, you have to give space to the text. And definitely, you can't use overflow: hidden to hide the text.
  3. The good approach is to give fixed width to buttons. And for height, use min-height with some value that at least cover most of the buttons. Now, design the page, let say taking your image example, give display:flex to the container, and set 3 buttons on each row for desktop. Now in this way, even if the text increase, definitely height will increase but each row will contain 3 buttons that make your page layout good. Now for tablet screens, set 2 buttons on each row. And for mobile screens, set single button on each row.

Obviously you can adjust your page as you wish. But the point is that, you have to give space to your text but you still maintain your page responsiveness. Hope it would be helpful.

huangapple
  • 本文由 发表于 2023年5月14日 19:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247368.html
匿名

发表评论

匿名网友

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

确定