我有A< p>空的元素,当我在执行过程中填充文本时,如何给它高度而不将元素推下元素?
当用户使用该应用程序做事时,我想拥有一个烦人的< p>
元素告诉他们他们刚刚做了什么。因此,我设计了一个简单的代码来显示我的问题。每当我将文本放入最初的空< p>
标签中时,当它最终具有文本和高度时,它都会按屏幕上的其他所有内容。有没有办法强制< p>
标签具有高度并在没有文本的情况下占用其空间?我已经尝试将其放入div
中,即使没有浮子,也要对其进行清晰的修复。我已经尝试了::
和:: after
content:“” 。我所有的其他尝试也失败了。我知道我可以在其中写一些文本,要么是一个空间,要么具有与背景相同的颜色以迫使其工作的颜色,但我试图找到一个更优雅的解决方案。有一个吗?
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>
When the user does things with the app, I want to have an annoying <p>
element telling what they just did. 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. Is there a way to force the <p>
tag to have height and take up its space without the text in it? I have tried putting it in a div
and doing a clear fix on it even with no floats. I have tried ::before
and ::after
with 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在解开
&lt; br&gt;
标签上,在您的空&lt; p&gt;
上添加线折,同时也解开时,设置
innerhtml
(替换)。此
带有&lt; p&gt;
标记为textcontentinninhtml
)&lt; br&gt;
完整代码:
Add a linebreak to your empty
<p>
tag with<br>
Also while unflashing, set the
innerHTML
(replace.textContent
withinnerHTML
) of this<p>
tag to<br>
Full code: