【问题标题】:adjust the content inside the width调整宽度内的内容
【发布时间】:2021-08-05 03:57:20
【问题描述】:

我已经创建了一个工具提示,它显示的数据正在溢出。所以想要将内容放在工具提示的宽度内。它更多的是悬停时的徽章,我需要显示内容,我们也可以有 500px 的最大宽度和高度没有限制。您还可以看到,在悬停时,工具提示并没有完全落在它悬停的相应项目上。这是我尝试过的,下面是我的代码

 .lightgrey-badge {
width: auto;
height: 20px;
padding: 0px 8px 0px 8px;
border-radius: 4px;
background-color: #f2f2f2;
  }
  .lightgrey-badge-text {
font-size: 12px;
font-weight: 600;
text-align: center;
color: #595959;
vertical-align: middle;
  }
.border {
  border-radius: 20px;
  border: solid 2px #595959;
}

.lightgrey-badge-text-elipses {
  display: inline-block;
  max-width: 40px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tooltiptext {
  visibility: hidden;
  max-width: 250px;
  height: 50px;
  opacity: 0.6;
  box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.04);
  background-color: #000000;
  color: #ffffff;
  text-align: center;
  border-radius: 6px;
  position: absolute;
  z-index: 9999;
  margin-top: 28px;
  margin-left: -150px;
}

.tooltiptext::after {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent black transparent;
}

.lightgrey-badge:hover .tooltiptext {
  visibility: visible;
}
 <span
        class="lightgrey-badge border"
        style="margin-right: 5px"
      >
        <span
          class="lightgrey-badge-text lightgrey-badge-text-elipses"
          >Element
          <span class="tooltiptext">
            <span style="width: fit-content"> this s the dummy data the orifinal will look something like this a big long test.i have created a tooltip the data its showing is getting overflowed. so want to fit the content inside the width of the tooltip</span></span
          >
        </span>
      </span>

【问题讨论】:

    标签: html css vue.js


    【解决方案1】:

    元素宽度不合适的原因是它应该增加最大宽度。我将最大宽度更改为 50px。它是 40 像素

    .lightgrey-badge-text-elipses {
      max-width: 50px;
    }
    

    我为长文本添加了最小宽度、最大宽度和填充,如下所示

    .tooltiptext {
      min-width: 100px;
      max-width: 500px;
      padding-left: 100px;
      padding-right: 50px;
    
    }
    

    您可以断开段落的行以获取不同行的长文本,这样您就可以看到整个内容。像这样

    <span class = "new-content"><span style="width: fit-content"> this s the dummy data the orifinal will look something like this a big long test.i have created a tooltip the data its showing is getting overflowed. so want to fit the content inside the width of the tooltip</span></span></span>
    

    您还必须在 css 中添加一个类。

    .new-content {
      white-space: pre-wrap;
    } 
    

    【讨论】:

    • 不,实际上数据来自后端,所以我们无法破解代码,这对我不起作用
    • 哦,在这种情况下,我认为您可以添加white-space。我更改了代码。这可能会工作
    • 只是一个小改动.new-content { white-space: normal; }这对我有用
    【解决方案2】:

    试试这个代码它会帮助你

      .tooltip {
        position: relative;
        display: inline-block;
        border-bottom: 1px dotted black;
      }
      
      .tooltip .tooltiptext {
        visibility: hidden;
        background-color: black;
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 5px 0;
        /* Position the tooltip */
        position: absolute;
        display: block;
        z-index: 1;
      }
      
      .tooltip:hover .tooltiptext {
        visibility: visible;
      }
    <!DOCTYPE html>
    <html>
    
    
    <body style="text-align:center;">
    
      <p>Move the mouse over the text below:</p>
    
      <div class="tooltip">Hover over me
        <span class="tooltiptext">Tooltip text</span>
      </div>
      <br><br>
      <div class="tooltip">Hover over me Hover over me
        <span class="tooltiptext">blah bla blaa a bal  blak a ba b alj ang</span>
      </div>
      <br><br>
      <div class="tooltip">Hover over me Hover over me 3
        <span class="tooltiptext">askdjf ksfjlskd flsaa flsjdlfj sdfk sadkjflskd af sdkjf lsdkf sdljf</span>
      </div>
    </body>
    
    </html>

    验证我的回答是否适合您

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2012-10-13
      • 2011-09-06
      • 2016-11-06
      相关资源
      最近更新 更多