【问题标题】:Image gets push down from the grid, I have tried to put margin-top: -20px but doesn't work图像从网格中向下推,我试图将 margin-top: -20px 但不起作用
【发布时间】:2023-02-22 09:36:00
【问题描述】:

图像从网格中向下推/滑动,我尝试将 margin-top: -20px、position: relative 放在 css 中,但它没有改变。我也尝试为添加text-overflow: ellipsis;

.popularArea > div > p 下的标记,但它仍然在网格中显示 const myData = Test 文本。有人可以建议吗? JS小提琴: 注意:我无法在反应降价的此呈现的 html 输出上添加 html 标签。

https://jsfiddle.net/1w5cex6r/17/

body {
  background-color: rgb(232, 236, 250);
}

.trendingArea {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
  grid-gap: 25px !important;
  margin: 10px 20px 10px 20px !important;
  border-color: #E2E8F0;
}

.popularArea {
  display: flex;
  height: 230px;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  background-color: #fff !important;
  border-radius: 15px 15px 15px 15px !important;
}

.dataDate.tags.readmoreLink.views {
  columns: 2 auto;
}

.popularArea p img {
  height: 200px;
  max-width: 100%;
  display: block;
  position: relative;
  margin-top: -2% !important;
  height: 220px;
  border-radius: 15px 0px 0px 15px;
}

.popularArea div p {
  width: 100%;
  color: rgb(92, 90, 90);
  text-decoration: none;
  text-align: left;
  margin-left: 10px;
  font-weight: lighter;
  font-size: 14px;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.popularArea div h3 {
  width: 450px;
  color: rgb(6, 6, 6) !important;
  text-align: left;
  margin-left: 10px;
  font-size: 14px;
}

.popularArea div h5,
h6 {
  width: 450px;
  color: rgb(6, 6, 6) !important;
  margin: 25px 0px 0px 210px;
}
<div class="row">
  <div class="trendingArea">
    <a href="https://google.com">
      <div class="popularArea">
        <div class="dataDate renderElipsis tags readmoreLink views">
          <h5>22 Feb 2023</h5>
          <h6>cypress</h6>
          <p>
            <img src="https://picsum.photos/200" alt="">
          </p>
          <h3>Main Header text here</h3>
          <p>Best heading added here.The most relevant data added here. Greatest of all time. Print the whole text here. Ideas are always usefull....</p>
          <p>
            <code>const myData = Test</code>
          </p>
        </div>
      </div>
    </a>
  </div>
</div>

【问题讨论】:

  • 请在问题本身中包含所有相关代码,最好是minimal reproducible example。不要让我们离开现场去帮助你
  • 我在问题中添加了 JS Fiddle 以供参考。将代码再次添加到问题块中。
  • @JonP 我已经添加了,请看!
  • 你想在左边显示图像,在右边显示文本吗?
  • 是的,左边是图片,右边是文字

标签: css


【解决方案1】:

在您的 HTML 中,图像上方和下方都有文本元素。如果您希望图像和文本按照您希望的方式流动,您最好重构 HTML,让事情尽可能自然地流动,而不是违背自然流动。

body {
  background-color: rgb(232, 236, 250);
}

.trendingArea {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
  grid-gap: 25px;
  margin: 10px 20px 10px 20px;
  border-color: #E2E8F0;
}

.popularArea {
  display: flex;
  height: 230px;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  background-color: #fff;
  border-radius: 15px 15px 15px 15px;
}

.dataDate.tags.readmoreLink.views {
  columns: 2 auto;
}

/*Note the p selector has been removed*/
.popularArea img {
  height: 200px;
  max-width: 100%;
  display: block;
  height: 220px;
  border-radius: 15px 0px 0px 15px;
}

.popularArea div p {
  width: 100%;
  color: rgb(92, 90, 90);
  text-decoration: none;
  text-align: left;
  margin-left: 10px;
  font-weight: lighter;
  font-size: 14px;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.popularArea div h3 {
  width: 450px;
  color: rgb(6, 6, 6) !important;
  text-align: left;
  margin-left: 10px;
  font-size: 14px;
}

.popularArea div h5,
h6 {
  width: 450px;
  color: rgb(6, 6, 6);
  margin: 25px 0px 0px 210px;
}
<div class="row">
  <div class="trendingArea">
    <a href="https://google.com">
      <div class="popularArea">
        <div class="dataDate renderElipsis tags readmoreLink views">
          <img src="https://picsum.photos/200" alt="">
          <h5>22 Feb 2023</h5>
          <h6>cypress</h6>
          <h3>Main Header text here</h3>
          <p>Best heading added here.The most relevant data added here. Greatest of all time. Print the whole text here. Ideas are always usefull....</p>
          <p>
            <code>const myData = Test</code>
          </p>
        </div>
      </div>
    </a>
  </div>
</div>

附带说明一下,您确实应该避免使用!important。

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    相关资源
    最近更新 更多