如何添加一个宽度为100%且position为fixed的按钮

3

我在stackoverflow上尝试了很多解决方法,但都没有起作用。

a.buttongc {
  border-radius: 5px;
  background: #f5b220;
  color: #fff;
  font-size: 17px;
  height: 44px;
  line-height: 42px;
  color: #fff;
  text-decoration: none;
  text-align: center;
  box-sizing: border-box;
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  appearance: none;
  white-space: nowrap;
  margin: 10px;
  text-overflow: ellipsis;
  font-family: inherit;
  cursor: pointer;
  width: 100%;
  overflow: hidden;
  display: block;
}

.gc-button-center {
  position: fixed;
  left: 0;
  right: 0;
  display: block;
  width: 100%;
  bottom: 50px;
  z-index: 999999999;
}
<div class="gc-button-center">
  <a href="#" class="buttongc startgc">test</a>
</div>

我想要左右留出边距,但只有左边起作用,而按钮则超出了滚动条。有什么解决办法吗?

屏幕截图


https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing - DBS
box-sizing: border-box - Ivan Rubinson
box-sizing不起作用。 - desmeit
这里的“not working”是什么意思? - bukart
2
如果您只是从 a 中删除宽度,框尺寸就会正确工作。 - DBS
这两个 width:100% 都是无用的:https://jsfiddle.net/0r69a5zt/ - Temani Afif
2个回答

5

不需要花哨的东西。 只需删除您的width: 100% 如果显示为block,并且未提供宽度,则宽度将自动调整大小以适应父级。

    a.buttongc{
     border-radius: 5px;
     background: #f5b220;
        color: #fff;
     font-size: 17px;
        height: 44px;
        line-height: 42px;
        color: #fff;
        text-decoration: none;
        text-align: center;
        box-sizing: border-box;
        -webkit-appearance: none;
        -moz-appearance: none;
        -ms-appearance: none;
        appearance: none;
        white-space: nowrap;
     margin:10px;
        text-overflow: ellipsis;
        font-family: inherit;
        cursor: pointer;
     overflow:hidden;
       display: block;
        
    }
    
    .gc-button-center{
      position:fixed;
      left:0;
      right:0;
      display:block;
      width: 100%;
      bottom: 50px;
      z-index:999999999;
    
    }
    <div class="gc-button-center">
    <a href="#" class="buttongc startgc">test</a>
    </div>


哦,这真的很简单,非常好。但是有没有包括滚动条的解决方案? - desmeit
@desmeit 很难在没有看到它的外观之前做出判断。是哪个滚动条? - Marius

4
只需更改 width: calc(100% - 20px); 减去 margin 即可。

a.buttongc{
    border-radius: 5px;
    background: #f5b220;
    color: #fff;
    font-size: 17px;
    height: 44px;
    line-height: 42px;
    color: #fff;
    text-decoration: none;
    text-align: center;
    box-sizing: border-box;
    -webkit-appearance: none;
    -moz-appearance: none;
    -ms-appearance: none;
    appearance: none;
    white-space: nowrap;
    margin:10px;
    text-overflow: ellipsis;
    font-family: inherit;
    cursor: pointer;
    width: 100%;
    overflow:hidden;
    display: block;

}

.gc-button-center{
  position:fixed;
  left:0;
  right:0;
  display:block;
  width: calc(100% - 20px);
  bottom: 50px;
  z-index:999999999;

}
<div class="gc-button-center">
  <a href="#" class="buttongc startgc">test</a>
</div>


谢谢。这个有效。有可能包括滚动条吗? - desmeit
你能简要地告诉我如何包含滚动条吗? - Hiren Vaghasiya
边距计算包括滚动条在内的10像素。我想设置没有滚动条的10像素。 - desmeit
@desmeit,您可以根据滚动条的宽度设置边距,但如果没有滚动条,则无法按要求工作,并且移动设备和桌面设备的滚动条宽度不同。 - Hiren Vaghasiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接