CSS

[CSS] ์ฝ˜ํ…์ธ ์˜ ๊ธธ์ด์™€ ์ƒ๊ด€ ์—†์ด footer ํ•˜๋‹จ ๊ณ ์ •

hid1 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์„ ์ ์šฉํ•˜์˜€๋‹ค.

 

 

 

 

 

๋ฐ˜์‘ํ˜•