我正在尝试在框中间的div内移动文本,我需要在页面中间移动div框。
#move {
border: 4px solid black;
display: inline-block;
width: 400px;
text-align: center;
height: 200px;
}
<div id="move">Move Me Center</div>
答案 0 :(得分:3)
margin:auto
以使div
成为中心line-height:200px;
#move {
border: 4px solid black;
width: 400px;
text-align: center;
height: 200px;
margin:auto;
line-height:200px
}
&#13;
<div id="move">Move Me Center</div>
&#13;
答案 1 :(得分:1)
除了使用line-height
之外,您还可以围绕文字创建span
并应用transform:translateY(50%)
并在{margin:auto
中将inline-block
更改为block
1}}
#move
&#13;
#move {
border: 4px solid black;
display: block;
width: 400px;
text-align: center;
height: 200px;
margin:auto
}
#move span {
transform: translateY(50%);
display:block;
height:100%
}
&#13;
答案 2 :(得分:0)
使line-height
与div的高度相同:line-height:200px;
答案 3 :(得分:0)
如果您的用户大多使用现代浏览器,则可以始终使用flexbox
作为解决方案。
克里斯在CSS-Tricks上有一个很棒的flexbox指南,here。
<div class="flex-container">
<div class="move">
<p>Move me center</p>
</div>
</div>
.flex-container {
display: flex;
flex-direction: row; /* Change to columns for vertical axis */
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
}
.move {
display: flex;
align-items: center;
justify-content: center;
height: 200px;
width: 400px;
border: 1px solid red;
}