要居中一个HTML元素,我可以使用CSS左:50%;。但是,这使得元素相对于整个窗口居中。
我有一个元素,它是<div>元素的子元素,我想让子元素以<div>这个父元素居中,而不是整个窗口。
我不希望容器<div>的所有内容居中,只是一个特定的子。
要居中一个HTML元素,我可以使用CSS左:50%;。但是,这使得元素相对于整个窗口居中。
我有一个元素,它是<div>元素的子元素,我想让子元素以<div>这个父元素居中,而不是整个窗口。
我不希望容器<div>的所有内容居中,只是一个特定的子。
当前回答
我喜欢魔法。
html, body {
height: 100%;
}
.flex-container{
display: -ms-flexbox;
display: -webkit-box;
display: flex;
-ms-flex-align: center;
-ms-flex-pack: center;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
}
<div class="flex-container">
<div>Content</div>
</div>
其他回答
<html>
<head>
</head>
<style>
.out{
width:200px;
height:200px;
background-color:yellow;
}
.in{
width:100px;
height:100px;
background-color:green;
margin-top:50%;
margin-left:50%;
transform:translate(-50%,50%);
}
</style>
<body>
<div class="out">
<div class="in">
</div>
</div>
</body>
</html>
div是固定宽度还是流体宽度?无论哪种方式,对于流体宽度,您可以使用:
#element { /* this is the child div */
margin-left:auto;
margin-right:auto;
/* Add remaining styling here */
}
或者你可以设置父div为text-align:center;子div的text-align:left;。
和左:50%;当div设置为position:absolute时,只根据整个页面居中。如果你将div设置为左侧:50%;它应该相对于父div的宽度。对于固定宽度,这样做:
#parent {
width:500px;
}
#child {
left:50%;
margin-left:-100px;
width:200px;
}
有很多方法可以让元素居中,可以使用显示属性,位置,浮动,边距。但通常我更喜欢使用flexbox,因为它简单易用。我知道不同的人有不同的偏好,但这完全取决于开发人员的偏好和元素之间的关系。
.parent { 显示:块; } .sub-parent-1 { 显示:flex; justify-content:中心;/ /水平居中 对齐项目:中心;/ /垂直定心 } 身体< > < div class = "父" > < div class = " sub-parent-1”> < div class = " child-1 " >…< / div > < / div > < div class = " child-2 " >…< / div > < div class = " child-3 " >…< / div > < div class = " child-4 " >…< / div > < div class = " child-5 " >…< / div > < / div > 身体< / >
设置text-align:中心;到父div,页边距:auto;到子div。
#parent { text-align:center; background-color:blue; height:400px; width:600px; } .block { height:100px; width:200px; text-align:left; } .center { margin:auto; background-color:green; } .left { margin:auto auto auto 0; background-color:red; } .right { margin:auto 0 auto auto; background-color:yellow; } <div id="parent"> <div id="child1" class="block center"> a block to align center and with text aligned left </div> <div id="child2" class="block left"> a block to align left and with text aligned left </div> <div id="child3" class="block right"> a block to align right and with text aligned left </div> </div>
这是一个很好的资源来集中几乎任何东西。 http://howtocenterincss.com/
例如,我有一个文章div里面的主要内容div.. 在文章里面有一个图片,图片下面是一个按钮,样式如下:
.article {
width: whatever;
text-align: center;
float: whatever (in case you got more articles in a row);
margin: give it whatever margin you want;
} .article {
}
/*在文章里面,我想让图像居中*/
文章{
float: none;
margin: 0 auto;
padding: give it a padded ridge if you want;
}
/*现在,在同一article元素的这张图片下,应该有一个按钮,如阅读更多。当然,当它在响应式设计中时,你需要使用em或%
.button {
background: #whatever color:
padding: 4.3567%; /*Instead of fixed width*/
text-align: cente;
font: whatever;
max-width: 41.4166%;
float: none;
margin: 0 auto; /* You could make the zero into any margin you want on the top and bottom of this button.. Just notice the float: none property.. this wil solve most your issues, also check if maybe position and displaay might mess up your code..*/
}
祝你好运