简体   繁体   中英

How to align text left in centered object

I've got the following code:

<center><div class="body">
<span style="margin-left:5px;text-align:left;">A text</span>
</div></center>

The text-align:left; isn't working. I've also tried margin: 0px auto; and that also didn't work.

Please help me...

Apply display: block on span

 .body span{margin-left:5px;text-align:left; display: block;} 
 <center><div class="body"> <span>A text</span> </div></center> 

Please try following code:

<center>
   <div class="body">
     <div style="margin-left: 5px; text-align: left;">A text</div>
   </div>
</center>

However <center> tag is deprecated in HTML5 bcz it is related with alignment of content for which its better to use CSS.. A better approach is as follows:

<div class="body">
    <span>A text</span>
</div>

.body {
    text-align: center;
}
.body span {
    text-align: left;
    margin-left: 5px;
    display: block;
}

I would not use a SPAN in this case, I would use a DIV or a P. A SPAN element is not for showing lots of content and you can not put other block elements inside the SPAN otherwise it won't validate. I only say that because I misused the SPAN element for years by myself ;).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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