繁体   English   中英

使用CSS将两个span元素对齐到同一行,而不管字体大小和内容长度如何

[英]Align two span elements to same line irrespective of font size and content length using CSS

我有两排。 第一行有一个指标和一个数字。 第二行只有一个数字。 我希望两行的数字垂直对齐。 但是我做不到。 跨度应该对齐,而不管字体大小和内容长度如何。

以下是代码段:

 #top-area { border: 3px solid #000; } #top-area>div:nth-of-type(1) { width: 100%; } div#header { background-color: #000; color: #fff; padding-top: 2px; padding-bottom: 2px; } div { text-align: center; } #top-area>div:nth-of-type(2) { width: 100%; font-weight: bold; margin-bottom: -1px; } .DownTrend-diamond { -webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg); -ms-transform: rotate(-45deg); -o-transform: rotate(-45deg); transform: rotate(-45deg); -webkit-transform-origin: 50% 50%; -moz-transform-origin: 50% 50%; -ms-transform-origin: 50% 50%; -o-transform-origin: 50% 50%; transform-origin: 50% 50%; margin-right: 10px; display: inline-block; } #top-area>div:nth-of-type(3) { width: 100%; } 
 <div id="top-area"> <div id="header" style="font-weight:normal;font-size: 16px;background-color: #5F6B6D;color: #000;">Header</div> <div style="font-size:14px; color: #008000!important ;font-weight:normal;background-color:#fff" class="DownTrend "> <div class="DownTrend-diamond" style="background-color:#FF0000 !important;width:10.5px;height:10.5px;"></div> <span style="color:#000 !important;">15.0</span> </div> <div style="font-size:14px; color: #000 !important ;font-weight:normal;background-color:#fff" class=" "><span style="color:#000 !important;">250.0</span></div> </div> 

如何解决此问题? 我不能太多更改HTML结构。 可以使用纯CSS完成吗? 任何帮助将不胜感激。

预期产量: 在此处输入图片说明

使用flex获得相同的效果

更新html添加类

<div id="top-area">
  <div id="header" style="font-weight:normal;font-size: 16px;background-color: #5F6B6D;color: #000;">Header</div>
  <div style="font-size:14px; color: #008000!important ;font-weight:normal;background-color:#fff; margin-top:5px;" class="DownTrend  ">
    <div class="squre">
      <div class="DownTrend-diamond" style="background-color:#FF0000 !important;width:10.5px;height:10.5px;">&nbsp;</div>
    </div>
    <div class="value">
      <span style="color:#000 !important;">15.0</span>
    </div>
  </div>
  <div>
    <div class="squre">
      <div class="" style="background-color:#FF0000 !important;"></div>
    </div>
    <div class="value">
      <span style="color:#000 !important;">25000.0</span>
    </div>
  </div>

更新css部分

#top-area>div:nth-of-type(2) {
    width: 100%;
    font-weight: bold;
    margin-bottom: -1px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-flow: row wrap;
}
 #top-area>div:nth-of-type(3) {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-flow: row wrap;
}
.value {
    flex: 1 0 35%;
    align-self: flex-start;
    text-align: left;
}
.squre {
    flex: 1 0 30%;
    align-self: flex-start;
    text-align: right;
}

 #top-area { border: 3px solid #000; } #top-area>div:nth-of-type(1) { width: 100%; } div#header { background-color: #000; color: #fff; padding-top: 2px; padding-bottom: 2px; } div { text-align: center; } #top-area>div:nth-of-type(2) { width: 100%; font-weight: bold; margin-bottom: -1px; display: flex; justify-content: center; align-items: center; flex-flow: row wrap; } #top-area>div:nth-of-type(3) { display: flex; justify-content: center; align-items: center; flex-flow: row wrap; } .value { flex: 1 0 35%; align-self: flex-start; text-align: left; } .squre { flex: 1 0 30%; align-self: flex-start; text-align: right; } .text-left { text-align: left; } .DownTrend-diamond { -webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg); -ms-transform: rotate(-45deg); -o-transform: rotate(-45deg); transform: rotate(-45deg); -webkit-transform-origin: 50% 50%; -moz-transform-origin: 50% 50%; -ms-transform-origin: 50% 50%; -o-transform-origin: 50% 50%; transform-origin: 50% 50%; margin-right: 10px; display: inline-block; } 
 <div id="top-area"> <div id="header" style="font-weight:normal;font-size: 16px;background-color: #5F6B6D;color: #000;">Header</div> <div style="font-size:14px; color: #008000!important ;font-weight:normal;background-color:#fff; margin-top:5px;" class="DownTrend "> <div class="squre"> <div class="DownTrend-diamond" style="background-color:#FF0000 !important;width:10.5px;height:10.5px;">&nbsp;</div> </div> <div class="value"> <span style="color:#000 !important;">15.0</span> </div> </div> <div> <div class="squre"> <div class="" style="background-color:#FF0000 !important;"></div> </div> <div class="value"> <span style="color:#000 !important;">25000.0</span> </div> </div> 

请检查以下内容:

 #top-area { border: 3px solid #000; } #top-area>div:nth-of-type(1) { width: 100%; } div#header { background-color: #000; color: #fff; padding-top: 2px; padding-bottom: 2px; } div { text-align: center; } #top-area>div:nth-of-type(2) { width: 100%; font-weight: bold; margin-bottom: -1px; } .DownTrend-diamond { -webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg); -ms-transform: rotate(-45deg); -o-transform: rotate(-45deg); transform: rotate(-45deg); -webkit-transform-origin: 50% 50%; -moz-transform-origin: 50% 50%; -ms-transform-origin: 50% 50%; -o-transform-origin: 50% 50%; transform-origin: 50% 50%; margin-right: 10px; display: inline-block; } #top-area>div:nth-of-type(3) { width: 100%; } 
 <div id="top-area"> <div id="header" style="font-weight:normal;font-size: 16px;background-color: #5F6B6D;color: #000;">Header</div> <div style="font-size:14px; color: #008000!important ;font-weight:normal;background-color:#fff; padding-left: 50%; text-align: left; width: 50%" class="DownTrend "> <div class="DownTrend-diamond" style="background-color:#FF0000 !important;width:10.5px;height:10.5px; position: absolute; margin-left: -30px; margin-top: 4px"></div> <span style="color:#000 !important;">15.0</span> </div> <div style="font-size:14px; color: #000 !important ;font-weight:normal;background-color:#fff; padding-left: 50%; text-align: left; width: 50%" class=" "><span style="color:#000 !important;">250.0</span></div> </div> 

尝试这个

在跨度样式中添加一个左边距

<div style="font-size:14px; color: #000 !important ;font-weight:normal;background-color:#fff" class="  "><span style="color:#000 !important;margin-left: 30px;">250.0</span></div>

您将获得预期的结果

#row1 div {
    float: left;
    left: 47%;
    margin-right: 0 !important;
    margin-top: 3px;
    position: relative;
}
#row1 span {
    float: left;
    left: 48.3%;
    position: relative;
}
#row2 {
    clear: left;
    position: relative;
    text-align: left;
}
#row2 span {
    left: 50%;
    position: relative;
}

https://jsfiddle.net/wazz/77r72nwL/

暂无
暂无

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

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