html - 附加多个 div 并使它们响应

标签 html css responsive

大家好,

我正在使用 HTML 和 CSS 创建加载动画。由于我不太擅长响应式前端,所以我真的很难让文本和圆圈响应。

我真正想要的是将带有背景图像和文本的 div 附加到栏中并使它们响应,以便不移动并保持在同一位置。

这是我想要实现的: Loading

这是我目前拥有的代码。我尝试过修复附件之类的东西,但主要问题是当我使用最大高度/宽度时图像会不断缩放,并且文本会根据网站的宽度向右移动。

希望你能帮助我,谢谢指教。

body {
  background: #111 url("");
  background-size: 25vmin;
  background-repeat: no-repeat;
  background-position: center 40%;
  height: 100vh;
  margin: 0;
}

.logo {
  background: url("https://openclipart.org/download/256338/whitecircle.svg");
  background-size: 25vmin;
  background-repeat: no-repeat;
  position: absolute;
  left: 28%;
  bottom: 10vh;
  height: 25vh;
  width: 100px;
  max-width: 150px;
}

h1 {
  color: #fff;
  position: absolute;
  bottom: 20vh;
  left: 35%;
}

.progress {
  width: 400px;
  max-width: 85vw;
  height: 8px;
  position: absolute;
  bottom: 20vh;
  left: 50%;
  background: rgba(255, 255, 255, 0.5);
  transform: translate(-50%, -50%);
  overflow: hidden;
}

.progress:after {
  content: '';
  display: block;
  width: 100%;
  height: 8px;
  background: #fff;
  animation: load 5s linear;
}

@-moz-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-webkit-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-o-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
<div class="logo"> </div>
<h1 class="title"> Loading </h1>
<div class="progress"> </div>

最佳答案

为了使元素具有响应性并且许多部分需要紧密协作,我通常会做的是创建一个容器来容纳所有相关的元素。然后在容器内,我使用 % 对齐元素,以便它们可以很好地缩放。主容器(在我的示例中称为 loader)我使用 vh 和 vw 单位使用宽度和高度。

这是解决此问题的一种方法。我还将 SVG 替换为使用 css 制作的圆圈。这样你就不需要加载图像了。它将使您的页面资源更少。如果您特别想使用 SVG,请告诉我,我可以更新示例。

注意:我在 loader div 中添加了一个浅色边框,这样您就可以在调整窗口大小时看到它是如何调整大小的。将其复制到页面时将其删除。

body {
  height: 100vh;
  margin: 0;
  background-color: black;
  display: flex;
  justify-content: center;
  align-items: center;
}

.loader {
  position: relative;
  height: 30vh;
  width: 50vw;
  min-width: 200px;
  min-height: 100px;
  border: 1px solid #444; // added to see the responsiveness
}

.circle {
  width: 55px;
  height: 55px;
  bottom: calc(40% - 27.5px);
  left: -2px;
  position: absolute;
  border: 5px solid white;
  border-radius: 50%;
}

h1 {
  color: #fff;
  position: absolute;
  left: 75px;
  bottom: 32%;
}

.progress {
  position: absolute;
  width: calc(100% - 60px);
  height: 8px;
  bottom: 40%;
  left: 60px;
  background: rgba(255, 255, 255, 0.5);
  overflow: hidden;
}

.progress:after {
  content: '';
  display: block;
  width: 100%;
  height: 8px;
  background: #fff;
  animation: load 5s linear;
}

@-moz-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-webkit-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-o-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
<div class="loader">
  <!--<div class="logo"> </div>-->
  <!--<img class="img-logo" src="https://openclipart.org/download/256338/whitecircle.svg">-->
  <div class="circle"></div>
  <h1 class="title">Loading</h1>
  <div class="progress"></div>
</div>

关于html - 附加多个 div 并使它们响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51821105/

相关文章:

php - 将显示一行的 wpdb 查询结果转换为单个变量(如果重要,请使用短代码)

javascript - CSS字体大小以保持相同的字宽

html - 如何在 ionic 2 中制作高度响应屏幕?

javascript - 如何根据元素的高度更改元素的上边距?

javascript - 获取 nodeValue 时输出为 null

css - 如何在页面顶部制作如下图所示的固定水平菜单

javascript - jQuery 父级或父级获取表标签中的数据属性

css - 我的响应式设计不适用于平板电脑/手机

javascript - Foreach if 语句通过表单 javascript 循环

javascript - 将 HTML 元素分隔成具有适当间距的列(JQuery 和 CSS)