英文:
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方面是新手,学习起来一直充满挑战。
英文:
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.
答案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.
<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论