CSS位置相对于div的前50%不起作用

问题描述 投票:-3回答:1

我有定位问题:

我无法在照片和文字上准确设置top: 50%left: 50%,因为它不是50%。我尝试手动设置50%,更像是46%。

当我改变窗口文本的大小移动时。我不知道该做什么,我正在寻找2个小时的答案

.photo-box {
  background: linear-gradient(250deg, rgba(251, 10, 237, 0.84), rgba(0, 186, 255, 0.91) 50%, rgba(15, 226, 98, 1));
  width: 100vw;
  text-align: center;
  font-size: 30px;
  padding-top: 70px;
  padding-bottom: 70px;
  margin-left: auto;
  margin-right: auto;
  color: #f0eeee;
  font-family: sans-serif;
  position: relative;
}

.tytul {
  display: flex;
  text-align: center;
  position: absolute;
  top: 46%;
  left: 45.3%;
}

.zdj-pierw img {
  border-radius: 100px;
  align-items: center;
  justify-content: center;
}
<div class="photo-box" style="">

  <div class="tytul">Strona WWW </div>
  <div class="zdj-pierw">
    <img src="https://picsum.photos/200/200/?random" width="200" height="200" />

  </div>


</div>
html css css-position positioning
1个回答
0
投票

您应该在父级上使用flex属性,而不仅仅是在要放置的项目上。

我也把你的border-radius变成了50%,因为这是一个完美的圆形,无论图像大小如何。

.photo-box {
  background: linear-gradient(250deg, rgba(251, 10, 237, 0.84), rgba(0, 186, 255, 0.91) 50%, rgba(15, 226, 98, 1));
  width: 100vw;
  font-size: 30px;
  padding-top: 70px;
  padding-bottom: 70px;
  color: #f0eeee;
  font-family: sans-serif;
  position: relative;
  
  display: flex;
  justify-content: center;
  align-items: center;
}

.tytul {
  position: absolute;
}

.zdj-pierw img {
  border-radius: 50%;
}
<div class="photo-box" style="">
  <div class="tytul">Strona WWW </div>
  <div class="zdj-pierw">
    <img src="https://picsum.photos/200/200/?random" width="200" height="200" />
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.