与UL和Flex CSS混淆,2行,2列,共4项

时间:2018-10-24 05:00:41

标签: html css wordpress flexbox related-posts

我正在尝试将WordPress帖子的样式设置为2行2行。

我创建了科学怪人!

如何使它像这张图片一样。

enter image description here

Click me for Dev Website

PHP / HTML

SELECT code,total, ... as name FROM inventory; 


         === inventory ===
+------------+-----------+----------+
|    code    |   total   |  name    |
+------------+-----------+----------+
| el_pr_25   |     45    | test1    |
| el_pr_11   |     33    | test2    |
| mob_tp_x93 |     23    | test 66  |
| mob_tp_t55 |     33    | test 22  |
| el_pr_x73  |     25    | test3    |
| mob_tp_25  |     22    | test 323 |
+------------+-----------+----------+

CSS

    <ul class="svcta_listing_related_posts_ul">
    <li class="svcta_listing_related_posts_li">
        <?php foreach( $posts as $post): ?>
        <?php setup_postdata($post); ?>
        <?php the_post_thumbnail('listing_post_thumbnail'); ?> 
            <div class="svcta_listing_related_posts_link"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    </li>
        <?php endforeach; ?>
</ul>

2 个答案:

答案 0 :(得分:0)

您可以使用CSS网格进行此操作。检查下面的代码段

.svcta_listing_related_posts_ul {
  list-style: none;
  margin: 0px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  display: grid;
  grid-template-columns: auto auto;
}

.svcta_listing_related_posts_li {
  margin-right: 75px;
}
<ul class="svcta_listing_related_posts_ul">
  <li class="svcta_listing_related_posts_li">
    <?php foreach( $posts as $post): ?>
    <?php setup_postdata($post); ?>
    <?php the_post_thumbnail('listing_post_thumbnail'); ?>
    <div class="svcta_listing_related_posts_link">
      <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>test
      </a>
    </div>
  </li>
 <li class="svcta_listing_related_posts_li">
    <?php foreach( $posts as $post): ?>
    <?php setup_postdata($post); ?>
    <?php the_post_thumbnail('listing_post_thumbnail'); ?>
    <div class="svcta_listing_related_posts_link">
      <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>test
      </a>
    </div>
  </li>
 <li class="svcta_listing_related_posts_li">
    <?php foreach( $posts as $post): ?>
    <?php setup_postdata($post); ?>
    <?php the_post_thumbnail('listing_post_thumbnail'); ?>
    <div class="svcta_listing_related_posts_link">
      <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>test
      </a>
    </div>
  </li>
 <li class="svcta_listing_related_posts_li">
    <?php foreach( $posts as $post): ?>
    <?php setup_postdata($post); ?>
    <?php the_post_thumbnail('listing_post_thumbnail'); ?>
    <div class="svcta_listing_related_posts_link">
      <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>test
      </a>
    </div>
  </li>

  <?php endforeach; ?>
</ul>

下面突出显示的部分应该用li换行 enter image description here

答案 1 :(得分:0)

<ul class="svcta_listing_related_posts_ul">
  <?php foreach( $posts as $post): ?>
    <li class="svcta_listing_related_posts_li">
      <?php
        setup_postdata($post);
        the_post_thumbnail('listing_post_thumbnail');
      ?> 
      <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
      </a>
    </li>
  <?php endforeach; ?>
</ul>

.svcta_listing_related_posts_ul {
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  margin: -75px 0 0 0;
}
.svcta_listing_related_posts_li {
  flex-basis: calc(50% - 37.5px);
  padding: 75px 0 0 0;
}

enter image description here

我会避免将div放到li

编辑

如果现在要在同一行上显示4张图像,将会有些不同,这是完整的代码

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <meta charset="utf-8">
  <style>
    .svcta_listing_related_posts_ul,
    .svcta_listing_related_posts_ul * {
      box-sizing: border-box;
    }
    .svcta_listing_related_posts_ul {
      list-style-type: none;
      padding: 10px;
      display: flex;
      flex-wrap: wrap;
      align-items: flex-start;
      justify-content: space-between;
      margin: -10px 0 0 10px;
    }
    .svcta_listing_related_posts_li {
      flex-basis: 25%;
      max-width: 250px;
      padding: 10px 0 0 10px;
    }

    .svcta_listing_related_posts_li img {
      max-width: 100%;
    }

    .svcta_listing_related_posts_li a,
    .svcta_listing_related_posts_li img {
      display: block;
    }

    @media screen and (max-width: 1024px) {
      .svcta_listing_related_posts_li {
        flex-basis: 50%;
        max-width: none;
      }
    }

    @media screen and (max-width: 768px) {
      .svcta_listing_related_posts_li {
        flex-basis: 100%;
        text-align: center;
      }
      .svcta_listing_related_posts_li img {
        margin: auto;
      }
    }
  </style>
</head>
<body>
  <ul class="svcta_listing_related_posts_ul">
    <li class="svcta_listing_related_posts_li">
      <img src="http://svcta.lainternet.biz/wp-content/uploads/2016/09/hotel_room_wedding_romance-200x200.jpg" class="attachment-listing_post_thumbnail size-listing_post_thumbnail wp-post-image" alt="Hotel Room Wedding Romance" width="200" height="200"> 
      <a href="http://svcta.lainternet.biz/3-rehearsal-dinner-ideas-in-simi-valley/">
        3 Rehearsal Dinner Ideas in Simi Valley      </a>
    </li>
    <li class="svcta_listing_related_posts_li">
      <img src="http://svcta.lainternet.biz/wp-content/uploads/2016/09/hotel_room_wedding_romance-200x200.jpg" class="attachment-listing_post_thumbnail size-listing_post_thumbnail wp-post-image" alt="Hotel Room Wedding Romance" width="200" height="200"> 
      <a href="http://svcta.lainternet.biz/3-things-you-need-to-know-before-booking-your-room/">
        3 Things you Need to Know Before Booking Your Room      </a>
    </li>
    <li class="svcta_listing_related_posts_li">
      <img src="http://svcta.lainternet.biz/wp-content/uploads/2018/10/Wood_Ranch_Feature_500x360-200x200.jpg" class="attachment-listing_post_thumbnail size-listing_post_thumbnail wp-post-image" alt="Wood Ranch Feature 500x360" width="200" height="200"> 
      <a href="http://svcta.lainternet.biz/stay-in-simi-valley-spend-the-day-outdoors/">
      Stay In Simi Valley &amp; Spend The Day Outdoors      </a>
    </li>
    <li class="svcta_listing_related_posts_li">
      <img src="http://svcta.lainternet.biz/wp-content/uploads/2018/10/Gallery_Bestwestern_Wedding_500x400-200x200.jpg" class="attachment-listing_post_thumbnail size-listing_post_thumbnail wp-post-image" alt="Gallery Bestwestern Wedding 500x400" width="200" height="200"> 
      <a href="http://svcta.lainternet.biz/stay-in-simi-wedding-guide/">
        Stay in Simi Wedding Guide      </a>
    </li>
</ul>
</body>
</html>

将其保存到.html文件中以查看其工作方式。我没有将其放在摘要中,因为您必须通过调整大小来查看结果。