aboutsummaryrefslogtreecommitdiff
path: root/files/ko/conflicting/learn/css/css_layout/introduction/index.html
blob: 5437902bf10a0b6dd16cd3b1edaa17b92a08184a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
---
title: Introduction to CSS Layout
slug: Learn/CSS/Basics/Layout
translation_of: Learn/CSS/CSS_layout/Introduction
translation_of_original: Learn/CSS/Basics/Layout
---
<p>{{PreviousNext("Learn/CSS/Basics/Box_model","Learn/CSS/Howto/style_text")}}</p>

<p class="summary"><span class="seoSummary">CSS layout is the art of using various CSS properties to alter the positioning of elements on a document in order to fit the design requirements.</span> CSS provides many layout mechanisms, the more advanced and modern techniques are so complex that they get their own articles. In this article, we will introduce the basic techniques that have been used for years.</p>

<p>To properly layout a document with CSS, there are a few notions that one must know. The most important of these is {{Glossary("HTML")}} text flow. We will cover it in this article. Articles about other layout mechanisms will, at some point or the other, refer back to what we are discussing here.</p>

<h2 id="The_flow">The flow</h2>

<p>At its most basic level an HTML document is a text document structured with {{Glossary("tag","tags")}}. In such a document, the text <em>flows</em>. That means text is displayed in the reading direction (from left to right, for example, in Latin based languages like English or French) and is broken automatically - creating new lines - each time the text reaches an edge of the document.</p>

<p><img alt="" src="https://mdn.mozillademos.org/files/11955/line-break-flow.svg" style="height: auto; max-width: 450px;"></p>

<p><img alt="" src="https://mdn.mozillademos.org/files/11955/line-break-flow.svg#large" style="height: auto; max-width: 450px;"></p>

<p>Each {{Glossary("element","elements")}} in the document alters the flow of text in various ways:</p>

<ul>
 <li>Some elements can simply follow the text flow as if they were non-existent.</li>
 <li>Some elements can force a line break at any point in the flow whether it has reached the edge of the document or not.</li>
 <li>Some elements can create a new text flow for their inner content independent from the main text flow.</li>
</ul>

<h3 id="디스플레이_요소_(Elements_display_categories)">디스플레이 요소 (Elements display categories)</h3>

<p>CSS is used to define how an HTML element behaves within the flow and how it get in and out of that flow. The element behavior is defined using the property {{cssxref('display')}}. This property can take tons of values but let's focus on the four most important:</p>

<dl>
 <dt>none</dt>
 <dd>모든 요소를 제거 합니다.</dd>
 <dt>inline</dt>
 <dd>줄을 바꾸지 않고, 해당 위치에서 다른 요소들과 같은 선상에 위치하려는 성질을 말합니다. </dd>
 <dt>block</dt>
 <dd>This value is for the element to break the text flow with a forced line break before and after it. Its content is no longer part of the global text flow and flows only within the constraints provided by the element <a href="/en-US/docs/Learn/CSS/Basics/Box_model">box model</a>.</dd>
 <dt>inline-block</dt>
 <dd>This value makes the element somewhat in between inline and block type display: like <code>inline</code> boxes it flows with the text normally but, like block boxes, it's content is no longer part of the global text.</dd>
</dl>

<p>Let's see an example.</p>

<p>HTML:</p>

<pre class="brush: html">&lt;p class="none"&gt;
  1. I'm a big black cat,
  &lt;span&gt;walking under a ladder,&lt;/span&gt;
  and I can see broken mirrors everywhere.
&lt;/p&gt;

&lt;p class="inline"&gt;
  2. I'm a big black cat,
  &lt;span&gt;walking under a ladder,&lt;/span&gt;
  and I can see broken mirrors everywhere.
&lt;/p&gt;

&lt;p class="block"&gt;
  3. I'm a big black cat,
  &lt;span&gt;walking under a ladder,&lt;/span&gt;
  and I can see broken mirrors everywhere.
&lt;/p&gt;

&lt;p class="inline-block"&gt;
  4. I'm a big black cat,
  &lt;span&gt;walking under a ladder,&lt;/span&gt;
  and I can see broken mirrors everywhere.
&lt;/p&gt;
</pre>

<p>CSS:</p>

<pre class="brush: css">span {
  width: 5em;
  background: yellow;
}

.none span         { display: none;         }
.inline span       { display: inline;       }
.block span        { display: block;        }
.inline-block span { display: inline-block; }</pre>

<p>Results:</p>

<p>{{EmbedLiveSample('Elements_display_categories', '100%', '300px')}}</p>

<h2 id="Altering_the_flow">Altering the flow</h2>

<p>By setting the display property you're already altering the flow, but you can go further.</p>

<h3 id="Text_layout">Text layout</h3>

<p>While an HTML document is nothing more than a long text flow, CSS provides many properties to deal with simple text layout. The text layout is everything that allows changing the text line breaking rules and the way the text is positioned over the natural text line.</p>

<p>Those properties are: {{cssxref("hyphens")}}, {{cssxref("text-align")}}, {{cssxref("text-align-last")}}, {{cssxref("text-indent")}}, {{cssxref("vertical-align")}}, {{cssxref("white-space")}}, {{cssxref("word-break")}}, and {{cssxref("word-wrap")}}.</p>

<p>Except for <code>text-align and<font face="Open Sans, Arial, sans-serif"></font></code><code>text-indent </code>the other properties have subtle effects on the text and <code>vertical-align</code> is often used with inline-block boxes.</p>

<p>An example will make things clearer.</p>

<p>HTML:</p>

<pre class="brush: html">&lt;p lang="en"&gt;WHEN Scrooge awoke, it was so dark, that looking out of bed, he could scarcely distinguish the transparent window from the opaque walls of his chamber. He was endeavouring to pierce the darkness with his ferret eyes, when the chimes of a neighbouring church struck the four quarters. So he listened for the hour. To his great astonishment the heavy bell went on from six to seven, and from seven to eight, and regularly up to twelve; then stopped. Twelve! It was past two when he went to bed. The clock was wrong. An icicle must have got into the works. Twelve! He touched the spring of his repeater, to correct this most preposterous clock. Its rapid little pulse beat twelve: and stopped.&lt;/p&gt;
&lt;p class="format" lang="en"&gt;WHEN Scrooge awoke, it was so dark, that looking out of bed, he could scarcely distinguish the transparent window from the opaque walls of his chamber. He was endeavouring to pierce the darkness with his ferret eyes, when the chimes of a neighbouring church struck the four quarters. So he listened for the hour. To his great astonishment the heavy bell went on from six to seven, and from seven to eight, and regularly up to twelve; then stopped. Twelve! It was past two when he went to bed. The clock was wrong. An icicle must have got into the works. Twelve! He touched the spring of his repeater, to correct this most preposterous clock. Its rapid little pulse beat twelve: and stopped.&lt;/p&gt;
</pre>

<p>CSS:</p>

<pre class="brush: css">.format {
  /* The first line is "pull" to a 2em distance */
  text-indent: -2em;

  /* We need to compensate the negative indent
     to avoid unwanted text clipping and keep
     the whole text within the boundary of its
     element box */
  padding-left: 2em;

  /* The text is aligned on both edges, adjusting
     spacing between words as necessary */
  text-align: justify;

  /* The last line of the block of text is centered */
  -moz-text-align-last: center;
       text-align-last: center;

  /* Rather than line break between two words the line
     break can occur inside words, according to the rules
     defined for the text language. This makes nice word cut
     with a clear hyphen dash. If you don care about word
     breaking rules, you could just use word-break or
     word-wrap instead  */
  -webkit-hyphens: auto;
     -moz-hyphens: auto;
      -ms-hyphens: auto;
          hyphens: auto;
}</pre>

<div class="note">
<p>As you might notice, some properties are written multiple times with some prefix. This is because those prefixed properties are still experimental for some browsers and it is considered best practice to use them all with the standard property at the end of the list in order to provide the best backward compatibility possible.</p>
</div>

<p>Results:</p>

<p>{{ EmbedLiveSample('Text_layout', '100%', '350') }}</p>

<div class="note">
<p>It's worth noting that the trick we used to compensate the negative text indentation is a very common trick. Any property that accepts a length also accepts negative values. By fiddling with negative values and compensating them with other properties,  it's possible to produce very clever effects on the layout, especially when it applies to properties of the box model.</p>
</div>

<h3 id="Floating">Floating</h3>

<p>Okay, handling text is nice, but at some point what we really want is to move boxes around the document. The first way to handle that is to deal with floating boxes. Floating boxes are still attached to the global text flow,  but the text will flow around. Sounds weird, so let's see an example.</p>

<h4 id="Simple_floating">Simple floating</h4>

<p>HTML:</p>

<pre class="brush: html">&lt;div&gt;
  &lt;p class="excerpt"&gt;"Why, it isn't possible," said Scrooge, "that I can have slept through a whole day and far into another night. It isn't possible that anything has happened to the sun, and this is twelve at noon!" &lt;/p&gt;
  &lt;p&gt; The idea being an alarming one, he scrambled out of bed, and groped his way to the window. He was obliged to rub the frost off with the sleeve of his dressing-gown before he could see anything; and could see very little then. All he could make out was, that it was still very foggy and extremely cold, and that there was no noise of people running to and fro, and making a great stir, as there unquestionably would have been if night had beaten off bright day, and taken possession of the world. This was a great relief, because "three days after sight of this First of Exchange pay to Mr. Ebenezer Scrooge or his order," and so forth, would have become a mere United States' security if there were no days to count by.&lt;/p&gt;
&lt;/div&gt;</pre>

<p>CSS:</p>

<pre class="brush: css">.excerpt {
  /* A floating box will act like a block whatever
     the value of display we are using */
  display: block;

  /* Our box is floating to the left, which means
     it will stack on the left side of the containing
     block and the text will flow on its right side. */
  float: left;

  /* It is required to set a width to a floating box.
     If we don't its width will be set
     automatically, which means that it will grow as much
     as possible and nothing will flow around it, like an
     ordinary block box */
  width: 40%;

  /* We set some margins on the right and bottom side of
     the box to avoid having the text flowing around being
     in direct visual contact of our floating box */
  margin: 0 1em 1em 0;

  /* We make our floating box more visible with
     a simple background color */
  background: lightgrey;

  /* As we have a solid background color it's a nice idea
     to push the content a little bit away from the edges
     of the box */
  padding: 1em;
}</pre>

<p>Results:</p>

<p>{{ EmbedLiveSample('Simple_floating', '100%', '280') }}</p>

<h4 id="Layout_with_floating">Layout with floating</h4>

<p>This is a very simple effect to start tweaking the flow to our wishes. Now it's possible to do better and start performing some true layout. A floating box that floats in a given direction stacks horizontally, it's a very convenient way to create rows of boxes instead of natural columns: In the flow, block boxes stack in columns and floating boxes stack in rows.</p>

<p>Once again, an example will make things clearer.</p>

<p>HTML:</p>

<pre class="brush: html">&lt;div class="layout"&gt;
  &lt;div class="row"&gt;
    &lt;p class="cell size50"&gt;Scrooge went to bed again, and thought, and thought, and thought it over and over and over, and could make nothing of it. The more he thought, the more perplexed he was; and the more he endeavoured not to think, the more he thought.&lt;/p&gt;
    &lt;p class="cell size50"&gt;Marley's Ghost bothered him exceedingly. Every time he resolved within himself, after mature inquiry, that it was all a dream, his mind flew back again, like a strong spring released, to its first position, and presented the same problem to be worked all through, "Was it a dream or not?"&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="row"&gt;
    &lt;p class="cell size100"&gt;Scrooge lay in this state until the chime had gone three quarters more, when he remembered, on a sudden, that the Ghost had warned him of a visitation when the bell tolled one. He resolved to lie awake until the hour was passed; and, considering that he could no more go to sleep than go to Heaven, this was perhaps the wisest resolution in his power.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="row"&gt;
    &lt;p class="cell size33"&gt;The quarter was so long, that he was more than once convinced he must have sunk into a doze unconsciously, and missed the clock. At length it broke upon his listening ear.&lt;/p&gt;
    &lt;p class="cell size33"&gt;
      "Ding, dong!"&lt;br&gt;
      "A quarter past," said Scrooge, counting.&lt;br&gt;
      "Ding, dong!"&lt;br&gt;
      "Half-past!" said Scrooge.&lt;br&gt;
      "Ding, dong!"&lt;br&gt;
      "A quarter to it," said Scrooge.&lt;br&gt;
      "Ding, dong!"&lt;br&gt;
      "The hour itself," said Scrooge, triumphantly, "and nothing else!"
    &lt;/p&gt;
    &lt;p class="cell size33"&gt;
      He spoke before the hour bell sounded, which it now did with a deep, dull, hollow, melancholy ONE. Light flashed up in the room upon the instant, and the curtains of his bed were drawn.
    &lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;</pre>

<p>CSS:</p>

<pre class="brush: css">/* This is our main layout container */
.layout {
  /* A background makes it visible */
  background: lightgrey;

  /* We add a small visual spacing to harmonize
     the distance between the cells content and
     and the layout border */
  padding   : 0.5em;
}

/* A floating box gets somewhat out of
   the flow, so if their container is empty
   it will have a zero height size and the
   floating box will overflow it. To
   avoid such a situation, we make sure floating
   boxes are not allowed to overflow. In
   that specific context, with an overflow
   hidden, floating boxes won't be clipped,
   the parent box will extend to avoid any
   floating box overflow.  */
.row {
  overflow: hidden;
}

/* Each cell is a left floating box */
.cell {
  float : left;

  /* we add padding to our cell to create
     some nice visual gutters between them */
  padding   : 0.5em;

  /* Because we are adding padding, we need
     to be sure that it will not impact
     the box width. */
  box-sizing: border-box;

  /* As margins cannot be controlled with the
     box-sizing property, we need to be sure
     there is none applied to our cell. */
  margin    : 0;
}

/* Those are the sizes we can apply to our boxes */
.size33  { width:  33%; } /* Not exactly a third, but good enough */
.size50  { width:  50%; } /* A half */
.size100 { width: 100%; } /* A full row */</pre>

<p>Results:</p>

<p>{{ EmbedLiveSample('Layout_with_floating', '100%', '520') }}</p>

<p>Using floating boxes this way, is what many CSS frameworks do. It's a robust and well-known technique but it has limits as everything must go with the flow: it's not possible to handle boxes in arbitrary order, variable sizing can be quite tricky to achieve, and vertical centering is impossible. We encourage you to dig deeper as <a href="http://www.positioniseverything.net/articles/onetruelayout/">floating boxes has been studied for long</a> and they are among the most robust solutions for a simple layout that must be compatible with legacy browsers.</p>

<p>If you want to better understand all the subtleties of floating boxes, we encourage you to read <em><a href="https://css-tricks.com/all-about-floats/">All about float</a></em> by Chris Coyer.</p>

<h3 id="Positioning">Positioning</h3>

<p>If floating boxes are still part of the flow, another mechanism exists to perform some layouts by extracting boxes out of the flow: CSS Positioning. Positioning is acheived by defining a positioning context with the {{cssxref("position")}} property and then allows boxes to be positioned using {{cssxref("top")}}, {{cssxref("left")}}, {{cssxref("right")}}, and {{cssxref("bottom")}} properties.</p>

<p>The {{cssxref("position")}} property can take on four different values:</p>

<dl>
 <dt>static</dt>
 <dd>This is the default value for all elements: they are part of the flow and don't define any specific positioning context.</dd>
 <dt>relative</dt>
 <dd>With this value, elements are still part of the flow, but they can be visually moved around their positions with {{cssxref("top")}}, {{cssxref("left")}}, {{cssxref("right")}}, and {{cssxref("bottom")}}. They also define a positioning context for their children elements.</dd>
 <dt>absolute</dt>
 <dd>With this value, elements are pushed out of the flow and no longer influence it. The position of such blocks is defined by the {{cssxref("top")}}, {{cssxref("left")}}, {{cssxref("right")}}, and {{cssxref("bottom")}} properties. The 0,0 position point for the top/left corner of the box is the top/left corner of the closest parent element which defines a positionning context other than <em>static</em>. If there is no parent with a positioning context, then, the 0,0 position point for the top/left corner of the box is the top/left corner of the document.</dd>
 <dt>fixed</dt>
 <dd>With this value, elements are pushed out of the flow and no longer influence it. The position of such blocks is define by the {{cssxref("top")}}, {{cssxref("left")}}, {{cssxref("right")}}, and {{cssxref("bottom")}} properties. The 0,0 position point for the top/left corner of the box is the top/left corner of the browser window {{Glossary("viewport")}}.</dd>
</dl>

<p>Such positioned boxes can stack on top of each other. In that case, it's possible to change the stacking order by using the {{cssxref("z-index")}} property.</p>

<p>Okay, let's see an example to visualize it at work.</p>

<p>HTML:</p>

<pre class="brush: html">&lt;div class="relative"&gt;
    &lt;div class="absolute-one"&gt;
        &lt;p&gt;Position:absolute&lt;/p&gt;&lt;/br&gt;
        &lt;p&gt;Top Right&lt;/p&gt;
    &lt;/div&gt;

    &lt;div class="absolute-two"&gt;
        &lt;p&gt;Position:absolute&lt;/p&gt;&lt;/br&gt;
        &lt;p&gt;Bottom Centre&lt;/p&gt;
    &lt;/div&gt;

    &lt;div class="absolute-three"&gt;
        &lt;p&gt;Position:static&lt;/p&gt;&lt;/br&gt;
        &lt;p&gt;Where it falls&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;</pre>

<p>CSS:</p>

<pre class="brush: css">p {
  text-align:  centre;
  color: #fff;
}


/* Setting the position to relative
allows any child elements to be positioned
anywhere, in <em><strong>relation</strong> to its container. */
</em>
.relative {
  position: relative;
  width: 95%;
  margin: 0 auto;
  height: 300px;
  background-color: #fff;
  border: 3px solid #ADD8E6;
}

/* Just some styles for text alignment /*
.relative div {
  text-align: center;
  padding: 5px;
  display: block;
  width: 125px;
  height: 125px;
  background-color: #ADD8E6;
}

/* By setting this div to position absolute
we can position this element anywhere in relation
to the 'relative' div /*
.absolute-one {
   position: absolute;
   top: 0;
   right: 0;
}

/* Unlike the first div which was positioned at the
top right corner of the container div. '.absolute-two'
is positioned bottom centre. By setting both left and right
to 0, along with margin:auto.
.absolute-two {
   position: absolute;
   bottom: 0;
   right: 0;
   left: 0;
   margin: auto;
}

/* Where the div would fall naturally within it's container.
This is also useful for returning elements from a floated
position. E.g. on responsive styles. /*
.absolute-three {
   position: static;
}
</pre>

<p>Results:</p>

<p>{{ EmbedLiveSample('Positioning', '100%', '400') }}</p>

<p>If CSS positioning isn't really used for full layout, it is used quite often to deal with trick UX effect such as navigation layout, tooltip and such. This is something you'll see on a regular basis so we encourage you to get into it. Among various resources about it, we suggest you take a look at the article <em><a href="http://alistapart.com/article/css-positioning-101">CSS positioning 101</a></em> by Noah Stokes.</p>

<h2 id="What's_next">What's next</h2>

<p>The flow, the floating boxes,  and CSS positioning are the basic CSS knowledge that will drive you into CSS layout. Now you are ready to use CSS to its full potential! You should now take some time looking at <a href="/en-US/docs/Learn/CSS/Howto">practical usage of CSS</a>. If you want to dig even deeper into layouts, you should definitely take a look at the other layout mechanism that exists with CSS: Table display, <a href="/en-US/docs/Web/CSS/CSS_Columns/Using_multi-column_layouts">multiple columns layout</a>, and <a href="/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes">flexible box layout</a>.</p>

<p>{{PreviousNext("Learn/CSS/Basics/Box_model","Learn/CSS/Howto/style_text")}}</p>

<div class="grammarly-disable-indicator"> </div>