在另一个 div 容器的中间添加文本

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

Add text in the middle of another div container

问题

我有两个div容器。一个是文本,另一个是加载器。
现在两个div容器垂直排列在一起。但我想把文本放在中间,覆盖在“loader”容器上方。这样“loading”文本就会出现在预加载器的中间。

我应该怎么做?

<!-- 开始片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-css -->

#loader {
  margin: auto;
  border: 10px solid #f3f3f3;
  border-radius: 50%;
  border-top: 10px solid #003750;
  width: 100px;
  height: 100px;
  -webkit-animation: spin 2s linear infinite;
  /* Safari */
  animation: spin 2s linear infinite;
}


/* Safari */

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

#Text {
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
}

<!-- 语言: lang-html -->

<div id="loader"></div>
<div id="Text">

  <p>
    <b>加载中</b>
  </p>

</div>

<!-- 结束片段 -->

[1]: https://i.stack.imgur.com/fH6AY.png
英文:

I have two div containers. One is for Text and another is for loader.
Right now both div containers are placed vertically one after another. But I would like to place the text in the center, that is overlying on top of "loader" container. So that the "loading" text comes in the middle of my preloader.

How do I do this?

在另一个 div 容器的中间添加文本

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#loader {
  margin: auto;
  border: 10px solid #f3f3f3;
  border-radius: 50%;
  border-top: 10px solid #003750;
  width: 100px;
  height: 100px;
  -webkit-animation: spin 2s linear infinite;
  /* Safari */
  animation: spin 2s linear infinite;
}


/* Safari */

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

#Text {
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
}

<!-- language: lang-html -->

&lt;div id=&quot;loader&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;Text&quot;&gt;

  &lt;p&gt;
    &lt;b&gt;Loading&lt;/b&gt;
  &lt;/p&gt;

&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

只需使用绝对定位或将两者包裹在一个容器内并使用flex。

英文:

Just position absolute or wrap both in a container and flex that

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#loader {
  margin: auto;
  border: 10px solid #f3f3f3;
  border-radius: 50%;
  border-top: 10px solid #003750;
  width: 100px;
  height: 100px;
  -webkit-animation: spin 2s linear infinite;
  /* Safari */
  animation: spin 2s linear infinite;
}


/* Safari */

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

#Text {
  position: absolute;
  top:40px;
  margin-left: 280px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
}

<!-- language: lang-html -->

&lt;div id=&quot;loader&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;Text&quot;&gt;

  &lt;p&gt;
    &lt;b&gt;Loading&lt;/b&gt;
  &lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 03:12:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923000.html
匿名

发表评论

匿名网友

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

确定