首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何垂直叠加居中的div

如何垂直叠加居中的div
EN

Stack Overflow用户
提问于 2019-02-20 09:36:47
回答 2查看 544关注 0票数 0

我似乎想不出一种方法来把我的div垂直地叠起来,同时仍然将它们对齐在页面的中间。

这是我当前的html:

代码语言:javascript
复制
.loading-page {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.loading-page .image-container {
  position: relative;
  display: inline-block;
}

.loading-page .image-container .image-grey {
  display: block;
  -webkit-filter: grayscale(100%);
  filter: grayscale(1);
}

.loading-page .image-container:after {
  position: absolute;
  content: "";
  top: 0;
  left: 0%;
  width: var(--image-load);
  height: 100%;
  background-position: top left;
}

.counter {
  text-align: center;
  position: relative;
  width: 200px;
}

.counter h1 {
  color: black;
  font-size: 60px;
  font-weight: bold;
  margin-top: -10px;
  margin-bottom: -10px;
}

.counter hr {
  background: #000000;
  border: none;
  height: 1px;
}
代码语言:javascript
复制
<div class="loading-page">

  <div class="image-container" style="--image-load: 50%;">
    <style>
      .image-container:after {
        background-image: url(http://i.imgur.com/bZt9ZwE.png);
      }
    </style>
    <img class="image-grey" src="http://i.imgur.com/bZt9ZwE.png" />
  </div>
  <div class="counter">
    <h1>50%</h1>
    <hr/>
  </div>

</div>

一个有用的例子:

JsFiddle

目标是"50%“文本低于图像,而两个div都是居中的,无论是垂直还是水平。图像大小将是动态的,因此,我不能只是设置所有固定的宽度/高度和玩的垫子。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-20 09:45:14

flex-direction:column;设置为.loading-page

代码语言:javascript
复制
body{
  margin:0px;
}

.loading-page {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.loading-page .image-container {
  position: relative;
  display: inline-block;
}

.loading-page .image-container .image-grey {
  display: block;
  -webkit-filter: grayscale(100%);
  filter: grayscale(1);
}

.loading-page .image-container:after {
  position: absolute;
  content: "";
  top: 0;
  left: 0%;
  width: var(--image-load);
  height: 100%;
  background-position: top left;
}

.counter {
  text-align: center;
  position: relative;
  width: 200px;
}

.counter h1 {
  color: black;
  font-size: 60px;
  font-weight: bold;
  margin-top: -10px;
  margin-bottom: -10px;
}

.counter hr {
  background: #000000;
  border: none;
  height: 1px;
}
代码语言:javascript
复制
<div class="loading-page">

  <div class="image-container" style="--image-load: 50%;">
    <style>
      .image-container:after {
        background-image: url(http://i.imgur.com/bZt9ZwE.png);
      }
    </style>
    <img class="image-grey" src="http://i.imgur.com/bZt9ZwE.png" />
  </div>
  <div class="counter">
    <h1>50%</h1>
    <hr/>
  </div>

</div>

票数 3
EN

Stack Overflow用户

发布于 2019-02-20 09:42:32

这就是你要的!提示:不要使用像素(Px),它们不能响应.相反,使用百分比作为衡量单位,以使其响应。

编辑:我更新了我的答案..。现在它有反应了。希望能帮上忙!

代码语言:javascript
复制
    .loading-page {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.loading-page .image-container {
  position: relative;
  display: inline-block;
}

.loading-page .image-container .image-grey {
  display: block;
  -webkit-filter: grayscale(100%);
  filter: grayscale(1);
}

.loading-page .image-container:after {
  position: absolute;
  content: "";
  top: 0;
  left: 0%;
  width: var(--image-load);
  height: 100%;
  background-position: top left;
}

.counter {
  text-align: center;
  position: static;
  width: 200px;
}

.counter h1 {
  color: black;
  font-size: 60px;
  font-weight: bold;
  margin-top: 5%;
  margin-bottom: -5%;
}

.counter hr {
  background: #000000;
  border: none;
  height: 1px;
}
代码语言:javascript
复制
<div class="loading-page">

<div class="image-container" 
		 style="--image-load: 50%;">
	<style>.image-container:after { background-image: url(http://i.imgur.com/bZt9ZwE.png);}</style>
	<img class="image-grey" src="http://i.imgur.com/bZt9ZwE.png"/>
</div>
<div class="counter">
  <h1>50%</h1>
<hr/>
</div>

</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54783060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档