https://codepen.io/sahandz/pen/BrYGKa
我正在创建一个网站,我希望修复侧边栏,标题和搜索栏,同时搜索结果可滚动。我正在使用React,但渲染的HTML看起来像这样:
.topdiv {
max-height: 100px;
min-height: 100px;
background-color: #DCDCDC;
margin-top: 0px;
margin-right: 0px;
margin-left: 0px;
position: sticky;
top: 0px;
z-index: 1;
width: 100%;
border-bottom: solid;
overflow-x: hidden;
}
.topdiv h1 {
font-family: "Comic Sans MS";
font-weight: bold;
position: absolute;
top: 5px;
text-align: center;
width: 100%;
}
.container-fluid {
padding: 0px;
width: 100%;
height: 100%;
overflow-x: hidden;
}
@media (max-width: 766px) {
.container-fluid {
height: 1200px;
}
}
.sidebar {
position: fixed;
width: 300px;
top: 100px;
height: 100%;
border-right: solid;
padding: 0px;
}
.noofpeople {
position: relative;
padding-top: 20px;
padding-right: 0px;
padding-bottom: 20px;
padding-left: 50px;
right: 0px;
width: 100%;
}
.noofpeople-wrapper input {
width: 55px;
}
.dinneroverview {
width: 100%;
padding: 0px;
margin: 0px;
}
.dish-overview-header {
border-bottom: solid;
border-top: solid;
height: 25px;
padding-left: 25px;
padding-right: 30px;
margin: 0px;
background-color: #d4d5d8;
}
.dish-name {
float: left;
}
.cost {
float: right;
}
.dinneroverview-price {
color: red;
padding-top: 50px;
padding-bottom: 0px;
padding-right: 30px;
}
.dinneroverview-pricetag {
float: right;
}
.confirm {
padding-left: 10px;
text-align: center;
padding-top: 40px;
}
@media (max-width: 766px) {
.sidebar {
width: 100%;
height: 40%;
position: static;
display: none;
background-color: #e5e6e8;
padding-bottom: 20px;
}
.confirm {
padding-top: 40px;
}
}
@media (min-width: 767px) {
.sidebar {
display: block !important;
border-right: solid;
height: 100% !important;
}
#hamburger {
display: none;
}
.hamburgerdiv {
display: none;
}
.menuitem {
width: 270px;
left: 20px;
}
}
@media (max-width: 360px) {
.confirm button {
width: 30px;
}
}
.dishsearch .keyword {
padding-right: 0px;
}
.dishsearch .select {
padding: 0px;
}
.dishsearch .search {
padding: 0px;
}
.searchbar {
border-style: solid;
border-color: black;
}
.main-bar {
position: relative;
left: 300px;
right: 0px;
}
.dishsearch {
padding-left: 10px;
padding-bottom: 20px;
border-bottom: solid;
position: sticky;
top: 100px;
z-index: 1;
background-color: white;
}
.dishsearch-dish {
text-align: center;
display: inline-block;
padding: 10px;
margin-top: 20px;
}
.dishreel {
top: 196px;
padding-top: 40px;
text-align: center;
}
.imgframe {
width: 146px;
height: auto;
margin: 0 auto;
border: solid;
background-color: #DCDCDC;
}
.dishsearch-img {
width: 140px;
height: 140px;
}
@media (min-width: 768px) {
.main-bar {
width: calc(100% - 297px);
}
}
<div id="root">
<div class="App">
<header class="App-header">
<div class="topdiv">
<h1 class="App-title">Dinner Planner</h1>
</div>
</header>
<div class="SelectDish">
<div class="Sidebar" id="sidebar">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 sidebar collapse">
<div>
<div class="noofpeople">
<div class="row noofpeople-wrapper">
<label>People <input type="number" min="0" id="numberOfGuests" class="form-control"></label></div>
</div>
<div id="dinneroverview" class="dinneroverview">
<div class="dish-overview-header"><span class="dish-name">Dish name</span><span class="cost">Cost</span></div>
<div id="sidebar-dishes"></div>
<div class="dinneroverview-price">
<span id="sidebar-total-price" class="dinneroverview-pricetag">SEK 0.00</span>
</div>
<div class="confirm"><button id="confirm" type="button" class="btn btn-default">Confirm Dinner</button></div>
</div>
</div>
</div>
</div>
<div class="Dishes" id="dishSearcher">
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12 main-bar">
<div class="row dishsearch">
<div class="col-xs-12">
<h4>Find a dish</h4>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-4 keyword">
<input type="text" id="searchbar" class="form-control" value=""></div>
<div id="dishsearcher-select" class="col-lg-3 col-md-3 col-sm-3 col-xs-4 select">
<select id="select" class="form-control">
</select>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-4 search">
<button id="search-button" type="button" class="btn btn-default">search</button>
</div>
</div>
<div class="dishreel" id="dishreel">
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/cabbage-salad-with-peanuts-723984.jpg" class="dishsearch-img" alt="img">
<p>Cabbage Salad with Peanuts</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/Garlicky-Pasta-with-Swiss-Chard-and-Beans-482788.jpg" class="dishsearch-img" alt="img">
<p>Garlicky Pasta with Swiss Chard and Beans</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/Slow-Cooker-Vegetarian-Chili-with-Butternut-Squash-482574.jpg" class="dishsearch-img" alt="img">
<p>Slow Cooker Vegetarian Chili with Butternut Squash</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/Beef-Liver-with-Fig--Bacon-and-Caramelized-Onion-Compote-[or-the-ultimate-beef-liver-]-551452.png" class="dishsearch-img" alt="img">
<p>Beef Liver with Fig, Bacon and Caramelized Onion Compote [or the ultimate beef liver ]</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/steak-potato-kebabs-with-creamy-cilantro-sauce-for-two-695646.jpg" class="dishsearch-img" alt="img">
<p>Steak & Potato Kebabs with Creamy Cilantro Sauce for Two</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/Roasted-vegetable---feta-tostada-222482.jpg" class="dishsearch-img" alt="img">
<p>Roasted vegetable & feta tostada</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/blackened-salmon-tacos-with-cilantro-yogurt-sauce-551484.jpg" class="dishsearch-img" alt="img">
<p>blackened salmon tacos with cilantro yogurt sauce</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/Three-Bean-Greek-Kale-Salad-609091.jpg" class="dishsearch-img" alt="img">
<p>Three Bean Greek Kale Salad</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/Cedar-Planked-Salmon-with-Fresh-Sorrel-Sauce-610524.jpg" class="dishsearch-img" alt="img">
<p>Cedar-Planked Salmon with Fresh Sorrel Sauce</p>
</div>
</div>
<div class="dishsearch-dish handhover">
<div id="dish.id" class="imgframe">
<img src="https://spoonacular.com/recipeImages/liver-and-onions-w_-gravy-2-146557.jpg" class="dishsearch-img" alt="img">
<p>Liver and Onions W/ Gravy</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
令我非常沮丧的是,标题“正式晚餐计划”并没有坚持到顶部。我尝试了一堆fixed
和sticky
元素的组合,但没有任何诀窍。如何像侧边栏和搜索栏一样滚动时,如何使标题保持其位置?
答案 0 :(得分:2)
这就是你想要的,对吧? 基本上你有四个div。三个是固定的(顶部标题,搜索栏和侧栏),最后一个(搜索结果)是相对的。首先考虑这一点,然后创建HTML结构,然后创建CSS。请看下一个简单的例子:
html, body{
margin: 0;
padding: 0;
}
#top-header{
background-color: #000;
color: #fff;
position: fixed;
height: 100px;
left: 0;
right: 0;
top: 0;
z-index: 100;
}
#side-menu{
background-color: #ff0000;
color: #fff;
position: fixed;
left: 0;
width: 200px;
top: 100px;
bottom: 0;
z-index: 100;
}
#search-bar{
background-color: #00ff00;
color: #000;
position: fixed;
left: 200px;
right: 0;
top: 100px;
height: 50px;
z-index: 100;
}
#search-results{
position: relative;
z-index: 50;
height: 1500px; /*remove me*/
background-color: #ccc;
margin-left: 200px;
margin-top:150px;
}
&#13;
<div id="root">
<div class="App">
<header id='top-header'>
top header
</header>
<div id='side-menu'>
side bar
</div>
<div id='search-bar'>
search bar
</div>
<div id='search-results'>
search results
</div
</div>
</div>
&#13;
您只能在搜索结果中滚动,如您所愿。使用这个简单的示例,您可以更新CSS代码。
使用position: fixed;
使其保持不变,相对容器使用margin-top
和margin-left
将其向下推。确保固定元素上的z-index
值大于相对元素中的值。