css 文本框后面出现重叠文本框内容的英雄图像,如何在不使用媒体查询的情况下修复?

cl25kdpy  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(106)

我有一个英雄形象,有一个文本框重叠的英雄一点点,但当我添加任何内容下面的框,它出现在文本框。一些媒体查询添加到我的代码下面,它的工作,但我觉得必须有一种方法来包含英雄和重叠的文本框到自己的一节。如果必须添加更多的文字,我'我必须通过和调整所有的媒体查询,使它看起来很好,所以我怎么能做到这一点,而不使用媒体查询?
下面是我的代码:

.shell {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

/* overlap */

.shell:before {
  content: "";
  background-image: url(https://i.ibb.co/x866XdV/test-hero.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: bottom;
  position: relative;
  height: 300px;
  width: 100%;
}

.shell-header {
  color: #fff;
  padding: 0px 20px;
}

.shell-body {
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.25);
  background-color: #fff;
  max-width: 85%;
  position: absolute;
  top: 80%;
}
img {
  width: 20%;
  height: 20%;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

body {
  margin: 0;
  font-family: sans-serif;
}

.wrapper {
  padding-top: 12rem;
}

@media only screen and (min-width: 800px) {
  .wrapper {
    padding-top: 8rem;
  }
}

@media only screen and (max-width: 360px) {
  .wrapper {
    padding-top: 11rem;
  }
}
<div class="shell">
  <div class="shell-header"></div>
  <div class="shell-body">
    <h1>Title</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat vel ducimus illo consectetur commodi ex nulla aut amet ipsum maiores itaque, iusto quam mollitia facilis consequatur tempora neque quod eligendi?</p>
  </div>
</div>
<div class="wrapper">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3f/Placeholder_view_vector.svg" alt="">
</div>
x8diyxa7

x8diyxa71#

使用padding是理想的,但是你可以随意调整元素的位置,例如,你可以将body设置为position: relative;,然后将.wrapper嵌套在.shell中,并将.wrapper设置为position: absolute;

.shell {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* overlap */

.shell:before {
  content: "";
  background-image: url(https://i.ibb.co/x866XdV/test-hero.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: bottom;
  position: relative;
  height: 300px;
  width: 100%;
}

.shell-header {
  color: #fff;
  padding: 0px 20px;
}

.shell-body {
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.25);
  background-color: #fff;
  max-width: 85%;
  position: absolute;
  top: 80%;
}

img {
  width: 20%;
  height: 20%;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

body {
  margin: 0;
  font-family: sans-serif;
  position: relative;
}

.wrapper {
  position: absolute;
  bottom: -80%;
}
<div class="shell">
  <div class="shell-header"></div>
  <div class="shell-body">
    <h1>Title</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat vel ducimus illo consectetur commodi ex nulla aut amet ipsum maiores itaque, iusto quam mollitia facilis consequatur tempora neque quod eligendi?</p>
  </div>
  <div class="wrapper">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/3f/Placeholder_view_vector.svg" alt="">
  </div>
</div>

使用负margin的示例。
一个二个一个一个

相关问题