【发布时间】:2012-10-12 11:39:47
【问题描述】:
我正在使用 css3 开发一个移动 HTML5 页面,我有如下页面
<!DOCTYPE html>
<html>
<head>
<title>Starter page</title>
<style>
.float-right{float:right}
.float-left{float:left}
.clear{clear:both}
</style>
</head>
<body>
<div class="content" id="home">
<div class=" ">
<!--START: Div to be centered to page-->
<div class="DivToBeCentered">
<div class="float-left">
<!--START: This paragraph content is dynamic from database, i want to center align the div with the class
DivToBeCentered with reference to its parent-->
<p class="">dynamic para 1 with background image</p>
<!--END: This paragraph content is dynamic from database, i want to center align the div with the class
DivToBeCentered with reference to its parent-->
</div>
<div class=" float-left">
<p class="">static para 2 without background image</p>
</div>
<div class="clear"></div>
</div>
<!--END: Div to be centered to page-->
</div>
</div>
</body>
</html>
我想将上面代码中带有“DivToBeCentered”类的 div 水平居中对齐到其父 div。在那个 div 里面有两个段落标签,因为一个段落标签的内容是动态的,所以它的宽度在加载页面时可能会改变,我如何将该 div 与“DivToBeCentered”类居中对齐到它的父级。如果你解决了这个问题,对我会更有帮助,我今天被困在这个问题上。提前谢谢
【问题讨论】:
-
可以指定要居中的div的宽度吗?
-
div 的宽度是动态的,因为要居中的 div 内的段落标签包含来自数据库的内容。
-
给那个 div 一个最小宽度。 & 应用边距:0 auto;
-
@SVS 我像你建议的那样给出它不起作用,现在我尝试了 firefox.element.style { margin: 0 auto;最小宽度:10px; }
-
查看我在回答中提供的演示