åºç¨æè±ç¦»æµå¼å¸å±
å¨ä¹åçææ¡£ä¸æè§£éäºå¸¸è§æµä¸åï¼blockï¼åè¡ï¼inlineï¼å¸å±ï¼å¸¸è§æµä¸çææå ç´ é½ä¼ä»¥è¿ç§å¸å±æ¹å¼è¿ä½ã
å¨ä¸é¢çä¾åä¸ï¼ææ°å»ºäºä¸ä¸ªæ é¢ï¼h1 å ç´ ï¼ï¼ä¸ä¸ªæ®µè½ï¼p å ç´ ï¼ï¼ä¸ä¸ªæ åºå表ï¼ul å ç´ ï¼åä¸ä¸ªå å« strong å ç´ çæ®µè½ï¼p å ç´ ï¼ï¼æ é¢å段è½é½æ¯å级ï¼block levelï¼ï¼strong å ç´ ä¸ºè¡çº§ï¼inlineï¼ãå表ä¸ç项éè¿å¼¹æ§çå¸å±ææä¸è¡ï¼ä½æ¯å表æ¬èº«ä»ç¶å¤äºååå èå¸å±ä¸ - ä»ç display 屿§ä¸º blockã
<div class="box">
<h1>A heading</h1>
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney.
</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<p>
Their names were <strong>Stephen and Joseph Montgolfier</strong>, they were
papermakers by trade, and were noted as possessing thoughtful minds and a
deep interest in all scientific knowledge and new discovery.
</p>
</div>
body {
font: 1.2em sans-serif;
}
.box > * {
border: 1px solid green;
}
ul {
display: flex;
justify-content: space-around;
list-style: none;
margin: 0;
}
å¯ä»¥è¯´ï¼ç¤ºä¾ä¸çææå ç´ é½å±äºå¸¸è§æµï¼æç §æºä»£ç ä¸çé¡ºåºæ¸²æå°é¡µé¢ä¸ã
è±ç¦»å¸¸è§æµçå ç´
ä¸åå ç´ ä¼è±ç¦»å¸¸è§æµï¼
- floated itemsãæµ®å¨çå ç´
- items with
position: absolute(includingposition: fixedwhich acts in the same way)ãéè¿è®¾ç½® position 屿§ä¸º absolute æè fixed çå ç´ - the root element (
html) æ ¹å ç´
è±ç¦»å¸¸è§æµçå ç´ ä¼å建ä¸ä¸ªæ°çåçº§æ ¼å¼åä¸ä¸æï¼Block Formatting Context: BFCï¼ç¯å¢ï¼å ¶ä¸å å«çææå ç´ ææäºä¸ä¸ªå°çå¸å±ç¯å¢ï¼ä¸é¡µé¢ä¸çå ¶ä»å 容åé弿¥ãèæ ¹å ç´ ï¼ä½ä¸ºé¡µé¢ä¸ææå 容ç容å¨ï¼èªèº«è±ç¦»å¸¸è§æµï¼ä¸ºæ´ä¸ªææ¡£å建äºä¸ä¸ªåçº§æ ¼å¼åä¸ä¸æç¯å¢ã
Floated Items
å¨è¿ä¸ªä¾åä¸ï¼ææä¸ä¸ª divï¼ä»¥å两个段è½ãæå·²ç»ä¸ºæ®µè½æ·»å äºèæ¯é¢è²ï¼ç¶åå° div å左浮å¨ãdiv ç°å¨å·²ç»ä¸åå¤äº flow ä¸äºã
ä½ä¸ºä¸ä¸ªæµ®å¨çå ç´ ï¼å®é¦å æ ¹æ®æ£å¸¸ flow å¸å±ï¼ç¶å仿µå¨ä¸ååºå¹¶å°½å¯è½å°å左移å¨
<div class="box">
<div class="float">I am a floated box!</div>
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney. Their names
were Stephen and Joseph Montgolfier, they were papermakers by trade, and
were noted as possessing thoughtful minds and a deep interest in all
scientific knowledge and new discovery.
</p>
<p>
Before that nightâa memorable night, as it was to proveâhundreds of millions
of people had watched the rising smoke-wreaths of their fires without
drawing any special inspiration from the fact.
</p>
</div>
body {
font: 1.2em sans-serif;
}
p {
background-color: #ccc;
}
.float {
float: left;
font-weight: bold;
width: 200px;
border: 2px dotted black;
padding: 10px;
}
ä½ å¯ä»¥çå°ä¸é¢æ®µè½çèæ¯é¢è²å¨ä¸é¢è¿è¡ï¼åªæ¯è¯¥æ®µè½çè¡æ¡å·²è¢«ç¼©ç以导è´å¨æµ®å¨å¨å´å 裹å å®¹çææãæä»¬æ®µè½çæ¹æ¡ä»ç¶æç §æ£å¸¸æµç¨è§åæ¾ç¤ºãè¿å°±æ¯ä¸ºä»ä¹è¦å¨æµ®å¨é¡¹ç®å¨å´çåºç©ºé´ï¼å¿ é¡»ä¸ºé¡¹ç®æ·»å è¾¹è·ï¼ä»¥ä¾¿å°çº¿æ¡æ¨ç¦»å®ãä½ æ æ³å¯¹ä»¥ä¸å æå 容åºç¨ä»»ä½å 容æ¥å®ç°æ¤ç®çã
Absolute Positioning
设置å
ç´ å±æ§ä¸º position: absolute æè
position: fixed ä¼ä½¿æ¤å
ç´ è±ç¦»ææ¡£æµï¼ä»æ¬æ¥å æ®ç空é´ä¹ä¼è¢«ç§»é¤ãå¨ä¸é¢çä¾åä¸ï¼æå®ä¹äºä¸ä¸ª p å
ç´ ï¼å¹¶ä¸å°ç¬¬äºä¸ª p å
ç´ è®¾ç½®ä¸º position absolute, top: 30px , right: 30px. 使å
¶è±ç¦»ææ¡£æµã
<div class="box">
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney.
</p>
<p class="abspos">
Their names were Stephen and Joseph Montgolfier, they were papermakers by
trade, and were noted as possessing thoughtful minds and a deep interest in
all scientific knowledge and new discovery.
</p>
<p>
Before that nightâa memorable night, as it was to proveâhundreds of millions
of people had watched the rising smoke-wreaths of their fires without
drawing any special inspiration from the fact.
</p>
</div>
body {
font: 1.2em sans-serif;
}
.box {
width: 70%;
}
p {
border: 2px solid green;
}
.abspos {
position: absolute;
background-color: green;
color: #fff;
top: 30px;
right: 30px;
width: 400px;
}
设置 position: fixed ä¹è½ä½¿å
ç´ è±ç¦»ææ¡£æµï¼ä½æ¯åç§»é伿 ¹æ®è§å£è䏿¯ç¶å
ç´ æ¥å®ã
å½éè¿å®ä½æ¹å¼ä½¿å ç´ è±ç¦»ææ¡£æµï¼å ç´ å 容å¯è½éå ï¼è¿éè¦ä½ èªå·±å»å¤çãè±ç¦»ææ¡£æµæå³ç页é¢ä¸çå ¶ä»å ç´ ä¸ç¥é该å ç´ çåå¨ï¼æ ä¸ä¼å¯¹è¯¥å ç´ ååºååºã
Relative Positioning and Flow
å¦æä½ ç»ä¸ä¸ªå ç´ å¼å¯ç¸å¯¹å®ä½ï¼é£è¯¥å ç´ ä¾ç¶ä¼ä½äºææ¡£æµä¸ï¼ç¶èä½ å¯ä»¥éè¿è®¾ç½®åç§»å¼çæ¹å¼æ¥ç§»å¨ä»ãæ£å¦ä¸é¢çä¾åæå±ç¤ºçï¼è¯¥å ç´ çåå å æ®ç空é´ä»ç¶ä¼è¢«ä¿çã
<div class="box">
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney.
</p>
<p class="relative">
Their names were Stephen and Joseph Montgolfier, they were papermakers by
trade, and were noted as possessing thoughtful minds and a deep interest in
all scientific knowledge and new discovery.
</p>
<p>
Before that nightâa memorable night, as it was to proveâhundreds of millions
of people had watched the rising smoke-wreaths of their fires without
drawing any special inspiration from the fact.
</p>
</div>
body {
font: 1.2em sans-serif;
}
.box {
width: 70%;
}
p {
border: 2px solid green;
}
.relative {
position: relative;
background-color: green;
color: #fff;
bottom: 50px;
left: 50px;
width: 400px;
}
å½ä½ ç§»å¨ä¸ä¸ªå ç´ æè 使å ç´ è±ç¦»ææ¡£æµï¼ä¸ºé²æ¢éå ï¼ä½ å¯è½éè¦å¯¹å ç´ å 容åå ç´ å¨å´çå 容åä¸äºç®¡çï¼è¦ä¹æ¸ 餿µ®å¨ï¼è¦ä¹ä¿è¯ç¸å¯¹å®ä½ä¸ä¼è¦çå ¶ä»å ç´ ã
Summary
In this guide we have covered the ways to take an element out of flow in order to achieve some very specific types of positioning. In the next guide we will look at a related issue, that of creating a Block Formatting Context, in Formatting Contexts Explained.
See Also
- Learn Layout: Positioning