๋งˆํฌ์—…/Html & Css

[Css][๋ฐ˜์‘ํ˜• ์›น] flex ์ด์šฉํ•˜์—ฌ div ๋ฐ•์Šค๋ฅผ ์›ํ•˜๋Š” ์œ„์น˜๋กœ ๋‚ด๋ฆฌ๊ธฐ

Rainbow๐ŸŒˆCoder 2022. 12. 8. 14:44
728x90

๊ธฐ๋ณธ ์ŠคํŽ™ : react

 

์‚ฌ๊ฑด์˜ ์ „๋ง

 

์ด ํฌ์ŠคํŒ…์€ ์›ํ•˜๋Š” ๋งˆํฌ์—…์„ ์œ„ํ•ด ์ขŒ์ถฉ์šฐ๋Œ css๋ฅผ ๋งŒ์กŒ๋˜ ๊ณผ์ •์„ ๋‹ด์€ ์ž‘์—…๊ธฐ์ด๋‹ค.

์ด์ „ ์ƒํƒœ

๋„์‹œ๋ณ„ ํ˜„์žฌ ๋‚ ์”จ ์ •๋ณด ์•ฑํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ • ์ค‘์— div ๋ฐ•์Šค๋ฅผ ์ ์ ˆํ•œ ์œ„์น˜๋กœ ๋‚ด๋ฆฌ๊ณ  ์‹ถ์—ˆ๋‹ค.

 

 

์›ํ–ˆ๋˜ ์œ„์น˜1
์›ํ–ˆ๋˜ ์œ„์น˜2

CSS position

 

[css] position ์š”์†Œ์˜ ์œ„์น˜ ์„ค์ •

CSS / POSITION position ํŠน์ง• ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์†์„ฑ static, relative, absolute, fixed, sticky๋กœ position์— ๊ฐ’์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ์œ„์น˜๋ฅผ ์„ค์ •ํ•  ๋•Œ์—๋Š” top,right,left,bottom์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์š”์†Œ

itjy2.tistory.com

์œ„ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•˜์—ฌ, div๋ฅผ ์›ํ•˜๋Š” ์œ„์น˜์— ์˜ฎ๊ฒจ๋ณด์•˜๋‹ค. 

 

.container{
  position:relative;
  
  /*์›๋ž˜ ์žˆ์–ด์•ผ ํ•  ์œ„์น˜์—์„œ ์œ„์—์„œ๋ถ€ํ„ฐ 100px ๋–จ์–ด์ง„๋‹ค*/
  top:100px; 
}

 

ํ˜„์žฌ ์›น ํฌ๊ธฐ์— ๋งž๊ฒŒ div๊ฐ€ ๋‚ด๋ ค๊ฐ€๊ธฐ๋Š” ํ•˜์˜€์ง€๋งŒ, ์›นํŽ˜์ด์ง€๋ฅผ ์ค„์˜€๋‹ค ๋Š˜๋ ธ๋‹ค(๊ฐ€๋กœ๋กœ, ์„ธ๋กœ๋กœ๋„) ํ–ˆ์„ ๋•Œ

ํ•œ ํ™”๋ฉด ์•ˆ์— ์žˆ๋˜ ์š”์†Œ๋“ค์˜ ๋น„์œจ์ด ์ด์ƒํ•˜๊ฒŒ ๋ณ€๊ฒฝ๋จ + ํŒŒ๋ž€ ๋ฒ„ํŠผ๋“ค์ด ํ™”๋ฉด ์•„๋ž˜๋กœ ๋ฐ€๋ ค๋‚˜๊ฐ + ๋ฐฐ๊ฒฝ ์‚ฌ์ง„์ด full ์ด ์•„๋‹ˆ๋ผ ์•„๋ž˜ ํฐ์ƒ‰ ๊ณต๋ฐฑ์ด ์ƒ๊น€ ; ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

์•„๋งˆ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ ์”จ info ๋ฐ•์Šค์˜ ํด๋ž˜์Šค์—   top:100px;  ์†์„ฑ์„ ์ฃผ์–ด์„œ ์• ๋จผ ๋ธ”๋ฃจ ๋ฒ„ํŠผ ๊ทธ๋ฃน์ด ๋ฐ€๋ ค๋‚ด๋ ค๊ฐ„ ๊ฒƒ ๊ฐ™์•˜๋‹ค. 

์›ํ–ˆ๋˜ ์œ„์น˜1

 

๋ฐ˜์‘ํ˜• ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€

 

์ผ๋‹จ ์›น์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์˜€์„ ๋•Œ ๋ฐฐ๊ฒฝ์‚ฌ์ง„์ด

๋ฐ˜์‘ํ˜• ์›น๊ณผ ๊ฐ™์ด ์™„์ „ํžˆ ๋ณ€ํ™”ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ ์„ ์•Œ๊ฒŒ ๋˜์–ด ์„œ์น˜๋ฅผ ๋Œ๋ ค๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•์œผ๋กœ ์ข…ํšก๋น„ ์œ ์ง€ํ•˜์—ฌ ์ด๋ฏธ์ง€ ํ‘œ์‹œํ•˜๊ธฐ

๋ฐ˜์‘ํ˜• ์›น์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ` ํƒœ๊ทธ๊ฐ€ ์•„๋‹Œ, ํŠน์ • card์™€ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ์— ๊ฐ€๋กœ ์„ธ๋กœ ๋น„์œจ์„ ์œ ์ง€ํ•˜๋ฉด์„œ background-image`๋ฅผ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœ๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

velog.io

 


background-image

<div> ํƒœ๊ทธ ๋“ฑ์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ์— ๋Œ€ํ•ด์„œ background-image ์†์„ฑ์„ ์ฃผ์–ด์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ™”๋ฉด ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ผ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค๋ฉฐ ๋ฐ˜์‘ํ˜•์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ  ์ด๋ฏธ์ง€๋ฅผ ๋ธŒ๋ผ์šฐ์ €์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๊ฝ‰ ์ฑ„์šธ ๊ฒฝ์šฐ

.card{
    width: 100%;
    height: 100vh;
    background-image: url("image-url");
    background-repeat: no-repeat // background-image๊ฐ€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ฐ€๋“ ์ฑ„์šฐ์ง€ ๋ชปํ•  ๊ฒฝ์šฐ์—๋„ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.
    background-size: cover; // ์‚ฌ์ด์ฆˆ๊ฐ€ container์— ๋งž์ง€ ์•Š์•„๋„ ๊ฝ‰ ์ฐจ๋„๋ก ์ฑ„์šด๋‹ค.
    background-position: center; // background-image๊ฐ€ ์ปจํ…Œ์ด๋„ˆ์— ๊ฐ€์šด๋ฐ๋กœ ์˜ค๋„๋ก ํ•œ๋‹ค. 
}

๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€๋Š” ์œ„์˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•œ๊ฒฐ ๋ฐ˜์‘ํ˜•์Šค๋Ÿฌ์›Œ์กŒ๋‹ค.

 

 

ํ…์ŠคํŠธ ์ค‘์•™์ •๋ ฌ

 

๊ทธ ๋‹ค์Œ ๋‚ ์”จ ์ •๋ณด ํ…์ŠคํŠธ ๋“ค์„ ์ „๋ถ€ ์ค‘์•™์ •๋ ฌ ์‹œ์ผœ ์ฃผ์—ˆ๋‹ค.

์ฐธ๊ณ  : https://biyanbiyan.tistory.com/367

 

[css] button ํ•œ ์ค„๋กœ ๊ฐ€์šด๋ฐ ์ •๋ ฌ

๋ฒ„ํŠผ div์ผ๋•Œ ๊ฒ‰์— ๋‹ค์‹œ div๋กœ ๊ฐ์‹ธ์คŒ button1 button2 ๊ทธ๋ฆฌ๊ณ  ๊ฒ‰์— ๊ฐ์‹ผ div์— width: 100%, text-align: center ์ฃผ๊ณ  ๋ฒ„ํŠผ ๊ฐ์‹ผ div์— display:inline-block ํ•˜๋ฉด ๋จ.

biyanbiyan.tistory.com

 

๊ทธ ์™ธ์—๋Š” flex๋ฅผ ๋Œ๋ ค๋ง‰์œผ๋ฉฐ...

์›ํ•˜๋Š” ๋ฐ˜์‘ํ˜• ์Šคํƒ€์ผ์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์ตœ์ข…

์ œ๋ฒ•(?) ๊ทผ์‚ฌํ•œ ๋ฐ˜์‘ํ˜• ์›น์ด ์™„์„ฑ๋˜์—ˆ๋‹ค. 

 


์ฝ”๋“œ ๊ณต๊ฐœ (react ๊ธฐ๋ฐ˜)

* ์ฐธ๊ณ ๋กœ ํŒŒ๋ž€ ๋ฒ„ํŠผ๋“ค์€ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ์ผ์Šต๋‹ˆ๋‹ค.

 

App.js

 

๋งˆํฌ์—… ์ฐธ๊ณ ์— ํ•„์š”ํ•˜๋‹ค ์‹ถ์€ ์ฝ”๋“œ๋“ค๋งŒ ๊ธ์–ด์„œ ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค.

์ „์ฒด ์ฝ”๋“œ ๊ถ๊ธˆํ•˜์‹ ๋ถ„๋“ค ๋Œ€์ƒ์œผ๋กœ ๊นƒํ—ˆ๋ธŒ ๋งํฌ ๊ณต์œ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

import './App.css';
import { useEffect, useState } from 'react';
import WeatherBox from "./component/WaetherBox";
import WeatherButton from "./component/WeatherButton";
import 'bootstrap/dist/css/bootstrap.min.css';


function App() {
	return (
		<div>
			<div className='main'>

				<WeatherBox />

				<div className='cityButtonGroup'>
					<WeatherButton title="Seoul" />
					<WeatherButton title="NewYork" />
					<WeatherButton title="Toronto" />
					<WeatherButton title="Paris" />
					<WeatherButton title="Rondon" />
				</div>
			</div>
			<div>
			</div>
		</div >
	);
}

export default App;

 

WeatherBox.js

import React from 'react'

const WaetherBox = () => {
	return (
		<div className='weather-info-parents'>
			<div className='weather-info'>
				<div className='weather-info-text'>Your Weather</div>
				<h2 className='weather-info-text'>30๋„ / 230 ํ™”์”จ</h2>
				<h2 className='weather-info-text'>๋ง‘์€ ํ•˜๋Š˜</h2>
			</div>
		</div>
	)
}

export default WaetherBox

 

WeatherButton.js

import React from 'react'
import { Button } from 'react-bootstrap';

const WeatherButton = (props) => {
	return (
		<div>
			<Button variant="primary">{props.title}</Button>
		</div>
	)
}

export default WeatherButton

 

App.css

body{
	background-image : url(https://wallpaperaccess.com/full/5466320.jpg);
	height: 100vh;
	width: 100%;
	background-repeat: no-repeat;
	background-size: cover;
	background-position: center;
}

.main{
	display: flex;
	flex-direction: column;
	margin: auto;
	justify-content: center;
	justify-content: space-evenly;
	height: 100vh;
}

.weather-info-parents{
	display: flex;
	justify-content: center;
}

.weather-info{
	width: 500px;
	border: 3px solid #aec6fe;
	color: white;
	padding: 50px;
}

.weather-info-text {
	width: 100%;
	text-align: center;
}

.cityButtonGroup{
	display: flex;
	justify-content: center;
	justify-content: space-evenly;
	align-items: center;
	position:relative;
	width: 100%;
	overflow: hidden;
}

 

- Happy Ending -

 

+ .weather-info๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์กฐ๊ธˆ ๋” ๋‹ค๋“ฌ์—ˆ๋‹ค.

background-color:rgba(0, 0, 0, 0.2);/*๊นŒ๋งŒ์ƒ‰(0,0,0) 20% ๋ถˆํˆฌ๋ช…๋„*/

.weather-info{
	width: 800px;
	border: 3px solid #aec6fe;
	border-radius: 10px;
	color: white;
	background-color:rgba(108, 144, 202, 0.4);
	padding: 100px;
}

์ œ๋ฒ• ๋งˆ์Œ์— ๋“ ๋‹ค :}

 

728x90