简体   繁体   English

如何使按钮保持相同的大小/位置和固定的高度

[英]How do I keep the buttons the same size/position with a fixed height

Here are things that I need to explain:以下是我需要解释的事情:

  • I need to set my height of the buttons to 35px;我需要将按钮的高度设置为 35px;
  • If there is less text, the buttons seem to create a bit of a gap on the top如果文字较少,按钮似乎会在顶部产生一些空隙
  • Less text also does not center the text in the middle of the button (awkward gap in the button)更少的文本也不会将文本置于按钮中间(按钮中的尴尬间隙)

1个

HTML: HTML:

<div class="up-promos">
     <div class="tabs">    
       <a href="#" class="r-button active" data-tab-name="promo-category-0"><p>Lorem ipsum</p></a>
       <a href="#" class="r-button " data-tab-name="promo-category-1"><p>Lorem et</p></a>
       <a href="#" class="r-button " data-tab-name="promo-category-2"><p>Lorem ipsum et ipsum dolor sit</p></a>
     </div>
</div>

CSS: CSS:

  .tabs {
    text-align: center;
  }
  
  .up-promos {
    background-color: black;
  }
  
  .up-promos .tabs a {
    border: 1px solid #fff;
    color: #fff;
    padding: 10px;
    border-radius: 8px;
    display: inline-block;
    text-align: center;
    margin: 0 0 10px 10px;
    text-decoration:none;
    text-align: center;
    width:150px;
    height: 35px;
  }
  
  .up-promos .tabs a.active {
    background-color: #fff;
    color: black;
    font-weight: bold;
  }

JS Fiddle: https://jsfiddle.net/6k20489o/ JS 小提琴: https://jsfiddle.net/6k20489o/

My first attempt is to just center the texts inside the button but it had them outside due to my position: I would like to know how do I fix this:我的第一次尝试是将文本居中放置在按钮内,但由于我的position:我想知道如何解决这个问题:

2个

What I tried:我尝试了什么:

.tabs a p{
    position: absolute;
    top: 50%;
    left: 50%;
    margin-right: -50%;
    transform: translate(-50%, -50%)
  }

Another thing I cannot figure out is the spacing above the button if the text is short, there is no margin or padding even if I check with inspect element我无法弄清楚的另一件事是如果文本很短,按钮上方的间距,即使我检查检查元素也没有边距或填充

3个

I would like some help in a solution to my issues.我需要一些帮助来解决我的问题。

 .tabs { text-align: center; }.up-promos { background-color: black; }.up-promos.tabs { display: flex; align-items: center; justify-content: center; padding: 5px 0; }.up-promos.tabs a { display: flex; align-items: center; justify-content: center; border: 1px solid #fff; color: #fff; padding: 10px; border-radius: 8px; text-align: center; margin: 0 0 10px 10px; text-decoration: none; width: 150px; height: 35px; }.up-promos.tabs a.active { background-color: #fff; color: black; font-weight: bold; }
 <div class="up-promos"> <div class="tabs"> <a href="#" class="r-button active" data-tab-name="promo-category-0"> <p>Lorem ipsum</p> </a> <a href="#" class="r-button " data-tab-name="promo-category-1"> <p>Lorem et</p> </a> <a href="#" class="r-button " data-tab-name="promo-category-2"> <p>Lorem ipsum et ipsum dolor sit</p> </a> </div> </div>

Hey instead of positioning you can use flexbox to center text inside the button嘿,而不是定位,你可以使用 flexbox 将按钮内的文本居中

   .tabs {
    display: flex;
   justify-content: center;
   align-items: center;
   flex-wrap: wrap;
  }
  
  .up-promos {
    height:100vh;
    background-color: black;
    display: grid;
    place-items: center;
  }
  
  .up-promos .tabs a {
    border: 1.5px solid #fff;
    color: #fff;
    padding: 10px;
    border-radius: 8px;
    text-decoration: none;
    margin:10px;
    width:150px;
    height: 35px;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align:center;
  }
  
  .up-promos .tabs a.active {
    background-color: #fff;
    color: black;
    font-weight: bold;
  }

Output Image Output 图片

You can visit this pen for reference: https://codepen.io/monishsoni/pen/MWBGgME您可以访问此笔以供参考: https://codepen.io/monishsoni/pen/MWBGgME

In your code, you are giving position absolute to .tabs ap but there is no position given to its parent that's why .tabs ap will be positioned according to the nearest parent element with positioning or the body element if there is no parent element with the position.在你的代码中,你给 position 绝对给.tabs ap但没有 position 给它的父级,这就是为什么.tabs ap将根据最近的具有定位的父元素或 body 元素定位,如果没有父元素与position。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM