【问题标题】:Bootstrap breakpoints not working in React Bootstrap引导断点在 React Bootstrap 中不起作用
【发布时间】:2020-05-05 15:41:11
【问题描述】:

我正在使用 react Bootstrap。我设计了一个布局,我想在小屏幕上进行重构。我想做的就是,在某些断点处,我希望我的 h3 元素和图像调整大小。我一直在使用以下内容:

index.css 中的代码

// Extra small devices (portrait phones, less than 576px)
@media (max-width: 575.98px) { 
    .thumbnailImg{
    width: 30%;
    }

    h3{
        font-size: 1rem;
    }
 }

// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) and (max-width: 767.98px) { 
    .thumbnailImg{
    width: 35%;
    }

    h3{
        font-size: 1rem;
    }

     }

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) and (max-width: 991.98px) {
    .thumbnailImg{
    width: 40%;
    }

    h3{
        font-size: 1.5rem;
    }

}

现在由于某些原因,没有一个断点起作用。 h3 始终保持 1.75 rem。不仅是 h3 元素,而且图像也没有按照定义调整大小。谁能告诉我我在这里做错了什么。

这是我一直使用这种样式的组件

import React from 'react';
import { Image } from 'react-bootstrap';
import './index.css';

export const MyIcons = ({source,name}) => (
        <div className="col-sm-4 addMargin50 text-center">
            <Image className="thumbnailImg" src={ process.env.PUBLIC_URL + source } alt={name} roundedCircle />
            <h3 className="font-nunito text-uppercase"> {name} </h3>
        </div>
    )

我还尝试创建一个单独的类来定义不同断点上的字体大小,并将该类添加到我的图像和 h3 元素中,但是它们都没有工作。

【问题讨论】:

  • 你能在这些屏幕尺寸上显示带有检查器的图片吗?

标签: css reactjs react-bootstrap


【解决方案1】:

问题不是反应,而是您的媒体查询的顺序。如果您有重叠的规则,它将始终选择被删除的最终规则。如果你颠倒你的媒体查询的顺序,你应该得到你想要的。

我已编辑此答案以使其更清晰。

您应该从最大的设备到最小的设备对媒体查询进行排序。

/* Medium devices (tablets, 768px and up) */
@media (min-width: 768px) and (max-width: 991.98px) {
    .thumbnailImg{
    width: 40%;
    }

    h3{
        font-size: 1.5rem;
    }

}

/* Small devices (landscape phones, 576px and up)*/
@media (min-width: 576px) and (max-width: 767.98px) { 
    .thumbnailImg{
    width: 35%;
    }

    h3{
        font-size: 1rem;
    }

     }



/* Extra small devices (portrait phones, less than 576px) */
@media (max-width: 575.98px) { 
    .thumbnailImg{
    width: 30%;
    }

    h3{
        font-size: 1rem;
    }
 }

https://jsfiddle.net/txL5chk0/2/

另外,我确信我不需要告诉您,您的 css 文件中的 cmets 不是 CSS 中的有效 cmets,不应包含在您的代码中......

【讨论】:

  • 试过没有成功。您能详细说明一下颠倒订单是什么意思吗?
  • 因此您可能会将您的 cmets 包含在 css 文件中(我在答案中提到过)。如果您查看 JS fiddle,则 image/h3 元素会调整大小。你能提供一个真正重现这个问题的链接吗?
  • 是的,那是因为我一直在使用不正确的 cmets(尽管我从 bootstrap 的官方网站复制了代码)。无论如何,非常感谢您的帮助!
猜你喜欢
  • 2018-09-14
  • 2016-03-25
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 2018-12-21
  • 2021-05-10
  • 2021-08-20
相关资源
最近更新 更多