How to create an image that changes state? - html

What is the best way to create an HTML image that displays various states (e.g. for a button: normal, hovered, selected, disabled)?
Ideally, I want to do via CSS classes (including the :hover meta-class).
Also, I've seen multiple states combined into a single image (which makes image pre-loading easier), but I don't know how to display a single slice of the image as needed.
Thanks!

I assume you are talking about CSS Sprites. Here's a A List Apart article to get you started: http://www.alistapart.com/articles/sprites. The basic idea is that you have all of your images combined into a single, large image.
Whereas normally you will have img tags or small elements with background image applied, now you have the single image applied to multiple elements as a background image, and each of them have different background position values to position the correct image into position. An example of this would be jQuery UI's icons - the single combined image look like this:
Then each of the individual icons share a single class, with a backgroun-image set:
.ui-icon{width:16px;height:16px;background-image:url(../images/ui-icons_808080_256x240.png);}
As well as individual background-position for each of the different icons:
.ui-icon-carat-1-n{background-position:0 0;}
.ui-icon-carat-1-ne{background-position:-16px 0;}
.ui-icon-carat-1-e{background-position:-32px 0;}
.ui-icon-carat-1-se{background-position:-48px 0;}
The same can be done for the individual interactive states - change the background-position of the element on :hover, and you get a different color or icon.

Check this out: http://josefaeti.com/tutorials/css/css-how-to-use-multiple-images-stored-in-a-single-file.html#box-0
and this: http://www.alistapart.com/articles/sprites

Related

CSS switch input text is not visible when background colour applied to parent element

I am looking at the topcoat library and using one of there components; Topcoat Switch.
The example functions fine here:
http://codepen.io/Topcoat/pen/upxds
But in my app I nest the switch in an unordered list and have to apply a background colour to the containing list element for styling purposes. As mimicked here:
http://codepen.io/anon/pen/ekKEc
This crude example masks the text from being visible which is highly undesirable.
Similarly if i apply a background colour to the label element the same issue is evident.
Any help on this would be nice as i spent the last day messing about with z-index etc and just figured out it was the background colour.
Increasing the z-index on the .topcoat-switch class should do the trick.
Check it out: http://codepen.io/anon/pen/vcqGh

Standard compliant and/or best practice way to display an image in html/css [duplicate]

This question already has answers here:
When to use IMG vs. CSS background-image?
(31 answers)
Closed 9 years ago.
What's the best practice and established way to portray an image? I am working from the assumption that it shouldn't be done in HTML anymore so I am not using the img tag in my markup. Instead, I am linking the image in my CSS via background: url('dir'). If I am already going wrong with this assumption, let me know.
If linking an image in CSS is the correct (or at least a good, well established) way to go, then the next question is what HTML tag to use in the document? Technically, images can be applied to just about any element in HTML, so which one's the right one to use?
Case in point: Right now I am working on a row of icons where each icon has some text underneath it. At the moment, I have it set up as an unordered, horizontal list and I am applying the image and text in my CSS to each individual list item tag. However, there are several other ways I could do this. I could have a row of divs or spans, for example. Or I could use the img tag without defining the src property in the HTML and instead setting it in the CSS (at least I think this is possible). Or several other ways I can think.
What is the best practice in cases like this?
Images should be displayed with the <img> tag if the image is contextual information on the page. For example, if you have a web page showing custom swimming pools you build, your image of the swimming pools (under normal circumstances) should be set on the page using the <img> tag with an appropriate alt attribute.
// Shows a picture of a pool, provides contextual fallback text
<img src="pool.png" alt="The 'Custome' style pool." />
CSS background images are used more for stylist or decorations on the page. The background image of your webpage is not contextual, so it is set with a background image, for example. However, CSS background images are used for things such as sprites or JavaScript sliders. In these cases, special measures are taken to make sure they are still specified as contextual content (such as setting the title attribute).
// Alert icon is just a design element and does not
// provide context to the page
<div class="alert-icon"></div>
.alert-icon{background:url('alert.png');width:42px;height:42px}
It is also important to note that, by default, <img> will be printed and background-image will not. Chrome, as well as other browsers, now have an option to turn on background printing, but if you want to be 100% certain an image prints, <img> is the way to go.
TL;DR: For the majority of cases, use <img> for contextual images with proper alt attributes. Use CSS background images for decorations or sprites that do not convey context to the site.
Using CSS to display images using the background property is best if you want to display a sprite whose image can be manipulated via class or id. Other than that, using the <img> tag is still in practice.

CSS/JS text-image masking

Wow, I could really use a hand with this one. I've got some #font-face text that needs an image mask on hover.
Off state:
Hover state
You can see that there's a fire background to the hover text, but ONLY on the next and not the entire item. I'm trying to avoid individual image rollovers so that this effect can apply to any text shown in the menu, but I'll resort to fixed images if I have to.
Is there any way to achieve this without photoshopping every single link?
Nope! Extract images from the photoshop file for both inactive and active states, then apply on css :hover!

How to make select inputs look the same in all browsers on all platforms?

i'm solving a problem to make select inputs look the same in all browsers (Chrome and Safari on Mac renders them differently) how to do that ?
The ONLY way to make them look the same right now would be to hide the original inputs, and replace them with appropriately styled html equivalents (of god forbig Flash objects), which would act as proxies, passing the functionality over to the hidden inputs.
That may be automated with JavaScript. But that would be WRONG. You are not supposed to force a different look on to OS styled elements of the webpage. It conflicts with a lot of usability and accessibility practices.
So, the only way is to make your design flexible enough to support differently looking control elements on a web page, and also use different stylesets for different browsers, to ease the adjustment of the styles (at the moment there are no inputs that would look and act the same on all browsers with the same style rules applied).
Unfortunately, life just kinda sucks on this one. Just wait till you need to style a file input...now that's some fun!
if you dont mind using js you can simply design your own look (a jpg img it can even be the same img as the original select element or if you wish you can model parts of it in css)
Then place a div on top of that image that div will contain the text which select element would usually contain
<div id="selectTxt" >
then set another div on top of that with the select element inside it.
<div id="transparentSelect" class="transparent">
<select id="selectCar" name="selectCar">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
</div>
Now the trick is to set the select element opacity to zero
you can do this by adding by adding a class transparent
and then applying the class to the div
.transparent
{
filter:alpha(opacity=0);
-moz-opacity: 0;
opacity: 0;
}
now the element is hidden but when you click on it the list will still show up.
So the list will always look like the default list in the browser
now use js to extract the select value every time you click on the select
and set the inner html of selectTxt div to its value.
This way you get the text of the select on top of an image you want
you can make the image animated with the hover effect in css or with js
I also make a select that looks the same in all browsers but it doesnt work when you click directly on the arrow...
so its an inferior version but if you wish to look at it here it is
http://jsfiddle.net/fiddlerOnDaRoof/LM73V/
it also lacks the arrow image but you can print screen that from your browser
good luck
You should apply a CSS to reset the styles (not just for the inputs, this is a highly recommended practice for all element so that your page looks almost the same in all browsers) there are many, just google a little, for example this one, and then apply your desired styles (border color and width, background, etc...) take a look at this tutorial on how to style form elements

When to use IMG vs. CSS background-image?

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
In what situations is it more appropriate to use an HTML IMG tag to display an image, as opposed to a CSS background-image, and vice-versa?
Factors may include accessibility, browser support, dynamic content, or any kind of technical limits or usability principles.
Proper uses of IMG
Use IMG if you intend to have
people print your page and you want the image to be included by default.
—JayTee
Use IMG (with alt text) when the image has an important semantic meaning, such as a warning icon. This ensures that the meaning of the image can be communicated in all user-agents, including screen readers.
Pragmatic uses of IMG
Use IMG plus alt attribute if the image
is part of the content such as a logo or diagram or person (real person, not stock photo people).
—sanchothefat
Use IMG if you rely on browser scaling to render an image in proportion to text size.
Use IMG
for multiple overlay images in IE6.
Use IMG with a z-index in order
to stretch a background image to fill its entire window.Note, this is no longer true with CSS3 background-size; see #6 below.
Using img instead of background-image can dramatically improve performance of animations over a background.
When to use CSS background-image
Use CSS background images if the
image is not part of the content.
—sanchothefat
Use CSS background images when
doing image-replacement of text eg. paragraphs/headers.
—sanchothefat
Use background-image if you intend to have
people print your page and you do not want the image to be included by default.
—JayTee
Use background-image if you need to improve download times, as
with CSS sprites.
Use background-image if you need for only a portion of the image to be visible, as with CSS sprites.
Use background-image with background-size:cover in order to stretch a background image to fill its entire window.
It's a black and white decision to me. If the image is part of the content such as a logo or diagram or person (real person, not stock photo people) then use the <img /> tag plus alt attribute. For everything else there's CSS background images.
The other time to use CSS background images is when doing image-replacement of text eg. paragraphs/headers.
I'm surprised no one's mentioned this yet: CSS transitions.
You can natively transition a div's background image:
#some_div {
background-image:url(image_1.jpg);
-webkit-transition:background-image 0.5s;
/* Other vendor-prefixed transition properties */
transition:background-image 0.5s;
}
#some_div:hover {
background-image:url(image_2.jpg);
}
This saves any kind of JavaScript or jQuery animation to fade an <img/>'s src.
More information about transitions on MDN.
Above answers considers only Design aspect . I am listing it in SEO aspects.
When to use <img />
When Your Image need to be indexed by search engine
If it has relation to content, including cards (click areas), but not related to design. Design is probably the most difficult thing to parse here because so it's all design right. I would say perhaps functional design (Cards, thumbnails, profile images, things you can click) vs Aesthetic design which is mostly used for sites appeal.
List item
If your image is not too small ( not iconic images ).
Images where you can add alt and title attribute.
Images from a webpage which you want to print using print media css
When to use CSS background-image
Images Purely Used to Design.
No Relation With Content.
Small Images which we can play with CSS3.
Repeating Images ( In blog author icon , date icon will be repeated for each article etc.,).
As i will use them based on these reasons. These are Good practices of Search Engine Optimization of Images.
Browsers aren't always set to print background images by default; if you intend to have people print your page :)
If you have your CSS in an external file, then it's often convenient to display an image that's used frequently across the site (such as a header image) as a background image, because then you have the flexibility to change the image later.
For example, say you have the following HTML:
<div id="headerImage"></div>
...and CSS:
#headerImage {
width: 200px;
height: 100px;
background: url(Images/headerImage.png) no-repeat;
}
A few days later, you change the location of the image. All you have to do is update the CSS:
#headerImage {
width: 200px;
height: 100px;
background: url(../resources/images/headerImage.png) no-repeat;
}
Otherwise, you'd have to update the src attribute of the appropriate <img> tag in every HTML file (assuming you're not using a server-side scripting language or CMS to automate the process).
Also background images are useful if you don't want the user to be able to save the image (although I haven't ever needed to do this).
About the same as sanchothefat's answer, but from a different aspect. I always ask myself: if I would completely remove the stylesheets from the website, do the remaining elements only belong to the content? If so, I did my job well.
Some answers overcomplicate the scenario here. This is a dead simple situation.
Just answer to this question every time you'd like to place an image:
Is this part of the content or part of the design?
If you can't answer this, you probably don't know what you're doing or what you want to do!
Also, DO NOT consider beside the two technique, just because you'd wish to be "printer friendly" or not. Also DO NOT hide content from a SEO point of view with CSS. If you find yourself managing your content in CSS files, you shot yourself in the leg. This is just a trivial decision of what is content or not. Every other aspect should be ignored.
I would add another two arguments:
An img tag is good if you need to resize the image. E.g. if the original image is 100px by 100 px, and you want it to be 80px by 80px, you can set the CSS width and height of the img tag. I don't know of any good way to do this using background-image. EDIT: This can now also be done with a background-image, using the background-size CSS3 attribute.
Using background-image is good when you need to dynamically switch between sprites. E.g. if you have a button image, and you want a separate image displayed when the cursor is hovering over the element, you can use a background image containing both the normal and hover sprites, and dynamically change the background-position.
One more benefit from using the <IMG> tag is related to SEO - i.e. you can provide additional information about the image in the ALT attribute of the image tag, while there's no way to provide such information when specifying the image through CSS and in that case only the image file name may be indexed by search engines. The ALT attribute definitely gives the <IMG> tag SEO advantage over the CSS approach. That's why according to me it is better to specify the images you want to rank well in the image search results (e.g. Google Image Search) using the <IMG> tag.
Foreground = img.
Background = CSS background.
Use background images only when necessary e.g. containers with image that tiles.
One of the major PROS by using IMAGES is that it is better for SEO.
Using a background image, you need to absolutely specify the dimensions. This can be a significant problem if you don't actually know them in advance or cannot determine them.
A big problem with <img /> is overlays. What if I want an CSS inner shadow on my image (box-shadow:inset 0 0 5px rgb(0,0,0,.5))? In this case, since <img /> can't have child elements, you need to use positioning and add empty elements which equates to useless markup.
In conclusion, it's quite situational.
A couple of other scenarios where background-image should be used:
When you want the image to change when the mouse is hovered upon it.
When you want to add rounded corners to the image. If you use img, the image leaks out of the rounded corners.
Use CSS background-image in a case of multiple skins or versions of design. Javascript can be used to dynamically change a class of an element, which will force it to render a different image. With an IMG tag, it may be more tricky.
Here's a technical consideration: will the image be generated dynamically? It tends to be a lot easier to generate the <img> tag in HTML than to try to dynamically edit a CSS property.
What about the size of the image? If I use the img tag, the browser scales the image. If I use css background, the browser just cuts a chunk from the larger image.
img is an html tag for a reason, therefore it should be used. For referencing or to illustrate things, people e.g: in articles.
Also if the image has a meaning or has to be clickable an img is better than a css background. For all other situation, I think, a css background can be used.
Although, it is a subject that needs to be discussed over and over.
Web Student from Paris, France
In regards to animating images using CSS TranslateX/Y (The proper way to animate html) - If you do a Chrome Timeline recording of CSS background-images being animated vs IMG tags being animated you will see the paint times are drastically shorter for the CSS background-images.
There's another reason! If you have a responsive design and want to split usage of low, medium, and high-res images for devices through media queries, you should use backgrounds as well.
Also, i have a gallery section which has inconsistent picture sizes so even though those images are obviously considered content, I use background images and center them in divs with a set size. This is similar to what facebook does in their albums..
Just a small one to add, you should use the img tag if you want users to be able to 'right click' and 'save-image'/'save-picture', so if you intend to provide the image as a resource for others.
Using background image will (as far as I'm aware on most browsers) disable the option to save the image directly.
A small input,
I have had problems with responsive images slowing down the rendering on iphone for up to a minute, even with small images:
<!-- Was super slow -->
<div class="stuff">
<img src=".." width="100%" />
</div>
But when switching to using background images the problem went away, this is only viable if targeting newer browsers.
HTML is for content and CSS is for design. Is the image necessary and does it need to be picked up by screen readers? If the answer is yes, then put the image in the HTML. If it is purely for styling, then you can use the background-image property in CSS to inject the image. Just as a lot of people here have already mentioned, you can then use a pseudo element on the image if you like.
IMG load first because the src is in the html file itself whereas in the case of background-image the source is mentioned in stylesheet so the image loads after the stylesheet loaded, delaying the loading of the webpage.
Another background-image PRO: Background-images for <ul>/<ol> lists.
Use background images if they are part of the overall-design and are repeated on multiple pages. Preferably in background sprite form for optimization.
Use tags for all images that are not part of the overall design, and are most likely placed once, like specific images for articles, people, and important images that deserve to be added to google images.
** The only repeated image that I enclose in a <img> tag is the site/company logo. Because people tend to click it to go to the homepage, thus you wrap it with an <a> tag.
Also note that most search engine spiders don't index CSS background images therefore the background images will be ignored and you won't be able to get any traffic from search engines (no SEO benefit in short).
Where as all images defined with tags are indexed (unless manually excluded) and can bring in traffic from search engines if their title/alt attributes and filenames are optimized properly (w.r.t some keyword).
You can use IMG tags if you want the images to be fluid and scale to different screen sizes. For me these images are mostly part of the content. For most elements that are not part of the content, I use CSS sprites to keep the download size minimal unless I really want to animate icons etc.
I use image instead of background-image when i want to make them 100% stretchable which supported in most browsers.
If you want to add an image only for the special content on the page or for only one page the you should use IMG tag and if you want to put image on more than one pages then you should use CSS Background Image.