位置:固定元素溢出window

Position: Fixed element is overflowing window

我创建了一个固定在页面底部的容器。然而,容器随后溢出页面,填充规则被完全忽略。我在论坛上搜索过,但在我的问题上下文中找不到解决方案。我尝试过使用绝对位置,也尝试过使用 Javascript 来计算滚动条宽度,但都无济于事。

.restaurant-page {
  height: 100%;
}

.restaurant-page .book-table-container {
  position: fixed;
  width: 100%;
  bottom: 0;
  background-color: white;
  border-top: solid 1px #f2f2f2;
  padding-top: 12px;
  padding-bottom: 12px;
}

.restaurant-page .book-table-container button {
  width: 100%;
  border: 0;
  border-radius: 4px;
  background-color: #00ccbc;
  padding-top: 14px;
  padding-bottom: 14px;
  color: white;
  font-size: 16px;
  font-weight: 600;
}
<div class="restaurant-page">
  <div class="book-table-container">
    <button>Book Table</button>
  </div>
</div>

我想你的意思是按钮向右截断了。您可以通过添加以下内容来防止这种情况:.restaurant-page .book-table-container { left: 0; right: 0; }.

但是按钮将直接触摸屏幕的左侧和右侧。为了防止这种情况,我在左侧和右侧添加了 5px 的填充,并将填充代码从:padding-top: 12px; padding-bottom: 12px; padding-left: 5px; padding-right: 5px; 缩短为:padding: 12px 5px;.

.restaurant-page {
  height: 100%;
}

.restaurant-page .book-table-container {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: white;
  border-top: solid 1px #f2f2f2;
  padding: 12px 5px;
}

.restaurant-page .book-table-container button {
  width: 100%;
  border: 0;
  border-radius: 4px;
  background-color: #00ccbc;
  padding-top: 14px;
  padding-bottom: 14px;
  color: white;
  font-size: 16px;
  font-weight: 600;
}

/* for demonstration only */

body {
  overflow-y: scroll;
}
<div class="restaurant-page">
  <div class="book-table-container">
    <button>Book Table</button>
  </div>
</div>