margin
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015ë 7ì.
* Some parts of this feature may have varying levels of support.
margin CSS ìì±ì ììì ë¤ ë°©í¥ ë°ê¹¥ ì¬ë°± ììì ì¤ì í©ëë¤. margin-top, margin-right, margin-bottom, margin-leftì ë¨ì¶ ìì±ì
ëë¤.
ìëí´ ë³´ê¸°
margin: 1em;
margin: 5% 0;
margin: 10px 50px 20px;
margin: 10px 50px 20px 0;
margin: 0;
<section id="default-example">
<div id="container">
<div class="row"></div>
<div class="row transition-all" id="example-element"></div>
<div class="row"></div>
</div>
</section>
#container {
width: 300px;
height: 200px;
display: flex;
align-content: flex-start;
flex-direction: column;
justify-content: flex-start;
}
.row {
height: 33.33%;
display: inline-block;
border: solid #ce7777 10px;
background-color: #2b3a55;
flex-shrink: 0;
}
#example-element {
border: solid 10px #ffbf00;
background-color: #2b3a55;
}
ìì ìë ì¬ë°±ì ëì²´ ììê° ìë <span>, <code> ë± ì¸ë¼ì¸ ìììì ì무 í¨ê³¼ë ììµëë¤.
ì°¸ê³ :
marginì ììì 주ìì ë¹ ê³µê°ì ì¶ê°í©ëë¤. ë°ë©´ paddingì ììì ë´ë¶ì ë¹ ê³µê°ì ë§ëëë¤.
구문
/* ë¤ ë©´ 모ë ì ì© */ margin: 1em; margin: -3px; /* ì¸ë¡ë°©í¥ | ê°ë¡ë°©í¥ */ margin: 5% auto; /* ì | ê°ë¡ë°©í¥ | ìë */ margin: 1em auto 2em; /* ì | ì¤ë¥¸ìª½ | ìë | ì¼ìª½ */ margin: 2px 1em 0 auto; /* ì ì ê° */ margin: inherit; margin: initial; margin: unset;
margin ìì±ì í ê°, ë ê°, ì¸ ê°, í¹ì ë¤ ê°ì ê°ì¼ë¡ ì§ì í ì ììµëë¤. ê° ê°ì <length>, <percentage> ëë í¤ìë auto ì¤ íëì
ëë¤. ìì ê°ì ììì ì´ìì ê±°ë¦¬ê° ë ê°ê¹ìì§ëë¡ í©ëë¤.
- í ê°ì ê°ì 모ë ë¤ ë©´ì ì¬ë°±ì ì¤ì í©ëë¤.
- ë ê°ì ê°ì ì§ì íë©´ 첫 ë²ì§¸ë ìì ìë, ë ë²ì§¸ë ì¼ìª½ê³¼ ì¤ë¥¸ìª½ ì¬ë°±ì ì¤ì í©ëë¤.
- ì¸ ê°ì ê°ì ì§ì íë©´ 첫 ë²ì§¸ë ì, ë ë²ì§¸ë ì¼ìª½ê³¼ ì¤ë¥¸ìª½, ì¸ ë²ì§¸ ê°ì ìë ì¬ë°±ì ì¤ì í©ëë¤.
- ë¤ ê°ì ê°ì ì§ì íë©´ ê°ê° ì, ì°, í, ì¢ ììë¡ ì¬ë°±ì ì§ì í©ëë¤. (ìê³ë°©í¥)
ê°
<length>-
ì¬ë°±ì í¬ê¸°ë¡ ê³ ì ê° ì¬ì©.
<percentage>-
ì¬ë°±ì í¬ê¸°ë¡ 컨í ì´ë ë¸ë¡ ëë¹ì ë°±ë¶ì¨ ì¬ì©.
auto-
ë¸ë¼ì°ì ê° ì ì í ì¬ë°± í¬ê¸°ë¥¼ ì í. ì를 ë¤ì´ ìì를 ì¤ì ì ë ¬íê³ ì¶ì ë ì¬ì©í ì ììµëë¤.
íì 구문
margin =
<'margin-top'>{1,4}
<margin-top> =
<length-percentage> |
auto |
<anchor-size()>
<length-percentage> =
<length> |
<percentage>
<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )
<anchor-name> =
<dashed-ident>
<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline
ìì
>ê°ë¨í ìì
HTML
<div class="center">ì´ ììë ì¤ì ì ë ¬ì
ëë¤.</div>
<div class="outside">ì´ ììë 컨í
ì´ë ë°ì¼ë¡ ë¹ ì ¸ ëê°ìµëë¤.</div>
CSS
.center {
margin: auto;
background: lime;
width: 66%;
}
.outside {
margin: 3rem 0 0 -3rem;
background: cyan;
width: 66%;
}
ë ë§ì ìì
margin: 5%; /* 모ë 5% */
margin: 10px; /* 모ë 10px */
margin: 1.6em 20px;
/* ìí: 1.6em */
/* ì¢ì°: 20px */
margin: 10px 3% -1em;
/* ì: 10px */
/* ì¢ì°: 3% */
/* í: -1em */
margin: 10px 3px 30px 5px;
/* ì: 10px */
/* ì°: 3px */
/* í: 30px */
/* ì¢: 5px */
margin: 2em auto;
/* ìí: 2em */
/* ìí ì¤ìì ë ¬ */
margin: auto;
/* ìí: 0 */
/* ìí ì¤ìì ë ¬ */
ì°¸ê³
>ìí ì¤ìì ë ¬
íë ë¸ë¼ì°ì ìì ì´ë¤ ìì를 ì¤ìì ë°°ì¹íë ¤ë©´ display: flex; justify-content: center;를 ì¬ì©íë©´ ë©ëë¤.
íì§ë§ íë ì¤ ë°ì¤ ë ì´ììì ì§ìíì§ ìë Internet Explorer 8-9 ë± ì¤ëë ë¸ë¼ì°ì 를 ì§ìí´ì¼ íë©´ margin: 0 auto;를 ëì ì¬ì©íì¸ì.
ì¬ë°± ìì
ë ê° ììì ìì ìë ì¬ë°±ì ì¢ ì¢ í©ì³ì ¸ íëì ì¬ë°±ì´ ëê³ , ê·¸ í¬ê¸°ë ë ì¤ ë í° ì¬ë°±ê³¼ ê°ìì§ëë¤. ë ìì¸í ì ë³´ë ì¬ë°± ìì ì ë³µì ì°¸ê³ íì¸ì.
ëª ì¸
| Specification |
|---|
| CSS Box Model Module Level 3 > # margin > |
| ì´ê¸°ê° | as each of the properties of the shorthand:
|
|---|---|
| ì ì©ëì | all elements, except elements with table display types other than table-caption, table and inline-table. It also applies to ::first-letter. |
| ìì | no |
| Percentages | refer to the width of the containing block |
| ê³ì° ê° | as each of the properties of the shorthand:
|
| Animation type | a length |