display grid and position fixed goes out of body

只愿长相守 提交于 2019-11-27 09:54:05

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>

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