Help with Crafting Responsive CSS Design

I am seeking help to better comprehend the concept of responsive design through this question. Let's imagine a header on a standard desktop screen. Within that, I have a div element containing the information I wish to showcase.

.hdrinfo{
    width: 51%;
    margin: 0 auto;
    display: flex;
    flex-direction: row;
}

When this div displays on a tablet, my goal is for its width to be 75%, and on a smartphone, it should expand to 100%. Despite numerous attempts at media queries based on extensive reading, I've been struggling with implementing this. Can anyone guide me in the right direction?

UPDATE: I have modified the CSS to make it more evident whether the queries are functioning correctly:

.hdrcntr{
        width: 52%;
        height: 100%;
        margin: 0 auto;     
        background: green;
}
@media only screen and (max-width:992px){
    .hdrcntr{
        width: 75%;
        background: steelblue;
    } 
}
@media only screen and (max-width:412px){
    .hdrcntr{
        width: 100%;
        background: tomato;
    } 
}           

My approach involves using different background colors as indicators: green for default, steelblue for iPad, and tomato for Samsung S8+. This method works well for both desktops and iPads. However, despite trying to cater to the Samsung S8 dimensions provided by official specs and other sources, I haven't been successful in getting the correct display (always shows steelblue).

Answer №1

Here is an example of how the max-width values can be adjusted based on different devices. It's important to keep in mind that the rules will override each other depending on the order they are written, especially for devices with a width less than 400px.

.header{
    width: 50%;
    margin: 0 auto;
    display: flex;
    flex-direction: row;
}
@media (max-width: 768px) {
  .header {
     width: 75%;
  }
}
@media (max-width: 400px) {
  .header {
     width: 100%;
  }
}

Answer №2

Encountered an issue where the @media query was not responsive on mobile phone displays, despite even on devices with smaller pixel dimensions. To ensure proper display, remember to include the <meta name> in the <head> section of every HTML page:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
... continue with your page

The width=device-width attribute adjusts the page width based on the device's screen size (which varies per device) - as noted on w3school's site.

The initial-scale=1 attribute sets the default zoom level when initially loaded by the browser - as mentioned on w3schools site.

Further information can be found here: https://www.w3schools.com/cSS/css_rwd_viewport.asp


Additionally, it is possible that the presence of the keyword only could be causing issues. I haven't encountered any problems using media queries like this:

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

This example is sourced from a different discussion thread: Why are my CSS3 media queries not working on mobile devices?

Hoping this information aids in resolving your dilemma.

Answer №3

To achieve the desired outcome, consider using min-width and max-width properties in your code.

@media screen and (min-width:413px){
    .hdrcntr{
        width: 75%;
        background: steelblue;
    } 
}
@media screen and (max-width:412px){
    .hdrcntr{
        width: 100%;
        background: tomato;
    } 
}

Visit this link for a demonstration.

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

How to make a div stretch to full browser height using CSS and jQuery

I've been attempting to stretch a div to the browser's height using CSS in jQuery, but it doesn't seem to be working. I can successfully apply 100% width to the div, but height is proving to be a challenge. Any ideas why this might be happen ...

The v-bind class feature is failing to update the CSS on the object's properties

I'm attempting to dynamically modify the CSS of certain buttons based on object properties within a list. Below is the data I am rendering out, and despite following the documentation and ensuring my setup is correct, I seem to be overlooking somethin ...

I am experiencing difficulties with my HTML and CSS code not functioning as I had initially intended

I've been dabbling in HTML and attempting to create my own website, but I'm encountering an issue with the positioning of my <table> tag. It keeps appearing at the bottom of the site, despite my CSS instructions to prevent it. The position ...

Enhancing Image Quality upon Hovering

Is there a way to prevent the content of the article from moving when an image becomes absolutely positioned? The issue I am facing is that the image overlaps its container with overflow: hidden only when the img is absolute positioned. article { back ...

Calculate the sum of hours worked in a day using pure JavaScript, no external libraries required

Hello, I'm new to this website and please excuse me if my English is not perfect, I am trying my best to communicate :) I have been working on a page that calculates the total hours worked in a day and this is what I have achieved so far until 15/06 ...

Scrollbar not displaying on IE when set to overflow: auto

Greetings, all! I am facing yet another design challenge with Internet Explorer. I have a DIV with Overflow:auto on my website that works perfectly fine on Chrome. However, when it comes to IE, the scroll bar doesn't show up and all the images inside ...

Is Span responsible for creating a new line in this particular sentence?

I am struggling with aligning the following line of text that includes percentages like 100% and 99.8%. Is there a way to display all these elements in one line, side by side? <span style="font-size:12px;font-weight:bold;padding-left:800px;">99%& ...

Is the append() function malfunctioning in jQuery?

Why is it copying two lines when I only want one line to be cloned on button click? Is there a way to make sure that only a single line is copied each time the button is clicked? Here is my code: $(function() { $('.add-more-room-btn').clic ...

Move your cursor over X or Y to alter the color of Y exclusively

I am currently designing a navigation bar that includes icons followed by the title of their respective pages (for example, an icon of a home followed by the text 'Home'). I would like to change the color of only(!) the icon from black (default) ...

Applying Tailwind styles to dynamically inserted child nodes within a parent div

Currently, I am in the process of transitioning my website stacktips.com from using Bootstrap to Tailwind CSS. While initially, it seemed like a simple task and I was able to replace all the static HTML with tailwind classes successfully. However, now I ha ...

Having trouble aligning input elements in the middle of a div container

Can anyone help me with centering elements inside a div? Check out this example: I created a wrapper for the number inputs and tried applying the text-center property on them, but it's not working. I also changed the display to block, but that doesn& ...

Adjust the appearance of a glyphicon by modifying its color according to various criteria

Using angularjs, I implemented ng-repeat to display details in a table. In the final column, I aim to display the glyphicon 'glyphicon glyphicon-stop' in a specific color. <tr ng-repeat="ps in $ctrl.personLst"> <td>{{ ps.id}}</td& ...

Utilizing Next.js Image Component to Apply CSS for Width and Height Settings

Currently, I am utilizing the Image component provided by Next.js, which requires setting specific width and height values. To address this, I am incorporating Tailwind CSS utility classes to establish the desired dimensions. <Image src={imageSr ...

Tips for resizing and positioning an image within an input field

Is it possible to have a button overlapped on an input field in a responsive website like this: https://i.sstatic.net/tnVmF.png I tried using an input group addons, but the result is not quite what I expected: https://i.sstatic.net/xpMgr.png This seems t ...

Determine the height of a Bootstrap 4 row based on a specific column, rather than the tallest

I am facing an issue with a row that contains two columns. The first column has content that should be visible without overflowing, while the second column includes a long list that needs to be scrollable within the row. The challenge with responsive desi ...

Can you please explain what shape-id denotes and provide guidance on locating it within a CSS file?

I have a question that may seem trivial to experienced web developers - what exactly is "shape-id"? For example, in the code <hr shape-id="2">. I'm currently attempting to change the color of the horizontal lines (hr) on my website, but I can& ...

Solution for repairing the display location button on Android within a React Native Map

I am facing an issue with the Location Button in my React Native Maps app. When the app is opened for the first time and the map is rendered, the button disappears. However, if I close the app but leave it running in the background, upon reopening the app, ...

The web server is unaware of the CSS and JS references

After loading my ASP.NET MVC project on the web server, I encountered an issue where none of my CSS and JS references were loaded. Some of the references in my default layout are listed below: <head> <link href="/Scripts/css/bootstrap ...

Utilize jQuery to automatically assign numbers to <h1-h6> headings

Looking to develop a JavaScript function that can automatically number headings (h1- h6) for multiple projects without relying on CSS. Currently achieving this with CSS: body { counter-reset: h1; } h1 { counter-reset: h2; } h2 { counter-reset: h3; } ... T ...

I encountered an issue where my style.css file was not properly linking to my HTML code. Whenever I saved the file, it would open

I'm a beginner in the world of HTML/CSS. When I save my style.css file, it shows up as a Notepad+ settings file and doesn't seem to work with my HTML. Can anyone advise me on what steps I should take? ...