最初使粘性项目出现在屏幕中心

时间:2019-05-07 15:19:55

标签: html css

我的页面分为三个部分,第一个部分包含有关中心屏幕的页面标题和副标题:

<section class="sa">
  <div class="sticks">
    <h1>Title</h1>
    <p>Subtitle</p>
  </div>
</section>

我希望这两个词在向下滚动时成为粘稠的顶部栏。当我将标题与A部分分开时,此方法有效,但随后页面的其余部分向下移动,标题没有我想要的初始背景。如果我将“ sticks” div移至“ sa”部分,则只会在“ sa”内移动。

有什么办法可以使标题都位于“ sa”的中心并向下滚动到整个页面?这是整页的要点:https://gist.run/?id=d6830abc58ace11d9b59a40b9bdb1948

为清楚起见进行编辑:因此,对于以上代码,初始显示有效。但是当我向下滚动时,标题将一直移动到“ sa”部分的边界。我希望标题保留在页面其余部分的屏幕顶部,例如如果将上面的代码替换为:

<div class="sticks">
  <h1>Title</h1>
  <p>Subtitle</p>
</div>
<section class="sa">
</section>

问题在于标题的初始显示现在看起来很糟糕。

2 个答案:

答案 0 :(得分:1)

我不知道这是否正是您想要的,只是告诉我,我为您做了这把小提琴。

.sa{
  position:fixed;
  float: none;
  background-color: green;
  width: 100%;
  top:0;
  display:block;
}
<section class="sa">
  <div class="sticks">
    <h1>Title</h1>
    <p>Subtitle</p>
  </div>
</section>

<br>
<br>
<br>
<br>
<br>
<p>
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaa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aa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa

</p>

答案 1 :(得分:0)

您可以使用app.use('/app', express.static(path.join(__dirname, '../client/build/app'))); app.use( '/static', express.static(path.join(__dirname, '../client/build/static')) ); app.get('/apple-touch-icon.png', (req, res) => { res.sendFile( path.join(__dirname, '../client/build', 'apple-touch-icon.png') ); }); app.get('/apple-touch-icon-180x180.png', (req, res) => { res.sendFile( path.join(__dirname, '../client/build', 'apple-touch-icon.png') ); }); // Handle React routing, return all requests to React app app.get('*', function(req, res) { res.sendFile(path.join(__dirname, '../client/build', 'index.html')); }); 属性使标题变粘

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="row">
    <div class="col-md-4">
      1
    </div>
    <div class="col-md-4">
      2
    </div>
    <div class="col-md-4">
      3
    </div>
    <div class="col-md-4">
      4
    </div>
    <div class="col-md-4">
      5
    </div>
    <div class="col-md-4 d-none d-lg-block">
      6
    </div>
  </div>
</div>
position:fixed