How to get an overlay texture over inline image without extra mark-up using CSS? - css

I need to give an overlay texture to 100+ images
like this.
I have transparent .PNG texture file. if i use this as background then it will go behind the <img>. And I don't want to add another <img> or any extra span, div for texture and z-index.
Is there any other way to achieve it in CSS?
I need to use specific texture .png so i cannot use CSS gradient only.
I don't want to use main product image as background.

I'm afraid you're going to have a very hard time getting that texture overlaid on the image without some added element to put it on. If you can't affect the html output, a little javascript would do the trick.
Another option is to place the texture over the top of the other image with absolute positioning. It's hard to know if that's a viable option without more context, however. Here's an example: http://jsfiddle.net/cPSFQ/1/.

Glad your post is tagged with CSS3
http://jsfiddle.net/WQTeE/2/
You have to create a reverse mask of the overlay. I tested this in FF9 and Chrome 16
img.stockphoto{
-webkit-mask-box-image: url(http://koivi.com/php-gd-image-watermark/watermarks/Sample-trans.png);
-o-mask-image: url(http://koivi.com/php-gd-image-watermark/watermarks/Sample-trans.png);
-moz-mask-image: url(http://koivi.com/php-gd-image-watermark/watermarks/Sample-trans.png);
mask-image: url(http://koivi.com/php-gd-image-watermark/watermarks/Sample-trans.png);
}

You can try this.
http://jsfiddle.net/Bs7nv/
In this all I am doing is displaying an image and a div in which we can use the texture image as background and absolute positioning to display over the actual image.

There is no pure css solution to your question that's cross browser compatible. I realize that this answer doesn't meet your original criteria, but I figured I'd supply it anyways so that you could have it as an option.
Using pseudo elements (:before) would be a logical choice for CSS3, but alas, they don't work on img tags.
You'll have to do something, rather change the mark-up or add some javascript. Assuming you can't edit the mark-up (sometimes you can't control your source data), but can control the javascript, you could do it with pure javascript like this:
var transparentImage = "http://rd.cas.de/tim/native/image.png";
var imageList = document.getElementsByTagName("img");
var arrImages = [];
for (var i = 0; i < imageList.length; i++ ) {
// store the images as is first, otherwise the list is living and
// you loop forever...
arrImages.push(imageList[i]);
}
for (i = 0; i < arrImages.length; i++ ) {
// first wrap all the images in a relative positioned div.
var wrapper = document.createElement('div');
var newImg = document.createElement("img");
newImg.setAttribute("src", transparentImage);
newImg.style.position = "absolute";
wrapper.appendChild(newImg);
wrapper.appendChild(arrImages[i].cloneNode(true));
arrImages[i].parentNode.replaceChild(wrapper, arrImages[i]);
}
Here's a jsfiddle that does what you want (but with javascript).

Related

How to overwrite svg property on Google Charts

I have implementing Google TreeMap
Since I am using ExtJS Layout and within it is the TreeMap, I had to use iframe, otherwise it would not display anything.
But there is one problem, the Google Map Visualization is defining random svg property.
How to overwrite this random property in my html page and give it a fixed width and height value. Note: I defined fixed width to the charts div, also to the iframe, but nothing is working as svg is overwriting that property.
http://i.stack.imgur.com/i6Pze.png
Call document.getElementsByTagNameNS("http://www.w3.org/2000/svg","svg") to get all the svg elements then iterate through them and set height and width attributes as necessary.
var svgElements = document.getElementsByTagNameNS("http://www.w3.org/2000/svg","svg");
for (var i = 0; i < svgElements.length; i++) {
var svgElement= svgElements.item(i);
svgElement.setAttribute("width", "<whatever width you want>");
svgElement.setAttribute("height", "<whatever height you want>");
}

adjust font-size based on container size

I'm adapting a wordpress theme and it would be nice to have the text of the whole page scale depending on the width of each container div.
So far all the widths are in % but as w3c mentions the font-size property only takes into account the font-size of the parent and not the size of the container div.
As multiple other answers hint, I could do that with javascript and target each container div,
which though being a very helpful answer from my point of view doesn't apply to my problem because I dont know all the possible container ids or classes since I have to account for future plugin installations which could output text, e.t.c.
so is there any other way to do this?
Not without Javascript. CSS bases font-size percentages based on height of a line, there are no width-based controls.
I'd strongly recommend changing your design.
If you want to try to go with it, I'm not sure how proficient you are at Javascript, but you could iterate through DOM nodes, look for a common condition, find the width of the DIV, and move forward that way.
one possible solution could be to target only the body element and change its font-size, this way every font-size should adapt due to the parent changing.
this solution combined with media queries to target the most common screen size should be perfect.
Based on that, I pieced together the following ( http://jsfiddle.net/8TrTU/73/ )
function adaptFontSize() {
var defaultW = 100;
var defaultFontSize = 16;
var width = parseInt($("#header").width());
var fontSize = (defaultFontSize * width)/defaultW+"px";
$("#page").css('font-size', fontSize);
// alert(width +" "+ defaultFontSize * width+ " " fontSize);
}
$(document).ready(adaptFontSize);
$(window).resize(adaptFontSize);
opinions? possible improvements?

Space in the center of a Horizontal wordpress menu

I am looking for a solution to a unique problem. I have a wp_menu underneath the "Network Menu" on a theme I developing for a multisite build. There is a little ring in the center of the menu that holds the logo, and I am curious if there is a way to get a transparent list item to hold that place, and allow all other "legitimate" list items to wrap around that, so either to the left or right.
I already have a filter that inserts a list item that I can customize by adding css, I just don't know how to make one list item in particular hold the place where the circle exists.
The website is:
http://www.mountainjackscreative.com/sandbox/edts/sample-page/
Any help, or even just ideas would be great!
Thanks everyone.
Since you're using jQuery you could do some math on the menu.
EDIT: Put this before ending of your head tag
LAST_EDIT: Here's the JS fiddle http://jsfiddle.net/BsnFW/14/. Last try :)
$(document).ready(function() {
menuBreakPoint = 200; //width in px after which you want to insert the space (experiment with this)
menuWidth = 0;
rightMenuStart = 600; //width in px from left of menu container div to the right
$('.sub_site_menu li').each( function() {
menuWidth += $(this).width();
if (menuWidth >= menuBreakPoint) {
$(this).css('margin-left', (rightMenuStart - menuWidth));
return false; //break out of each loop
}
});
});
Use this.
Can you not breakup the list into two? float one left and the other right. Provide the appropriate ul margins so nothing shows up on the logo?
I am going to go with the jquery solution as it will most likely render the best results across all browsers. But I did stumble across this other resource regarding a soon to be implemented feature in CSS3
Included is a script that forces compatability with the new CSS3 "column" functionality.
http://www.csscripting.com/css-multi-column/

CSS Changing Postition Dynamically in IE9

Is there anyway to change the position style of an element dynamically from 'absolute' to 'fixed' dynamically in IE 9 and before?
In other words we want an element to move vertically on the page till a point when it would reach at the top of the window and then at that point make it fixed so it wont just go up anymore? Makes sense?
It just works in my copy of IE 9.
document.getElementById('foo').style.position = 'fixed';
What you're looking for is a way to change this value based on another in-page condition.
I'd suggest what you need is something akin to this (using jQuery):
var targetElement = $('#your-fixed-absolute-element');
var togglePixelY = 100; // change to suit your needs
$(window).bind('scroll resize',function(){
if($(this).css('scrollTop') <= togglePixelY && !targetElement.hasClass('absolute')) {
targetElement.addClass('absolute').removeClass('fixed');
} else if($(this).css('scrollTop') > togglePixelY && !targetElement.hasClass('fixed')) {
targetElement.addClass('fixed').removeClass('absolute');
}
});
Here is another useful question you can read up on:
Get current scroll position and pass it as a variable with a link?
or Position of a div relative to the window?
and there are plugins for this (look for 'sticky sidebar' for example) and a nice tutorial for it here: http://designwoop.com/2011/01/how-to-create-a-jquery-sticky-sidebar/
Using jQuery?
http://api.jquery.com/css/

resize images with canvas - css overflow issue

I'm working on a JavaScript image resizing feature which rely on the IE only DXImageTransform addon.
Wanting to address modern browsers as well, I gave a shot to canvas, with pretty good results.
However, I face an issue in my resize function, which is the following:
function Resize(oObj, flMultiplier)
{
var canvas = document.getElementById('canvas');
var canvasContext = canvas.getContext('2d');
oObj.style.visibility = 'hidden';
canvasContext.clearRect(0,0,canvas.width,canvas.height); // clear canvas
canvasContext.fillStyle = 'rgba(0,0,0,0.4)';
canvasContext.scale(flMultiplier,flMultiplier);
canvasContext.drawImage(oObj, 0, 0);
}
If my image becomes bigger than the canvas item, then one will see only a portion of the image, which is bad.
I tried to tweak the canvas size at runtime, but if affects the display of the resized image.
So I ended up declaring a big canvas element, which is pretty OK, except that the css overflow of my web page is adjusted on my biggest element, and not on my resized image, which is a pain.
So I guess there are two ways to solve my problem:
Try to exclude the big canvas element from the overflow scheme
Try to update the canvas element size when resizing (I may have missed something there)
I haven't tried that myself, but perhaps you can create a canvas element out of the Dom, with document.createElement. It could be of arbitrary size without disturbing the display.
Now, I lack context (why do you resize images this way instead of using width and height attributes, among other questions) so maybe I am missing the point.

Resources