justify-self
åºçº¿
广æ³å¯ç¨
*
èª 2017å¹´10æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
* æ¤ç¹æ§çæäºé¨åçæ¯æç¨åº¦å¯è½ææä¸åã
CSS justify-self 屿§è®¾ç½®å个çåå¨å
¶å¸å±å®¹å¨éå½è½´ä¸ç坹齿¹å¼ã
å°è¯ä¸ä¸
justify-self: stretch;
justify-self: center;
justify-self: start;
justify-self: end;
<section class="default-example" id="default-example">
<div class="example-container">
<div class="transition-all" id="example-element">ä¸</div>
<div>äº</div>
<div>ä¸</div>
</div>
</section>
.example-container {
border: 1px solid #c5c5c5;
display: grid;
width: 220px;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 40px;
grid-gap: 10px;
}
.example-container > div {
background-color: rgb(0 0 255 / 0.2);
border: 3px solid blue;
}
æ¤å±æ§çææåå³äºæä»¬æå¤çå¸å±æ¨¡å¼ï¼
- å¨å级å¸å±ä¸ï¼å®å°é¡¹ç®å¨å ¶å å«åçè¡åè½´ä¸å¯¹é½ã
- 对äºç»å¯¹å®ä½çå ç´ ï¼å®å°é¡¹ç®å¨å ¶å å«åçè¡åè½´ä¸å¯¹é½ï¼åæ¶èèé¡¶é¨ã左侧ãåºé¨åå³ä¾§çåç§»å¼ã
- å¨è¡¨æ ¼å¸å±ä¸ï¼è¿ä¸ªå±æ§è¢«å¿½ç¥ãäºè§£æ´å¤å ³äºåºåãç»å¯¹å®ä½ä»¥åè¡¨æ ¼å¸å±ä¸ç坹齿¹å¼
- å¨å¼¹æ§å¸å±ä¸ï¼è¿ä¸ªå±æ§è¢«å¿½ç¥ãäºè§£æ´å¤å ³äºå¼¹æ§å¸å±ä¸ç坹齿¹å¼
- 卿 æ ¼å¸å±ä¸ï¼å®å°é¡¹ç®å¨å ¶ç½æ ¼åºåå æ²¿å è轴对é½ãäºè§£æ´å¤å ³äºæ æ ¼å¸å±ä¸ç坹齿¹å¼
è¯æ³
/* åºç¡å
³é®å */
justify-self: auto;
justify-self: normal;
justify-self: stretch;
/* ä½ç½®å¯¹é½ */
justify-self: center; /* å¨ä¸é´æ¾ç½®å
ç´ */
justify-self: start; /* å¨å¼å§å¤æ¾ç½®å
ç´ */
justify-self: end; /* å¨ç»æå¤æ¾ç½®å
ç´ */
justify-self: flex-start; /* ä¸ 'start' çæã注æ justify-self å¨ Flexbox å¸å±ä¸è¢«å¿½ç¥ã */
justify-self: flex-end; /* ä¸ 'end' çæã注æ justify-self å¨ Flexbox å¸å±ä¸è¢«å¿½ç¥ã */
justify-self: self-start;
justify-self: self-end;
justify-self: left; /* å¨å·¦ä¾§æ¾ç½®å
ç´ */
justify-self: right; /* å¨å³ä¾§æ¾ç½®å
ç´ */
/* åºçº¿å¯¹é½ */
justify-self: baseline;
justify-self: first baseline;
justify-self: last baseline;
/* 溢åºå¯¹é½ï¼åªå¯¹ä½ç½®å¯¹é½æææï¼*/
justify-self: safe center;
justify-self: unsafe center;
/* å
¨å±å¼ */
justify-self: inherit;
justify-self: initial;
justify-self: unset;
è¿ä¸ªå±æ§å¯ä»¥æä¸ç§ä¸åçå½¢å¼ï¼
- åºæ¬å
³é®åï¼
normalï¼autoï¼æèstretchã - åºçº¿å¯¹é½ï¼
baselineå ³é®åï¼å ä¸å¯éçfirstæèlastã - å¯å®ä½å¯¹é½ï¼
centerãstartãendãflex-startãflex-endãself-startãself-endãleftærightã- å ä¸å¯éç
safeæèunsafeã
å¼
auto-
该å¼ä½¿ç¨å ¶ç¶çº§å ç´ çåç
justify-items屿§çå¼ï¼é¤é宿²¡æç¶çº§å ç´ ï¼æè æ¯ç»å¯¹å®ä½çï¼è¿äºæ åµä¸ï¼auto代表äºnormalã normal-
è¿ä¸ªææåå³äºæä»¬æå¤çå¸å±æ¨¡å¼ï¼
- å¨å级å¸å±ä¸ï¼è¿ä¸ªå
³é®åæ¯
startçåä¹è¯ã - å¨ç»å¯¹å®ä½çå¸å±ä¸ï¼å¨å¯ç½®æ¢ç»å¯¹å®ä½ççåä¸ï¼è¿ä¸ªå
³é®å表ç°çä¸
startç¸ä¼¼ï¼å¨å ¶ä»ç»å¯¹å®ä½ççåä¸ï¼å ¶è¡¨ç°å¾ä¸stretchç¸ä¼¼ã - å¨è¡¨æ ¼å¸å±ä¸ï¼ç±äºè¿ä¸ªå±æ§è¢«å¿½ç¥æä»¥è¿ä¸ªå ³é®å没æä½ç¨ã
- å¨å¼¹æ§å¸å±ä¸ï¼ç±äºè¿ä¸ªå±æ§è¢«å¿½ç¥æä»¥è¿ä¸ªå ³é®å没æä½ç¨ã
- 卿
æ ¼å¸å±ä¸ï¼è¿ä¸ªå
³é®åä¼å¯¼è´ç±»ä¼¼äº
stretchçè¡ä¸ºï¼é¤äºå ·æé«å®½æ¯æåºæå¤§å°ççåï¼å®çè¡ä¸ºç±»ä¼¼äºstartã
- å¨å级å¸å±ä¸ï¼è¿ä¸ªå
³é®åæ¯
start-
项ç®è¢«æ¾ç½®å°éå½çè½´ä¸ï¼æå对é½å®¹å¨çèµ·å§è¾¹ç¼ï¼å½¼æ¤é½å¹³ã
end-
项ç®è¢«æ¾ç½®å°éå½çè½´ä¸ï¼æå对é½å®¹å¨çæ«ç«¯è¾¹ç¼ï¼å½¼æ¤é½å¹³ã
flex-start-
对äºéå¼¹æ§å®¹å¨åå ç´ ç项ç®ï¼è¿ä¸ªå¼è¢«è§ä¸º
startã flex-end-
对äºéå¼¹æ§å®¹å¨åå ç´ ç项ç®ï¼è¿ä¸ªå¼è¢«è§ä¸º
end. self-start-
The item is packed flush to the edge of the alignment container of the start side of the item, in the appropriate axis.
self-end-
The item is packed flush to the edge of the alignment container of the end side of the item, in the appropriate axis.
center-
项ç®è¢«æ¾ç½®å¨å¯¹é½å®¹å¨çä¸å¿åå½¼æ¤å¹³é½ã
left-
项ç®å¨å¯¹é½å®¹å¨ç左边ç¼ï¼å¹¶å½¼æ¤å¯¹é½ã妿屿§çè½´ä¸å èè½´ä¸å¹³è¡ï¼åæ¤å¼çè¡ä¸ºç±»ä¼¼äº
startã right-
项ç®å¨å¯¹é½å®¹å¨çå³è¾¹ç¼ï¼å¹¶å½¼æ¤å¯¹é½ã妿屿§çè½´ä¸å èè½´ä¸å¹³è¡ï¼åæ¤å¼çè¡ä¸ºç±»ä¼¼äº
startã baseline first baselinelast baseline-
Specifies participation in first- or last-baseline alignment: aligns the alignment baseline of the boxâs first or last baseline set with the corresponding baseline in the shared first or last baseline set of all the boxes in its baseline-sharing group. The fallback alignment for
first baselineisstart, the one forlast baselineisend. stretch-
If the combined size of the items is less than the size of the alignment container, any
auto-sized items have their size increased equally (not proportionally), while still respecting the constraints imposed bymax-height/max-width(or equivalent functionality), so that the combined size exactly fills the alignment container. safe-
If the size of the item overflows the alignment container, the item is instead aligned as if the alignment mode were
start. unsafe-
Regardless of the relative sizes of the item and alignment container, the given alignment value is honored.
å½¢å¼å®ä¹
| åå§å¼ | auto |
|---|---|
| éç¨å ç´ | block-level boxes, absolutely-positioned boxes, and grid items |
| æ¯å¦æ¯ç»§æ¿å±æ§ | å¦ |
| 计ç®å¼ | as specified |
| å¨ç»ç±»å | 离æ£å¼ |
å½¢å¼è¯æ³
justify-self =
auto |
<overflow-position>? [ normal | <self-position> | left | right ] |
stretch |
<baseline-position> |
anchor-center
<overflow-position> =
unsafe |
safe
<self-position> =
center |
start |
end |
self-start |
self-end |
flex-start |
flex-end
<baseline-position> =
[ first | last ]? &&
baseline
示ä¾
>ç®åæ¼ç¤º
In the following example we have a simple 2 x 2 grid layout. Initially the grid container is given a justify-items value of stretch â the default â which causes the grid items to stretch across the entire width of their cells.
The second, third, and fourth grid items are then given different values of justify-self, to show how these override the justify-items value. These values cause the grid items to span only as wide as their content width, and align in different positions across their cells.
HTML
<article class="container">
<span>First child</span>
<span>Second child</span>
<span>Third child</span>
<span>Fourth child</span>
</article>
CSS
html {
font-family: helvetica, arial, sans-serif;
letter-spacing: 1px;
}
article {
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 40px;
grid-gap: 10px;
width: 300px;
justify-items: stretch;
}
span:nth-child(2) {
justify-self: start;
}
span:nth-child(3) {
justify-self: center;
}
span:nth-child(4) {
justify-self: end;
}
article span {
background-color: black;
color: white;
margin: 1px;
text-align: center;
}
article,
span {
padding: 10px;
border-radius: 7px;
}
article {
margin: 20px;
}
ç»æ
è§è
| è§è |
|---|
| CSS Box Alignment Module Level 3 > # justify-self-property > |
æµè§å¨å ¼å®¹æ§
åè§
- CSS Grid Guide: Box alignment in CSS Grid layouts
- CSS Box Alignment
- The
justify-itemsproperty