我已经用HTML创建了一个离散的图例,但我正在努力让图例标题正确对齐,而不管标题中的文本长度如何.

下面是一个例子,它在我的应用程序中看起来像一个简短的标题

enter image description here

... 而这是一个较长的标题

enter image description here

下面是一个简单的工作示例

/* Split the screen in half */
.split {
  height: 100%;
  width: 50%;
  position: fixed;
  z-index: 1;
  top: 0;
  overflow-x: hidden;
  padding-top: 20px;
}

/* Control the left side */
.left {
  left: 0;
}

/* Control the right side */
.right {
  right: 0;
  background-color: #999;
}
.box{
  background-color: black;
  height: 100%;
  width: 100%;
  
}

.my-legend .legend-title {
    text-align: center;
    margin-bottom: 5px;
    font-weight: bold;
    font-size: 30px;
    color: red;
}
.my-legend .legend-scale ul {
    margin: 0;
    margin-bottom: 5px;
    padding: 0;
    float: left;
    list-style: none;
}
.my-legend .legend-scale ul li {
    text-align: right;
    list-style: none;
    margin-left: 0;
    line-height: 18px;
    margin-bottom: 2px;
    color: white;
    font-size: 20px;
    padding-bottom: 40px;
}
.my-legend ul.legend-labels li span {
    display: block;
    float: right;
    height: 45px;
    width: 20px;
    margin-right: 0px;
    margin-left: 5px;
    border: 1px solid #999;
}
<div class="row" id="content">

  <!-- Image Panel -->
  <div class="split left" style="position: relative; height: 800px">

    <!-- This is a dynamically generated panel ... but just black here as example -->
    <div id="imagePanel" class="box"></div> 

    <!-- Legend entries -->
    <div class='my-legend' 
         style="position:absolute; 
                right: 10%; top: 50%;
                transform-box: fill-box; 
                transform: translate(0%,-50%);">

      <div class='legend-scale'>
        <ul class='legend-labels'>
          <li> <b>0</b> &nbsp; <span style="background: rgb(128,64,0)">   </span> </li>
          <li> <b>1</b> &nbsp; <span style="background: rgb(251,255,128)"></span> </li>
          <li> <b>2</b> &nbsp; <span style="background: rgb(55,128,0)">   </span> </li>
          <li> <b>3</b> &nbsp; <span style="background: rgb(128,255,140)"></span> </li>
          <li> <b>4</b> &nbsp; <span style="background: rgb(0,128,81)">   </span> </li>
          <li> <b>5</b> &nbsp; <span style="background: rgb(128,234,255)"></span> </li>
          <li> <b>6</b> &nbsp; <span style="background: rgb(0,38,128)">   </span> </li>
          <li> <b>7</b> &nbsp; <span style="background: rgb(157,128,255)"></span> </li>
          <li> <b>8</b> &nbsp; <span style="background: rgb(98,0,128)">   </span> </li>
          <li> <b>9</b> &nbsp; <span style="background: rgb(255,128,217)"></span> </li>
        </ul>
      </div>
    </div>

    <!-- Legend Title -->
    <div class='my-legend' 
         style="position:absolute; 
                right: 0; top: 50%; 
                transform-box: fill-box; 
                transform: translate(0%,-50%); 
                transform: rotate(-90deg)">

      <div class='legend-title' >True Label</div>      
    </div>

  </div>

  <!-- Controls -->
  <div class="split right" id="controlsPanel" >      
    Some Other Panel will go here
  </div>
</div>

推荐答案

writing-mode应该可以避开旋转造成的宽度/高度头痛,但我找不到一种方法来避免旋转180度来翻转它.

编辑:用网格替换绝对/相对定位,定位图例文本而不覆盖.

/* Split the screen in half */

body {
  margin: 0;
}

.row {
  display: grid;
  grid-template-columns: 50% 50%;
}

.left {
  display: grid;
  grid-template-columns: 4fr min-content 1fr;
  background-color: black;
}

.right {
  background-color: #999;
}

.legend-title {
  max-height: 40ch;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  text-align: center;
  font-weight: bold;
  font-size: 2rem;
  color: red;
}

.my-legend .legend-scale ul {
  margin: 0;
  margin-bottom: 5px;
  padding: 0;
  float: left;
  list-style: none;
}

.my-legend .legend-scale ul li {
  text-align: right;
  list-style: none;
  margin-left: 0;
  line-height: 18px;
  margin-bottom: 2px;
  color: white;
  font-size: 20px;
  padding-bottom: 40px;
}

.my-legend ul.legend-labels li span {
  display: block;
  float: right;
  height: 45px;
  width: 20px;
  margin-right: 0px;
  margin-left: 5px;
  border: 1px solid #999;
}
<div class="row" id="content">
  <!-- Image Panel -->
  <div class="split left">
    <!-- This is a dynamically generated panel ... but just black here as example -->
    <div id="imagePanel" class="box"></div>
    <!-- Legend entries -->
    <div class='my-legend'>
      <div class='legend-scale'>
        <ul class='legend-labels'>
          <li><b>0</b>&nbsp;<span style="background: rgb(128,64,0)"></span></li>
          <li><b>1</b>&nbsp;<span style="background: rgb(251,255,128)"></span></li>
          <li><b>2</b>&nbsp;<span style="background: rgb(55,128,0)"></span></li>
          <li><b>3</b>&nbsp;<span style="background: rgb(128,255,140)"></span></li>
          <li><b>4</b>&nbsp;<span style="background: rgb(0,128,81)"></span></li>
          <li><b>5</b>&nbsp;<span style="background: rgb(128,234,255)"></span></li>
          <li><b>6</b>&nbsp;<span style="background: rgb(0,38,128)"></span></li>
          <li><b>7</b>&nbsp;<span style="background: rgb(157,128,255)"></span></li>
          <li><b>8</b>&nbsp;<span style="background: rgb(98,0,128)"></span></li>
          <li><b>9</b>&nbsp;<span style="background: rgb(255,128,217)"></span> </li>
        </ul>
      </div>
    </div>
    <!-- Legend Title -->
    <div class='legend-title'>True Label test test test test test test test test test test test test test test test test test test test</div>

  </div>
  <!-- Controls -->
  <div id="controlsPanel" class="split right">
    Some Other Panel will go here
  </div>
</div>

Html相关问答推荐

子元素以元素X开始和结束的元素的XPath?

Angular 分量被循环

将表格背景图像置于行背景 colored颜色 之上

Tailwind CSS:overflow-y Property Not Scrolling Unreliably to Bottom with top-[63px]

Div中的图像问题-(TailWind CSS中的网格)

如何将内容元素放在侧边栏旁边?

浏览器是否可以呈现存储在代码点0x09处的字形?

水平行中按钮的垂直偏移

为什么字体大小而不是 colored颜色 属性适用于元素?

网格项未在同一行上对齐

当底部进入视图时从底部粘性定位

如何消除线性渐变线的模糊?

在锥形渐变进度条内创建空白的问题

增加第一个字母的大小不再正确居中文本

为什么在使用src与srcdoc时 iframe 内容高度呈现不同?

focusout() Select 器不适用于搜索框

我可以将两个元素之间的所有空间都放在弹性框或网格中吗?

为什么我的动画会向 Moz Firefox 中的微调器添加人工制品?

CSS过渡在开始时有黑色背景,为什么?

使用 :has() 伪类在select元素中设置样式选项