问题描述
我希望整个块在其父块中居中,但我希望块的内容左对齐.
示例最有用
在此页面上:
ascii 图像应该居中(正如它所显示的那样)但它应该排列并看起来像YAML".
或者这个:
错误消息应该像在控制台中一样排列.
重新发布另一个问题的有效答案:如何将可变宽度的浮动元素水平居中?
假设浮动并居中的元素是一个 id="content" 的 div ...
并应用以下 CSS
#wrap {向左飘浮;位置:相对;左:50%;}#内容 {向左飘浮;位置:相对;左:-50%;}
这是关于 http://dev.opera.com/articles/view/35-floats-and-clearing/#centeringfloats
I want a whole block to be centered in its parent, but I want the contents of the block to be left aligned.
Examples serve best
On this page :
the ascii art should be centered (as it appears) but it should line up and look like "YAML".
Or this :
the error message should all line up as it does in a console.
Reposting the working answer from the other question: How to horizontally center a floating element of a variable width?
Assuming the element which is floated and will be centered is a div with an id="content" ...
<body>
<div id="wrap">
<div id="content">
This will be centered
</div>
</div>
</body>
And apply the following CSS
#wrap {
float: left;
position: relative;
left: 50%;
}
#content {
float: left;
position: relative;
left: -50%;
}
Here is a good reference regarding that http://dev.opera.com/articles/view/35-floats-and-clearing/#centeringfloats
这篇关于CSS:居中块,但将内容向左对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!