我有A< p>空的元素,当我在执行过程中填充文本时,如何给它高度而不将元素推下元素?

发布于 2025-02-13 19:36:29 字数 2037 浏览 2 评论 0原文

当用户使用该应用程序做事时,我想拥有一个烦人的< 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风追烟花雨 2025-02-20 19:36:29

在解开&lt; br&gt;标签上,在您的空&lt; p&gt;上添加线折

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

,同时也解开时,设置innerhtml(替换)。此&lt; p&gt;标记为textcontent带有inninhtml&lt; br&gt;

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

完整代码:

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>

Add a linebreak to your empty <p> tag with <br>

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

Also while unflashing, set the innerHTML (replace .textContent with innerHTML) of this <p> tag to <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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文