html - 当元素可见时容器失去上边距

标签 html css

发生这种情况时,我正在尝试参加 CSS 挑战赛。一切似乎都按预期工作,但是当单击加号圆圈以显示 div#card 时,div.container 失去了它的上边距,我不明白为什么。如果有人可以帮助我,我将不胜感激 - 也许我们都可以从中学习 ;) 谢谢!

代码笔:https://codepen.io/albertrf147/pen/LMKKeK

HTML

html,
body {
  width: 100%;
  height: 100%;
}

body {
  margin: 0px;
  overflow: hidden;
  background: lightblue;
}

.container {
  width: 400px;
  height: 400px;
  margin: 20px auto !important;
  position: relative;
  display: flex;
  flex-wrap: wrap;
  background: white;
  padding: 2px;
}

.square {
  box-sizing: border-box;
  padding: 2px;
  width: 50%;
  height: 50%;
  position: relative;
  background: white;
}

.square>img {
  height: 100%;
  width: 100%;
  object-fit: cover;
  display: block;
  margin: auto;
}

.onhover {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  position: absolute;
  cursor: pointer;
  transition: all .6s ease-in-out;
}

.onhover:hover {
  background: rgba(0, 0, 0, 0.5);
}

.circle-aux {
  position: relative;
  width: 100%;
  height: 100%;
  transition: all .6s ease-in-out;
}

.circle-aux:hover .circle {
  visibility: visible;
  opacity: 1;
  transform: scale(0.2);
  transition: all .6s ease-in-out;
}

.circle {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  border-radius: 50%;
  width: 100%;
  height: 100%;
  background: salmon;
}

.circle:before {
  content: "";
  background: white;
  height: 50%;
  width: 6px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.circle:after {
  content: "";
  background: white;
  height: 6px;
  width: 50%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.container-card {
  visibility: hidden;
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}

.container-card>img {
  width: 100%;
  height: 60%;
  object-fit: cover;
}

.container-card:target {
  visibility: visible;
}

.avatar {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  box-shadow: 0px 0px 20px black;
  width: 25%;
  height: 25%;
  background: white;
  z-index: 2;
  text-align: center;
  font-family: calibri;
  font-weight: bold;
  color: white;
  font-size: 18px;
}

.avatar>img {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
  padding: 4px;
}

.mini-circle {
  display: inline-block;
  width: 15%;
  height: 15%;
  border: 1px solid white;
  border-radius: 50%;
  margin-top: 12px;
}

.mini-circle:hover {
  background: white;
  cursor: pointer;
}

footer {
  box-sizing: border-box;
  position: absolute;
  bottom: 0;
  left: 0;
  background: salmon;
  height: 50%;
  width: 100%;
  z-index: 1;
  padding: 2px;
}

.close {
  position: absolute;
  border-radius: 50%;
  width: 8%;
  height: 8%;
  background: black;
  transform: rotate(45deg);
  right: 10px;
  top: 10px;
  cursor: pointer;
}

.close:before {
  content: "";
  background: white;
  height: 60%;
  width: 2px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.close:after {
  content: "";
  background: white;
  height: 2px;
  width: 60%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<main>
  <div class="container">

    <div class="square">
      <div class="onhover">
        <div class="circle-aux">
          <a class="circle" href="#card"></a>
        </div>
      </div>
      <img src="https://images.pexels.com/photos/305241/pexels-photo-305241.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
    </div>
    <div class="square">
      <div class="onhover">
        <div class="circle-aux">
          <div class="circle"></div>
        </div>
      </div>
      <img src="https://images.pexels.com/photos/1546711/pexels-photo-1546711.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
    </div>
    <div class="square">
      <div class="onhover">
        <div class="circle-aux">
          <div class="circle"></div>
        </div>
      </div>
      <img src="https://images.pexels.com/photos/1800433/pexels-photo-1800433.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
    </div>
    <div class="square">
      <div class="onhover">
        <div class="circle-aux">
          <div class="circle"></div>
        </div>
      </div>
      <img src="https://images.pexels.com/photos/1757111/pexels-photo-1757111.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
    </div>

    <div id="card" class="container-card">
      <a class="close" href="#"></a>
      <img src="https://images.pexels.com/photos/1769331/pexels-photo-1769331.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
      <div class="avatar">
        <img src="https://images.pexels.com/photos/769772/pexels-photo-769772.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
        <span>David Craig</span>
        <span>
        <div class="mini-circle"></div>
        <div class="mini-circle"></div>
        <div class="mini-circle"></div>
      </span>
      </div>
      <footer></footer>
    </div>
  </div>
</main>

最佳答案

欢迎来到 SO!

由于出现的容器是position: absolute no margin 生效。

您需要使用 top 属性才能正确放置它。

检查一下:https://codepen.io/anon/pen/pGzVyL

有关更多信息,请单击此处:https://www.w3schools.com/css/css_positioning.asp

关于html - 当元素可见时容器失去上边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289393/

相关文章:

html - 如何在嵌入 HTML 的插件中更改字体大小?

html - 将表格高度设置为一个像素的问题

css - anchor 标记内的多个 float / block /div

javascript - 从 iframe 内部检测 &lt;iframe&gt; 高度和宽度

CSS Sprite - 每次调用类时是否加载图像

jquery - jquery slider 的CSS定位问题

html - 在html中绘制特定的线条

jquery - Bootstrap轮播箭头定位

jquery - 带有可折叠页脚的 Bootstrap 布局

html - 随后在 CSS 中选择子元素