display grid and position fixed goes out of body

社会主义新天地 提交于 2019-11-26 14:56:33

问题


.parent {
  position:fixed;
  width:100%;
  left:0;
  top:14px;
  display:grid;
  grid-template-columns:40% 60%;
  grid-gap:5px;
  background:#eee;
}
.left {
  border:2px solid red;
}
.right {
  border:2px solid red;
}
<div class='parent'>
  <div class='left'>LEFT</div>
  <div class='right'>RIGHT</div>
</div>

if position is not fixed there is no problem, but if position is fixed - parent is not entire visible on the right side.


回答1:


The issue isn't with width:100% like you think. It is with grid-template that you made 40% 60% and you also have a grid-gap of 5px which will make the total more than 100%.

Instead rely on the fr unit to split the free space considering the gap:

.parent {
  position:fixed;
  width:100%;
  left:0;
  top:14px;
  display:grid;
  grid-template-columns:4fr 6fr;
  grid-gap:5px;
  background:#eee;
}
.left {
  border:2px solid red;
}
.right {
  border:2px solid red;
}
<div class='parent'>
  <div class='left'>LEFT</div>
  <div class='right'>RIGHT</div>
</div>



回答2:


total 8px border horizontally including 5px gap making this problem.

.parent{
position:fixed;
width:100%;
left:0; top:14px;
display:grid;
grid-template-columns:40% 60%;

background:#eee;
}

.left{
border:2px solid red;
}

.right{
border:2px solid red;
margin-left:5px;
}
<div class='parent'>
<div class='left'>LEFT</div>
<div class='right'>RIGHT</div>
</div>


来源:https://stackoverflow.com/questions/52113420/display-grid-and-position-fixed-goes-out-of-body

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!