将 div 框集中到中间和内部文本,与屏幕大小无关
Centralize div box to the middle and the inside text, independent of screen size
我试图让框始终位于页面的中心(水平和垂直),而不受 window 浏览器大小的影响。框内的文本也应位于其所在框的中央。
body{
background-color: black;
}
.box{
border-style: solid;
width: 240px;
height: 240px;
margin: auto;
}
.boxInside{
border-style: solid;
width: 90%;
height: 90%;
margin: auto;
padding: 0px;
/* align a div vertically within its parent element */
position: relative;
top: 50%;
transform: translateY(-50%);
}
p{
text-align: center;
}
<div class="box" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div style="">
<p>Testing Display</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
对每个 div
使用 flexbox 并水平和垂直居中对齐。
body {
background-color: black;
}
div {
display: flex;
justify-content: center;
align-items: center;
}
.box {
border-style: solid;
width: 240px;
height: 240px;
margin: auto;
}
.boxInside {
border-style: solid;
width: 90%;
height: 90%;
margin: auto;
padding: 0px;
/* align a div vertically within its parent element
position: relative;
top: 50%;
transform: translateY(-50%);
*/
}
p {
text-align: center;
}
<div class="box" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div style="">
<p>Testing Display</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
我试图让框始终位于页面的中心(水平和垂直),而不受 window 浏览器大小的影响。框内的文本也应位于其所在框的中央。
body{
background-color: black;
}
.box{
border-style: solid;
width: 240px;
height: 240px;
margin: auto;
}
.boxInside{
border-style: solid;
width: 90%;
height: 90%;
margin: auto;
padding: 0px;
/* align a div vertically within its parent element */
position: relative;
top: 50%;
transform: translateY(-50%);
}
p{
text-align: center;
}
<div class="box" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div style="">
<p>Testing Display</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
对每个 div
使用 flexbox 并水平和垂直居中对齐。
body {
background-color: black;
}
div {
display: flex;
justify-content: center;
align-items: center;
}
.box {
border-style: solid;
width: 240px;
height: 240px;
margin: auto;
}
.boxInside {
border-style: solid;
width: 90%;
height: 90%;
margin: auto;
padding: 0px;
/* align a div vertically within its parent element
position: relative;
top: 50%;
transform: translateY(-50%);
*/
}
p {
text-align: center;
}
<div class="box" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div class="boxInside" style="background-color: white;">
<div class="boxInside" style="background-color: gray;">
<div style="">
<p>Testing Display</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>