๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/HTML CSS

[HTML/CSS] position: sticky ํ™œ์šฉํ•˜๊ธฐ

by Connecting-the-dots 2022. 3. 2.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • position: sticky ์†์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์šฐ์ธก์˜ ์ด๋ฏธ์ง€๋Š” ์œ„์น˜๊ฐ€ ๊ณ ์ •๋˜๊ณ  ์ขŒ์ธก์˜ ํ…์ŠคํŠธ๋งŒ ์Šคํฌ๋กคํ•˜๋ฉด ์›€์ง์ด๋„๋ก ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.
  • ๋ฐ˜์‘ํ˜•์œผ๋กœ ๋งŒ๋“ค๋ฉด ์„ธ๋กœ๋กœ 1์—ด์ด ๋˜์–ด ํฌ๊ฒŒ ์˜๋ฏธ๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ˆœ์ˆ˜ํ•˜๊ฒŒ PC ํ™”๋ฉด์šฉ์œผ๋กœ ๋งŒ๋“ค๊ณ  ๋ฐ˜์‘ํ˜•์€ ๋”ฐ๋กœ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋‹ค.

๐Ÿ’œ HTML

  • image div ํƒœ๊ทธ๋Š” ์šฐ์ธก์—, text div ํƒœ๊ทธ๋Š” ์ขŒ์ธก์— ๋ฐฐ์น˜ํ•  ์˜ˆ์ •์ด๋ฉฐ ํŽธํ•˜๊ฒŒ ๋ณด๊ธฐ ์œ„ํ•ด image div ํƒœ๊ทธ๋ฅผ ์ƒ๋‹จ์— ์ ์—ˆ๋‹ค. (์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ์–ด์ ์–ด๋„ float ๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์น˜ ์ง€์ •์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค.)
  • float ์†์„ฑ์„ ์ด์šฉํ–ˆ์œผ๋‹ˆ ์ค‘๊ฐ„์ค‘๊ฐ„์— div ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด clear : both ์†์„ฑ์„ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ๋„ ๊นŒ๋จน์ง€ ๋ง๊ธฐ!
<div class="grey">
    <div class="image">
        <img src="./img/appletv.jpg" width="100%">
    </div>
    <div class="text">
        Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quisquam, inventore ullam. 
        Repudiandae distinctio dolorum perspiciatis odio praesentium.
        Quae amet facilis officia vel reprehenderit repellendus earum non, nihil, ab tempora autem?
    </div>
    <div style="clear:both"></div>
    <div class="text">
        Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quisquam, inventore ullam. 
        Repudiandae distinctio dolorum perspiciatis odio praesentium. 
        Quae amet facilis officia vel reprehenderit repellendus earum non, nihil, ab tempora autem?
    </div>
    <div style="clear:both"></div>
    <div class="text">
        Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quisquam, inventore ullam. 
        Repudiandae distinctio dolorum perspiciatis odio praesentium. 
        Quae amet facilis officia vel reprehenderit repellendus earum non, nihil, ab tempora autem?
    </div>
    <div style="clear:both"></div>
    <div class="text">
        Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quisquam, inventore ullam. 
        Repudiandae distinctio dolorum perspiciatis odio praesentium. 
        Quae amet facilis officia vel reprehenderit repellendus earum non, nihil, ab tempora autem?
    </div>
    <div style="clear:both"></div>
    <div class="text">
        Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quisquam, inventore ullam. 
        Repudiandae distinctio dolorum perspiciatis odio praesentium. 
        Quae amet facilis officia vel reprehenderit repellendus earum non, nihil, ab tempora autem?
    </div>
    <div style="clear:both"></div>
    <div class="text">
        Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quisquam, inventore ullam. 
        Repudiandae distinctio dolorum perspiciatis odio praesentium. 
        Quae amet facilis officia vel reprehenderit repellendus earum non, nihil, ab tempora autem?
    </div>
    <div style="clear:both"></div>
</div>

๐Ÿ’œ CSS

  • image ๋Š” float : right ์†์„ฑ์„ ์ฃผ์–ด ์šฐ์ธก์— ๋ฐฐ์น˜ํ•˜๊ณ  position : sticky ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๊ณ ์ •์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
  • sticky ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ณ ์ •ํ•  ์ขŒํ‘œ(top, right, bottom, right ์‚ฌ์šฉ)๋„ ํ•จ๊ป˜ ์ ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • text ๋Š” float: left ์†์„ฑ์„ ์ฃผ์–ด ์ขŒ์ธก์— ๋ฐฐ์น˜ํ–ˆ๋‹ค.
body {
  background: #eee;
  height: 2000px;
}

.grey {
  height: 1600px;
  margin-top: 400px;
}

.image {
  float: right;
  width: 600px;
  position: sticky;
  top: 400px;
}

.text {
  margin-bottom: 200px;
  float: left;
  width: 600px;
}
728x90
๋ฐ˜์‘ํ˜•