Html 对齐浮动:左div到中心?
我想让一组图像水平显示在整个页面上。每个图像下面都有一些链接,所以我需要在每个图像/链接组周围放置一个容器Html 对齐浮动:左div到中心?,html,css,Html,Css,我想让一组图像水平显示在整个页面上。每个图像下面都有一些链接,所以我需要在每个图像/链接组周围放置一个容器 我最接近我想要的是把它们放在浮动的div中:左。问题是我希望容器对齐中心,而不是左对齐。如何实现这一点。使用显示:内联块而不是浮点 您不能使浮动居中,但内联块会像文本一样居中,因此在“行”的外部整体容器上,您可以设置text align:center然后,对于每个图像/标题容器(即内联块;),如果需要,您可以将文本重新向左对齐 <div id="divContainer">
我最接近我想要的是把它们放在浮动的div中:左。问题是我希望容器对齐中心,而不是左对齐。如何实现这一点。使用
显示:内联块代码>而不是浮点
您不能使浮动居中,但内联块会像文本一样居中,因此在“行”的外部整体容器上,您可以设置text align:center
然后,对于每个图像/标题容器(即内联块;
),如果需要,您可以将文本重新向左对齐
<div id="divContainer">
<div class="divImageHolder">
IMG HERE
</div>
<div class="divImageHolder">
IMG HERE
</div>
<div class="divImageHolder">
IMG HERE
</div>
<br class="clear" />
</div>
<style type="text/css">
#divContainer { margin: 0 auto; width: 800px; }
.divImageHolder { float:left; }
.clear { clear:both; }
</style>
我在这儿
我在这儿
我在这儿
CSS这些日子。有关flexbox的良好教程
这在所有较新的浏览器中都可以正常工作:
#容器{
显示器:flex;
柔性包装:包装;
证明内容:中心;
}
.街区{
宽度:150px;
高度:150像素;
背景色:#中交;
利润率:10px;
}
1.
2.
3.
4.
5.
只需将浮动元素包装在
中,并为其提供以下CSS:
.wrapper {
display: table;
margin: auto;
}
也许这就是你要找的-
CSS:
HTML:
1.
2.
3.
下载
.下载容器{
宽度:90%;
保证金:0自动;
文本对齐:居中;
边界:0px;
填充:20px;
}
.下载单元{
垂直对齐:居中;
高度:100px;
宽度:340px;
边框:1px纯黑;
边界半径:8px;
字体大小:24px;
字体颜色:黑色;
z指数:2;
利润率:20px;
背景:白色;
颜色:黑色;
显示:内联块;
利润率:20px;
填充顶部:20px;
}
如果有人正在处理元素之间出现的空格,请通过定义诸如“”或“”之类的元素来删除代码中的空白,或者设置父元素字体大小:0
并在子元素上还原它。或者在父对象上使用字母间距:-.31em
,在子对象上使用字母间距:0
。@Baumr使用flexbox代替。使用display:inline block
时,可能需要添加vertical align:middle
。这适用于水平居中。垂直居中如何?若要垂直居中,请为容器指定一个高度(例如高度:500px;)并添加对齐项目:居中;OP在他的问题中没有在块内指定任何复杂的代码,但他只是在图像和链接周围指定了一个包装元素。强烈反对只发布代码而不进行解释。@StephenMIrving c/p同样的评论,也不鼓励向下投票垃圾邮件。问题是:“对齐一个float:左div到中心?”并且给出了类“contentWrapper”和“repeater”的答案,这是不言自明的。这不是一个只包含代码内容的复杂算法问题的答案。我建议你先分析一下自己的判断,然后再自动装瓶/发送垃圾邮件,只发送带有downvote+相同注释的代码答案。强烈反对只发送代码而不提供解释。不鼓励只发送代码而不提供解释。另外,
标记上的type=“text/css”
是多余的,并且已经好几年没有必要了,对
标记应用clear类是一种反模式。
.wrapper {
display: table;
margin: auto;
}
#container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.block {
width: 150px;
height: 150px;
margin: 10px;
}
<div id="container">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
</div>
<html>
<head>
<title>Downloads</title>
<style>
.download-container{
width:90%;
margin:0 auto;
text-align:center;
border:0px;
padding:20px;
}
.download-cell{
vertical-align:center;
height:100px;
width:340px;
border:1px solid black;
border-radius:8px;
font-size:24px;
font-color:black;
z-index:2;
margin:20px;
background:white;
color:black;
display:inline-block;
margin:20px;
padding-top:20px;
}
</style>
</head>
<body>
<!--DOWNLOADS-->
<div class="download-container">
<br>
<div class="download-cell">
<a target="_blank" class="module" href="http://www.woocommerce.com">
<img src="Images/woo.png" style="width:60px; height:60px;" />
WooCommerce
</a>
</div>
<div class="download-cell">
<a target="_blank" class="module" href="http://magento.com">
<img src="Images/magento.png" style="width:60px; height:60px;" />
MAGENTO
</a>
</div>
<div class="download-cell">
<a target="_blank" class="module" href="http://www.lightspeedhq.com">
<img src="Images/lightspeed.png" style="width:60px; height:60px;" />
LIGHTSPEED
</a>
</div>
<div class="download-cell">
<a target="_blank" class="module" href="http://www.shopify.com">
<img src="Images/shopify.png" style="width:60px; height:60px;" />
SHOPIFY
</a>
</div>
<div class="download-cell">
<a target="_blank" class="module" href="http://opencart.com">
<img src="Images/opencart.png" style="width:60px; height:60px;" />
OPENCART
</a>
</div>
<div class="download-cell">
<a target="_blank" class="module" href="http://www.prestashop.com">
<img src="Images/prestashop.png" style="width:60px; height:60px;" />
PRESTASHOP
</a>
</div>
<div class="download-cell">
<a target="_blank" class="module" href="http://www.wix.com">
<img src="Images/wix.png" style="width:60px; height:60px;" />
WIX eCommerce
</a>
</div>
</div>
</body>
</html>