.heading text-align: right; /* truyền thông media queries */
media (max-width: 400px) text-align: left; I tried the following but it throws a syntax error & fails to compile.

Bạn đang xem: Hướng dẫn dùng lệnh @media trong css

heading: { textAlign: "right",


Introducing: Trending sort You can now choose khổng lồ sort by Trending, which boosts votes that have happened recently, helping lớn surface more up-to-date answers.

Trending is based off of the highest score sort & falls back to lớn it if no posts are trending.

Try it Dismiss
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
You can make truyền thông queries inside React:

import React, Component from "react";class phầm mềm extends Component constructor(props) super(props) this.state = matches: window.matchMedia("(min-width: 768px)").matches ; componentDidMount() const handler = e => this.setState(matches: e.matches); window.matchMedia("(min-width: 768px)").addEventListener("change", handler); render() return (
); export mặc định App;https://stackblitz.com/edit/react-cu8xqj?file=src/App.js


If you have a special cases, when you need lớn get truyền thông media query result inside you react phầm mềm (for example, you want khổng lồ show some component at mobile version), you can use helpers lượt thích react-responsive or react-media-hook.


You cannot set media queries inline. You will need to lớn create a separate CSS stylesheet & then import the stylesheet.

Xem thêm: Soạn Sinh Lớp 8 Bài 56 : Tuyến Yên, Tuyến Giáp, Tải Soạn Sinh 8 Bài 56: Tuyến Yên, Tuyến Giáp

So the following code would go in a new styles.css file for example.

.heading text-align: right; /* truyền thông media queries */
media (max-width: 400px) text-align: left; Then you can import your new CSS styles tệp tin into your react file. For example, you could showroom import "./styles.css" to the top of your App.jsx file (assuming they are both at the root level), or you could import it directly into a specific react component file.



The answer given by Ferit turned out to be quite useful, however, it was only the example in class components, but it is usually difficult or cumbersome khổng lồ apply that lớn functional components, và since sometimes it is problematic lớn transform a functional component lớn a class one, here I leave the example using Hooks

import React, useState, useEffect from "react";const app = () => { const = useState( window.matchMedia("(min-width: 768px)").matches ) useEffect(() => window .matchMedia("(min-width: 768px)") .addEventListener("change", e => setMatches( e.matches )); , <>); return (
You can use styled-components if you"re used lớn using css. It would look something lượt thích this.

Xem thêm: Cách Xóa Các Bài Đăng Trên Facebook Chi Tiết Nhất

import React from "react";import Styled from "styled-components";function YourComponent() const heading = Styled.h1` Text-align:right;
media (max-width: 400px) text-align: left; `; return( This is my heading )If you have a lot of styling you need to bởi vì you can bởi vì your styles in another js file và import each style as needed. If you vì chưng this don"t forget to export your style.

in my case i was using a custom hook to lớn generate some breakpoint values for me:

import useMediaQuery from "react-responsive";export const useBreakpoints = () => const isMobileSmall = useMediaQuery( query: "(max-width: 325px)" ); const isMobileMid = useMediaQuery( query: "(max-width: 375px)" ); const isMobileFloor = useMediaQuery( query: "(max-width: 425px)" ); const isTabletFloor = useMediaQuery( query: "(max-width: 426px)" ); const isTabletMid = useMediaQuery( query: "(max-width: 768px)" ); const isTabletCeil = useMediaQuery( query: "(max-width: 1024px)" ); const isLaptopFloor = useMediaQuery( query: "(max-width: 1025px)" ); const isLaptopCeil = useMediaQuery( query: "(max-width: 1440px)" ); const isXHDFloor = useMediaQuery( query: "(max-width: 1441px)" ); const isXHDCeil = useMediaQuery( query: "(max-width: 4096px)" ); return isMobileSmall, isMobileMid, isMobileFloor, isTabletFloor, isTabletMid, isTabletCeil, isLaptopFloor, isLaptopCeil, isXHDFloor, isXHDCeil, ;;and i was calling it in my component inside of useMemo, which is incorrect :)