元素从容器顶部溢出 flex
Elements overflows the flex from top of container
/* Side Bar */
#sidebar {
box-sizing: border-box;
display: flex;
position: fixed;
justify-content: space-around;
flex-direction: column;
overflow-y: scroll;
left: 0;
bottom: 0;
background: blueviolet;
height: 92%;
width: 17%;
margin: 0 auto;
padding-left: 10px;
}
.side, .subs {
padding: 5px;
margin-left: 5px;
}
#things, #others, #subscriptions {
box-sizing: border-box;
display: flex;
flex-direction: column;
padding-bottom: 10px;
}
#things div, #others div, #subscriptions div {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
#things div i, #others div i, #subscriptions div i {
margin-right: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- sidebar -->
<div id="sidebar">
<div id="things">
<div><i class="fa-solid fa-house side"></i><p>Home</p></div>
<div><i class="fa-regular fa-compass side"></i><p>Explore</p></div>
<div><i class="fa-solid fa-s side"></i><p>Shorts</p></div>
<div><i class="fa-solid fa-photo-film side"></i><p>Subscriptions</p></div>
</div>
<div id="others">
<div><i class="fa-solid fa-file-video side"></i><p>Library</p></div>
<div><i class="fa-solid fa-clock-rotate-left side"></i><p>History</p></div>
<div><i class="fa-solid fa-circle-play side"></i><p>Your Videos</p></div>
<div><i class="fa-regular fa-clock side"></i><p>Watch later</p></div>
<div><i class="fa-solid fa-thumbs-up side"></i><p>Liked Videos</p></div>
<div><i class="fa-solid fa-angle-down side"></i><p>Show more</p></div>
</div>
<div id="subscriptions">
<h4 class="subs">SUBSCRIPTIONS</h4>
<div><i class="fa-solid fa-circle side"></i><p>TKA Team</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Freecodecamp</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Crypto Gurus</p></div>
</div>
</div>
如何防止元素在调整浏览器大小时从顶部溢出容器?
Image of the overflow
PS:我想让项目居中。谢谢。
重置范围顶部的 padding
、margin
和 box-sizing
-
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
#sidebar {
box-sizing: border-box;
display: flex;
position: fixed;
justify-content: space-around;
flex-direction: column;
overflow-y: scroll;
left: 0;
bottom: 0;
background: blueviolet;
height: 92%;
width: 17%;
margin: 0 auto;
padding-left: 10px;
}
.side, .subs {
padding: 5px;
margin-left: 5px;
}
#things, #others, #subscriptions {
box-sizing: border-box;
display: flex;
flex-direction: column;
padding-bottom: 10px;
}
#things div, #others div, #subscriptions div {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
#things div i, #others div i, #subscriptions div i {
margin-right: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="sidebar">
<div id="things">
<div><i class="fa-solid fa-house side"></i><p>Home</p></div>
<div><i class="fa-regular fa-compass side"></i><p>Explore</p></div>
<div><i class="fa-solid fa-s side"></i><p>Shorts</p></div>
<div><i class="fa-solid fa-photo-film side"></i><p>Subscriptions</p></div>
</div>
<div id="others">
<div><i class="fa-solid fa-file-video side"></i><p>Library</p></div>
<div><i class="fa-solid fa-clock-rotate-left side"></i><p>History</p></div>
<div><i class="fa-solid fa-circle-play side"></i><p>Your Videos</p></div>
<div><i class="fa-regular fa-clock side"></i><p>Watch later</p></div>
<div><i class="fa-solid fa-thumbs-up side"></i><p>Liked Videos</p></div>
<div><i class="fa-solid fa-angle-down side"></i><p>Show more</p></div>
</div>
<div id="subscriptions">
<h4 class="subs">SUBSCRIPTIONS</h4>
<div><i class="fa-solid fa-circle side"></i><p>TKA Team</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Freecodecamp</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Crypto Gurus</p></div>
</div>
</div>
</body>
</html>
/* Side Bar */
#sidebar {
box-sizing: border-box;
display: flex;
position: fixed;
justify-content: space-around;
flex-direction: column;
overflow-y: scroll;
left: 0;
bottom: 0;
background: blueviolet;
height: 92%;
width: 17%;
margin: 0 auto;
padding-left: 10px;
}
.side, .subs {
padding: 5px;
margin-left: 5px;
}
#things, #others, #subscriptions {
box-sizing: border-box;
display: flex;
flex-direction: column;
padding-bottom: 10px;
}
#things div, #others div, #subscriptions div {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
#things div i, #others div i, #subscriptions div i {
margin-right: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- sidebar -->
<div id="sidebar">
<div id="things">
<div><i class="fa-solid fa-house side"></i><p>Home</p></div>
<div><i class="fa-regular fa-compass side"></i><p>Explore</p></div>
<div><i class="fa-solid fa-s side"></i><p>Shorts</p></div>
<div><i class="fa-solid fa-photo-film side"></i><p>Subscriptions</p></div>
</div>
<div id="others">
<div><i class="fa-solid fa-file-video side"></i><p>Library</p></div>
<div><i class="fa-solid fa-clock-rotate-left side"></i><p>History</p></div>
<div><i class="fa-solid fa-circle-play side"></i><p>Your Videos</p></div>
<div><i class="fa-regular fa-clock side"></i><p>Watch later</p></div>
<div><i class="fa-solid fa-thumbs-up side"></i><p>Liked Videos</p></div>
<div><i class="fa-solid fa-angle-down side"></i><p>Show more</p></div>
</div>
<div id="subscriptions">
<h4 class="subs">SUBSCRIPTIONS</h4>
<div><i class="fa-solid fa-circle side"></i><p>TKA Team</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Freecodecamp</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Crypto Gurus</p></div>
</div>
</div>
如何防止元素在调整浏览器大小时从顶部溢出容器?
Image of the overflow
PS:我想让项目居中。谢谢。
重置范围顶部的 padding
、margin
和 box-sizing
-
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
#sidebar {
box-sizing: border-box;
display: flex;
position: fixed;
justify-content: space-around;
flex-direction: column;
overflow-y: scroll;
left: 0;
bottom: 0;
background: blueviolet;
height: 92%;
width: 17%;
margin: 0 auto;
padding-left: 10px;
}
.side, .subs {
padding: 5px;
margin-left: 5px;
}
#things, #others, #subscriptions {
box-sizing: border-box;
display: flex;
flex-direction: column;
padding-bottom: 10px;
}
#things div, #others div, #subscriptions div {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
#things div i, #others div i, #subscriptions div i {
margin-right: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="sidebar">
<div id="things">
<div><i class="fa-solid fa-house side"></i><p>Home</p></div>
<div><i class="fa-regular fa-compass side"></i><p>Explore</p></div>
<div><i class="fa-solid fa-s side"></i><p>Shorts</p></div>
<div><i class="fa-solid fa-photo-film side"></i><p>Subscriptions</p></div>
</div>
<div id="others">
<div><i class="fa-solid fa-file-video side"></i><p>Library</p></div>
<div><i class="fa-solid fa-clock-rotate-left side"></i><p>History</p></div>
<div><i class="fa-solid fa-circle-play side"></i><p>Your Videos</p></div>
<div><i class="fa-regular fa-clock side"></i><p>Watch later</p></div>
<div><i class="fa-solid fa-thumbs-up side"></i><p>Liked Videos</p></div>
<div><i class="fa-solid fa-angle-down side"></i><p>Show more</p></div>
</div>
<div id="subscriptions">
<h4 class="subs">SUBSCRIPTIONS</h4>
<div><i class="fa-solid fa-circle side"></i><p>TKA Team</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Freecodecamp</p></div>
<div><i class="fa-solid fa-circle side"></i><p>Crypto Gurus</p></div>
</div>
</div>
</body>
</html>