I am attempting to, as the title suggests, limit the size of an image based on the user's browser window. The code that does this is below:
.image {
max-height: 15%;
width: auto;
}
This class was referenced here:
<ul>
<li>
<img class="image" src="imgs/pie-chart.png">
</li>
</ul>
I am trying to make the image became smaller on smaller windows, or at its max size on larger windows. However, nothing is happening to the image itself when I try to display it on the webpage. Also, if it helps, I am using bootstrap with this.
I think you can use viewport width (vw). There is also a viewport height(vh). Try to click "full page" and resize browser window.
.image {
max-height: 15%;
width: 10vw;
}
<ul>
<li>
<img class="image" src="https://pngimg.com/d/free_PNG90785.png">
</li>
</ul>
The best way to limit the size of an image based on the user's browser window is to use CSS media queries. CSS media queries allow you to specify different styling rules for different screen sizes. For example, you could use the following media query to make the image smaller on smaller windows:
#media (max-width: 500px) {
.image {
max-height: 10%;
width: auto;
}
}
This will make the image 10% of the maximum height of the window when the window is 500px or smaller. You can also use media queries to specify different styling rules for different window sizes. For example, you could use the following media query to make the image larger on larger windows:
#media (min-width: 1000px) {
.image {
max-height: 20%;
width: auto;
}
}
This will make the image 20% of the maximum height of the window when the window is 1000px or larger.
Related
So, I want to create a header image for my site. I want it to be responsive and I'm taking the 'mobile first' approach. I have a picture, and as title suggest, I want it to be displayed differently based on device's display size BUT it still has to be the same image file. For example, on mobile I will see only small part of the image, but as soon as I hit certain width, it will change to full size. This site http://adopciaki.pl has exactly what I want - I tried to replicate their layout but to no avail. Thanks for help!
there're several possibility's to achieve this, for example, on mobile:
img{
position: relative
width:auto;
height: 100%;
left:50%;
transform: translateX(-50%);
}
this will position the image centered, give the wrapper element a overflow hidden
then on tablet or desktop you can set the width to 100% and the height auto and so on...
One solution would be to use an SVG copy of the image and use CSS media queries to size it based on the screen size - https://jsfiddle.net/rkr9psbf/1/
#media (min-width: 1200px) {
img {
width: 300px; height: 300px;
}
}
#media (max-width: 600px) {
img {
width: 150px; height: 150px;
}
}
You'll see the image shrink to half the size by making your browser window smaller. Hope this helps!
I am using the tinymce editor to allow users to create their own content. When they upload images they cause side scrolling when the output is viewed on mobile devices.
How can I shrink the content of the whole box where the images are to large?
I have tried using css media queries but as the images are set to a certain width they are still to large.
I have changed my css to this, but I am still having the same problem:
.coupon
{
min-height:202px;
background-color:#fff;
color:#000;
padding:2px;
text-align:left;
max-width:100%;
height: auto;
}
Please see example.Play the quiz and select random answers for the two questions to see the overscrolling image http://www.quizzerland.com/?q=bbv.php&desc=vvv
To have images use their natural width or the width given by the width= attribute on wide viewports and shrink them on narrow viewports you can use media queries, like this:
#media screen and (max-width: 640px) {
img {
height: auto;
max-width: 100%;
}
}
Replace 640px in accordance with the actual design. If you cannot trust your users at all then you may want to limit the max-width of images at all times, not only on narrow viewports; in this case, just get rid of the media query:
img {
height: auto;
max-width: 100%;
}
So, i'm a beginner html/css coder and trying to make a simple site.
Now I have a neat background that behaves perfectly.
But when adding a logo at the top center it looks perfect on the current window size. But when I resize the window, half of the logo is cut off.
My CSS style:
.header-logo {
background: url(images/header-logo.png);
position: relative;
height: 200px;
margin-left:auto;
margin-right:auto;
background-size:cover;
width: 971px;
z-index: 2;
}
I suppose there is an auto scale css/js setting for that but i'm not lucky enough to find it.
All help is appreciated!
Louis
The issue is these two lines of code:
height: 200px;
width:971px;
When you use "px" it's a fixed amount of pixels which means it doesn't change based on screen size. If you use "em" instead then the image will change based on the screen size of the visitor.
Here are two quick references that I hope may be helpful.
http://www.w3schools.com/cssref/css_units.asp
http://kyleschaeffer.com/development/css-font-size-em-vs-px-vs-pt-vs/
To fix it you might do something like this:
height: 100em;
width:486em;
(Don't use my exact values of course.)
EDIT:
Alternatively it may be good to use a percentage like this:
width: 971px;
max-width:100%
EDIT 2:
It was pointed out to me that you'd probably want to include this line as well:
height:auto;
It happens because your width is setted to be fixed on 971px, you should use width: 100% and set max-width or at least use #media to set your logo width.
using #media:
#media screen and (min-width: 480px) {
.header-logo{
width: 250px;
background-position: center;
}
}
It seems like you want a 971px wide logo and you have an issue when the screen size is less than that because the logo gets cut off.
What you need is a media query like this one and place it at the end of you css
#media (max-width: 971px) {
.header-logo {
width: 100%;
}
}
That way any screen size under 971px will change the width property to 100% of screen size.
You don't need to redeclare all the properties of the class in the media query, it will just change the ones that have to adapt to the new screen size.
Read more on media queries here : https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
Say I have a 400 px wide and 250 px high image. The user resizes the screen or loads the page on a smartphone.
Say the screen width is 320 px wide. The 400 px image won't fit.
Is there a way to automatically resize the image (and keep proportions) when the screen is not wide enough, using CSS?
In other words the image should be resized from 400px wide to 320px wide (for example).
Use
max-width: 100%;
Google responsive images for more information.
http://mobile.smashingmagazine.com/2013/07/08/choosing-a-responsive-image-solution/
No need to use mediaqueries for this specific case: just define
#yourimage {
width: 100%;
max-width: 400px;
}
this will ensure a full-width image for every viewport width up to 400px
You need to specify both min and max:
demo: http://jsfiddle.net/abhitalks/Vv9RT/
css:
img {
min-width: 220px;
max-width: 420px;
width: 100%;
}
Try changing the panel size in the fiddle. min-height will ensure a minimum acceptable size when the screen size gets too low. max-height will ensure a maximum size so that it doesn't get huge.
100% width will keep it within bounds.
by using the css3 media queries u can do make possible
ex: #media screen (max-width:480px){
img{
width:320px;
}
}
or
img{ max-width:100%}
or else you can use both.. 'img{ max-width:100%}' place before the media quires
I have some question about html and css.
Here are the case. I'm building a mobile web, which my base line is 240px width. So all my elements's dimension is set base on the 240 screen size. But when I view the web in a larger phone like Samsung Galaxy note. All things seem to be too small for user to click on it.
Now the question, is it possible to use variable kind of css for width and height ??
Lets say, the thumbnail I use in 240px width device is 50px, so when I view my thumbnail in a 480px width device, the thumbnail will be display in 100px, which means the thumbnail will be increase its size based on the percentage of the screen increased.
Yes, obviously you can, what you are looking for is called Responsive Design, to accomplish that, you will need #media queries.
Demo (Resize the window to see the effect)
div {
height: 200px;
width: 200px;
background-color: tomato;
}
#media all and (max-width: 400px) {
div {
height: 100px;
width: 100px;
}
}
Use percentages for your width instead of fixed width to make the images responsive.
For example, use 100% instead of 50px. It will automatically fill the container (in which the container is also responsive) and automatically resize on your browser.
yes ,it is possible,responsive design achieve in following ways,
All dimension in %
media queries
yes, you can use #media queries for responsive design
example:
div{
width: 50px;
}
#media all and (min-width: 480px){
div{
width: 100px;
}
}
(or) u can also use percentages at certain cases like width: 20%