css - 让 UL 移动友好

标签 css wordpress

我正在尝试将相关帖子添加到我的博客 https://reviewyourgear.com

我可以让它在台式机/平板电脑上以我想要的方式显示...但移动设备看起来很糟糕。

我试图在小屏幕上每行一行,在大屏幕上每行 2 行。

知道我做错了什么吗?提前致谢!!!

我正在为我的 UL 使用以下 CSS:

.relatedposts {width: 100%; margin: 5px 1px 1px 1px; float: left; font-size: 15px; background-color: #000000;}
.relatedposts h3 {color :#ffffff; font-size: 20px; margin: 5px 5px 5px 5px; }
.relatedthumb {margin: 0 1px 0 1px; float: left;}
.relatedthumb img {margin: 0 0 3px 0; padding: 0;}
.relatedthumb a {color :#ffffff; text-decoration: none; display:block; padding: 1px; width: 150px;}
.relatedthumb a:hover {background-color: #000000; color: #ada771;}
.relatedposts ul {
    margin-top: 10px;
    list-style-type:none;
    display:flex;
    justify-content: center;    
}

@media (max-width: 960px) and (min-width: 501px) {
    .relatedposts li { width: 50%; } /* Show 2 logos per row on medium devices (tablets, phones in landscape) */
}
@media (max-width: 500px) {
    .relatedposts li { width: 100%; } /* On small screens, show one logo per row */
}

这是我的相关帖子代码(在 Wordpress 的 single.php 中):

<div align="center">        
                <div class="relatedposts">
<?php $orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 3, // Number of related posts that will be shown.
'caller_get_posts'=>1
);

$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h3>Related Reviews and Articles</h3><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post();?>

<div class="relatedthumb">
<li style="list-style: none;"><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div class="relatedcontent">
<a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</div>
      </div>
</li>
<?
}
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
                </div></div>

最佳答案

在您的 css 中,您应该将 flex-wrap 添加到列表中。

.relatedposts ul {
    flex-wrap: wrap;
}

W3schools flex 包装 tutorialdemo .

更多信息,前面问题中的不同方法:

  1. > number of items per row using media queries in flexbox
  2. > Flexbox - Cater for 2,3 or 1 items per row

关于css - 让 UL 移动友好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56129171/

相关文章:

jquery - 您建议在 WordPress 网站上的哪个位置加载 jQuery 库?

html - 在 iframe 中嵌入 wordpress 管理

css - 为维基百科制作侧边栏

css - 隐藏 Flexbox 溢出

css - 使 SVG 图标的元素在起始位置的中心旋转

javascript - 调用 onchange 函数时,动态下拉菜单获取错误的值

html - 是否可以使用带有单个 DIV 的普通 HTML/CSS 创建两列布局?

html - 如何停止自动换行并按照我希望的方式集中线路?

mysql - Docker Wordpress 连接到本地主机上的数据库服务器

css - Wordpress 水平滚动错误