Make WooCommerce Product Images smaller into mobile view? - css

I work on small project , and need to make product images smaller when is seen from mobile view. Currently images are too big, and spacing between each other (i have two products in line) is too small, so want to make separated so the client can get a better overview of the products.
image from smartphone
i tryed to play with image CSS, like this:
#media only screen and (max-width: 600px) {
.woocommerce ul.products li.product a img {
height:265px; //to make image smaller so can get more spacing between.
}
}
but that seems to make images smaller, but also cut it off, so its not ok. Can someone to help me, how to make images smaller, but image do not crop? Example from site here.

A rule of thumb is to let the browser render as much as possible on its own, to stay responsive and keep the performance up. The more you do yourself, the less well suited your site will be.
Using flex boxes make this really handy:
#media only screen and (max-width: 500px) {
.item {
max-width: 150px;
}
}
.item {
max-width: 200px;
}
#container {
display: flex;
flex-wrap: wrap;
}
img {
width: 100%;
}
<div id="container">
<div class="item">
<img src="https://reinigungsmittel-grass.de/wp-content/uploads/2018/06/Carwash-foam_20-500x597.png">
</div>
<div class="item">
<img src="https://reinigungsmittel-grass.de/wp-content/uploads/2018/06/Carwash-foam_20-500x597.png">
</div>
<div class="item">
<img src="https://reinigungsmittel-grass.de/wp-content/uploads/2018/06/Carwash-foam_20-500x597.png">
</div>
<div class="item">
<img src="https://reinigungsmittel-grass.de/wp-content/uploads/2018/06/Carwash-foam_20-500x597.png">
</div>
<div class="item">
<img src="https://reinigungsmittel-grass.de/wp-content/uploads/2018/06/Carwash-foam_20-500x597.png">
</div>
</div>
Note: As you have a resolution bigger then 500px all such boxes will be at maximum 200 pixels wide, if it is smaller they shrink to 150px. Everything in that box will resize and by setting the image width to 100% it does also resize according to its parent box. That way you won't have to mess around with all those values across your css and html.
Of cause you can replace the pixel values by percentages as you did on your side, resulting in the style you wanted to have.
Here is a nice summary on flex boxes and what you can do with them: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Most phone devices are 425px or less in width. Because your images are size hard-coded, I'm going to provide a hard-coded solution aswell
#media only screen and (max-width: 425px) {
.woocommerce ul.products li.product a img {
height: 200px;
width: 70%;
margin: 0 auto;
}
}
You can tweak the values as you want.
You need to adjust both height and width accordingly to not cut the images off.

Related

Display image original size, if possible, else shrink

There are lots of questions on this general subject, but none that do what I need.
I need to present multiple images, each in a div of its own, but I have no prior knowledge of whether they're portrait, landscape, or low resolution. What I want to do is show them original size (and centered) if they will fit (i.e. I don't want to stretch a low-resolution image), but if they're larger than the div then shrink them appropriately according to their aspect ratio.
This sounds simple but I gave up with the img element, and solutions suggesting width:100%;height:auto; were neglecting the fact that this presumes a landscape image.
I started using background properties and had much more success. If I could make this work then it would have the added benefit that it worked with IE11 too. For instance:
background-image: url("http://example.jpg");
background-position:center center;
background-size: auto;
background-repeat: no-repeat;
works fine if the image is smaller than the div, but crops the image if the original size is too large.
Is there a way to scale the size appropriately in this case (as with background-size:contain), but display original size when possible?
I think you can achieve what you want with a combination of the two properties: max-width: 100% and max-height: 100%:
.container {
display: flex;
}
.item {
margin: 1rem;
width: 200px;
height: 200px;
border: 1px solid;
display: flex;
}
img {
max-width: 100%;
max-height: 100%;
margin: auto;
}
<div class="container">
<div class="item">
<img src="https://via.placeholder.com/150x150" />
</div>
<div class="item">
<img src="https://via.placeholder.com/300x200" />
</div>
<div class="item">
<img src="https://via.placeholder.com/200x300" />
</div>
</div>
1st item: image is smaller, keeps its original size.
2nd item: image is landscape, shrinked to fit.
3rd item: image is portrait, shrinked to fit.
Is this what you needed?

CSS to hide element in order to prevent wrapping

Ok, I'm aware of media queries but they are strictly related to a certain width. Sometimes it's not convenient. Say I have bootstrap navbar with logo image and h1 in one line, like here
<nav class="navbar">
<a class="navbar-brand">LOGO</a>
<h1 class="d-inline">Long enough header</h1>
</nav>
https://jsfiddle.net/zzmaster/qno7utp0/2/
On a certain screen width it wraps into two lines. I'd prefer to hide the logo and make this process independent of the width of the second element. Ideally I think of a class of hiding priority like this
<div class="container">
<div class="prio-1">one</div>
<div class="prio-2">two</div>
<div class="prio-3">three</div>
</div>
meaning preventing line wrap by hiding less priority-objects. Is it possible?
I believe this is the solution. So as per this css
If width is more than 500px all the three prio-3, prio-2, prio-1 will be visible.
If width is between 200px and 500px both prio-2, prio-1 will be visible.
If width is less than 200px only prio-1 will be visible.
#media screen and (max-width: 500px) {
prio-3 {
display: none;
}
}
#media screen and (max-width: 200px) {
prio-2 {
display: none;
}
prio-3 {
display: none;
}
}

Why can't I make these images responsive and stack effectively using float left

This is probably a really simple fix but has tied me up in knots for a couple of hours now... I was hoping someone may be able to help,
I just want to make the four images each have a width of 25% and fit 100% of the width of the page. However I get this white gap on the right of the images and can't get seem to get all four to fit across the page.
Any ideas at all? Thanks in advance.
CSS:
body {
padding: 0px;
margin: 0px;
}
.thumbar {}
.thumbnails {
width: 25%;
height: auto;
background: white;
min-width: 300px;
}
HTML:
<div class="thumbar">
<img class="thumbnails " <img src="images/02.jpg">
<img class="thumbnails " <img src="images/02.jpg">
<img class="thumbnails " <img src="images/02.jpg">
<img class="thumbnails " <img src="images/02.jpg">
<img class="thumbnails " <img src="images/02.jpg">
</div>
What size images are you using? It's likely because the last image does not have enough room to fit the size of the screen. I don't see a float call in your style.
I would try using boot strap and surround everything in a container div.
And you'll want to set:
display: inline-block.
Check out Bootstrap as it offers responsive restyling particular for media items.
http://getbootstrap.com/components/#media
Firstly, it appears some of your HTML is malformed. This:
<img class= "thumbnails " <img src="images/02.jpg">
Should be:
<img class= "thumbnails" src="images/02.jpg" />
Since one image has to be defined using one img element.
Even though you did not show how this output is being displayed, the images are probably being stacked vertically. This is because images are block elements, not inline elements.
You can fix this by adding:
float: left;
to the thumbnails class or by making it display inline:
display: inline-block;
Hope this helps you!
Edit:
After playing around with the code for a bit, I've discovered that:
min-width: 300px;
is messing up the layout of the images at lower document widths. You should get rid of this property, or simply move it to the outer thumbar class.
Check out this JSFiddle.
Edit:
Now I see the confusion. What you're looking to do it create a media-query. The property you are looking for is max-width and it is defined, like so:
#media screen and (max-width : 320px) {
.thumbnails {
width: 100%;
float: none;
}
}
Check out this updated, responsive JSFiddle.

How to make 3 to 2 column responsive design with variable heights?

Making 3 column responsive design using media queries is simple, but in case I want the middle column to stay in mid with 100% height and the right column goes on the left column it gets tricky.
The problem emerge because mid column height is variable, therefore it will be impossible to give negative margin-top to the transformed right column.
here is css code example:
/* 3 columns css */
.colLeft, .colRight, .colMid{
width: 280px;
float: left;
}
.colMid{
width: 500px;
}
.colRight{
width: 240px;
}
.container{
width: 1020px;
margin: 0 auto;
}
/* Media queries */
#media screen and (max-width: 1020px) {
.container {
width: 780px!important;
}
.rightCol{
width: 280px;
}
}
Html:
<div class="container">
<div class="leftCol">
</div>
<div class="midCol">
</div>
<div class="rightCol">
</div>
</div>
this code works fine, But mid column height push the transformed right column down.
My question: Is there HTML/CSS solution for this issue? or do I have to change HTML rendering when screen re-size?
Thanks,
Looks like your float: left is being applied to your .colRight class. This could be what is causing your right column to display on your left column.
I found the solution, using only CSS/HTML. Here is the explanation:
I created a duplicate rightCol div and placed it inside leftCol div, and named it righCol-left.
Initially the rightCol-left is set to "display:none"
when media screen is lower than 1020px, hide rightCol (display:none), and display rightCol-left (display:block)
here is the CSS:
.rightCol-left{display:none;} /*duplicate div*/
#media screen and (max-width: 1020px) {
.container {
width:780px!important;
}
.rightCol{
display:none;
}
.rightCol-left{
display:block;
width:280px;
}
}
here is the HTML:
<div class="leftCol">
/*Left column content*/
<div class="rightCol-left"> /* will be displayed when screen is below 1020px */
</div>
</div>
<div class="midCol">
/* mid column content */
</div>
<div class="rightCol"> /* gets hidden when screen is below 1020px */
</div>
This works perfect, the only bad thing would be the repeated HTML code. But this is not noticeable and very minor, after all, HTML code produce far less load to the webpage compared to JavaScript or anything else.

How do I make the background color responsive on mobile devices?

I have created a website using bootstrap and the responsive features works fine, except for the background color of the div. The elements (images) are stacked but the background color remains only behind the first image in the row. I am looking for a way to extend the background-color on mobile devices.
HTML:
<div id="omos">
<div class="row">
<div class="col-md-6 col-xs-12">
<h2>Kristoffer Andreasen</h2>
<img style="height:280px; width:420px;" src="http://i.imgur.com/874qJmM.png" class="img-responsive">
<div class="Krille">
<p>Indhold</p>
<p>Marketing</p>
<p>Webdesign</p>
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="Kalli">
<h2>Kasper Hjortsballe</h2>
<img style="height:200px; width:200px;" src="http://i.imgur.com/kTleong.png" class="img-responsive">
<p>Programmør</p>
<p>Layout</p>
<p>Grafik</p>
</div>
</div>
</div>
</div>
CSS:
#omos {
background-color: #f7f7f7;
height: 80vh;
clear: both;
text-align: center;
}
I have tried several options but no one seems to solve the problem. Does anyone know what to do?
Remove the height property of the div, and it should work.
Bootstrap uses media queries to show things differently on differnet sized screens. If you're saying your CSS works on desktop but not on mobile, it would be because you're not using media queries.
/* Custom, iPhone Retina */
#media only screen and (min-width : 320px) {
}
/* Extra Small Devices, Phones */
#media only screen and (min-width : 480px) {
}
/* Small Devices, Tablets */
#media only screen and (min-width : 768px) {
/* your mobile css, change this as you please */
#omos {
background-color: #f7f7f7;
height: 80vh;
clear: both;
text-align: center;
}
}
/* Medium Devices, Desktops */
#media only screen and (min-width : 992px) {
}
/* Large Devices, Wide Screens */
#media only screen and (min-width : 1200px) {
/* your desktop css */
#omos {
background-color: #f7f7f7;
height: 80vh;
clear: both;
text-align: center;
}
}
let's think about what your style sheet does,
height: 80vh;
that makes us do something that is 80% of the viewports height, so if our div stacks outside of that when switching to 12 col it is not going to get the color, as your color is not based on the div, but is based relative to the viewport, twitters media queries however will change your child elements and override this wrapping divs height, so you get stuck, I'd see if min-height will work, or remove height all together if possible(not sure what you're vision is exactly)
Set the height property to auto, it will work fine.
height: auto

Resources