CSSå± ä¸ä¹ { left:50%; top:50%; transform:translate(-50%,-50%); }
left:50%; top:50%; transform:translate(-50%,-50%);
left:50%; top:50%; transform:translate(-50%,-50%);
ä¹å¯ä»¥åæ: left:50%; top:50%; translate: -50% -50%;
left:50%; top:50%; translate: -50% -50%;
-
ç¨ translate çå¿ é¡»æ¯å级å ç´ , æè absolute,fixed æä¼çæ
æ¯å¦ translate å¨ relative+inline æ¶ä¸ä¼èµ·ä½ç¨ -
left å top å¨ position:relative,absolute,fixed æä¼çæ
æ以positionä¸è½æ¯static ; displayä¸è½æ¯inline,
-
relative+inline æ¶, leftåtopèµ·ä½ç¨, translateä¸èµ·ä½ç¨
-
relative+block æ¶, relative使å¾leftåtopèµ·æ , block使å¾translateèµ·æ
æ¹æ³1: ä¸ç¨è®¾ç½®ç¶å
ç´ , 被å±
ä¸å
ç´ position:relative, å¿
é¡»æ¯åå
ç´ [ block | inline-block | flex | inline-flex | grid | inline-grid | table | inline-table | â¦çå级å
ç´ ] , ä¸è½æ¯ inline
.被å±
ä¸å
ç´ {position:relative; display:block; left:50%; top:50%; transform:translate(-50%,-50%);}
.被å±
ä¸å
ç´ {position:relative; display:inline-block; left:50%; top:50%; transform:translate(-50%,-50%);}
.被å±
ä¸å
ç´ {position:relative; display:grid; left:50%; top:50%; transform:translate(-50%,-50%);}
.被å±
ä¸å
ç´ {position:relative; display:inline-grid; left:50%; top:50%; transform:translate(-50%,-50%);}
.被å±
ä¸å
ç´ {position:relative; display:flex; left:50%; top:50%; transform:translate(-50%,-50%);}
.被å±
ä¸å
ç´ {position:relative; display:inline-flex; left:50%; top:50%; transform:translate(-50%,-50%);}
.被å±
ä¸å
ç´ {position:relative; display:table; left:50%; top:50%; transform:translate(-50%,-50%);}
.被å±
ä¸å
ç´ {position:relative; display:inline-table; left:50%; top:50%; transform:translate(-50%,-50%);}
æ¹æ³2: ç¶å ç´ è®¾ç½®position:< relative | absolute | fixed> , è¢«å± ä¸å ç´ position:absoluteæfixed, ä¸è½æ¯relative , å¯ä»¥ä¸ç¨è®¾ç½®display
.ç¶å
ç´ {position:relative;}
.被å±
ä¸å
ç´ {position:absolute; left:50%; top:50%; transform:translate(-50%,-50%);}
.ç¶å
ç´ {position:absolute;}
.被å±
ä¸å
ç´ {position:absolute; left:50%; top:50%; transform:translate(-50%,-50%);}
.ç¶å
ç´ {position:fixed;}
.被å±
ä¸å
ç´ {position:absolute; left:50%; top:50%; transform:translate(-50%,-50%);}
.ç¶å
ç´ {position:relative;}
.被å±
ä¸å
ç´ {position:fixed; left:50%; top:50%; transform:translate(-50%,-50%);}
.ç¶å
ç´ {position:absolute;}
.被å±
ä¸å
ç´ {position:fixed; left:50%; top:50%; transform:translate(-50%,-50%);}
.ç¶å
ç´ {position:fixed;}
.被å±
ä¸å
ç´ {position:fixed; left:50%; top:50%; transform:translate(-50%,-50%);}
transform:translate(-50%,-50%)
å¯ä»¥åæ translate -50% -50%
.被å±
ä¸å
ç´ {position:relative; display:block; left:50%; top:50%; translate -50% -50%;}
.ç¶å
ç´ {position:relative;}
.被å±
ä¸å
ç´ {position:absolute; left:50%; top:50%; translate -50% -50%;}
ä¾å
<div id="B" style="width: 800px; display: grid; grid: auto / 10fr 10fr">
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>ä¸å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative;</label>
<div
class="å±
ä¸å®¹å¨"
style="
position: relative;
border: 1px solid black;
width: 100%;
height: 200px;
"
>
<span
class="å±
ä¸å
ç´ "
style="
position: absolute;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::position:relative;<br />åå
ç´ :: position:absolute;</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: block;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:block</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: inline-block;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:inline-block</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: flex;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:flex</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: inline-flex;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:inline-flex</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: grid;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:grid</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: inline-grid;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:inline-grid</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: table;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:table</label>
<div
class="å±
ä¸å®¹å¨"
style="border: 1px solid black; width: 100%; height: 200px"
>
<span
class="å±
ä¸å
ç´ "
style="
position: relative;
display: inline-table;
left: 50%;
top: 50%;
translate: -50% -50%;
width: 200px;
height: 100px;
font-size: 66px;
border: 1px solid red;
text-align: center;
"
>å±
ä¸
</span>
</div>
<label>ç¶å
ç´ ::<br />åå
ç´ :: position:relative; display:inline-table</label>
</div>