admin管理员组

文章数量:1391784

I'm trying to build a horizontal scroll with multiple content boxes that belong to one title. So I'd like the title to stay while the content scrolls past it, until the next section with a new title es.

Here's what I was trying to do: /

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.scroll {
  display: flex;
  flex-direction: row;
  width: 100vw;
  height: 100px;
  background: yellow;
  overflow: scroll;
}

.item {
  flex-shrink: 0;
  width: 200px;
  height: 100%;
  margin-right: 20px;
}

.scroll .item .title {
  position: sticky;
  left: 0;
  top: 0;
  width: 100%;
  height: 40px;
}

.item .content {
  width: 100%;
  height: 60px;
  border: 3px solid green;
}
<div class="scroll">

  <div class="item">
    <div class="title">
      Title 1
    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">

    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">
      Title 2
    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">

    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">

    </div>
    <div class="content">
      Content
    </div>
  </div>

</div>

I'm trying to build a horizontal scroll with multiple content boxes that belong to one title. So I'd like the title to stay while the content scrolls past it, until the next section with a new title es.

Here's what I was trying to do: https://jsfiddle/kjo4duts/23/

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.scroll {
  display: flex;
  flex-direction: row;
  width: 100vw;
  height: 100px;
  background: yellow;
  overflow: scroll;
}

.item {
  flex-shrink: 0;
  width: 200px;
  height: 100%;
  margin-right: 20px;
}

.scroll .item .title {
  position: sticky;
  left: 0;
  top: 0;
  width: 100%;
  height: 40px;
}

.item .content {
  width: 100%;
  height: 60px;
  border: 3px solid green;
}
<div class="scroll">

  <div class="item">
    <div class="title">
      Title 1
    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">

    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">
      Title 2
    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">

    </div>
    <div class="content">
      Content
    </div>
  </div>

  <div class="item">
    <div class="title">

    </div>
    <div class="content">
      Content
    </div>
  </div>

</div>

Is there a way to address the "position: sticky" property to the outer parent (.scroll)? Or is there a smooth way to do it in JavaScript? I tried to change the HTML structure, but with Flexbox you need a container for each box to get a horizontal layout..

Thanks in advance!

Edit: For anyone with the same problem. The solution is to add a relative position to the outer parent and the change the HTML structure a bit.

See updated Fiddle: https://jsfiddle/r2czqwn7/20/

Share Improve this question edited Jun 22, 2021 at 11:14 Hans Peter asked Jun 22, 2021 at 10:40 Hans PeterHans Peter 331 gold badge1 silver badge4 bronze badges 4
  • 1 yoo looking for scroll snap – Robert Commented Jun 22, 2021 at 10:43
  • Does this answer your question? CSS - Sticky div on horizontal scroll? – Yana Trifonova Commented Jun 22, 2021 at 10:45
  • Parent (.scroll) has to be position: relative at first as you can check in your modified pen but I would consider different structure (all contents with same title could be in same div) to stick it over multiple items. – Jax-p Commented Jun 22, 2021 at 10:54
  • 1 Thanks @Jax-p The position relative was what I needed! If you like, you can post an answer and I will accept it. I modified the HTML structure as you suggested and it works like a charm! link – Hans Peter Commented Jun 22, 2021 at 11:09
Add a ment  | 

1 Answer 1

Reset to default 3

Parent (.scroll) has to be position: relative at first as you can check below but I would consider different structure (all contents with same title could be in same div) to stick it over multiple items.

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.scroll {
  position: relative;
  display: flex;
  flex-direction: row;
  width: 100vw;
  height: 100px;
  background: yellow;
  overflow: scroll;
}

.item {
  flex-flow: column;
  flex-shrink: 0;
  width: 200px;
  height: 100%;
  margin-right: 20px;
}

.scroll .item .title {
  position: sticky;
  display: inline;
  left: 0;
  top: 0;
  width: 100%;
  height: 40px;
}

.item .content {
  width: 100%;
  height: 60px;
  border: 3px solid green;
}


.item .content:first-child {
  margin-top: 1rem;
}
<div class="scroll">
  
  <div class="item">
    <div class="title">
      Title 1
    </div>
    <div class="content">
      Content
    </div>
  </div>
  
  <div class="item">
    <div class="content">
      Content
    </div>
  </div>
  
  <div class="item">
    <div class="title">
      Title 2
    </div>
    <div class="content">
      Content
    </div>
  </div>
  
  <div class="item">
    <div class="content">
      Content
    </div>
  </div>
  
  <div class="item">
    <div class="content">
      Content
    </div>
  </div>

</div>

本文标签: javascriptCSS quotposition stickyquot on horizontal scrollStack Overflow