不同的文本溢出属性适用于 display: block 与 display: inline。

huangapple go评论63阅读模式
英文:

Different text overflow properties for display: block vs display: inline

问题

这个问题对我来说太微妙了,无法在Google上找到答案,我一直在努力寻找一个好的答案。

我希望当span元素溢出一个div时,它们能够被省略号截断。问题在于,这种行为只有在我将span的display属性设置为block时才有效,但这会导致其他没有足够长以溢出div的span的宽度被扩展,而我希望它们保持它们内部内容的大小。

在这个在线编辑器中,这里有一个演示 - 尝试将"display"从"block"更改为"inline",以了解我的意思。
http://tpcg.io/_0UNNZM

任何帮助将不胜感激,我在css方面是新手,学习起来一直充满挑战。 不同的文本溢出属性适用于 display: block 与 display: inline。

英文:

This question is too nuanced for me to find an answer on Google and I've been struggling with it for so long trying to find a good answer.

I want span elements to cut off with an ellipsis when they overflow a div. The problem is that this behavior only works when I set the span's display to block, but this causes the other spans who aren't long enough to overflow the div have their widths expanded, when I want them to stay only the size of their inner content.

Here's a demonstration in an online editor - try changing display from "block" to "inline" to see what I mean.
http://tpcg.io/_0UNNZM

Any help would be appreciated, I am new at css and it has been treacherous to learn so far. 不同的文本溢出属性适用于 display: block 与 display: inline。

答案1

得分: 0

My solution is a bit hacky. I gave the second span a classname.

	<span class="secondElement">000</span>

I then set the second element to display: inline:

.secondElement {
    display: inline;
}

It solved the issue. Here is the working solution: http://tpcg.io/_PJLSI5 .

Edit:
Since the elements are dynamic. I implemented the change the functionality with the help of JavaScript.

First, I add a class name spansContainer so that I can reference it with JavaScript. And I also removed the class on the second span element:

 <div class="spansContainer">
      <span>0000000000000000000000</span>
      <br />
      <span>000</span>
    </div>

In the CSS, I set display to inline and added a new class block, that sets display to block:

  span {
        background-color: #0000ff80;
        overflow: hidden;
        text-overflow: ellipsis;
        display: inline;  /* set it to inline*/
      }
    /* this class is added to the span element using JavaScript */
      .block {
        display: block;
      }

From there, I wrote the JavaScript solution:

const spansContainer = document.querySelector(".spansContainer");
      const spanElements = document.querySelectorAll("span");
     // add the class `block` to all span elements
     // that have a width greater than the parent 
     // on page load.
      spanElements.forEach((spanEl) => {
        if (spanEl.offsetWidth > spansContainer.offsetWidth) {
          spanEl.classList.add("block");
        }
      });

      // configuration of the observer:
      config = {
        attributes: true,
        childList: true,
        characterData: true,
        subtree: true,
      };
      // create an observer instance
      var observer = new MutationObserver(function (mutations) {
        // loop through the mutated span elements
        mutations.forEach(function (mutation) {
          const spanEl = mutation.target.parentElement;
          if (spanEl.offsetWidth > spansContainer.offsetWidth) {
            spanEl.classList.add("block");
          } else {
            // remove class 'block' if span's width is 
            // smaller than the div
            spanEl.classList.remove("block");
          }
        });
      });

      // pass in the target node, as well as the observer options
      observer.observe(spansContainer, config)

I created the codepen for the solution here: https://codepen.io/Stanleyu/pen/bGjJwxO

英文:

My solution is a bit hacky. I gave the second span a classname.

	&lt;span class=&quot;secondElement&quot;&gt;000&lt;/span&gt;

I then set the second element to display: inline:

.secondElement {
	    display: inline;
	}

It solved the issue. Here is the working solution: http://tpcg.io/_PJLSI5 .

Edit:
Since the elements are dynamic. I implemented the change the functionality with the help of JavaScript.

First, I add a class name spansContainer so that I can reference it with JavaScript. And I also removed the class on the second span element:

 &lt;div class=&quot;spansContainer&quot;&gt;
      &lt;span&gt;0000000000000000000000&lt;/span&gt;
      &lt;br /&gt;
      &lt;span&gt;000&lt;/span&gt;
    &lt;/div&gt;

In the CSS, I set display to inline and added a new class block, that sets display to block:

  span {
        background-color: #0000ff80;
        overflow: hidden;
        text-overflow: ellipsis;
        display: inline;  /* set it to inline*/
      }
    /* this class is added to the span element using JavaScript */
      .block {
        display: block;
      }

From there, I wrote the JavaScript solution:

const spansContainer = document.querySelector(&quot;.spansContainer&quot;);
      const spanElements = document.querySelectorAll(&quot;span&quot;);
     // add the class `block` to all span elements
     // that have a width greater than the parent 
     // on page load.
      spanElements.forEach((spanEl) =&gt; {
        if (spanEl.offsetWidth &gt; spansContainer.offsetWidth) {
          spanEl.classList.add(&quot;block&quot;);
        }
      });

      // configuration of the observer:
      config = {
        attributes: true,
        childList: true,
        characterData: true,
        subtree: true,
      };
      // create an observer instance
      var observer = new MutationObserver(function (mutations) {
        // loop through the mutated span elements
        mutations.forEach(function (mutation) {
          const spanEl = mutation.target.parentElement;
          if (spanEl.offsetWidth &gt; spansContainer.offsetWidth) {
            spanEl.classList.add(&quot;block&quot;);
          } else {
            // remove class &#39;block&#39; if span&#39;s width is 
            // smaller than the div
            spanEl.classList.remove(&quot;block&quot;);
          }
        });
      });

      // pass in the target node, as well as the observer options
      observer.observe(spansContainer, config)

I created the codepen for the solution here: https://codepen.io/Stanleyu/pen/bGjJwxO

huangapple
  • 本文由 发表于 2023年2月7日 03:28:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定