Problems with Bootstrap's extra small breakpoint

"Greetings everyone, I am a newcomer to this field, currently tackling the third module assignment for Coursera's HTML, CSS, JS course. Facing an issue where applying styles for h1 under @media (max-width: 767px) does not display correctly at 767px but only up to a maximum of 766px."

What could be causing this problem and how can it be resolved?

Here are the git links:

Website

Git repository

Appreciate your assistance.

@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..",...
      </p>
    </div>
  
</div>

Answer №1

Here is a demonstration of how media queries function. Take a look at the example below and refer to the comments for clarification. Click on the "run code example" button and then select the "full page" link. Resize your browser window to see different styles applied at various widths.

Pay attention to the sequence in which the media queries are arranged. They need to be structured in a way that they override previous sizes 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*/
  color: #333;
  border: 1px solid green;
}

@media (max-width: 900px){
  /*
  These styles take precedence when the screen has a maximum width of 900px.
  In other words, when the screen is 899px wide or smaller
  */
  h1{
    color: blue;
    background-color: #CCC;
  }
}

@media (max-width: 767px){
  /*
  These styles take precedence when the screen has a maximum width of 767px.
  In other words, when the screen is 766px wide or smaller
  */
  h1{
    color: red;
  }
}
<div>Width: <span id="size-display"></span>px</div>

<h1>Test</h1>

While this method works, it is recommended to approach it differently. Use min-width in your media queries instead. Begin with default styles for the smallest browser size and gradually add more styles or override existing ones as the browser size increases. This approach is simpler to write and comprehend.

const display=document.querySelector('#size-display');
const resizeFn=()=>{display.innerHTML=window.innerWidth;};
window.addEventListener('resize',resizeFn);
resizeFn();
h1 {
  /*Default styles*/
  color: #333;
  border: 1px solid green;
}

@media (min-width: 767px){
  /*
  These styles take precedence when the screen has a minimum width of 767px.
  In other words, when the screen is 768px wide or larger
  */
  h1{
    color: red;
  }
}

@media (min-width: 900px){
  /*
  These styles take precedence when the screen has a minimum width of 900px.
  In other words, when 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>

Answer №2

My suggestion would be to opt for the updated syntax as it is much simpler to grasp.

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

In other words, we are specifying that when the width is equal to or less than 767 pixels, then...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

String Replacement in JavaScript

Here's the scenario: I have a string var str="abc test test abc"; Is there a way to specifically replace the second occurrence of "abc" in the string using the str.replace() method? ...

The navigation elements are misaligned when viewed on a mobile device

The layout of my navigation items is problematic on mobile view as they are taking up two lines. The search bar, hamburger menu, and logo are not aligned on a single line which is confusing for users. Here is an example of the issue in mobile view: https ...

Optimal approach for blending release and debug code in a web application

Currently, I am in the process of developing a simple browser-based game for two players. While my main focus lies on the backend, I need to monitor the states of both players simultaneously. To achieve this without the hassle of working with two separate ...

Tips on how to maintain the value of an integer variable even after clicking a submit button, enabling its reuse on the same page

Take a look at the first part of this code where we are using the variable $Tno. if(isset($_POST['submit'])) { $Tno=$_POST['Tno']; $query="SELECT * FROM docs WHERE Tno='$Tno'"; $result=mysql_query($query) or di ...

Embeddable javascript code can be enhanced by incorporating references into the header

I have developed a basic calculator application using HTML, JavaScript, JQuery, and CSS. My intention is to allow others to embed this calculator app on their own web pages by utilizing a file named generate.js. Within this file, there are several documen ...

How can I showcase Bootstrap cards horizontally within a razor foreach iteration?

I seem to be experiencing a mental block on how to arrange bootstrap cards in a horizontal layout using a for each loop. By default, they appear vertically. Below is my razor code: @foreach (var item in Model) { var imgUrl = Url.Content("~/Conte ...

What is the best location for storing css files in Laravel?

I've come to realize that I can easily make changes to the app.scss file in my Laravel projects and see those changes reflected by running npm run dev or npm run watch. However, I'm faced with a dilemma when dealing with multiple .css files. Whe ...

The Bootstrap tabs are not correctly displaying the content

Hey there, I'm currently working on a project that involves using bootstrap tabs (BS4) within a form. One of the requirements is to have forward and back buttons for form navigation, and I'd like to leverage BS4's tab('show') fun ...

Using the linearGradient feature within an SVG to create a transitional effect on the Fill property

Looking to implement a stepper functionality, everything is working smoothly except for the transition on the fill. let step = 0; document.querySelector("button").addEventListener('click', () => { step++; document.querySelector("svg ...

Lack of consideration for height within tables positioned absolutely

In the case of an absolutely positioned element with display: table, if the height is explicitly set and the content exceeds this height, the element will adjust to wrap around the content rather than maintaining the specified height value. #main { wi ...

How to use Iframe for "bookmarks" without causing the main browser to jump?

I currently have multiple iframes on my main page, each with a src attribute set to an empty value. I've used jQuery to create a function that when a button is clicked, the src for the corresponding iframe fills up with website.com/#title1. My issue ...

Combine lists with floating elements positioned to the left or right

My HTML includes the following ul tag: <ul style="position:absolute"> <li>list 1</li> <li>list 2</li> <li>list 3</li> <li> list 4 <p>list 4 content goes here</p> ...

I created an image that can be clicked on, but unfortunately it only functions properly on the

I am currently working on creating an image that can be clicked to cycle through different images all within the same frame. While I have managed to get it to work, I am facing a limitation where it only responds to one click. count = 1; function myF ...

Techniques for ensuring input validity using JavaScript

One of my form inputs is required: <input type="text" id="input-id" required> After the user submits it, I send the value using ajax and then clear it with $("#input-id").val(""). However, after this action, the input appears invalid. I want to ...

Rearrange the following <div> to appear before the previous one on medium and small devices using Bootstrap

I'm working with this div from the Bootstrap framework: <div class="card-header py-md-0 py-lg-0 py-xl-0 pl-0 pr-0"> <div class="d-flex justify-content-between align-items-center"> <div class="h5 mb-0">Text text</div&g ...

Bidirectional updates in AngularJS with CSS styling

On the backend, certain HTML elements store their position and size persistently and retrieve them when the page loads. These elements can be dragged and resized by users, with any updates needing to be saved on the backend for consistency across sessions. ...

Initializing an HTML5 video player directly from an Array

I am trying to populate a video player on my webpage with an array of videos. Here is the code I have so far: var current = 0; var videos = ["01", "02", "03", "04"]; function shuffle(array) { var currentIndex = array.length, temporaryValue, randomInde ...

Hamburger must be used in order for the menu to be displayed

Whenever I visit the site, the menu remains hidden. However, when I resize the page and use the hamburger icon, the menu appears as expected. Even after resizing the page back to its original size, the menu continues to be visible. Refreshing the sit ...

What is the difference in speed between drawing on a transparent canvas and determining if a point is transparent compared to determining if a point is within a complex polygon?

I'm curious if anyone has done research on adding event support to a game engine. Specifically, I am working on incorporating pixel-perfect hover over 2D object event support into my own game engine. I am exploring different ways of achieving this eff ...

In which specific location within the Bootstrap CSS files can the `footer` class be found?

Being new to coding, I've grasped HTML and CSS classes and am now delving into Bootstrap. Despite my efforts, I can't seem to locate the footer class in any of the Bootstrap CSS files like bootstrap.css. Similarly, I'm having trouble findi ...