skip to Main Content

"Hey everyone, I am new to the field and currently working on the 3rd module assignment of Coursera’s HTML, CSS, JS course. The issue is that when I type style for h1 under @media (max-width: 767px), the results are not displayed at 767px, but only for screen sizes with maximum value of 766px."

Why is this happening and what can I do to fix this?

Here are the git links:

Website

Git repository

Thanks for your time.

@media (max-width: 767px){
h1{
    color: red;
}}  

<div class="main-content container">
<div class="row">

    <h1 class="text-center">Our menu</h1>
    <div class="col-xs-12">
      <h2 class="text-center">Chicken</h2>
      <p>
        Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
      </p>
    </div>
  
</div>

2

Answers


  1. This is how the media queries work. See my below example and the comments to explain it. Press the "run code example" button and then the "full page" link. Now resize your browser window to see the different styles apply at different widths.

    Please note the order in which the media queries are written too. You need them to override the previous size as the browser width changes.

    const display=document.querySelector('#size-display');
    const resizeFn=()=>{display.innerHTML = window.innerWidth;};
    window.addEventListener('resize',resizeFn);
    resizeFn();
    h1 {
      /*Default styles to apply*/
      color: #333;
      border: 1px solid green;
    }
    
    @media (max-width: 900px){
      /*
      These styles override any other styles when: the screen has a maximum width of 900px.
      Another way of saying that is: the screen is 899px wide or smaller
      */
      h1{
        color: blue;
        background-color: #CCC;
      }
    }
    
    @media (max-width: 767px){
      /*
      These styles override any other styles when: the screen has a maximum width of 767px.
      Another way of saying that is: the screen is 766px wide or smaller
      */
      h1{
        color: red;
      }
    }
    <div>Width: <span id="size-display"></span>px</div>
    
    <h1>Test</h1>

    So this works… however it is a recommended practice to do this in the opposite way from how you are doing it. Using min-width in your media queries instead, your default styles should be for the smallest possible browser size, and then as the browser size increases you add on more styles or override previous ones. It’s much easier to write and understand this way too.

    const display=document.querySelector('#size-display');
    const resizeFn=()=>{display.innerHTML=window.innerWidth;};
    window.addEventListener('resize',resizeFn);
    resizeFn();
    h1 {
      /*Default styles to apply*/
      color: #333;
      border: 1px solid green;
    }
    
    @media (min-width: 767px){
      /*
      These styles override any other styles when: the screen has a minimum width of 767px.
      Another way of saying that is: the screen is 768px wide or LARGER
      */
      h1{
        color: red;
      }
    }
    
    @media (min-width: 900px){
      /*
      These styles override any other styles when: the screen has a minimum width of 900px.
      Another way of saying that is: the screen is 901px wide or LARGER
      */
      h1{
        color: blue;
        background-color: #CCC;
      }
    }
    <div>Width: <span id="size-display"></span>px</div>
    
    <h1>Test</h1>
    Login or Signup to reply.
  2. Welcome to StackOverflow

    I would recommend using the new syntax, which is way easier to understand.

    @media (width <= 767px) {
      color: red;
    }
    

    So we write "If the width is less than, or equal to, 767, then …"

    Hope this helps.

    Good luck on your assignment.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search