gpt4 book ai didi

html - 如何使用转换对齐 HTML 中的图例?

转载 作者:行者123 更新时间:2023-12-05 05:44:16 25 4
gpt4 key购买 nike

我使用 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 - 如何使用转换对齐 HTML 中的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71620272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com