css 如何动态改变html容器的大小?

p8h8hvxi  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(121)

我有一个容器div,它是一个随窗口动态改变大小的背景。在那个容器中,我有一个图像div,我想随背景容器改变大小。我似乎不能让它动态地调整大小,即使使用高度和宽度百分比。有人能指导我正确的方向吗?

@import url("https://fonts.googleapis.com/css?family=Fira+Sans:400,500,600,700,800");
* {
  box-sizing: border-box;
}

body {
  background-color: #af39fe;
  background-image: linear-gradient(147deg, #af39fe 0%, #4c38fd 74%);
  min-height: 100vh;
  font-family: "Fira Sans", sans-serif;
  display: flex;
}

.info-container {
  width: 95%;
  position: relative;
  max-width: 800px;
  margin: auto;
  background: #fff;
  box-shadow: 0px 14px 80px rgba(34, 35, 58, 0.2);
  padding: 25px;
  border-radius: 10px;
  height: auto;
}

.info-container__img {
  width: 400px;
  flex-shrink: 0;
  height: 400px;
  background-image: linear-gradient(147deg, #af39fe 0%, #4c38fd 74%);
  box-shadow: 4px 13px 30px 1px rgba(252, 56, 56, 0.2);
  border-radius: 10px;
  transform: translateX(-80px);
  overflow: hidden;
}

.info-container__img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 100;
  border-radius: 10px;
}

.info-container__content {
  padding-right: 25px;
}

.info-container__content>* {
  opacity: 100;
}

.info-container__title {
  font-size: 24px;
  font-weight: 700;
  color: #0d0925;
  margin-bottom: 20px;
}

.info-container__text {
  color: #4e4a67;
  margin-bottom: 30px;
  line-height: 1.5em;
}

.info-container__button {
  display: inline-flex;
  background-image: linear-gradient(147deg, #5b0a6b 0%, #16034b 74%);
  padding: 15px 35px;
  border-radius: 50px;
  color: #fff;
  box-shadow: 0px 14px 80px rgba(0, 0, 0, 0.4);
  text-decoration: none;
  font-weight: 500;
  justify-content: center;
  text-align: center;
  letter-spacing: 1px;
}
<div class="info-container">
  <div class="info-container__img">
    <img src="https://www.dmarge.com/wp-content/uploads/2021/01/dwayne-the-rock-.jpg">
  </div>
  <div class="info-container__content">
    <div class="info-container__title">About Me</div>
    <div class="info-container__text">This is fake info about a persons life.</div>
    <a href="About.html" class="info-container__button">READ MORE</a>
  </div>
</div>
zaqlnxep

zaqlnxep1#

你用像素设置.info-container__img的宽度。把它改成百分比,它就会按照你想要的那样工作。我设置了max-width: 400px,并根据容器的样式修改了width: 50%

@import url("https://fonts.googleapis.com/css?family=Fira+Sans:400,500,600,700,800");
* {
  box-sizing: border-box;
}

body {
  background-color: #af39fe;
  background-image: linear-gradient(147deg, #af39fe 0%, #4c38fd 74%);
  min-height: 100vh;
  font-family: "Fira Sans", sans-serif;
  display: flex;
}

.info-container {
  width: 95%;
  position: relative;
  max-width: 800px;
  margin: auto;
  background: #fff;
  box-shadow: 0px 14px 80px rgba(34, 35, 58, 0.2);
  padding: 25px;
  border-radius: 10px;
  height: auto;
}

.info-container__img {
  width: 50%;
  max-width: 400px;
  flex-shrink: 0;
  height: auto;
  background-image: linear-gradient(147deg, #af39fe 0%, #4c38fd 74%);
  box-shadow: 4px 13px 30px 1px rgba(252, 56, 56, 0.2);
  border-radius: 10px;
  transform: translateX(-80px);
  overflow: hidden;
}

.info-container__img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 100;
  border-radius: 10px;
}

.info-container__content {
  padding-right: 25px;
}

.info-container__content>* {
  opacity: 100;
}

.info-container__title {
  font-size: 24px;
  font-weight: 700;
  color: #0d0925;
  margin-bottom: 20px;
}

.info-container__text {
  color: #4e4a67;
  margin-bottom: 30px;
  line-height: 1.5em;
}

.info-container__button {
  display: inline-flex;
  background-image: linear-gradient(147deg, #5b0a6b 0%, #16034b 74%);
  padding: 15px 35px;
  border-radius: 50px;
  color: #fff;
  box-shadow: 0px 14px 80px rgba(0, 0, 0, 0.4);
  text-decoration: none;
  font-weight: 500;
  justify-content: center;
  text-align: center;
  letter-spacing: 1px;
}
<div class="info-container">
  <div class="info-container__img">
    <img src="https://www.dmarge.com/wp-content/uploads/2021/01/dwayne-the-rock-.jpg">
  </div>
  <div class="info-container__content">
    <div class="info-container__title">About Me</div>
    <div class="info-container__text">This is fake info about a persons life.</div>
    <a href="About.html" class="info-container__button">READ MORE</a>
  </div>
</div>

相关问题