2

In the CSS code my class is ok but for some reason when I go to the Chrome dev tools I can see that media gets "triggered" on 600px but it doesn't override my CSS. Anyone know why?

.main__text{
    color:red;
    font-size: 2rem;
    margin: 1rem 0 2rem 0;
}

@media (min-width: 600px) {
    p{
        font-size: 1rem;
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
0sergej
  • 31
  • 3
  • 3
    It’s probably a specificity problem - the selector for `.main__text` is much more specific than simply `p`. – esqew Dec 12 '21 at 18:09

4 Answers4

0

Try adding the !important flag to your CSS.

@media (min-width: 600px) {
    p{
        font-size: 1rem !important;
    }
}
Jerfov2
  • 5,264
  • 5
  • 30
  • 52
Taha Malik
  • 21
  • 6
  • Hello, Welcome to StackOverflow! Can you please explain your code and why it is suitable for the given question? – Jerfov2 Dec 13 '21 at 03:37
0

Class selectors take priority over tag selectors (re: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity, https://www.w3schools.com/css/css_specificity.asp).

Add a class to the p you'd like to change and target that.

.main__text {
  color: red;
  font-size: 2rem;
  margin: 1rem 0 2rem 0;
}

@media (min-width: 600px) {
  .main__text__alt {
    font-size: 1rem;
    color: blue;
  }
}
<p class="main__text">Some Text</p>
<p class="main__text main__text__alt">Some Text that will change color and size at 600px</p>
Ali Klein
  • 1,811
  • 13
  • 13
0

I'm not sure about your html but change the 'p' selector to '.main__text'.

try this:

.main__text {
  color: red;
  font-size: 2rem;
  margin: 1rem 0 2rem 0;
}

@media only screen and (min-width: 600px) {
  .main__text {
    font-size: 1rem;
  }
}

An easier way to understand media queries would be trying this:

.box {
  display: block;
  background: red;
  height: 100px;
  width: 100px;
}

@media only screen and (min-width: 600px) {
  .box {
    width: 200px;
  }
}
<section>
  <div class="box"></div>
</section>

Also keep in mind that 'min-width' and 'max-width' act oppositely... 'min-width' applies those styles when the screen size becomes larger than the specified unit while 'max-width' applies the style when the screen size smaller than the specified unit.

-1

For the @media to work you need to specify something like screen like this:

@media only screen and (min-width: 600px) {
 p {
  font-size: 1rem;
 }
}
YT_Xaos
  • 335
  • 4
  • 19
  • No. Specifying the default media, `screen`, or even `only screen` has been common for backward compatibility, but modern browsers don't need it. See: https://stackoverflow.com/questions/8549529/what-is-the-difference-between-screen-and-only-screen-in-media-queries – Ingo Steinke Dec 12 '21 at 18:29