简体   繁体   English

我有一个<p>为空的元素,在执行期间用文本填充元素时,如何给它高度而不将元素向下推?

[英]I have a <p> element that is empty, how do I give it height and not push elements down when I fill the element with text during execution?

When the user does things with the app, I want to have an annoying <p> element telling what they just did.当用户对应用程序进行操作时,我希望有一个烦人的<p>元素来告诉他们刚刚做了什么。 So I devised a simple code to show my problem.所以我设计了一个简单的代码来显示我的问题。 Every time I put text into my initially empty <p> tag, it pushes everything else on the screen down when it finally has text and height.每次我将文本放入最初为空的<p>标记时,当它最终具有文本和高度时,它会将屏幕上的其他所有内容向下推。 Is there a way to force the <p> tag to have height and take up its space without the text in it?有没有办法强制<p>标签具有高度并在没有文本的情况下占用它的空间? I have tried putting it in a div and doing a clear fix on it even with no floats.我已经尝试将它放在一个div中并对其进行明确修复,即使没有浮动。 I have tried ::before and ::after with content: "" .我试过::before::after content: "" All of my other attempts have failed as well.我所有的其他尝试也都失败了。 I know that I could put some text in it, either a single space or text with the same color as the background to force it to work, but I was trying to find a more elegant solution.我知道我可以在其中放置一些文本,可以是单个空格,也可以是与背景颜色相同的文本来强制它工作,但我试图找到一个更优雅的解决方案。 Is there one?有吗?

 const actionButton = document.getElementById("actionButton"); let flashSomeText = function() { const flashTextEl = document.getElementById("popupText"); flashTextEl.textContent = "You Just Got Paid"; flashTextEl.style.background = "rgb(18, 163, 49)"; flashTextEl.style.color = "white"; let unflashSomeText = function() { flashTextEl.textContent = "" flashTextEl.style.background = "white"; flashTextEl.style.color = "black" }; setTimeout(unflashSomeText, 1500); }; actionButton.addEventListener("click", flashSomeText);
 @charset "utf-8"; body { background: rgb(211, 211, 211); max-width: 1680px; height: 100vh; } #wrapper { background: white; width: 90%; margin: 0 auto; min-height: 100%; } header>h1 { font-size: 2rem; font-weight: bold; padding: 1rem 0; text-align: center; } #popupText { width: 200px; margin: 0 auto; text-align: center; } #actionButtonDiv { margin-top: 10px; text-align: center; } #footerText { text-align: center; }
 <div id="wrapper"> <header> <h1>Some Header Text</h1> </header> <p id="popupText"></p> <div id="actionButtonDiv"> <button type="button" id="actionButton">Flash Text</button> </div> <footer id="footerText"> <p>Some Footer Text</p> </footer> </div>

Add a linebreak to your empty <p> tag with <br>使用<br>为空的<p>标签添加换行符

<p id="popupText"><br></p>

Also while unflashing, set the innerHTML (replace .textContent with innerHTML ) of this <p> tag to <br>此外,在取消闪烁时,将此<p>标记的innerHTML (将.textContent替换为innerHTML )设置为<br>

let unflashSomeText = function() {
  flashTextEl.innerHTML = "<br>"
  flashTextEl.style.background = "white";
  flashTextEl.style.color = "black"
};

Full code:完整代码:

 const actionButton = document.getElementById("actionButton"); let flashSomeText = function() { const flashTextEl = document.getElementById("popupText"); flashTextEl.textContent = "You Just Got Paid"; flashTextEl.style.background = "rgb(18, 163, 49)"; flashTextEl.style.color = "white"; let unflashSomeText = function() { // resetting the text content to a linebreak again flashTextEl.innerHTML = "<br>" flashTextEl.style.background = "white"; flashTextEl.style.color = "black" }; setTimeout(unflashSomeText, 1500); }; actionButton.addEventListener("click", flashSomeText);
 @charset "utf-8"; body { background: rgb(211, 211, 211); max-width: 1680px; height: 100vh; } #wrapper { background: white; width: 90%; margin: 0 auto; min-height: 100%; } header>h1 { font-size: 2rem; font-weight: bold; padding: 1rem 0; text-align: center; } #popupText { width: 200px; margin: 0 auto; text-align: center; } #actionButtonDiv { margin-top: 10px; text-align: center; } #footerText { text-align: center; }
 <div id="wrapper"> <header> <h1>Some Header Text</h1> </header> <!-- replacing empty text with a linebreak --> <p id="popupText"><br></p> <div id="actionButtonDiv"> <button type="button" id="actionButton">Flash Text</button> </div> <footer id="footerText"> <p>Some Footer Text</p> </footer> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM