FE/HTML,CSS

[css] float

mandelina 2022. 4. 12. 10:39

 ๐Ÿš€ float : ๋‘ฅ๋‘ฅ๋– ์žˆ๋‹ค๋Š”๋œป

 

๊ทธ๋ฆผ๊ณผ ๊ธ€์ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ๋ฅด๋Š” ํ…์ŠคํŠธ ๋ ˆ์ด์•„์›ƒ์„ ์›น์—์„œ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํƒ„์ƒํ•˜์˜€๋‹ค.

 

 

<์˜ˆ์‹œ>

 

float Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla, ad, autem quaerat sed impedit cupiditate tenetur recusandae facere, alias ab nihil nisi eligendi eaque ea molestiae dolore accusantium repellat maiores? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae sed similique deleniti commodi incidunt ipsam rerum illum odio saepe aperiam, sit odit hic labore a impedit aliquam? Officiis, accusamus nobis. Ratione mollitia doloremque ipsum doloribus inventore rerum! Iste voluptatum placeat illo fugiat, laudantium consectetur cum vel aperiam. Possimus ducimus ullam porro culpa, maxime consequuntur obcaecati error, id vero nemo magnam. Eius, nulla delectus. Rem corporis, voluptates obcaecati corrupti a tempora dolore quas enim assumenda cumque placeat neque. Odit et laborum corrupti libero quasi officiis nemo? Voluptates, ipsa minus. Labore, voluptas.

 

- float : right๋กœ ์ค€ ๊ฒฐ๊ณผ ์‚ฌ์ง„์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐฐ์—ด๋˜๊ณ  , ๊ธ€์ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ๋ฅด๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

๐Ÿฅธ  float๋ฅผ ์“ฐ๋ฉด inline์š”์†Œ๋กœ ์ž‘์šฉํ•˜๊ณ  , ๊ทธ ๋‹ค์Œ์š”์†Œ๋Š” inline-block์š”์†Œ๋กœ ๋ฐ”๋€๋‹ค. 

 

์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

See the Pen Untitled by mandelina (@mandelina) on CodePen.

 

๋ถ„๋ช… block์š”์†Œ์ธ div 2๊ฐœ์— ๋Œ€ํ•ด์„œ 1๋ฒˆ์งธ์š”์†Œ์—๊ฒŒ float ์†์„ฑ์„ ์ ์šฉ์‹œํ‚ค๋ฉด "ํ•ด๋‹น text"๋งŒํผ์˜ ๊ณต๊ฐ„๋งŒ์„ ์ฐจ์ง€ํ•˜๊ณ  ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐฐ์น˜๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค .

 

 

 ๐Ÿฅธ ์ž์‹์š”์†Œ๋“ค์ด ๋ชจ๋‘ float์†์„ฑ์„ ๊ฐ–๋Š”๋‹ค๋ฉด ์ž์‹ ์š”์†Œ๋“ค์˜ ๋†’์ด๋ฅผ ๋ฐ˜์˜ ํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ž์‹ (๋ถ€๋ชจ)์˜ ๋†’์ด ๋งŒํผ ๊ฐ–๋Š”๋‹ค.

 

์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

See the Pen Untitled by mandelina (@mandelina) on CodePen.

 

 

 

๋ญ”๊ฐ€ ์ด์ƒํ•˜๋‹ค. ์˜ˆ์ƒ ๊ฒฐ๊ณผ๋กœ์„  float๋ฅผ ์“ฐ๋ฉด inline-block์œผ๋กœ ๋˜๋‹ˆ ๋ถ€๋ชจdiv ์•ˆ์— ์ž์‹ div 2๊ฐœ๊ฐ€ ์™ผ์ชฝ์œผ๋กœ ๋ฐฐ์น˜ ๋˜์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ ๊ฒฐ๊ณผ๋Š” ๋ถ€๋ชจ๊ฐ€ ์ž์‹ ๋“ค์„ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ณ  ์žˆ๋‹ค.

 


 

Q . ์™œ ์ž์‹๋“ค์˜ ์กด์žฌ๋ฅผ ์ธ์‹ํ•  ์ˆ˜ ์—†๋‚˜?

- ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์š”์†Œ๋“ค์„ ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ค„์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์—์„œ ํ•ด๋‹ต์„ ์–ป์„์ˆ˜ ์žˆ๋‹ค.

  ์•„๋ž˜์— ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ด 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

- ๋Œ€๋ถ€๋ถ„ ์š”์†Œ๋“ค (block , inline)์€ normal flow์— ๋”ฐ๋ผ ๋ ˆ์ด์•„์›ƒ์ด ๊ฒฐ์ •๋œ๋‹ค.

(normal flow : ๊ธฐ๋ณธ์ ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ์ธ๋ผ์ธ์š”์†Œ๋Š” ํ•„์š”ํ•œ ๋„ˆ๋น„๋งŒ ์ฐจ์ง€ํ•œ๋‹ค๋˜์ง€ , ๋งˆ์ง„๋ณ‘ํ•ฉ ํ˜„์ƒ ๋“ฑ์ด ์žˆ๋‹ค.)

- ํ•˜์ง€๋งŒ float , postion (absolute , fixed) ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด normal flow์—์„œ ๋ฒ—์–ด๋‚˜๊ฒŒ ๋˜์–ด normal flow (์ฆ‰ ์˜ˆ์ œ์—์„  ๋ถ€๋ชจ)๊ฐ€ float , position์ด ์ ์šฉ๋œ ์š”์†Œ๋ฅผ ์ธ์‹์„ ํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜์–ด ๋ฒ„๋ฆฐ๋‹ค.

 


Q. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋Ÿฐ (๋ถ€๋ชจ๊ฐ€ ์ž์‹์„ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋Š”) ๋ฌธ์ œ์ ์„

   ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ?

 

1. ๋ถ€๋ชจ์— overflow ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ

- ๋Œ€๋ถ€๋ถ„ ์‹ค๋ฌด์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค .

- ํ•˜์ง€๋งŒ ์ด๊ฒฝ์šฐ ๋ถ€๋ชจ์˜ hight๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ์œผ๋ฉด ์ž์‹์ด ์งค๋ฆด์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

 overflow: hidden;

See the Pen Untitled by mandelina (@mandelina) on CodePen.

 

 

 

2. ๋ถ€๋ชจ์š”์†Œ์— ๋†’์ด๊ฐ’์„ ์ง์ ‘ ์„ค์ •ํ•˜๊ธฐ

- ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋‹ค . ์™œ๋ƒ๋ฉด ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ๋งˆ๋‹ค height๊ฐ’์„ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

 

3. float์ด ์‚ฌ์šฉ๋œ ์š”์†Œ ๋ฐ”๋กœ ๋‹ค์Œ ํ˜•์ œ์š”์†Œ์— clear ์†์„ฑ ์‚ฌ์šฉํ•˜๊ธฐ 

- float์ด ์‚ฌ์šฉ๋œ ๋ฐฉํ–ฅ์œผ๋กœ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

See the Pen Untitled by mandelina (@mandelina) on CodePen.

 

4. clear - fix ๋ฐฉ๋ฒ•

-  ๋ถ€๋ชจ์š”์†Œ์— ๊ฐ€์ƒ์œผ๋กœ ๋งˆ์ง€๋ง‰ child ์š”์†Œ๋ฅผ ๋ง๋ถ™์—ฌ ๋ถ€๋ชจ์š”์†Œ๊ฐ€ ์ž์‹์š”์†Œ๋“ค์„ ์•Œ์•„๋ณด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

.wrap::after {
	content:'';
	display:block;
	clear:both;
}

์ด๋ ‡๊ฒŒ ๋ง๋ถ™์ด๋ฉด ๋œ๋‹ค.

 

 

๐ŸŒˆ๊ทธ๋ฐ–์—

 

- ๋น”์บ ํ”„ ์ข…์ฐฌ๋‹˜๊ป˜์„œ ์˜ˆ์ „์— ํ•œ๋ฒˆ ์–ธ๊ธ‰ํ•ด์ฃผ์…จ๋˜ ๋‚ด์šฉ์ธ BFC ์—ญ์‹œ float์š”์†Œ ์‚ฌ์šฉ์‹œ ๊ด€๋ จ์ด ์žˆ๊ธฐ์—

  ์ž์„ธํžˆ ํฌ์ŠคํŒ… ํ•ด๋ณด์•„์•ผ๊ฒ ๋‹ค.

 

- ๋ผ์ธ๋ธŒ๋ ˆ์ดํฌ ? ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜๋ฉด ๋งˆ์ง„์ด ์ƒ๊ธด๋‹ค.

 

- ๋ ˆ์ด์•„์›ƒ ์œ„์น˜๋ฅผ ์žก์„๋•Œ๋Š” position์œผ๋กœ ์žก๊ฒŒ๋˜๋Š”๊ฒฝ์šฐ ์ˆ˜์ •ํ• ๋•Œ ์œ„ํ—˜ํ•˜๊ธฐ๋•Œ๋ฌธ์— float,grid,flex ๋“ฑ๋“ฑ์„ ์‚ฌ์šฉํ•˜์ž.

 

- ์žฌํ˜„๊ฐ•์‚ฌ๋‹˜์ด ์ถ”์ฒœํ•˜์‹  "์œ ๋ฐ๋ฏธ"

 

- ๋ฉ”์ธ์•ˆ์— ํŒจ๋”ฉ๊ฐ’์„ ์ฃผ์ž! (์ซ€๋“ํ•œ ui๋ฅผ ์œ„ํ•ด)

 

๐Ÿ”ฅ ํšŒ๊ณ 

์˜ค๋Š˜์€ ์ด๋ก ๋ณด๋‹จ ์ฝ”๋“œ๋ฆฌ๋ทฐ ์œ„์ฃผ์˜ ์ˆ˜์—…์ด์˜€๋‹ค. ์—ญ์‹œ๋‚˜ ๋‹ค์–‘ํ•œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด๋ณด๋Š”๊ฑด ์žฌ๋ฐŒ์—ˆ๋‹ค.

์•„์ง section๊ณผ article ์‚ฌ์šฉ์€ ์ข€ ํ—ท๊ฐˆ๋ฆฌ์ง€๋งŒ... ๊ทธ๋ž˜๋„ ๋ ˆ์ด์•„์›ƒ์„ ๋ฐฐ์น˜ํ• ๋•Œ position์ด ์•„๋‹Œ

float๋กœ ๋ฐฐ์น˜ํ•˜๋Š”๊ฒƒ์— ์ต์ˆ™ํ•ด์ง„๊ฒƒ์— ๋งŒ์กฑํ•œ๋‹ค .

 

 

 

'FE > HTML,CSS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[CSS] grid ?  (0) 2022.04.14
[css] Flex๋ž€?  (0) 2022.04.13
[css] position  (0) 2022.04.11
[css] ์ค‘์•™์ •๋ ฌ ์‹œ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ ๋ชจ์Œ  (0) 2022.04.07
overflow : hidden vs display : none ์˜ ์ฐจ์ด์ ์€?  (0) 2022.04.07