简体   繁体   English

在div的左侧也将其对齐

[英]Align text at left but also at center of my div

Im trying to align some links at center of my div, but I also want that text of my links are aligned at left. 我试图在div的中心对齐一些链接,但我也希望链接的文本在左侧对齐。

But Im not having sucess doing this, I am able to align to the left or to the center but not both simultaneously. 但是我没有成功执行此操作,因此我可以向左或向中心对齐,但不能同时对齐。

Do you know how we can do this? 你知道我们怎么做吗?

Here I have demonstration of what Im getting: http://jsfiddle.net/HRKN4/ 在这里,我演示了我正在得到什么: http : //jsfiddle.net/HRKN4/

I have this html: 我有这个HTML:

<div  class="container">
    <h3>Title:</h3>
    <div class="content">
       <ul class="links">
          <li> <a  href="#">&raquo;&raquo; This link is bigger</a></li>
          <li> <a  href="#">&raquo;&raquo; Smaller 1</a></li>
          <li> <a  href="#">&raquo;&raquo; link 1</a></li> 
       </ul>
     </div>
</div>

And this CSS: 而这个CSS:

.container
{
    text-align:center;
    width:100%;
    margin:0 auto 0 auto;
    background:pink;
}

.content
{
    width:100%;
    height:300px;
}

.links 
{
    list-style-type:none;
    margin:0 auto; 
}

.links li a 
{
    text-decoration:none;
    background:red;
    color:#000;
    margin-top:20px;
    margin:0 auto; 
}

You can use table layout : 您可以使用表格布局:

.links {
   display:table;
}

And

.links li a {
   display:table-row;
}

Example

Or just 要不就

.links li{
    text-align : left;
}

Instead of display:table-row; 代替display:table-row;

Example

You can specify a width for the "content" div and then center it using margin:0 auto; 您可以为“内容” div指定宽度,然后使用margin:0 auto;居中margin:0 auto; that way you can still use text-align:left; 这样,您仍然可以使用text-align:left; to left-align the text within the div while still keeping the div centered on the document. 使div中的文本左对齐,同时仍将div保持在文档中心。

Here is an updated JSFiddle 这是更新的JSFiddle

You can try: 你可以试试:

.links 
{
    display: inline-block;
    list-style-type:none;
    margin:0 auto; 
    text-align: left;
}

See demo at: http://jsfiddle.net/audetwebdesign/m9daD/ 参见演示: http : //jsfiddle.net/audetwebdesign/m9daD/

Another option: 另外一个选项:

.links 
{
    list-style-type:none;
    width:50%;
    float:right;
}

http://jsfiddle.net/HRKN4/17/ http://jsfiddle.net/HRKN4/17/

If you set ..containerlinks to display:inline-block; 如果您将..containerlinks设置为display:inline-block; and text-align:center; text-align:center; it should be enough. 应该足够了。

ul will take the width of the longest link and will be centered from text-align:center; ul将采用最长链接的宽度,并以text-align:center; All links can be aligned left inside it . 所有链接都可以在其中对齐。

DEMO 演示


.container {
    text-align:center;
    background:pink;
}
.content {
}
.links {
    list-style-type:none;
    display:inline-block;/* as an inline boxe it will centered from text align on .container */
    text-align:left;/* content will be aligned towards left */
}
.links li a {
    text-decoration:none;
    background:red;
    color:#000;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM