【问题标题】:Can anybody help me with limiting an html buttons border to a certain length? [duplicate]任何人都可以帮助我将 html 按钮边框限制为一定长度吗? [复制]
【发布时间】:2021-05-22 01:46:24
【问题描述】:

这是(错误的)代码:

.btn-group .button {
  background-color: #fffff1;
  border: 0px solid black;
  border-radius: 8px;
  border-bottom: 1px solid black;
  color: #3c4043;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  font-size: 16px;
  cursor: pointer;
  width: 150px;
  display: block;
}

.button1 {
  border-bottom: 1px solid black;
}

.btn-group .button:not(:last-child) {
  
}

.btn-group .button:hover {
  background-color: #fffff1;
  box-shadow: 0 0px 4px 0 rgba(0,0,0,0.24), 0 0px 32px 0 rgba(0,0,0,0.19);
}
<!DOCTYPE html>
<html>
<head>

</head>
<body>


<div class="btn-group">
  <button class="button button1">Button</button>
  <button class="button button2">Button</button>
  <button class="button button3">Button</button>
  <button class="button button4">Button</button>
</div>

</body>
</html>

所需的结果(图像经过照片处理): image (请帮帮我)

我试图将按钮长度的边框限制为一定大小

【问题讨论】:

    标签: html css button


    【解决方案1】:

    您可以使用伪元素after 来完成这项工作。以下是我所做的您应该厌倦的更改:

    .button {
      background-color: salmon /* so you can see the changes easier */
      position: relative /* this is so we can anchor the :after element */
    }
    .button:after {
      content: "";
      background: black;
    
      /* so we can adjust the position of the border bottom */
      position: absolute;
    
      /* bottom: 0 is to put it at the bottom*/
      bottom: 0;
    
      /* left: 5% is to center it */
      left: 5%;
    
      height: 1px;
      width: 90%;
    }
    

    .btn-group .button {
      /* background-color: #fffff1; */
      background-color: salmon;
      border: none;
      border-radius: 8px;
      color: #3c4043;
      padding: 15px 32px;
      text-align: center;
      text-decoration: none;
      font-size: 16px;
      cursor: pointer;
      width: 150px;
      display: block;
      position: relative;
    }
    
    .btn-group .button:after {
      content: "";
      background: black;
      position: absolute;
      bottom: 0;
      left: 5%;
      height: 1px;
      width: 90%;
    }
    
    .btn-group .button:hover {
      background-color: #fffff1;
      box-shadow: 0 0px 4px 0 rgba(0,0,0,0.24), 0 0px 32px 0 rgba(0,0,0,0.19);
    }
    <!DOCTYPE html>
    <html>
    <head>
    
    </head>
    <body>
    
    
    <div class="btn-group">
      <button class="button button1">Button</button>
      <button class="button button2">Button</button>
      <button class="button button3">Button</button>
      <button class="button button4">Button</button>
    </div>
    
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      您可以使用伪元素来做到这一点。代码如下:

      .btn-group .button {
        background-color: #fffff1;
        border: 0px solid black;
        border-radius: 8px;
       /* border-bottom: 1px solid black;*/
        color: #3c4043;
        padding: 15px 32px;
        text-align: center;
        text-decoration: none;
        font-size: 16px;
        cursor: pointer;
        width: 150px;
        display: block;
      }
      
      .button {
      position: relative;
      margin-bottom: 1rem;
      box-sizing:border-box;
      }
      
      .button::after{
        content: '';
        position: absolute;
        bottom: 0;
        width: 100px;
        height: 1px;
        background: black;
        left: 50%;
        transform: translateX(-50%);
      }
      
      .button1 {
       /* border-bottom: 1px solid black;*/
      }
      
      .btn-group .button:not(:last-child) {
        
      }
      
      .btn-group .button:hover {
        background-color: #fffff1;
        box-shadow: 0 0px 4px 0 rgba(0,0,0,0.24), 0 0px 32px 0 rgba(0,0,0,0.19);
      }
      <!DOCTYPE html>
      <html>
      <head>
      
      </head>
      <body>
      
      
      <div class="btn-group">
        <button class="button button1">Button</button>
        <button class="button button2">Button</button>
        <button class="button button3">Button</button>
        <button class="button button4">Button</button>
        <button class="button button4">Button</button>
        <button class="button button4">Button</button>
      </div>
      
      </body>
      </html>

      【讨论】:

        【解决方案3】:

        您可以为按钮设置最大宽度。这会将边框限制为您定义的宽度。

        .btn-group .button{
            max-width: 20px;
        }
        

        你可以放任何值,20px只是一个随机值,你可以根据自己的需要调整。

        【讨论】:

        • 这不起作用,边框底部仍然弯曲到侧边框。
        • @shreyas 边框“向上弯曲”,因为有border-radius
        • 对,对不起,我误解了你的问题,请尝试删除“半径”。
        猜你喜欢
        • 1970-01-01
        • 2021-12-13
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        • 2019-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多