How to fix responsive design issue with css - html

I am creating a responsive landing page. I have mentioned different settings for different screen size in CSS. I have mentioned 3 screen sizes, i.e. max-width 320, max-width 375 and max-width 780. However phone having screen width 320 is taking the properties of screen width 375px from the css.
<style>
#media only screen and (max-width: 320px)
{
div.auto-style16 {
height: 95px;
padding-top: 10px;
}
}
#media only screen and (max-width: 375px)
{
div.auto-style16 {
height: 55px;
padding-top: 0px;
}
}
#media only screen and (max-width: 780px)
{
div.auto-style16 {
height: 5px;
padding-top: 30px;
}
}
</style>
As per the above code, a phone having max-width 320px, should take the height 95px. Similarly a phone having max-width 375px should take the height 55px. However a phone with max-width 320px is taking height 55px (which is actually for max-width 375px).

the correct order should be like this:
#media only screen and (max-width: 780px) {
div.auto-style16 {
height: 5px;
padding-top: 30px;
}
}
#media only screen and (max-width: 375px) {
div.auto-style16 {
height: 55px;
padding-top: 0px;
}
}
#media only screen and (max-width: 320px) {
div.auto-style16 {
height: 95px;
padding-top: 10px;
}
}

The order of your media queries matter in css! Check here to know more about it.
Why does the order of media queries matter in CSS?
It means that, if you apply two rules that collide to the same elements, it will choose the last one that was declared, unless the first one has the !important marker or is more specific (e.g. html > body vs just body, the latter is less specific).

I think you need to take a moment to analyze what your code is doing.
#media only screen and (max-width: 320px)
{
div.auto-style16 {
height: 95px;
padding-top: 10px;
}
}
What this does is if the browser window size is upto 320px then apply the styles within the block
#media only screen and (max-width: 375px)
{
div.auto-style16 {
height: 55px;
padding-top: 0px;
}
}
This applies styles for the window screens with max-width 375px. The gotcha here is that if the screen width is 320px then it is not greater than 375px and hence both the styles in the media query is applied and the existing ones are overridden in the order they are parsed.
if you want some styles to specifically for screens that are greater than 320px but less than 375px then you would use
#media only screen and (min-width: 320px) and (max-width: 375px)

Related

CSS media query with max height and max width not working

I am using media queries, in the below order:
#media screen and (max-width: 1600px) and (max-height: 1024px) {
.img {
width: 150px;
}
}
#media screen and (max-width: 1600px) and (max-height: 900px) {
.img {
width: 100px;
}
}
When my screen resolution is 1600x1024, the 1600x900 rule kicks in. When my screen resolution is 1600x1024, the 1600x900 rule also kicks in. This is according to the Developer Tools -> Elements -> Styles tab in chrome. The other rule is always crossed out for some reason. If I switch the order of the rules, then the 1600x1024 rule always kicks in. Am I misunderstanding something?
The problem is that you are applying both rules at the same time eg. your max-width is identical in both rules. Also remember that max-width targets the specified number and below and that min-width targets the specified number and above.
Can you try the following example?
#media screen and (max-width: 1600px) and (max-height: 1024px) {
.img {
width: 150px;
}
#media screen and (max-height: 900px) {
.img {
width: 100px;
}
}
In this case we don't have the same pixels specified as max-width.
More to be found about Media Queries here: https://www.w3schools.com/CSS/css3_mediaqueries_ex.asp

Conditional CSS height for mobile

Is there a way to rewrite this code so when the screen is full screen and tablet then the blue-cus-box margin-top is -175 and if the screen is, for example, iPhone 10 or other mobile devices then the margin-top is -150?
#media screen and (max-width: 767px) {}
.blue-cus-box {
margin-top: -175px !important;
z-index: 999 !important;
}
Appreciate any help
Have you considered just using a media query? The below snippet would set the margin-top to -175px on any screen size over 768px. A guide to using media queries in CSS can be found here: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
.blue-cus-box {
margin-top: -150px;
}
#media screen and (min-width: 768px) {
.blue-cus-box {
margin-top: -175px;
}
}

Using only CSS & HTML, how can I set a div with a padding of 20% to change to 0% if the window is reduced to a specific size?

Currently I have several divs with the CSS
.bodyText {
margin:auto;
padding: 1% 20%;
}
But when the screen gets reduced to the size of, say, an iPhone screen I want to reduce the left & right padding to 1%.
Thanks!
What you're looking for is known as a media query. These are denoted by the # symbol in CSS, and can not only target screen widths, but also specific orienations:
/* Any iPhone */
#media screen and (max-device-width: 480px) {
.bodyText {
padding: 1%;
}
}
/* iPhone Portrait */
#media screen and (max-device-width: 480px) and (orientation:portrait) {
.bodyText {
padding: 1%;
}
}
/* iPhone Landscape */
#media screen and (max-device-width: 480px) and (orientation:landscape) {
.bodyText {
padding: 1%;
}
}
You can do it with #media queries. There are rules for set specific size of screen, or more.
Example:
#media (max-size: 800px) {
// here is your code
}

Width: x% changing depending on content size

I need help to change % width on my category images within my site whenever my screen gets smaller. Or the images will get super small on smaller resolution.
What I want to achieve is something like this: http://www.twitch.tv/directory
I've tried to do this by using this code. (but it isnt working)
.category-list-item {
float: left;
#media screen and (max-width: 769px) {width: 20%;};
#media screen and (min-width: 480px) {width: 25%;};
#media screen and (max-width: 480px) {width: 33.33%;};
padding: 1em;
Would be super greatful for any help!
/ Martin
As rekire says, you messed the syntax, and you need to set full rules.
Also, there shouldn't be semicolons after the rules.
And lastly, your 2 final rules cover all the posibilities, width being greater or smaller than 480px, so the first rule will never apply.
I have changed it so that you have an style for lower res, another for higher res, and the default applied in between
.category-list-item {
background-color:red;
}
#media screen and (min-width: 769px) {
.category-list-item { background-color: green; }
}
#media screen and (max-width: 480px) {
.category-list-item { background-color: blue; }
}
<div class="category-list-item">Test</div>

bootstrap paragraph formating for different devices

Hi I'm fairly new to bootstrap and what I'm trying to achieve is to have a jumbotron on top of my page with different paragraph formatting to accommodate for a background image which takes lets say 30% of full width space.
I have offset my text by padding-left: 300px; and it looks fine on desktops but this rule also applies to a paragraph in mobile device mode resulting it being very skinny and tall.
Is there a way where I can set lets say 3 different paragraphs each showing under certain screen size?
Just use media queries:
#media screen and (max-width: 320px)
{
p{
padding-left: 0;
}
}
#media screen and (min-width: 321px) and (max-width:800px)
{
p{
padding-left: 100px;
}
}
#media screen and (min-width: 801px)
{
p{
padding-left: 300px;
}
}