css - 将元素在设备尺寸上居中对齐 =< 小

标签 css flexbox tailwind-css

我有一个卡片组件,它在小型移动设备上方的屏幕尺寸上呈现如下第一张图片所示的效果,我将该组件设置为 flex-wrap。当 flex-wrap 处于事件状态时,我的图像会被推到卡片的左侧,即使它的容器设置为 w-full 并且我尝试使用 justify-center 居中>。仅当设备较小且较小时,我才尝试将图像居中。我尝试设置 sm: justify-center 但不起作用。有人对如何重构以使此功能正常工作有想法吗?谢谢

import React from "react"

export default function FeatureCard(props) {
  return (
    <div className="flex flex-row flex-wrap w-2/3 h-auto bg-gray-200 p-2 rounded-lg">
      <div className="flex xl:w-1/3 lg:w-1/3 md:w-1/3 sm:w-full xs:w-full">
        <img src={props.image} />
      </div>
      <div className="flex xl:w-2/3 lg:w-2/3 md:w-2/3 sm:w-full xs:w-full text-center self-center justify-center font-bold">
        <ul>
          {props.features.map(feature => (
            <li>{feature}</li>
          ))}
        </ul>
      </div>
    </div>
  )
}
      <div className="flex flex-row flex-wrap w-full h-auto justify-center -py-2">
        <div className="flex xl:w-1/2 lg:w-1/2 md:w-full sm:w-full xs:w-full h-auto justify-center py-2">
          <FeatureCard
            features={[
              "Modern Website Design",
              "Progressive Web Applications",
              "Content Management Systems",
              "JAMstack",
            ]}
            image={"https://img.icons8.com/color/96/000000/monitor.png"}
          />
        </div>
</div>

Desktop Display

Mobile display

最佳答案

所以,如果我理解正确的话,您希望 props.image 在小屏幕上居中吗?

如果您将类似的内容添加到 <div className="flex flex-row flex-wrap w-full h-auto justify-center -py-2"> 会怎样?分区:

@media (max-width: 600px) {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    }

它的基本作用是,当屏幕小于 600px 时,将 Flex 方向更改为列而不是行,这在 tailwind-css 中可能翻译为:

sm:flex-col sm:justify-center sm:items-center

关于css - 将元素在设备尺寸上居中对齐 =< 小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62913611/

相关文章:

javascript - 向 d3.js 图表添加水平十字准线

css - Chrome 用户代理样式表表格边框颜色未被覆盖

css - 你如何防止图像扩展到它的容器之外?

javascript - Next JS 如何导入自定义字体?

hover - 边框样式不适用于顺风悬停

javascript - 如何将左幕菜单的幻灯片转换为右幕菜单的幻灯片

html - 带列表的 Div 背景不起作用

html - 如何将 'X' 按钮移动到右侧末尾(与文本左侧内联)?

html - A4 论文样 CSS : taking citations from the main column and placing them into the other

css - 如何在 Web 组件 shadow-dom 中设置根字体大小?