html/css 中的图像定位问题

Image Positioning Issue in html/css

我有以下代码:

@import url('https://fonts.googleapis.com/css?family=Merriweather|Open+Sans');

.blogmaster {
  display: flex;
  margin: 60px;
  margin-top: 0;
  margin-bottom: 0;
}

.blogmaster div {
  width: 100%;
  max-width: 500px;
}

@media all and (max-width: 500px) {
  .blogmaster {
    flex-flow: wrap;
    margin: 20px;
    margin-top: 0;
    margin-bottom: 0;
  }
}

.container1 {
 display: flex;
 padding: 20px;
  justify-content: center;
    overflow: hidden;
}

.square:hover {
  -webkit-transform: translate(20px, -10px);
  -ms-transform: translate(10px, -10px);
  transform: translate(10px, -10px);
  -webkit-box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
}

.square {
  position: relative;
  height: 100% !important;
  padding:3px;
  background: white;
  border-radius: 4px;
  box-shadow: 0px 5px 20px #D9DBDF;
  -webkit-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

.mask {
  clip: rect(0px, 450px, 220px, 0px);
  border: 5px solid #555;
  border-radius: 4px;
  position: absolute;
  width:100%;
  height: 100%;
  top:0;
  left:0;
  z-index:1;
}
@media screen and (max-width: 480px) {
.mask{
  width: 100% !important;
}
}

.h11 {
  margin: auto;
  text-align: left;
  margin-top: 240px;
  padding-left: 30px;
  padding-right: 30px;
  font-family: 'Merriweather', serif;
  font-size: 24px;
}

p9 {
  text-align: justify;
  font-family: 'Open Sans', sans-serif;
  font-size: 12px;
  color: #C8C8C8;
  line-height: 18px;
  padding-left: 30px;
  padding-right: 30px;
  display: block;
}

.button56 {
  background-color: #3EDD84;
  color: white;
  width: 100px;
  padding: 10px 18px;
  border-radius: 3px;
  text-align: center;
  text-decoration: none;
  display: block;
  margin: 15px 70px 13px 30px;
  font-size: 12px;
  cursor: pointer;
  font-family: 'merriweather';
}

.parent-div {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
}

@media screen and (max-width: 480px) {
  .parent-div {
    flex-direction: column;
  }
}
<section>
  <div class="section-title">
    <h2>Featured Blogs Of The Day</h2>
  </div>
  <div class="parent-div">
    <div class="child-div">
      <div class="blogmaster">
        <div class="column1">
          <div class="row1">
            <div class="container1">
              <div class="square">
                <img src="https://images.unsplash.com/photo-1504610926078-a1611febcad3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=e1c8fe0c9197d66232511525bfd1cc82&auto=format&fit=crop&w=1100&q=80" class="mask" style="border: 5px solid #555">
                <div class="h11">“Chances Of My Uni/College Admission?”</div>
                <p9>It is that time of the year again (yay!) where we — high school students — are supposed to fill out the applications and land in our dream Universities/Colleges!</p9>

                <div><a href="https://m-hussainomer03.medium.com/chances-of-my-uni-college-admission-20bc0efec0af" target="_" class="button56">Read More</a></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- new start of blog -->
    <div class="parent-div">
      <div class="child-div">
        <div class="blogmaster">
          <div class="column1">
            <div class="row1">
              <div class="container1">
                <div class="square">
                  <img src="https://www.isfasports.gr/image/cache/data/products/pr_3-1100x1100.jpg" class="mask" style="border: 5px solid #555">
                  <div class="h11">My Career Advice To You: Take These Steps...</div>
                  <p9>Humans tend to make mistakes — and its completely normal as it results in the growth and development of an individual — either psychologically or physically.</p9>

                  <div><a href="https://m-hussainomer03.medium.com/my-career-advice-to-you-take-these-steps-to-ultimate-prosperity-6c0687ce9c9f" target="_" class="button56">Read More</a></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>





</section>
<!-- End blogs Section -->

所以我将上面的代码嵌入到我的网站中,所以当我在 较小的屏幕上查看我的网站时,我的输出如下所示:

图像的宽度不适合整张卡片。我所说的“适合”是指 只是宽度而不是高度 。就像,换句话说,我只想拉伸图像的宽度。就这些。图片高度没问题

预期产出

在大屏幕上:

这部分已经为我工作,但我只是向您展示以防万一。提供建议时,请不要修改适用于大屏幕的这部分代码。我上面发送的代码的输出正是我在大屏幕上寻找的

在较小的屏幕上:

因此,在较小的屏幕上,我只希望图像能稍微拉伸到卡片的末尾。这就对了。高度和其他一切都很好。关于如何完成此操作并使图像的宽度延伸到卡片,有什么建议吗?

已编辑

这是我现在在小屏幕上获得的输出。它仍然不适合该卡

在 css 中,您应该删除 .mark 的高度或将其更改为自动

.mask {
     ...
     height: auto;
}
  • 您好,我已经解决了您的问题。
  • 尝试理解盒子模型CSS。它将帮助您在 HTML/CSS.
  • 中创建任何类型的布局
  • MDN Box model

@import url('https://fonts.googleapis.com/css?family=Merriweather|Open+Sans');

.blogmaster {
  display: flex;
  margin: 60px;
  margin-top: 0;
  margin-bottom: 0;
}

.container1 {
 display: flex;
  flex-wrap: wrap;
 padding: 20px;
  justify-content: center;
    overflow: hidden;
}

.square {
  margin: 20px;
  position: relative;
  width: 100%;
  max-width: 500px;
  height: 100% !important;
  background: white;
  border-radius: 4px;
  box-shadow: 0px 5px 20px #D9DBDF;
  -webkit-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

.square:hover {
  -webkit-transform: translate(20px, -10px);
  -ms-transform: translate(10px, -10px);
  transform: translate(10px, -10px);
  -webkit-box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
}

.square .square-image img{
 width: 100%;
  height: 250px;
  object-fit: cover;
  border-top-left-radius: 4px;
 border-top-right-radius: 4px;
}
.square .square-details{
  padding: 20px 30px 30px;
}

.h11 {
  margin: auto;
  text-align: left;
  font-family: 'Merriweather', serif;
  font-size: 24px;
}

p {
  text-align: justify;
  font-family: 'Open Sans', sans-serif;
  font-size: 12px;
  color: #C8C8C8;
  line-height: 18px;
  display: block;
}

.button56 {
  background-color: #3EDD84;
  color: white;
  width: 100px;
  padding: 10px 18px;
  border-radius: 3px;
  text-align: center;
  text-decoration: none;
  display: block;
  font-size: 12px;
  cursor: pointer;
  font-family: 'merriweather';
}

.parent-div {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
}

@media screen and (max-width: 480px) {
  .parent-div {
    flex-direction: column;
  }
}
<section>
<section>
  <div class="section-title">
    <h2>Featured Blogs Of The Day</h2>
  </div>

  <div class="row1">
            <div class="container1">
              <div class="square">
                <div class="square-image">
                                  <img src="https://images.unsplash.com/photo-1504610926078-a1611febcad3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=e1c8fe0c9197d66232511525bfd1cc82&auto=format&fit=crop&w=1100&q=80">
                </div>

                <div class="square-details">
                  <h3 class="h11">“Chances Of My Uni/College Admission?”</h3>
                <p>It is that time of the year again (yay!) where we — high school students — are supposed to fill out the applications and land in our dream Universities/Colleges!</p>
                  <div><a href="https://m-hussainomer03.medium.com/chances-of-my-uni-college-admission-20bc0efec0af" target="_" class="button56">Read More</a></div>
                </div>
               
              </div>
              <div class="square">
                <div class="square-image">
                                  <img src="https://www.isfasports.gr/image/cache/data/products/pr_3-1100x1100.jpg">
                </div>

                <div class="square-details">
                  <h3 class="h11">My Career Advice To You: Take These Steps...</h3>
                <p>Humans tend to make mistakes — and its completely normal as it results in the growth and development of an individual — either psychologically or physically.</p>
                  <div><a href="https://m-hussainomer03.medium.com/my-career-advice-to-you-take-these-steps-to-ultimate-prosperity-6c0687ce9c9f" target="_" class="button56">Read More</a></div>
                </div>
               
              </div>
            </div>
          </div>


</section>
<!-- End blogs Section -->

可以通过relative显示,width设置100%height设置为px中的绝对高度来实现。

我的所有更改:

.mask {
  ...
  position: relative;
  min-width:100%;
  width:100%;
  height: 218px;
  top:-5px;
  left:-5px;
  ...
}

@import url('https://fonts.googleapis.com/css?family=Merriweather|Open+Sans');

.blogmaster {
  display: flex;
  margin: 60px;
  margin-top: 0;
  margin-bottom: 0;
}

.blogmaster div {
  width: 100%;
  max-width: 500px;
}

@media all and (max-width: 500px) {
  .blogmaster {
    flex-flow: wrap;
    margin: 20px;
    margin-top: 0;
    margin-bottom: 0;
  }
}

.container1 {
 display: flex;
 padding: 20px;
  justify-content: center;
    overflow: hidden;
}

.square:hover {
  -webkit-transform: translate(20px, -10px);
  -ms-transform: translate(10px, -10px);
  transform: translate(10px, -10px);
  -webkit-box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
}

.square {
  position: relative;
  height: 100% !important;
  padding:3px;
  background: white;
  border-radius: 4px;
  box-shadow: 0px 5px 20px #D9DBDF;
  -webkit-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

.mask {
  clip: rect(0px, 450px, 220px, 0px);
  border: 5px solid #555;
  border-radius: 5px;
  position: relative;
  min-width:100%;
  width:100%;
  height: 218px;
  top:-5px;
  left:-5px;
  z-index:1;
}
@media screen and (max-width: 480px) {
.mask{
  width: 100% !important;
}
}

.h11 {
  margin: auto;
  text-align: left;
  margin-top: 240px;
  padding-left: 30px;
  padding-right: 30px;
  font-family: 'Merriweather', serif;
  font-size: 24px;
}

p9 {
  text-align: justify;
  font-family: 'Open Sans', sans-serif;
  font-size: 12px;
  color: #C8C8C8;
  line-height: 18px;
  padding-left: 30px;
  padding-right: 30px;
  display: block;
}

.button56 {
  background-color: #3EDD84;
  color: white;
  width: 100px;
  padding: 10px 18px;
  border-radius: 3px;
  text-align: center;
  text-decoration: none;
  display: block;
  margin: 15px 70px 13px 30px;
  font-size: 12px;
  cursor: pointer;
  font-family: 'merriweather';
}

.parent-div {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
}

@media screen and (max-width: 480px) {
  .parent-div {
    flex-direction: column;
  }
}
<section>
  <div class="section-title">
    <h2>Featured Blogs Of The Day</h2>
  </div>
  <div class="parent-div">
    <div class="child-div">
      <div class="blogmaster">
        <div class="column1">
          <div class="row1">
            <div class="container1">
              <div class="square">
                <img src="https://images.unsplash.com/photo-1504610926078-a1611febcad3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=e1c8fe0c9197d66232511525bfd1cc82&auto=format&fit=crop&w=1100&q=80" class="mask" style="border: 5px solid #555">
                <div class="h11">“Chances Of My Uni/College Admission?”</div>
                <p9>It is that time of the year again (yay!) where we — high school students — are supposed to fill out the applications and land in our dream Universities/Colleges!</p9>

                <div><a href="https://m-hussainomer03.medium.com/chances-of-my-uni-college-admission-20bc0efec0af" target="_" class="button56">Read More</a></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- new start of blog -->
    <div class="parent-div">
      <div class="child-div">
        <div class="blogmaster">
          <div class="column1">
            <div class="row1">
              <div class="container1">
                <div class="square">
                  <img src="https://www.isfasports.gr/image/cache/data/products/pr_3-1100x1100.jpg" class="mask" style="border: 5px solid #555">
                  <div class="h11">My Career Advice To You: Take These Steps...</div>
                  <p9>Humans tend to make mistakes — and its completely normal as it results in the growth and development of an individual — either psychologically or physically.</p9>

                  <div><a href="https://m-hussainomer03.medium.com/my-career-advice-to-you-take-these-steps-to-ultimate-prosperity-6c0687ce9c9f" target="_" class="button56">Read More</a></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>



</section>
<!-- End blogs Section -->