1

I am connecting an external stylesheet to my React component, and I have a media query and I intend the logo to have a height of 100vh when the screen width is less than 300px. However this media query is ignored and only the original styles apply. I tried adding the css directly in the HTML file using the style tag. Here is the relevant code:

Logo.js

import React from "react";
import "./Logo.css";

export default class Logo extends React.Component {
  render() {
    return (
      <img 
        alt=""
        className="Logo"
        src="../logo.png"
      />
    );
  };  
};

Logo.css

.Logo {
  position: absolute;
  left: 42vw;
  bottom: calc(50vh + 4vw);
  height: 16vw;
};

@media screen and (max-width: 300px) {
  .Logo {
    height: 100vh;
  };
};
6
  • How did you test it? Commented Oct 31, 2020 at 14:35
  • @AdamAzad Inspect element and then used toggle device toolbar. Simulated using samsung galaxy fold. Commented Oct 31, 2020 at 14:38
  • @KotetsuChan galaxy fold's screen width might be larger than 300px. Did you try with a different value? Commented Oct 31, 2020 at 14:51
  • @ozgur On chrome it said the simulated width is 280px, which is less than 300. Commented Oct 31, 2020 at 14:59
  • @KotetsuChan check this please stackoverflow.com/questions/54491645/… Commented Oct 31, 2020 at 15:03

2 Answers 2

2

Using hooks in react:

import React from "react";
import { useMediaQuery } from "react-responsive";

export default function Logo() {
  const isDesktop = useMediaQuery({
    query: '(min-aspect-ratio: 1/1)'
  });
  let logo = {};

  if (isDesktop) {
    logo = {
      position: "absolute",
      left: "42vw",
      bottom: "calc(50vh + 4vw)",
      height: "16vw"
    };
  } else {
    logo = {
      position: "absolute",
      left: "38vw",
      bottom: "calc(50vh + 6vw)",
      height: "24vw",
    };
  };

  return (
    <img 
      alt=""
      style={logo}
      src="../logo.png"
    />
  );
};

Remember to download useMediaQuery by typing npm install react-responsive --save into the command line.

Sign up to request clarification or add additional context in comments.

1 Comment

is this the correct way to do it? why the style sheet doen't work? please if you can answer those question to me
0

Please try adding this to the head section of your code.

<meta name="viewport" content="width=device-width,initial-scale=1">

1 Comment

Is your browser zoom-ed at different than 100% level ? If so, can you zoom to 100% (Ctrl+MouseWheel) and try again?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.