简体   繁体   English

如何使用CSS将“ float:right”表外的文本向左对齐

[英]How to align left for a text outside a “float: right” table with CSS

I am trying to align the text " Legend " lefts the table. 我正在尝试将文本“ 图例 ”左对齐。 For the margin, it may not work when the windows screen has been resized (responsive). 为了保证页边距,在调整Windows屏幕大小(响应)时,它可能不起作用。 I tried the text-align: left or center but it didn't work as my expected. 我尝试了text-align: leftcenter但是没有按我预期的那样工作。

Actually, the div element can be changed by using another one or adding JS to make it work. 实际上,可以通过使用另一个元素或添加JS使其更改来更改div元素。

Here is the Fiddle link: https://jsfiddle.net/h4bsp4or/ 这是Fiddle链接: https : //jsfiddle.net/h4bsp4or/

See the following screen-shot of how it should look like: 请参见以下屏幕截图,该截图应为以下形式:

https://i.stack.imgur.com/hd92J.png https://i.stack.imgur.com/hd92J.png

 .tdAlignTop { vertical-align: text-top !important; } .legendTable { border: 1px solid #A6A6A6; width: auto; /*margin-right: 0px; margin-left: auto;*/ float: right; } .legendTable span { border: 1px solid #ccc; float: left; width: 12px; height: 12px; margin: 2px; } .legendTable .legendTO { background-color: #A9D08E; } .legendTable .legendT { background-color: #A6A6A6; } .legendTable .legendM { background-color: #FF0039; } .legendTable .legendN { background-color: #000000; } 
  <TD class=tdAlignTop> <DIV>Legend</DIV> <TABLE class=legendTable> <TBODY> <TR> <TD> <SPAN class=legendT></SPAN>T</TD> </TR> <TR> <TD> <SPAN class=legendM></SPAN>M</TD> </TR> <TR> <TD> <SPAN class=legendN></SPAN>N To L</TD> </TR> <TR> <TD> <SPAN class=legendTO></SPAN>T TO</TD> </TR> </TBODY> </TABLE> </TD> 

You can do that in this way. 您可以通过这种方式进行操作。 Add

clear:both;

for the legend table. 图例表。 and

float:right and margin:[some] px;

for the div caption. 用于div标题。 It will stick the left side of the table top even on window resize. 即使在调整窗口大小时,它也会粘在桌面的左侧。

 .tdAlignTop { vertical-align: text-top !important; } .legendTable { border: 1px solid #A6A6A6; width: auto; clear:both; /*margin-right: 0px; margin-left: auto;*/ float: right; } .legendTable span { border: 1px solid #ccc; float: left; width: 12px; height: 12px; margin: 2px; } .legendTable .legendTO { background-color: #A9D08E; } .legendTable .legendT { background-color: #A6A6A6; } .legendTable .legendM { background-color: #FF0039; } .legendTable .legendN { background-color: #000000; } 
  <TD class=tdAlignTop> <DIV style="float:right;margin-right:24px;">Legend</DIV> <TABLE class=legendTable> <TBODY> <TR> <TD> <SPAN class=legendT></SPAN>T</TD> </TR> <TR> <TD> <SPAN class=legendM></SPAN>M</TD> </TR> <TR> <TD> <SPAN class=legendN></SPAN>N To L</TD> </TR> <TR> <TD> <SPAN class=legendTO></SPAN>T TO</TD> </TR> </TBODY> </TABLE> </TD> 

You're floating .legendTable when you should be floating a wrapper around what's inside #tdAlignTop . 你浮.legendTable时,你应该漂浮在里面有什么包装#tdAlignTop

.legend-wrapper {
  float: right;
}

html: 的HTML:

<div class="legend-wrapper">
    <DIV>Legend</DIV>
    <TABLE class=legendTable>
      ...
    </TABLE>
</div>

See fiddle: https://jsfiddle.net/1hhya7xz/ 参见小提琴: https : //jsfiddle.net/1hhya7xz/

您也可以将以下代码用于图例,但这不是最好的方法:

<DIV align="right">Legend</DIV>

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

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