我有这段 CSS:
#manipulate
{
position:absolute;
width:300px;
height:300px;
background:#063;
bottom:0px;
right:25%;
}
我有以下的HTML代码:
<div id="manipulate" align="center">
</div>
我们怎样才能把那个div定位在屏幕底部中间位置?!?
div {
position: fixed;
left: 50%;
bottom: 20px;
transform: translate(-50%, -50%);
margin: 0 auto;
}
<div>
Your Text
</div>
align="center"
没有效果。
由于您使用了position:absolute
,我建议将其从左侧定位到50%,然后从左边距中减去一半的宽度。
#manipulate {
position:absolute;
width:300px;
height:300px;
background:#063;
bottom:0px;
right:25%;
left:50%;
margin-left:-150px;
}
使用负边距:
#manipulate
{
position:absolute;
width:300px;
height:300px;
margin-left:-150px;
background:#063;
bottom:0px;
left:50%;
}
关键在于使用 width
、left
和 margin-left
属性。
style="position:absolute;width:300px;height:300px;margin-left:-150px;background:#063;bottom:0px;left:50%;"
- Empire of E这里有一个使用两个div的解决方案:
HTML:
<div id="footer">
<div id="center">
Text here
</div>
</div>
CSS:
#footer {
position: fixed;
bottom: 0;
width: 100%;
}
#center {
width: 500px;
margin: 0 auto;
}
使用 Flexbox 对我有帮助:
#manipulate {
position: absolute;
width: 100%;
display: flex;
justify-content: center; // Centers the item
bottom: 10px; // Moves it up a little from the bottom
}
首先创建一个容器,这样您就可以更自由地移动它:
<div class="container">
<div id="manipulate"></div>
</div>
然后:
.container{
height: 100vh; ------>adjust it. Should fit wherever you need to put it
display: flex;
flex-direction: column;
justify-content: end;
}
#manipulate{
width: min(70vw - 5px, 750px); ------>responsive stuff
height: 90vh;
margin-inline: auto;
}
100%工作的单行(内联CSS解决方案)
<div style="padding: 20px; width: 100%; text-align: center;">Your Content Here</div>
100%可行的单行(内联CSS解决方案)
<div style="position: fixed; bottom: 10px; width: 100%; text-align: center;">Your Content Here</div>
margin-left
设置为-50%
,而不是给定一个静态值。 - Dev_noobleft: 50%
将左侧放置于中心位置,而不是将中间放置于中心位置。 - Jeppe