I'm searching a way to EASILY add responsive alternative images to content. For example I have three different images, one for each size (desktop, tablet and mobile) and would like to show them according to responsive breakpoints.
Foundation framework has "interchange" built in, but there is no easy way to add the images except by adding the code:
<img data-interchange="[/path/to/small-image.jpg, (small)], [/path/to/bigger-image.jpg, (large)]">
Anybody know any plugin that could do this?
You can insert all three images in the page and show every image for specific screen width;
<div>
<img class="image-small" src="/path/to/small-image.jpg" />
<img class="image-medium" src="/path/to/medium-image.jpg" />
<img class="image-big" src="/path/to/big-image.jpg" />
</div>
.image-medium,
.image-small{
display: none;
}
#media screen and (max-width: 800px){
.image-medium{
display: inline;
}
.image-big{
display: none;
}
}
#media screen and (max-width: 400px){
.image-small{
display: inline;
}
.image-big{
display: none;
}
}
i'd suggest to use the Srcset attribute and then implement fallbacks (ex. picturefill) for browsers you need to support.
by viewport
<img src="img-1x.jpg"
srcset="img-1x.jpg 500w, img-2x.jpg 800w"
alt="">
or by resolution
<img src="img-1x.jpg"
srcset="img-1x.jpg 1x, img-2x.jpg 2x"
alt="">
Ok, I solved it like this. (http://i.stack.imgur.com/jWcDD.png) I used the Visual Composer plugin and made a simple add-on plugin that enables content editor to attach two different images. The plugin just writes these image tags and Foundation frameworks css hides and show them accordingly:
<img src="default.jpg" class="show-for-medium-up">
<img src="small.jpg" class="show-for-small-only">
I couldn't get Foundations Interchange images to work inside Visual Composer. Maybe it has something to do about javascript queue. Visual Composer content loads after foundation or something..
Related
I'm writing an ebook in HTML and converting to MOBI with Kindlegen. I want to make sure the images never take up the whole page. However some images are doing just that.
I've tried multiple CSS styles but nothing seems to change. I'm testing on Kindle Previewer, iPhone X, kindle paper white (older device) and iPad. All these devices seem to react to CSS differently and the iPad seems to completely ignore my image styles. No matter what I set the iPAD images don't change. How can I make sure the images are never too large? I want the image to be small enough so that text is also on the same page. Ideal never larger than about 30% of the screen.
I've tried setting a percentage
width: auto;
height: 30%;
and setting em
width: auto;
height: 20em;
I get an error from Kindlegen if I use max-height
.image {
width: auto;
height: 30%;
}
.centerImg {
text-indent: 0;
margin: 1em 0 0 0;
padding: 0;
text-align: center;
}
<!-- Page 29 -->
<p class="centerImg">
<img class="image" alt="lock" src="images/page29.jpg" />
</p>
<p class="collector">
Text
</p>
<br />
<p class="description">
Text
</p>
<div class="pagebreak"></div>
What's the best way to do this?
CSS with ebooks on Amazon can be a bit daunting. I've even seen major bestsellers where the layout didn't work out as intended. Although I've never gotten an ebook to look exactly the same across all devices, I have been able to size my images satisfactorily. I use the free program Sigil for editing, then convert to .mobi with Calibre.
Because CSS can be so unreliable on ebooks, I sized the image in the HTML itself:
<div align="center"><img height="148" src="../Images/stars-300.jpg" width="200"/></div>
<br/>
<h1 class="cinz" id="sigil_toc_id_21">-21-</h1>
<br/>
<h1 class="toocinz sigil_not_in_toc">Between Worlds</h1>
Below is an image of the above code on Kindle Paperwhite. On the iPad, the image is a bit smaller, and some of the spacing is different, but it looks close enough. Another trick I've used to 'force' the ebooks to use your styling, is to use two CSS stylesheets. The first one simply refers to the second, "real" one. This can get around some of the default styles that override custom styles. I'm not sure how well it's worked, but it hasn't hurt:
Style0001.css has only this line:
#import url(../Styles/Style0002.css);
Style0002.css is where all my actual styling is. All my book pages link to the first stylesheet:
<link href="../Styles/Style0001.css" rel="stylesheet" type="text/css"/>.
I am new to media queries. I have it setup to swap an image based on the portal size. That is working great with the following code:
<header>
<div class="logo_div">
<img src="images/logo_full.png" class="logo_full">
<img src="images/logo_small.png" class="logo_small">
</div>
</header>
/* Logo DIV */
.logo_div {
margin: auto;
width: 50%;
}
/* Logo */
.logo_small {
display: none;
}
#media (min-width: 1200px) {
.logo_full {
display: block;
text-align: center;
}
.logo_small {
display: none;
}
}
My large logo is centered just fine. My small logo however sits to the right. I have tested this by simply changing the browser window size as well as on my iPhone XSM. On my phone, it is obvious that the small logo is to the far right.
Am I missing something here?
You can see this live also by going to http://thelavender.net/_fades/
For me the best way to center any object is:
#myobjectid{
display:table;
margin:0 auto;
}
In your case put it to your div container, and remove width.
Inside your image, put your width tag.
I want to add that you're making two http requests, for both the large and small logos, when you really need only one. Have you considered using picture?
The HTML <picture> element contains zero or more <source> elements and
one <img> element to provide versions of an image for different
display/device scenarios. The browser will consider each child
<source> element and choose the best match among them; if no matches
are found, the URL of the <img> element's src attribute is selected.
The selected image is then presented in the space occupied by the
<img> element.
This following snippet will produce only one logo at a time and shrink your CSS significantly.
<picture>
<source srcset="logo_full.png" media="(min-width: 1200px)" />
<source srcset="logo_small.png" />
<img srcset="logo_full.png" alt="My default image" />
</picture>
I'm working on a site that uses Bootstrap. I'm working to make this site work on both desktop and mobile browsers. Everything's working except for my banner image size.
I have an image that is 640x480. I have an image defined like this:
<img alt="My Picture" src="/wwwroot/img/banner.jpg" style="height: auto; max-height:320px; max-width: 100%;" />
On mobile pages, the image looks just like I want. However, on the desktop, the image is only 320px wide. However, on the desktop, I want the image to go as wide as it can go. Basically, I want to crop the right portion.
Is there a way to do this with CSS?
Thanks!
Stick your CSS in a class.
If using HTML5 remove the superfluous /
Change your max- to min- regards height.
Change your standard width: to auto, changing you
CSS:
.imageFun {
height: auto;
min-height:320px;
width:100%;
/* this is actually no longer needed but kept for posterity */
max-width: 100%;
}
HTML:
<img alt="My Picture" src="/wwwroot/img/banner.jpg" class="imageFun">
Use a media query to target the img in css file:
#media only screen and (min-width : 320px) {
img {
width:100%!important;
}
}
That should work changing the min-width to your desktop size.
More info here
on twitter-bootstrap you can use div with class responsive width :
<div class="col-md-12 col-sm-12 col-xs-12">
<img alt="My Picture"
src="/wwwroot/img/banner.jpg"
style="height: auto; width: 100%;"
/>
</div>
Where value 12 is width scala on grid bootstap. Please look at : https://getbootstrap.com/examples/grid/
i hope it what U want
You are using Booststrap, so remove the inline styling and add
class="img-responsive col-xs-12
That will fix it.
I am working with an HTML template, and I'm trying to replace the site's logo based on browser size using media queries.
I am trying the technique I found here: #media queries and image swapping
My custom.css file has this code:
.test-mobile-logo{
display: none;
}
#media only screen and (max-width: 500px){
.test-main-logo{
display: none;
}
.test-mobile-logo{
display: block;
}
}
My html file has this code for the logo:
<div class="logo">
<a href="index.html" >
<img src="images/logo-dark.png" alt="" class="test-main-logo">
<img src="images/logo-main.png" alt="" class="test-mobile-logo">
</a>
</div>
The page is showing both images at once though. But when I remove my style.css file, the images finally show one at a time and replace properly.
The style.css file: http://demos.webicode.com/html/lucian/html/css/style.css
I'm not sure what the conflict is, I'm still new to CSS. Does anyone have any ideas?
You have this style in your css that overrides your display styles.
img {
display: inline-block !important;
}
Remove the !important to make your media-query work.
I agree with #HenrikhKantuni use a background image and change the background image in the css media query.
Otherwise users will always be downloading 2 images, that's one unnecessary http request and kilobytes the user will be requesting, especially over mobile networks you want to reduce this as much as possible.
as #VincentOrback mentioned just delete the !important from img selector
Better technique: use background-image instead and just change the url, or (even better) use CSS Sprites
I have modified a Marketo responsive email template and need to swap out the header image when the media query hits its breakpoint. However many versions of outlook do not support background images (thank you Micro$uk) so is there a way to write the CSS to swap out an image in with src and not background-image? Here is a screen shot from litmus.com you can see the image doesn't appear in many versions of Outlook.
<img src="image1"/> to <img src="image2"/>
javascript probably wont work in a lot of email clients. i use the below code for my HTML email templates.
HTML
<a href="#" border="0">
<span id="mobile">
<img id="mainimg" class="headimg" src="#" alt="...">
</span>
</a>
CSS
#media only screen and (max-width: 450px) {
span[id=mobile] {
display:block;
background-image: url(#) !important;
background-repeat: no-repeat !important;
background-position: top center !important;
width: 100% !important;
height: # !important;
}
img[id=mainimg] {
display: none !important;
}
}
just replace the "#" w/ your links and/or height of the mobile img and you can set that breakpoint to whatever you need it to be. i get really good results and pass all of the litmus test w/ exception of lotus notes.
hopefully that helps.
In HTML...
<div style="src:www.google.com"></div>
And try this in javascript... (I haven't tested it)
Element.getElementsByTagName('img')[0].src='www.google.com';