当我调整(缩小)浏览器的大小时,标题元素将移至空白处。我希望它们保持在蓝色背景色之内,而与调整浏览器大小无关。
body,
html {
height: 100vh;
}
body {
margin: 0;
display: grid;
grid-template-columns: 15%;
grid-template-rows: auto;
}
aside {
grid-row: 1 / 5;
background-color: blue;
}
.sidebar {
display: flex;
flex-direction: column;
text-align: center;
position: fixed;
margin-left: 0.5rem;
}
<aside>
<div class="sidebar">
<h3>Home</h3>
<h3>About</h3>
<h3>Recent Projects</h3>
</div>
</aside>
答案 0 :(得分:0)
请勿使用%
作为值。否则,它将缩放到该百分比。
ie; grid-template-columns: 15%;
表示可用窗口大小的15%
。
最好设置静态宽度,而不是百分比宽度。
例如,您可以在width
CSS上设置aside
来覆盖百分比;
aside {
grid-row: 1 / 5;
background-color: blue;
width: 160px;
}
答案 1 :(得分:0)
我编辑了CSS,并澄清了尺寸。希望这可行:)
body,
html {
height: 100vh;
}
body {
margin: 0;
display: grid;
grid-template-columns: 15%;
grid-template-rows: auto;
}
aside {
grid-row: 1 / 5;
background-color: blue;
width: 150px;
}
.sidebar {
display: flex;
flex-direction: column;
text-align: center;
position: fixed;
margin-left: 0.5rem;
}
.sidebar h3 {
width: 130px;
}
答案 2 :(得分:0)
使用minmax()
并删除position:fixed
body,
html {
height: 100vh;
}
body {
margin: 0;
display: grid;
grid-template-columns: minmax(max-content,15%);
grid-template-rows: auto;
}
aside {
grid-row: 1 / 5;
background-color: blue;
}
.sidebar {
display: flex;
flex-direction: column;
text-align: center;
margin-left: 0.5rem;
}
<aside>
<div class="sidebar">
<h3>Home</h3>
<h3>About</h3>
<h3>Recent Projects</h3>
</div>
</aside>