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

Integrate the dForm plugin with a convenient time picker widget

Currently, I am attempting to integrate a time picker widget into a jQuery plugin called dForm. The specific timepicker widget can be found at the following link: https://github.com/jonthornton/jquery-timepicker Despite reviewing the dForm documentation, ...

Using SVG as a background image on Internet Explorer with zoom capabilities

I am having an issue with my SVG used as a background image: <div class="bubble"></div> Here is the CSS I am using: .bubble { background-image: url(https://beta.creasoft.cz/Images/bubble-small.svg); background-repeat: no-repeat; width ...

When the parent element is centered, align the children to the left

Can children be aligned to the left when the parent is aligned to the center? I have a list of elements where the parent block should be centered, but the children need to align to the left. Now I found a solution to align the children to the center as w ...

Utilizing Tailwind CSS for creating a responsive layout on a Next.js application

I'm just getting started with Tailwind CSS and I decided to build the user interface of my nextjs application using a "mobile first" approach like everyone suggests. I got the flex direction and background color working perfectly on mobile screens, so ...

Changing the size of a responsive navigation bar with React and adjusting it based on the window.scrollY position switches between collapsed and un

I'm struggling to create a responsive navbar that automatically adjusts its size once it surpasses the height of the navbar (currently set at 80px). However, when I scroll to around the 80px mark, it starts flickering between the collapsed and expande ...

Guide to Wrapping Inner or Wrapping All Elements Except for the Initial Div Class

I am looking to enclose all the elements below "name portlet-title" without including other elements within the "div class name portlet-title". I have attempted the following methods: 1) $("div.item").wrapAll('<div class="portlet-body"></div ...

Tips for wrapping a div element around another div at the top left corner

I am working with a particular layout. <div class="comment"> <div class="leftpart"> </div> <div class="rightpart"> </div> </div> When viewing on a desktop, I maintain the original styling where each div is in its own s ...

Media queries for Tailwind CSS in a Node JS application do not function properly when viewed on a mobile device

I recently developed a Node JS app using EJS templates and Tailwind CSS. Check it out live here: If you're curious, feel free to explore the code on Github. While the media queries function well on desktop devices, they seem to be ineffective on mo ...

Update the appearance of a cell if the value within it is equal to zero

I have discovered a way to achieve this using inputs. input[value="0"] { background-color:#F7ECEC; color:#f00;} Now, I am looking for assistance in applying the same concept to table cells. Can anyone provide guidance? Thank you. ...

Tips for creating semi-circular shapes using CSS gradients

I am trying to design a div element with a background made up of two colors divided by a half circle: My preference is to use gradients for easier animation purposes. I am looking to avoid using pseudo-elements or border-radius, and my HTML structure limi ...

Emphasizes the P tag by incorporating forward and backward navigation options

My goal is to enable users to navigate up and down a list by pressing the up and down arrow keys. For example, if "roma" is currently highlighted, I want it to change to "milan" when the user presses the down arrow key. Here is the HTML code I am working w ...

How to effectively use flexbox to resize child elements in a CSS layout

My goal is to create a layout where a child element fills the entire flex box of a flex layout. Here is an example of the HTML structure I am using: <div class="parent"> <div class="child1"> should have 100px height </div&g ...

Design a dynamic div element for banners that adjusts to different screen

Is there a way to create a responsive div similar to the img-responsive class in Bootstrap, but for a div element that changes its height relative to its width when the window size is adjusted? For example, if you look at the slider on this website "", yo ...

The case of the elusive Firefox overflow: hidden glitch

Recently, I integrated an image slider into my web design and it displayed perfectly on Internet Explorer and Chrome. However, when viewed on Firefox, I encountered a strange problem where the images in the slider were being pushed towards the right side o ...

Achieving Vertical Alignment of Text and Icon in the Same Line Using HTML and CSS

I am struggling to align these icons vertically center with the text. However, there seems to be a slight offset of a few pixels. I have experimented with different solutions, but this is the closest I could get to. Can anyone explain why it's not ali ...

Coordinate the timing of CSS animation with the loading of the page

Despite the abundance of similar questions, none have quite addressed my specific query. I've created a preloader using CSS animation and I want it to synchronize perfectly with the page load. While I can trigger the animation on page load, I'm s ...

Display words on screen and then alter hue using HTML and CSS

Is there a way to dynamically change the color of text inside <a> tags from black to red after a specific time interval and keep it permanently red? do { document.getElementById("code").innerHTML +="<a>Hello World</a><br>"; awa ...

Verify if the second list item contains the "current" class

When displaying a list of blog items on the blog page, I am using grid-column: 1 / -2;. In the first row, the first blog item spans two columns while the second and subsequent rows display three items in each row. This setup works well, but when moving to ...

The Next.js Image component does not implement the maxWidth attribute

<Image src="/assets/blog/image.webp" style={{ maxWidth: "700px" }} width={1400} height={1400} /> Issue Detected The image with src '/assets/blog/image.webp' has the following styles applied, but they are being overwrit ...

Concealing subcategories within a responsive menu using jQuery

My website's responsive menu changes based on screen resolution, but I encountered an issue. On mobile viewports, the sub items in the menu are displayed by default. I want them to only appear when the parent item is clicked. Currently, clicking the p ...