Why aren't my media queries making my app responsive?

Currently, I am in the process of developing a simple weather application. In order to ensure that the app is compatible with mobile devices, I have included media queries to make it responsive. However, despite my efforts, the app does not resize properly on smaller screens.

Here is the HTML code:

<body>
    <div id="weather_wrapper">
    <div class="weatherCard">

      <div class="currentTemp">
        <p class = "help">Click on the temp to change units</p> 
        <p class="location"></p>
            <p class="temp"></p>
        </div>


      <div class="currentWeather">
        <span class = "weather-description"></span>
        <span class="conditions"></span>
            <div class="info">
                <span class="rain"></span>
                <span class="wind"></span>
            </div>
        </div>

    </div>
  </div>




    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script>
    <script src="app.js"></script>
  </body>

CSS:

@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,700);
@import url(https://cdnjs.cloudflare.com/ajax/libs/weather-icons/1.2/css/weather-icons.min.css);


@media screen and (max-width: 350px) {
    body{
        width: 300px;
        height: 150px
    }
    #weather_wrapper{
        width: 300px;
    }
    .weatherCard{
        width: 300px;
    }

}

body{
background: linear-gradient(90deg, #7474BF 10%, #348AC7 90%);
font-family: 'Open Sans';
}

#weather_wrapper{
    width: 500px;
    margin-left: 450px;
  margin-top: 150px;
}
.weatherCard{
    width: 400px;
    height: 200px;
    font-family: 'Open Sans';
    position: relative;
}
.currentTemp{
    width: 220px;
    height: 200px;
    background: rgb(41, 41, 41);
  color: rgb(255, 255, 255);
  position: absolute;
}
.temp:hover{
    text-decoration: underline;
    cursor: pointer;
}
.currentWeather{
    width: 180px;
    height: 200px;
    background: rgb(237, 237, 237);
    margin: 0;
  position: absolute;
    top: 0;
    right: 0;
}
.help{
    font-size: 15px;
    text-align: center;
    top: 15px;
    position: absolute;
    color: rgb(255, 255, 255);
}
.info{
  background: rgb(42, 178, 234);
  position: absolute;
  bottom: 0;
  right: 0;
  width: 180px;
  height: 50px;
}
.temp{
    width: 100px;
    height: 50px;
    margin-top: 110px;
    margin-left: 80px;
    position: absolute;
    color: rgb(255, 255, 255);
}
.location{
    width: 120px;
    height: 30px;
    margin-top: 80px;
    margin-left: 80px;
    position: absolute;
    color: rgb(237, 237, 237)
}
.conditions{
    width: 100px;
    height: 30px;
    margin-top: 40px;
    margin-left: 60px;
    position: absolute;
}
.weather-description{
    margin-left: 59px;
    margin-top: 30px;
    margin-bottom: 50px;
    position: absolute;
}

.wind {
    width: 100%;
    margin-left: 30px;
    position: relative;
    font-size: 14px;
  }

  .wind::after {
    display: block;
    content: '\f050';
    font-family: weathericons;
    font-size: 25px;
    margin-left: 75px;
    position: absolute;
    bottom: -26px;
  }

JS Fiddle: https://jsfiddle.net/hpsfj653/

I have tried setting a maximum-width property to target smaller screens, but unfortunately, only the body width changes. Adjusting the margins for `#weather_Wrapper` and `.WeatherCard` did not yield the desired results either. Is there something critical that I might be overlooking?

Answer №1

For optimal styling, make sure to place your media queries below the rest of your CSS rules. This ensures that the media queries take precedence over the regular rules in all sizes, preventing any potential conflicts.

Answer №2

It appears that the left margin was not removed properly for responsiveness, and it is important to maintain CSS specificity. For a detailed explanation on CSS specificity,

Check out w3schools CSS specificity tutorials

You seem to have used media queries that do not adhere to specificity.

Alternatively, you can consider using !important which should resolve the issue.

Please refer to the code snippet below.

@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,700);
@import url(https://cdnjs.cloudflare.com/ajax/libs/weather-icons/1.2/css/weather-icons.min.css);

body{
background: linear-gradient(90deg, #7474BF 10%, #348AC7 90%);
font-family: 'Open Sans';
}

@media screen and (max-width: 350px) {

body{
width: 300px!important;
height: 150px!important;
}
#weather_wrapper{
width: 100%!important;
    margin-left: 0px!important;
}
.weatherCard{
width: 100%!important;
}

}


#weather_wrapper{
width: 500px;
margin-left: 450px;
  margin-top: 150px;
}
.weatherCard{
width: 400px;
height: 200px;
font-family: 'Open Sans';
position: relative;
}
.currentTemp{
width: 220px;
height: 200px;
background: rgb(41, 41, 41);
  color: rgb(255, 255, 255);
  position: absolute;
}
.temp:hover{
text-decoration: underline;
cursor: pointer;
}
.currentWeather{
width: 180px;
height: 200px;
background: rgb(237, 237, 237);
margin: 0;
  position: absolute;
top: 0;
right: 0;
}
.help{
font-size: 15px;
text-align: center;
top: 15px;
position: absolute;
color: rgb(255, 255, 255);
}
.info{
  background: rgb(42, 178, 234);
  position: absolute;
  bottom: 0;
  right: 0;
  width: 180px;
  height: 50px;
}
.temp{
width: 100px;
height: 50px;
margin-top: 110px;
margin-left: 80px;
position: absolute;
color: rgb(255, 255, 255);
}
.location{
width: 120px;
height: 30px;
margin-top: 80px;
margin-left: 80px;
position: absolute;
color: rgb(237, 237, 237)
}
.conditions{
width: 100px;
height: 30px;
margin-top: 40px;
margin-left: 60px;
position: absolute;
}
.weather-description{
margin-left: 59px;
margin-top: 30px;
margin-bottom: 50px;
position: absolute;
}

.wind {
width: 100%;
margin-left: 30px;
position: relative;
font-size: 14px;
  }

  .wind::after {
display: block;
content: '\f050';
font-family: weathericons;
font-size: 25px;
margin-left: 75px;
position: absolute;
bottom: -26px;
  }
  
  
  <body id="body">
    <div id="weather_wrapper">
  <div class="weatherCard">

      <div class="currentTemp">
        <p class = "help">Click on the temp to change units</p> 
        <p class="location"></p>
  <p class="temp"></p>
  </div>
      


      <div class="currentWeather">
        <span class = "weather-description"></span>
        <span class="conditions"></span>
  <div class="info">
  <span class="rain"></span>
  <span class="wind"></span>
  </div>
        </div>

    </div>
  </div>


    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script>
    <script src="app.js"></script>
  </body>

Answer №3

To achieve your goal, you can avoid using media queries altogether. Start by setting the height for both the html and body elements as follows:

html,body {
  height: 100%;
}

Next, eliminate fixed margins entirely and substitute the #weather_wrapper ID with the following styling:

#weather_wrapper{
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

By using display:flex, the container will dynamically adjust to different screen widths, while justify-content:center horizontally centers the content and align-items:center vertically centers it (this is why we set the height of html and body to 100%).

If you encounter any confusion or difficulties with this method, feel free to reach out.

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

Page-header bootstrap class is malfunctioning

I just started learning bootstrap and currently I'm working with bootstrap v5.2.0. I attempted to create a header with a line using the class="page-header" but the header line is not showing up. Can someone please clarify why? <!doctype html> ...

Fuzzy background when you scroll

My container has an image that blurs when you scroll down the page by 50 pixels. I feel like I've seen this effect somewhere before, but I can't quite recall where. I want the text to remain unaffected and not turn blue. Here is the HTML: <d ...

Include a sharing option within the magiczoomplus feature

My current project involves creating a WordPress site using Artisteer along with several plugins to showcase photo galleries. To enhance the user experience, I purchased an e-commerce WordPress theme which features a share button that I really like. Now, I ...

Ensure that the entire DIV is interactive, with the exception of the dropdown

I have successfully made the entire DIV clickable, but I am facing an issue with the dropdown menu. Whenever I click on the dropdown button, it not only opens up the dropdown menu but also quickly redirects to 'next.html'. Is there a way to preve ...

Is there a reason why using margin:auto doesn't work for vertically centering items?

I'm having trouble understanding how margin auto functions. In the scenario I'm dealing with, I have a parent div and a child div nested within it, both with specified widths and heights. I attempted to use margin auto in order to center the inne ...

Update button text in real-time using JavaScript

I am currently working on a dropdown list that contains 5 elements displayed inside a button when pressed. I want the button to show a default text "Filter by:" before displaying the selected tab value. Additionally, I need the caret symbol to be visible. ...

Issue with Implementing Custom CSS on Active Wordpress Website

Whenever I attempt to write CSS code for button or menu effects in the style.css file of my Wordpress website, the changes do not appear on the live site. The CSS works perfectly fine when tested locally and even after making changes in the page source (I ...

When the search is focused, I prefer for the expanding search box to overlay the menu section rather than pushing it aside, all without the use

I am working on a search box with a subtle animation: <input class="search" type="search" placeholder="Search"> To make it expand when focused, I used the following CSS: .search:focus { width: 225px; outline: none; } I also experimented w ...

Invoke a PHP function from a class located in a separate file using an HTML button

I have an HTML input form with a SAVE button at the end. I want the SAVE button to trigger a PHP function written in a separate class file. The PHP code at the beginning of the file nabava_vnos.php that contains the HTML form: <?php // Script for sta ...

What is the best method in JavaScript to create three different shades of a color?

My sass function generates lighter and darker versions of a base color, here is the code snippet: $colors: ( betpawa-green: #107A3D, lime-green: #8DC63F, yellow: #FBCD00, ); @mixin color-generator { @each $name, $hex in $colors { &-#{$nam ...

Display and conceal specific table cells based on a user's search for a singular cell

I recently wrote a jQuery script to filter rows in a table based on user input. However, I encountered an issue with my current code when trying to search for a specific string within the table. Here's the scenario: a|b|c|d| e|f|g|h| i ...

Designing a drop-down menu that appears visually above its parent

As I design a menu positioned at the bottom using position:absolute; bottom:0, I want the submenu items to align perfectly above their parent. Each li should appear above the ul itself. The trick to achieving this is by applying a negative margin. However ...

What could be causing certain link titles to appear outside of my <a> tags?

Check out this jsbin link to see the issue I am facing. I noticed that some of my link titles are appearing outside the anchor tags. Initially, I thought it was because some titles were enclosed in double quotes. I tried creating a function to fix this, b ...

Creating a table with adjustable row heights is a great way to enhance the user

Can the height of an HTML table row be adjusted dynamically by dragging and dropping, similar to how it's done in this demo (https://reactdatagrid.io/docs/row-resize), but for free? ...

Is there a way to adjust the dimensions of the <audio> tag using CSS?

//I am trying to adjust the size of the audio player based on the 'height' and 'width' properties, but it doesn't seem to be working as expected. <audio autoplay controls height="100px" width="100px"> ...

Position the image slider uniformly at the bottom using CSS (utilizing the Slick Carousel library)

I am utilizing the slick carousel library to create a unique and elegant slider. However, when using images with varying heights, I am encountering issues in aligning the images at the same bottom: https://i.stack.imgur.com/ab5s3.png Here is my code snip ...

What steps should I take to align my NAV next to each other on the top right of the page?

Can someone help me align my NAV elements next to each other? I am having trouble figuring out how to do it currently. #menu { display: inline-block; position: absolute; right:0; top: 10%; } <ul id="menu"> <nav> ...

The animations in fontawesome 6.2 are completely ineffective

I've been struggling to make a spinner icon spin on my webpage. I made sure to download the latest version of font awesome and created a simple HTML file to troubleshoot the issue. Take a look at the HTML code below: <!doctype html> <html la ...

Is there a way to adjust the transparency of an image without affecting any overlaid text while using Bootstrap's carousel feature?

I am currently working with Bootstrap v4 to build a carousel featuring an image with caption text. My goal is to have the image faded while leaving the text unaffected. Despite trying several non-Bootstrap solutions, I have been unsuccessful in achieving t ...

Intel XDK problem with HTML5/CSS3 footers overlapping headers in my project

I am in the process of developing an application using drag and drop features from the Intel XDK/App Designer. The main concept behind this app is to provide users with basic tutorials and incorporate geolocation services. My approach involved following th ...