Css 使元件水平居中
在许多情况下,水平定心不起作用。我检查了许多解决方案,但没有一个适合我。 我有两个包装器,我使用边距:0自动;在这里 要获得清晰的说明,您可以在这里看到JSFIDLE: 宽度:100%,它可以工作。 实际上,我想要一个固定长度的块来保持在中间的底部。 在我的开发过程中,类似代码还有另一个问题: 这是我的html:Css 使元件水平居中,css,alignment,margin,Css,Alignment,Margin,在许多情况下,水平定心不起作用。我检查了许多解决方案,但没有一个适合我。 我有两个包装器,我使用边距:0自动;在这里 要获得清晰的说明,您可以在这里看到JSFIDLE: 宽度:100%,它可以工作。 实际上,我想要一个固定长度的块来保持在中间的底部。 在我的开发过程中,类似代码还有另一个问题: 这是我的html: <html> <head lang="en"> <meta charset="UTF-8"> <title>test&
<html>
<head lang="en">
<meta charset="UTF-8">
<title>test</title>
<link href="test.css" rel="stylesheet">
</head>
<body>
<div class= 'container border'>
<div class="container-content border">
I should be in the middle
<div class="container-content-bottom border">
I also want go to middle
</div>
</div>
</div>
</body>
</html>
这将使文本居中。不确定这是否是期望的结果。如果没有,你可能想做一个你想要的油漆或其他东西的模型 CSS: 您不需要“位置:绝对”。此外,我给div加了一个<100%的宽度,你想把它放在中间 以下是我修改的css部分:
.container-content-bottom{
margin:0 auto; /*try use width:100% instead, strange effect happens*/
width:50%;
bottom: 0;
background-color:red;
}
使用
文本对齐:居中代码>和边距:0自动;显示:块
如果是块或图像,则应应用边距特性,并将其显示为块(如上所示)
例如:
.block {
margin-left: auto;
margin-right: auto;
display: block;
}
如果只是文本,则使用text align:center
附言:更多信息可以找到
只需添加右侧:0;左:0;保证金:自动代码>
将.container content bottom
的样式更改为
.container-content-bottom{
margin:auto;
position: absolute;
bottom: 0;
right:0;
left:0;
}
如果要继续使用“position:absolute”,可以为“left”定义一个值,使容器内容底部div居中
.container-content-bottom{
width:50%;
position:absolute;
bottom:0;
left:25%;
}
从这里可以定义div的宽度,并更改left的值,直到它居中为止
如果你想要的结果是“宽度:100%”,你也可以加上“宽度:100%”
.container-content-bottom{
width:100%;
position:absolute;
bottom:0;
}
对于页边距:0自动代码>若要工作,应为元素设置宽度。如果它是100%,它怎么能居中?或者,如果要使元素内容居中,请使用text align:center相反,我尝试设置宽度,但边距的设置不起作用。宽度:100%;文本对齐效果很好:),所以我仍然想知道为什么这个边距对meAh不起作用,我没有注意到您使用的是position:absolute代码>,为什么不文本对齐:居中代码>?@xFortyFourx text align对它不起作用o:第一种方法效果很好,但是为什么边距的东西对它不起作用呢?我想不出原因。我很确定它不起作用,因为“位置:绝对”;当元素绝对定位时,它从“左”、“右”、“上”和“下”获取其位置/坐标。这些值覆盖边距和填充。左边的默认值是0,如果你不定义任何其他东西,这可能是为什么它显示在左下角。它是相似的方式,宽度100%,实际上我想要一个固定长度的块留在中间的底部。
.container-content-bottom{
width:50%;
position:absolute;
bottom:0;
left:25%;
}
.container-content-bottom{
width:100%;
position:absolute;
bottom:0;
}