热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Howtomovefloateddivstothecentreofacontainer

IvegottworowsofboxesinsideacontainerandIneedthemtobeinthecenterofthepage.Ican

I've got two rows of boxes inside a container and I need them to be in the center of the page. I cannot set the width of the container because the stuff inside might change.

我在容器内有两排盒子,我需要它们在页面的中心。我无法设置容器的宽度,因为里面的东西可能会改变。

The reason why I don't break up the boxes into two rows within the HTML is because I'm using media queries to force two rows when the screen is tight, but it would naturally be one row when there is space on the screen.

我没有在HTML中将这些框分成两行的原因是因为我在屏幕紧张时使用媒体查询强制两行,但当屏幕上有空格时,它自然会是一行。

Note that I have an empty div for clearfix. Feel free to remove it if you think there is a better method.

请注意,我有一个空的div for clearfix。如果您认为有更好的方法,请随意删除它。

Here's my HTML:

这是我的HTML:

1
2
3
4

And here's my CSS:

这是我的CSS:

.box {
    width: 50px;
    height: 50px;
    background: red;
    color: white;
    margin: 10px;
    float: left;
}

.box:nth-child(3) {
    clear: both;
}

.container {
    background-color: grey;
}

The container is grey for illustration only, in my app it would be clear.

容器是灰色的仅用于说明,在我的应用程序中它将是清楚的。

Fiddle here

3 个解决方案

#1


1  

If you can wrap the elements, then you could set the display of the wrapper element to inline-block and then add text-align: center to the parent element for horizontal centering:

如果可以包装元素,则可以将包装元素的显示设置为内联块,然后将text-align:center添加到父元素以进行水平居中:

Updated Example

.container {
    background-color: grey;
    text-align: center;
}
.container .center-wrapper {
    display: inline-block;
}
.box {
    text-align: left;
}

.box {
    width: 50px;
    height: 50px;
    background: red;
    color: white;
    margin: 10px;
    float: left;
    text-align: left;
}
.box:nth-child(3) {
    clear: both;
}
.container {
    background-color: grey;
    text-align: center;
}
.container .center-wrapper {
    display: inline-block;
}
1
2
3
4


Using flexboxes, you could also use the following:

使用flexbox,您还可以使用以下内容:

Updated Example

.container {
    background-color: grey;
    display: flex;
    justify-content: center;
}

.box {
    width: 50px;
    height: 50px;
    background: red;
    color: white;
    margin: 10px;
    float: left;
}
.box:nth-child(3) {
    clear: both;
}
.container {
    background-color: grey;
    display: flex;
    justify-content: center;
}
1
2
3
4

#2


1  

You can just add a container div and give it a width and margin: 0 auto; http://jsfiddle.net/oc0w0n4n/1/

你可以添加一个容器div并给它一个宽度和余量:0 auto; http://jsfiddle.net/oc0w0n4n/1/

.center{
   margin: 0 auto;
   width:140px;
}

   
1
2
3
4

#3


0  

`
1
2
3
4
`

推荐阅读
  • Iamtryingtoachievethearrowpointingupwards..iamtryingtoachieveitinmycssiamnotabl ... [详细]
  • HowcanIaligntwoinline-blockssothatoneisleftandtheotherisrightonthesameline?Whyi ... [详细]
  • DatepickerlandedonChrome20,isthereanyattributetodisableit?MyentiresystemusesjQuery ... [详细]
  • 前端实用的CSS3技巧有哪些
    本文小编为大家详细介绍“前端实用的CSS3技巧有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“前端实用的CSS3技巧有哪些”文章能帮助大家 ... [详细]
  • 在本文中,我将向你介绍如何使用HTML5自定义数据属性。我还将向你介绍一些开发人员在工作中经常使用的优秀实例。为什么需要自定义数据属性?很多时候我们需要存储一些与不同DOM元素相关联的 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • css3中rem 与px算法
    看CCS文件的时候发现引入了一个新大小单位:rem,CSS文件里有介绍,看半天拿着手机计算了几下愣是没搞明白,怕是新东西搜出来的都是英文看着累就没找google,而是百度了一下,百度 ... [详细]
  • 一款清新的jQuery日历插件带日期的Tooltip提示543人浏览发表回复这是一款非常简单清新的jQuery日历插件,它并没有特别的日期或者时间选择功能,仅仅是展示一个简单的日历控件。这款jQu ... [详细]
  • 一篇文章搞定css3 3d效果
    css33d学习心得卡片反转魔方banner图首先我们要学习好css33d一定要有一定的立体感通过这个图片应该清楚的了解到了x轴y轴z轴是什么概念了。首先先给大家看一个小 ... [详细]
  •   html5与js,  本文介绍html5shiv.js和respond.min.js与大家分享,如下:  做 ... [详细]
  • 1.HTML5原生支持<video>简单使用:<videosrc..TestRestest.mp4autoplaycontrols><vide ... [详细]
  • js中运行机制的详细分析(示例解析)
    web前端|js教程node.js,html5,html,css,javascriptweb前端-js教程本篇文章给大家带来的内容是关于js中运行机制的详细分析(示例解析),有一定 ... [详细]
  • 你有什么见解
    本文主要介绍关于html5,css3的知识点,对【关于雾霾网站设计】和【你有什么见解】有兴趣的朋友可以看下由【-你在暮色中】投稿的技术文章,希望该技术和经验能帮到你解决你所遇的【】相关技术问题。你有 ... [详细]
author-avatar
潜伏在人间_144
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有