CSS:中心块,但将内容对齐到左边 [英] CSS: Center block, but align contents to the left
问题描述
我想要一个整个块在其父对象中居中,但我想让块的内容对齐。
I want a whole block to be centered in its parent, but I want the contents of the block to be left aligned.
示例最好
在此页面上:
ascii艺术应该居中
the ascii art should be centered (as it appears) but it should line up and look like "YAML".
或者:
错误消息应该与在控制台中一样排列。
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?
假设被浮动的元素和将被居中的元素是具有id =content的div ... ...
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>
并套用下列CSS
#wrap {
float: left;
position: relative;
left: 50%;
}
#content {
float: left;
position: relative;
left: -50%;
}
这里是一个很好的参考, http://dev.opera.com/articles/view/35-floats-and-clearing/ #centeringfloats
Here is a good reference regarding that http://dev.opera.com/articles/view/35-floats-and-clearing/#centeringfloats
这篇关于CSS:中心块,但将内容对齐到左边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!