在这个布局上,我们看到有一个带有新闻的区块,并且在这个区块中有新闻卡片。但是,如果您仔细观察,您会发现这些卡片的排列打破了行,并且第二行的卡片“浮动”到了第一行(第三行的卡片浮动到了第二行,等等)。
到目前为止,我只能这样做:
* {
box-sizing: border-box;
}
a {
color: inherit;
text-decoration: inherit;
}
body {
margin: 0;
}
.newsfeed-news-cards {
display: flex;
flex-flow: row wrap-reverse;
justify-content: space-between;
padding: 0 8em;
align-items: baseline;
}
.news-card__title.index-swiper-slide-info__title {
font-size: 1.2em;
font-weight: 500;
margin: 12px 0;
}
.news-card__tag.index-swiper-slide-info__tag {
font-family: 'Raleway', 'Montserrat', sans-serif;
font-weight: 300;
font-size: 1.1em;
/* line-height: 1.5em; */
margin: 6px 0;
}
.news-card-footer {
font-family: 'Montserrat', 'Raleway', sans-serif;
color: #BDBDBD;
font-size: 1em;
}
.news-card-footer-views {
display: flex;
align-items: center;
}
img.news-card-footer-views__icon {
margin-right: .5em;
}
.news-card {
max-width: 400px;
}
.news-card__image {
width: 100%;
}
.news-card-footer {
display: flex;
justify-content: space-between;
width: 50%;
}
.news-card-footer-views {
color: #BDBDBD;
}
.news-card-footer-views__icon {
width: 16px;
}
.newsfeed-news__title {
margin-bottom: 2em;
}
.newsfeed-more-load {
display: flex;
margin-top: 3em;
justify-content: center;
}
.newsfeed-more-load__btn {
display: flex;
align-items: center;
}
.newsfeed-more-load__btn {
font-family: "Raleway", sans-serif;
font-weight: 500;
color: #CB9374;
}
.newsfeed-more-load__btn:hover path,
.newsfeed-more-load__btn:hover {
background-color: #CB9374;
color: #fff;
stroke: #fff;
}
.newsfeed-more-load__arrow {
margin-left: 10px;
transform: rotate(-90deg);
}
.index-swiper-slide-info__tag {
text-transform: uppercase;
color: #CB9374;
}
.index-swiper-slide-info__title {
font-family: 'Lora', sans-serif;
color: #333;
font-weight: 700;
font-size: 1.5em;
margin: 1em 0;
}
<!doctype html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Lora:wght@400;500&family=Montserrat:wght@400;500;600&family=Raleway:wght@400;500;600&display=swap" rel="stylesheet">
<title>so</title>
</head>
<body>
<div class="newsfeed-news">
<div class="newsfeed-news-cards">
<div class="news-card">
<img src="https://i.ibb.co/6X228Zj/news-image.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/RPp1jVG/news-image1.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/ssMP4zb/news-image2.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/hfHNNmF/news-image3.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/ssMP4zb/news-image2.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/RPp1jVG/news-image1.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/HPgrBG7/news-image4.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/ssMP4zb/news-image2.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
<div class="news-card">
<img src="https://i.ibb.co/RPp1jVG/news-image1.png" alt="" class="news-card__image" />
<div class="news-card__tag index-swiper-slide-info__tag">коронавирус</div>
<div class="news-card__title index-swiper-slide-info__title">Эксперт назвал отличия между первой и второй волнами COVID-19</div>
<div class="news-card-footer">
<div class="news-card-footer__post-date">2 часа назад</div>
<div class="news-card-footer-views">
<img src="https://i.ibb.co/yYj3Z4j/eye.png" alt="" class="news-card-footer-views__icon">
<span class="news-card-footer-views__count">178</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
怎么弄成这样?或者如何使第二行的块浮动到第一行?
使用 CSS Flexbox 布局系统的变体。
最简单的选项是使用
column-count:
指定列数的属性。在下面的示例中,我们将图片排列成三列column-count: 3;
可以使用 CSS Grid 构建相同的网格。但是,在我看来,将Masonry库用于这些目的是最有效的。
这种“技术”被称为 - 砌体(类似于砌体)。
您可以使用 JS 库: https ://masonry.desandro.com/