-
[CSS] ์ฝํ ์ธ ์ ๊ธธ์ด์ ์๊ด ์์ด footer ํ๋จ ๊ณ ์ CSS 2022. 6. 10. 13:39
๐ฉ๋ชฉํ
์ฝํ ์ธ ์ ๊ธธ์ด์ ์๊ด ์์ด footer๋ฅผ ํ๋จ์ ๊ณ ์ ์์ผ์ผ ํ๋ค.
header์ ๊ฒฝ์ฐ ์คํฌ๋กค์ ๋ด๋ฆด์ ์๋จ๊ณ ์ ์ด ๋์ด์ผ ํ๋ค๋ฉด position:fixed๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.ํ์ง๋ง footer์ ๊ฒฝ์ฐ ์คํฌ๋กค์ด ์๊ธธ ๋ ํญ์ ๊ณ ์ ๋์ด ์์ด ์ด์ํ๋ค.position: absolute๋ก ํ๋จ์ ์์นํด๋ณด์๋ ์ฝํ ์ธ ๊ฐ ๊ธธ์ด์ ธ ์คํฌ๋กค์ด ๋์ด์๋ฉด ํ๋จ์ ์์นํ์ง ์๊ฒ ๋๋ค.
๐ก๋ฐฉ๋ฒ : min-height
min-height๋ฅผ ์ด์ฉํด ํ๋ฉด์ ๊ฝ์ฑ์ฐ๊ฒ ํ๋ค.
height์ ๊ธฐ๋ณธ ์์ฑ์ auto์ด๋ ์ปจํ ์ธ ์ ๊ธธ์ด๊ฐ ํ๋ฉด๋ณด๋ค ์งง์ผ๋ฉด ํ๋ฉด์ด ๊ฝ์ฐจ๊ฒ ๋์ค๊ณ ๊ธธ๋ค๋ฉด ๊ธธ์ด๋งํผ ์คํฌ๋กค์ด ๋ฐ์
<!-- html --> <body> <header>Header</header> <div class="content">Content</div> <footer>Footer</footer> </body>
/* css */ * { box-sizing: border-box; } header { width: 100%; height: 60px; position: fixed; top: 0; z-index: 9; } .content { min-height: calc(100vh - 30px); padding-top: 60px; } footer { width: 100%; height: 30px }
content ์์ญ
- min-height : calc(100vh - footer height ๊ฐ))
- padding-top : header์ height ๊ฐ
๋ฌธ์ ์ : vh ๋จ์์ ๋ํ์ฌ
PC ๋ธ๋ผ์ฐ์ ์์๋ ๋ฌธ์ ์์ด ๋ณด์ด๋ ๋ถ๋ถ์ด ๋ชจ๋ฐ์ผ์์๋ ์คํฌ๋กค์ด ๋ฐ์ํ๋ค.
์ด์ ๋ 100vh ๋จ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ฐ์ผ์์๋ ์ํ๋จ์ ๋ฐ ์์ญ์ ๋ฐ์ํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์
์๋จ ์ฃผ์์ฐฝ์ ์๋ถ๋ถ๋ถํฐ Viewport๋ก ์ธ์ํ๊ฒ ๋๋ค.
ํด๊ฒฐ๋ฐฉ๋ฒ 1 : % ๋จ์๋ฅผ ์ฐ๊ธฐ
/* css */ * { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; } header { width: 100%; height: 60px; position: fixed; top: 0; z-index: 9; } .content { min-height: calc(100% - 30px); padding-top: 60px; } footer { width: 100%; height: 30px; }
height: 100%๋ ์์์ ๋ถ๋ชจ์์ ๋๋น ๊ธฐ์ค์ผ๋ก 100%๋ก ์ฑ์ด๋ค๋ ์๋ฏธ์ด๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ html๊ณผ body์์์๋ height: 100%๋ฅผ ์ฃผ์๋ค.
!๋ฆฌ์กํธ ์ฌ์ฉ์ : #root, #root > div์๋ height: 100%์ ์ ์ฉํด์ผ ํ๋ค.
ํด๊ฒฐ๋ฐฉ๋ฒ 2 : JS window.innerHeight ์ด์ฉ
The trick to viewport units on mobile | CSS-Tricks
Viewport units have always been controversial and some of that is because of how mobile browsers have made things more complicated by having their own
css-tricks.com
/* css */ .content { height: calc(var(--vh, 1vh) * 100); }
// javascript let vh = window.innerHeight * 0.01 document.documentElement.style.setProperty('--vh', `${vh}px`) window.addEventListener('resize', () => { let vh = window.innerHeight * 0.01 document.documentElement.style.setProperty('--vh', `${vh}px`) })
var() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ CSS ์ฌ์ฉ์ ์์ฑ --vh ๋ณ์๋ฅผ ์์ฑ ํ window.innerHeight๋ฅผ ๋ด๋๋ค.
CSS var() ํจ์๋ ์ฌ์ฉ์ ์ง์ ์์ฑ, ๋๋ "CSS ๋ณ์"์ ๊ฐ์ ๋ค๋ฅธ ์์ฑ์ ๊ฐ์ผ๋ก ์ง์ ํ ๋ ์ฌ์ฉํ๋ค.
์ฒซ ๋ฒ์งธ ์ธ์๋ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ฌ์ฉ์ ์ง์ ์์ฑ์ ์ด๋ฆ์ด๋ค.
์ ํ์ ์ผ๋ก ์ ๊ณตํ ์ ์๋ ๋ ๋ฒ์งธ ์ธ์๋ ๋์ฒด๊ฐ์ผ๋ก, ๋์ ์ฌ์ฉ์ ์ง์ ์์ฑ์ด ์ ํจํ์ง ์์ ๊ฒฝ์ฐ ๋์ ์ฌ์ฉํ๋ค.์๊ฐํด๋ณผ์
header, content, footer ์์๋ฅผ container๋ก ๊ฐ์ธ์ง ๋ ์ด์์๋ก ๋ง๋ ๋ค์
flex๋ grid ์์ฑ์ ์ด์ฉํด ๊ตณ์ด header์ footer ๊ธธ์ด๋ฅผ ๊ฐ์ ธ์ ๊ณ์ฐํ์ง ์์ ์ ์๋ค.
<!-- html --> <body> <div id="container"> <header>Header</header> <div class="content">Content</div> <footer>Footer</footer> </div> </body>
flex ์ฌ์ฉ
* { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; } #container { height: 100%; display: flex; flex-direction: column; } header { width: 100%; height: 60px; position: fixed; top: 0; z-index: 9; } .content { flex-grow: 1; padding-top: 60px; } footer { width: 100%; height: 30px; }
grid ์ฌ์ฉ
/* css */ * { margin: 0; padding: 0; box-sizing: border-box; } html,body { height: 100%; } #container { height: 100%; display: grid; grid-template-rows: 1fr 30px; } header { width: 100%; height: 60px; position: fixed; top: 0; z-index: 9; } .content { padding-top: 60px; } footer { width: 100%; }
gird ์์ดํ ์์์ position: fixed ์์ฑ์ ์ฐ๋ฉด grid formatting context์์ ์ ๊ฑฐ๋๋ค.
content์ footer์ ๊ฐ๊ฐ 1fr์ 30px์ ์ ์ฉํ์๋ค.
๋ฐ์ํ'CSS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[CSS] non-boolean attribute warning (0) 2022.09.04 [CSS] gradient transition ์ ์ฉํ๊ธฐ (React) (0) 2022.07.26 [CSS] ์๋ ์์ฑ ์ input ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝ (0) 2022.07.15 [CSS] emotion ์ฌ์ฉ ์ :nth-child CSS selector ๊ฒฝ๊ณ (0) 2022.06.25 [CSS] aspect-ratio : ์์์ ๋น์จ ์ ์งํ๊ธฐ (0) 2022.06.01