rishubk
rishubk

Reputation: 451

position sticky not working

I am trying to stick a piece of text while a scroll down the web page, but it does not seem to work. I give it this css:

.left {
    position: sticky; /* doesnt work :( */
    top: 0px;
}

Here is the full code: http://jsfiddle.net/YYPKM/3281/

This div is on the left of a wrapper but I want the right to scroll and the left to stick to the top of div. I have tried many things like position fixed but this seems to be the best option, I just can't get position sticky to work.

Does anyone know what I am doing wrong?

Upvotes: 3

Views: 6635

Answers (4)

MaxisKao
MaxisKao

Reputation: 106

I think using display: table doesn't really solve the problem.

All you need to do is simply add align-self: flex-start; to set the height of flexbox element to auto.

.left {
  position: sticky;
  top: 0;
  align-self: flex-start;
}

Upvotes: 5

G-Cyrillus
G-Cyrillus

Reputation: 106058

An option could be to use display:table; for a similar layout :

.wrapper {
  display: table;
}

.description {
  margin-bottom: 50px;
}

.left {
  box-shadow: 0 0 0 5px;
  margin-right: 50px;
  position: sticky;
  /* doesnt work :( if display:table-cell or flex  is set or if it is a flex child */
  position: -webkit-sticky;
  top: 0;
}
.right{
  display:table-cell;/*NOTE: do not emulate display on .left */
}
.right,
.left {/*see me*/
  box-shadow: 0 0  5px;
}
<body>
  <h2>
    TITLE
  </h2>
  <div class='description'>
    Some words to describe this awesome web page
  </div>
  <div class="wrapper">
    <div class="left">
      want to keep this text all the way down
    </div>
    <div class="right">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>5</div>
      <div>6</div>
      <div>7</div>
      <div>8</div>
      <div>9</div>
      <div>10</div>
      <div>11</div>
      <div>12</div>
      <div>13</div>
      <div>14</div>
      <div>15</div>
      <div>16</div>
      <div>17</div>
      <div>18</div>
      <div>19</div>
      <div>20</div>
      <div>21</div>
      <div>22</div>
      <div>23</div>
      <div>24</div>
      <div>25</div>
      <div>26</div>
      <div>27</div>
      <div>28</div>
      <div>29</div>
      <div>30</div>
      <div>31</div>
      <div>32</div>
      <div>33</div>
      <div>34</div>
      <div>35</div>
      <div>36</div>
      <div>37</div>
      <div>38</div>
      <div>39</div>
      <div>40</div>
      <div>41</div>
      <div>42</div>
      <div>43</div>
      <div>44</div>
      <div>45</div>
      <div>46</div>
      <div>47</div>
      <div>48</div>
      <div>49</div>
      <div>50</div>
    </div>
  </div>
</body>

work around also here http://jsfiddle.net/YYPKM/3284/

Upvotes: 2

Andy Huynh
Andy Huynh

Reputation: 119

  1. Specify your desired width for .left and .right
  2. Add float: left to .left
  3. Set .right's padding to .left's width

Check out the fiddle here: http://jsfiddle.net/YYPKM/3283/

Upvotes: 2

The problem is that the parent is treated as a flex container, wich it makes the childen flex item. Now that the items are display as flex items, it stop working. if you remove the display:flex from wrapper, it works.

Upvotes: 2

Related Questions