change image sprite on click for button using CSS - css

change button image onclick then return to original image after click
<style>
img.button-send {
background: url(img/agentsprite.png) 0 0;
width:87px;
height:34px;
opacity:0;
}
img.button-send:active {
background: url(img/agentsprite.png) 34 174;
width:87px;
height:34px;
}
</style>
<button type="submit">
<img class="button-send" src="img/img_trans.gif" width="1" height="1" />
</button>
i'm using a sprite for this, and it seems that it won't go to that image position when i click it, is there any way for this to work ? i wan't to have a button effect for this field when pressed.

...........................
hi now used to px value in your background-position image
as like this
img.button-send:active {
background: url(img/agentsprite.png) 34px 174px;
}
or you can used this also
img.button-send:active {
background-position:34px 174px;
}
px value according to your design

Related

How to make input box show up from right to left?

I have the following HTML:
<div class="menus">
<di class="menu-option">
Weather
</di>
<di class="menu-option">
Travel
</di>
<di class="menu-option">
<input class="search-input" />
<span class="search-btn">Search</span>
</di>
</div>
CSS
.menus {
float:right;
}
.menu-option {
display: inline-block;
border-left: 1px solid black;
padding: 0 15px;
}
.search-input {
display: none;
}
Javascript:
$('.search-btn').click(function(e) {
$('.search-input').show();
});
At this moment, I able to show the search box from hidden when clicking the Search text. How can I make the search box show up by moving from right to left and pushing other text to the left side, instead of showing the search box all of a sudden.
This is the JSFIDDLE example:
https://jsfiddle.net/1btm224h/5/
Thanks!
You can go with:
$('.search-btn').click(function(e) {
$('.search-input').show("slow");
});
Or you can go with:
$('.search-btn').click(function(e) {
$('.search-input').show(3000);
});
From jQuery official: "The default duration is 400 milliseconds. The strings 'fast' and 'slow' can be supplied to indicate durations of 200 and 600 milliseconds, respectively."
So you only need to add animation duration, which is built into the .show() by default.
look, I don't sure if you need this.
$('.search-btn').click(function(e) {
$('.search-input').addClass('open');
$('.search-input').FadeIn(300);
$('.menus').FadeIn(300);
$('.menus').addClass('mover');
});
.search-input.open{
display:block;
float:right;
}
.menus.mover{
float:left;
}

CSS/Javascript to move background image up and reveal another image on click

I have a background image in a div that displays a US flag to represent English language. When the user clicks on the US flag, I want the flag to move up and display the Japanese flag below it so that the user can then switch languages when they click on either the US or Japanese flag.
I assume this, however, is what you want: http://jsfiddle.net/ctwheels/5fmjkkcf/
HTML
<body>
<div id="chooseLanguage">
<img style="z-index:100;" class="flags" src="http://static.buildasign.com/UPLOAD/images/listings/PID970_United_States_Flag.jpg" />
<img class="flags" src="http://worldwide.bose.com/electroforce/assets/images/learning_center/Japan_Flag.jpg" />
</div>
</body>
CSS
body {
background-color:grey;
}
#chooseLanguage {
margin-top:50px;
position:relative;
}
.flags {
height:30px;
width:40px;
position:absolute;
}
JS
$(".flags").click(function () {
flagHeight = $(this).height();
$(".flags").removeAttr('style');
$(this).css({
top: -flagHeight + "px"
});
});

Background Image and Scroll bar

There is a page with background. Before was given as img:src. But I couldn't insert text on it. Because of this I changed that code as background-image. so far everything is ok.
Then the text I'll insert is long, it exceed height of background. I must add an scroll. I inserted max-height but confused here.
here the page is : http://www.heptasarim.com/test/cezayir2/menuler2.html
Background adress : /cezayir/images/menuorta.png
HTML:
<div id="menuorta2">
<p style="background-image:url(images/menuorta.png); background-repeat:no-repeat; width:812px; height:404px;" id="myazi">
<span class="mbaslik">Kokteyl Prolonge Menü</span><br />
<strong>menü:</strong><br />
<strong>soğuk gezenler:</strong><br />
biberli zeytin ve marine somon<br />
tartolet içinde nar ekşili patlıcan salata<br />
hindi füme badem turşu ve hardal<br />
marine zeytin ve peynir şiş<br />
<strong>sıcak gezenler:</strong><br />
şişte ızgara limon ve kalamar<br />
susamlı et<br />
sigara böreği<br />
<strong>sıcak büfe:</strong><br />
dana külbastı<br />
kuru erikli tavuk<br />
patlıcan beğendi<br />
pilav ve salata<br />
<strong>tatlılar:</strong><br />
bal kabaklı cheese cake<br />
portakallı irmik helvası<br />
</p>
</div>
CSS:
#menuorta {
top:165px;
left:50px;
position:absolute;
z-index:0;
}
#menuorta2 {
top:127px;
left:20px;
position:absolute;
z-index:1;
}
#myazi {
color:#401c17;
font-weight:100;
font-family:Bookman Old Style;
font-size:13px;
padding:40px 65px;
max-height:200px;
overflow:hidden;
}
.mbaslik {
font-weight:bold;
font-size:16px;
}
Fixed :
background: url(../image/menuorta.png) no-repeat;
.. and /
Thank you very very very much to everyone. Especially to #ferne97
Set it to the width and height you want and add an overflow-y: scoll; to it.
#myazi {
width: 600px;
height: 200px;
overflow-y: scroll;
}
If you want to the background larger than the scroll area, place another div inside like this..
<div id="myazi">
<div class="inner-scroll">
<!-- content here -->
</div>
</div>
Then in the css add this..
#myazi {
background: url(path/image.png) no-repeat;
width: 800px; /* width of actual image */
height: 600px; /* height of actual image */
}
.inner-scroll {
width: 600px; /* width of content area */
height: 200px; /* height of content area */
overflow-y: scroll;
}
Please remove The following -
max-height :200px ;
This is setting the background image to 200px height .
And to improve the way your para is represented , you can utilize the css3 coloumn property to format it in better way . Scrolling is for me an unhealthy way to represent home page content.
Here's where you can see and learn to implement - http://www.quirksmode.org/css/multicolumn.html

Placeholder background/image while waiting for full image to load?

I have a few images on my page. I'm finding that the page starts to render before the images have been loading (which is good), but that the visual effect is not great. Initially the user sees this:
--------hr--------
text
Then a few milliseconds later the page jumps to show this:
--------hr--------
[ ]
[ image ]
[ ]
text
Is there a simple way that I can show a grey background image of exactly the width and height that the image will occupy, until the image itself loads?
The complicating factor is that I don't know the height and width of the images in advance: they are responsive, and just set to width: 100% of the containing div. This is the HTML/CSS:
<div class="thumbnail">
<img src="myimage.jpeg" />
<div class="caption">caption</div>
</div>
img { width: 100% }
Here's a JSFiddle to illustrate the basic problem: http://jsfiddle.net/X8rTB/3/
I've looked into things like LazyLoad, but I can't help feeling there must be a simpler, non-JS answer. Or is the fact that I don't know the height of the image in advance an insurmountable problem? I do know the aspect ratio of the images.
Instead of referencing the image directly, stick it within a DIV, like the following:
<div class="placeholder">
<div class="myimage" style="background-image: url({somedynamicimageurl})"><img /></div>
</div>
Then in your CSS:
.placeholder {
width: 300;
height: 300;
background-repeat: no-repeat;
background-size: contain;
background-image: url('my_placeholder.png');
}
Keep in mind - the previous answers that recommend using a div background approach will change the semantic of your image by turning it from an img into a div background. This will result in things like no indexing of these images by a search crawler, delay in loading of these images by the browser (unless you explicitly preload them), etc.
A solution to this issue (while not using the div background approach) is to have a wrapper div to your image and add padding-top to it based on the aspect ratio of the image that you need to know in advance. The below code will work for an image with an aspect ratio of 2:1 (height is 50% of width).
<div style="width:100%;height:0; padding-top:50%;position:relative;">
<img src="<imgUrl>" style="position:absolute; top:0; left:0; width:100%;">
</div>
Of course - the major disadvantage of this approach is that you need to know the aspect ratio of the image in advance.
There is a really simple thing to check before you start looking into lazy-loading and other JavaScript. Make sure the JPEG images you are loading are saved with the 'progressive' option enabled!
This will cause them to load the image iteratively, starting with a placeholder that is low-resolution and faster to download, rather than waiting for the highest resolution data before rendering.
It's very simple...
This scenario allows to load a profile photo that defaults to a placeholder image.
You could load multi CSS background-image into an element. When an avatar photo fails, the placeholder image appears default of div.
If you're using a div element that loads via a CSS background-image, you could use this style:
#avatarImage {
background-image: url("place-holder-image.png"), url("avatar-image.png");
}
<div id="avatarImage"></div>
Feel free to copy this:
<script>
window.addEventListener("load", function () {
document.getElementById('image').style.backgroundColor = 'transparent';
});
</script>
<body>
<image src="example.example.example" alt="example" id="image" style="background-color:blue;">
</body>
I got this from here: Preloader keeps on loading and doesnt disappear when the content is loaded.
Apart from all solutions already mentioned, the last solution would be to hide the document until everything is loaded.
window.addEventListener('load', (e) => {
document.body.classList.add('loaded');
});
body {
opacity: 0;
}
body.loaded {
opacity: 1;
}
<div id="sidebar">
<img src="http://farm9.staticflickr.com/8075/8449869813_1e62a60f01_b.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-1.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-2.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-3.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-4.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-5.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-6.jpg" />
</div>
Or show some animation while everything is loading:
window.addEventListener('load', (e) => {
document.body.classList.add('loaded');
});
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 70px;
height: 70px;
-webkit-animation: spin 2s linear infinite;
/* Safari */
animation: spin 2s linear infinite;
position: absolute;
left: calc(50% - 35px);
top: calc(50% - 35px);
}
#keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
body :not(.loader) {
opacity: 0;
}
body .loader {
display: block;
}
body.loaded :not(.loader) {
opacity: 1;
}
body.loaded .loader {
display: none;
}
<div class="loader"></div>
<div id="sidebar">
<img src="http://farm9.staticflickr.com/8075/8449869813_1e62a60f01_b.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-1.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-2.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-3.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-4.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-5.jpg" />
<img src="https://www.nla.gov.au/sites/default/files/pic-6.jpg" />
</div>
The only thing I can think of, to minimize the jump effect on your text, is to set min-height to where the image will appear, I would say - set it to the "shorter" image you know of. This way the jump will be less evident and you won't need to use lazyLoad or so... However it doesn't completely fix your problem.
Here's one naive way of doing it,
img {
box-shadow: 0 0 10px 0 rgba(#000, 0.1);
}
You can manipulate the values, but it creates a very light border around the image that doesn't push the contents. Images can load at whatever time they want, and you get a good user experience.
Here is what I did with Tailwind CSS, but it's just CSS:
img {
#apply bg-no-repeat bg-center;
body.locale-en & {
background-image: url("data:image/svg+xml;utf8,<svg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'><text x='50%' y='50%' style='font-family: sans-serif; font-size: 12px;' dominant-baseline='middle' text-anchor='middle'>Loading…</text></svg>");
}
body.locale-fr & {
background-image: url("data:image/svg+xml;utf8,<svg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'><text x='50%' y='50%' style='font-family: sans-serif; font-size: 12px;' dominant-baseline='middle' text-anchor='middle'>Chargement…</text></svg>");
}
}
You can find the width and height of the images in the developer tools console, for example in Chrome you can click the cursor icon in the developer tools console and when you hover on the page it will highlight all the properties of the elements in the page.
This will help you find the width and height of the images, because if you hover on top of your images it will give you the dimensions of the image and other more properties. You can also make an individual div for each image and make the div relative to the images width and height. You can do it like this:
The main div will contain the images and also the background-div which is below the image.
HTML:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div class=".mainDiv">
<div class="below"></div>
<img src="https://imgix.bustle.com/uploads/image/2020/2/13/da1a1ca4-95ec-40ea-83c1-4f07fac8b9b7-eqb9xdwx0auhotc.jpg" width="500"/>
</div>
</body>
</html>
CSS:
.mainDiv {
position: relative;
}
.below {
position: absolute;
background: #96a0aa;
width: 500px;
height: 281px;
}
img {
position: absolute;
}
The result will be that .below will be below the image and so when the image has trouble loading the user will instead see the grey .below div. You cannot see the .below div because it is hidden below the image. The only time you will see this is when the loading of the image is delayed. And this will solve all your problems.
I have got a way. But you will need to use JavaScript for it.
The HTML:
img = document.getElementById("img")
text = document.getElementById("text")
document.addEventListener("DOMContentLoaded", () => {
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAA1BMVEWIiIhYZW6zAAAASElEQVR4nO3BgQAAAADDoPlTX+AIVQEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwDcaiAAFXD1ujAAAAAElFTkSuQmCC";
text.innerHTML = "Loaded but image is not";
});
window.onload = function() {
img.src = "https://media.geeksforgeeks.org/wp-content/uploads/20190913002133/body-onload-console.png";
text.innerHTML = "Image is now loaded";
};
#img {
width: 400px;
height: 300px;
}
<hr>
<img id="img" src="https://media.geeksforgeeks.org/wp-content/uploads/20190913002133/body-onload-console.png">
<p>Here is the Image</p>
<p id="text">Not Loaded</p>

Firefox Sprite Sheet Image Not Showing Up

I'm trying to create a chat room with emoticons. When a user types an emoticon such as :) in the chatroom, the CSS takes the proper image off the sprite sheet. But my code only seems to work in Opera and IE (odd combination). It doesn't show in Firefox or Chrome (if you manage to hone in exactly where the image should be, you can find the image blank.gif which is used as the stand-in to place the background-url over). Does anyone know what might cause this in Firefox/Chrome?
Site: ttony21.byethost24.com
Relevant CSS:
image.emo {
width:19px;
height:19px;
}
image#smile {
background:url(img/diceSprite.png) 0 0;
}
image#bigsmile {
background:url(img/diceSprite.png) -19px 0;
}
etc...
Relevant HTML:
<img class="emo" id = "smile" src="img/blank.gif" width="1" height="1" alt=":)" title=":)" />
<img class="emo" id = "bigsmile" src="img/blank.gif" width="1" height="1" alt=":D" title=":D" />
etc...
change
image.emo {
width:19px;
height:19px;
}
to
img.emo {
width:19px;
height:19px;
}
and the others follow a similar pattern. The element selector is img, not image

Resources