How to change only the y-as on hover? - html

I use a image sprite for each post type. I call a image by using:
The markup that I am using is:
.format {
height: 60px;
width: 60px;
margin: 0 auto;
float:left;
background: #d7354a url(http://i.imgur.com/sVTJJMV.png) no-repeat 0 top;
}
span.format:hover {
background: #FFF url(http://i.imgur.com/sVTJJMV.png) no-repeat 0 bottom;
}
.format.image { background-position: 0 0; }
.format.location { background-position: -60px 0; }
.format.text { background-position: -120px 0; }
.format.profile { background-position: -180px 0; }
.format.sound { background-position: -240px 0; }
.format.link { background-position: -300px 0; }
<span class="format image"></span>
<span class="format location"></span>
<span class="format text"></span>
As you can see it will display three icons. When you hover over one span, the background change to white and the icon to red. The only problem is that .format:hover use the same x-as as for the normal state. I can create for each format type an hover state css, but I was wondering if it is also possible with only one hover, where the x-as will be the one of that format. Is this possible?
Thanks in advance.

When you update the background property on hover, you will actually have to redeclare all the x- and y- positions for each link element individually. background is simply a shorthand property and therefore will not intelligently "merge" with any exisitng background property values, but override it.
You can of course use background-position-y to override only the y positioning of the element, but it is not supported in some browsers. It is only recently (ca. 2014) introduced to the W3C specifications. An alternative would be using pseudo-elements, but I consider the solution posted by #nicooga as the best.
With regards to the pseudo-element solution, you can do it as follow:
The trick is to double the height of the pseudo-element (using bottom: -100%), and then moving the pseudo-element upwards by its own height (using top: -100%) on hover. In that case, you will only have to declare your background positions once for all your elements.
.format {
height: 60px;
width: 60px;
margin: 0 auto;
float: left;
position: relative;
overflow: hidden;
}
.format::before {
background: #d7354a url(http://i.imgur.com/sVTJJMV.png) no-repeat 0 top;
content: '';
display: block;
position: absolute;
top: 0;
bottom: -100%;
left: 0;
right: 0;
}
span.format:hover::before {
background-color: #fff;
top: -100%;
bottom: 0;
}
.format.image::before {
background-position: 0 0;
}
.format.location::before {
background-position: -60px 0;
}
.format.text::before {
background-position: -120px 0;
}
.format.profile::before {
background-position: -180px 0;
}
.format.sound::before {
background-position: -240px 0;
}
.format.link::before {
background-position: -300px 0;
}
<span class="format image"></span>
<span class="format location"></span>
<span class="format text"></span>

The background property is a short hand to set a bunch of other properties.
When you use it again in span.format:hover you are overriding some of the properties that make it look like you want. Just change the properties you need.
Use this as a rule of thumb: if you are repeating code you are probably doing something wrong.
http://www.w3schools.com/cssref/css3_pr_background.asp
.format {
height: 60px;
width: 60px;
margin: 0 auto;
float:left;
background-image: url("http://i.imgur.com/sVTJJMV.png");
background-color: rgb(215, 53, 74);
}
span.format:hover {
background-position-y: 100%;
background-color: rgb(255, 255, 255);
}
.format.image { background-position: 0 0; }
.format.location { background-position: -60px 0; }
.format.text { background-position: -120px 0; }
.format.profile { background-position: -180px 0; }
.format.sound { background-position: -240px 0; }
.format.link { background-position: -300px 0; }
<span class="format image"></span>
<span class="format location"></span>
<span class="format text"></span>

Related

How can I fill a text with an image in HTML? [duplicate]

This question already has answers here:
Transparent text cut out of background
(14 answers)
Cut-out text with CSS
(3 answers)
How to apply inverse text mask with CSS
(4 answers)
Cut out text in CSS. Possible? [duplicate]
(1 answer)
transparent text but opaque background in css [duplicate]
(1 answer)
Closed 3 years ago.
Let's say I have an image like this:
and I want to fill an <h1> tag with that image to look something like this:
I guess I could do something like this to start...
div {
width: 100%;
height: 100%;
background: url(https://i.stack.imgur.com/n8gtc.jpg) 50% 0 no-repeat;
background-size: cover;
}
h1 {
color: white;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
<div>
<h1>404</h1>
</div>
Should I have another 'absolute' div white covering the image and a transparent font over? Does anyone know how to accomplish this?
background-clip might be an option
https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip
The background-clip CSS property sets whether an element's background extends underneath its border box, padding box, or content box.
div {
width: 100%;
height: 100%;
}
h1 {
color: transparent;
font-size: 28vmax;
background-size: cover;
background: url(https://i.stack.imgur.com/n8gtc.jpg) 50% 0 no-repeat;
background-clip: text;
}
<div>
<h1>404</h1>
</div>
or mix-blend-mode:
https://css-tricks.com/almanac/properties/m/mix-blend-mode/
The mix-blend-mode property defines how an element’s content should blend with its background. This means that any images or text, borders or headings will be influenced by this property.
also
https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode
The mix-blend-mode CSS property sets how an element's content should blend with the content of the element's parent and the element's background.
* {
margin: 0;
}
div {
width: 100%;
height: 100vh;
background: url(https://i.stack.imgur.com/n8gtc.jpg) 50% 0 no-repeat;
background-size: cover;
}
h1 {
font-size: 15vmax;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
color: black;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
background: white;
mix-blend-mode: screen;
}
<div>
<h1>404</h1>
</div>
You can try using -webkit-background-clip: text;
This is called Knockout Text but you should be able to get the outcome you want with it.
Here are a few examples, but it might be easier to do it in photoshop and then insert the image. (Just an alternative)
/*
Based from this article from Divya Manian -
http://nimbupani.com/using-background-clip-for-text-with-css-fallback.html
*/
* {
margin: 0;
padding: 0;
}
*,
:before,
:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html,
body {
min-height: 100%;
}
body {
font-family: 'Oswald', sans-serif;
color: #fff;
background-color: #000;
}
.wrapper {
text-align: center;
}
.title {
font-size: 2em;
position: relative;
margin: 0 auto 1em;
padding: 1em 1em .25em 1em;
text-align: center;
text-transform: uppercase;
}
.title:after {
position: absolute;
top: 100%;
left: 50%;
width: 240px;
height: 4px;
margin-left: -120px;
content: '';
background-color: #fff;
}
/* Clip text element */
.clip-text {
font-size: 6em;
font-weight: bold;
line-height: 1;
position: relative;
display: inline-block;
margin: .25em;
padding: .5em .75em;
text-align: center;
/* Color fallback */
color: #fff;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.clip-text:before,
.clip-text:after {
position: absolute;
content: '';
}
/* Background */
.clip-text:before {
z-index: -2;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-image: inherit;
}
/* Text Background (black zone) */
.clip-text:after {
position: absolute;
z-index: -1;
top: .125em;
right: .125em;
bottom: .125em;
left: .125em;
background-color: #000;
}
/* Change the background position to display letter when the black zone isn't here */
.clip-text--no-textzone:before {
background-position: -.75em 0;
}
.clip-text--no-textzone:after {
content: none;
}
/* Use Background-size cover for photo background and no-repeat background */
.clip-text--cover,
.clip-text--cover:before {
background-repeat: no-repeat;
-webkit-background-size: cover;
background-size: cover;
background-position: 50% 50%;
}
/* Background image from http://thepatternlibrary.com/ and http://lorempixel.com */
.clip-text_one {
background-image: url(https://picsum.photos/480/200?random);
}
.clip-text_two {
background-image: url(https://picsum.photos/480/200?grayscale);
}
.clip-text_tree {
background-image: url(https://picsum.photos/480/200?grayscale&random=2);
}
.clip-text_four {
background-image: url(https://picsum.photos/480/200?grayscale&blur=3);
}
.clip-text_five {
background-image: url(https://picsum.photos/480/200?grayscale);
}
.clip-text_six {
background-image: url(https://picsum.photos/480/200?random=3);
}
.clip-text_seven {
background-image: url(https://picsum.photos/480/200?random=4);
}
.clip-text_eight {
background-image: url(https://picsum.photos/480/200?random=6);
}
.clip-text_nine {
background-image: url(https://picsum.photos/480/200?random=5);
}
.clip-text_ten {
background-image: url(https://picsum.photos/480/200?random=7);
}
.clip-text_eleven {
background-image: url(https://picsum.photos/480/200?random=8);
background-size: cover;
}
.clip-text_twelve {
background-image: url(https://picsum.photos/480/200?random=9);
}
.clip-text_thirteen {
background-image: url(https://picsum.photos/480/200?random=10);
}
.clip-text_fourteen {
background-image: url(https://picsum.photos/480/200?random=11);
}
.clip-text_fifteen {
background-image: url(https://picsum.photos/480/200?random=12);
}
<div class="wrapper">
<p class="title">Play with background-clip text</p>
<div class="clip-text clip-text_one">TEST</div>
<div class="clip-text clip-text_fifteen clip-text--no-textzone">TEST</div>
<div class="clip-text clip-text_twelve clip-text--cover">TEST</div>
<div class="clip-text clip-text_tree clip-text--no-textzone">TEST</div>
<div class="clip-text clip-text_two">TEST</div>
<div class="clip-text clip-text_fourteen clip-text--cover">TEST</div>
<div class="clip-text clip-text_tree">TEST</div>
<div class="clip-text clip-text_eleven clip-text--cover">TEST</div>
<div class="clip-text clip-text_four">TEST</div>
<div class="clip-text clip-text_five">TEST</div>
<div class="clip-text clip-text_six">TEST</div>
<div class="clip-text clip-text_seven">TEST</div>
<div class="clip-text clip-text_eight">TEST</div>
<div class="clip-text clip-text_nine">TEST</div>
<div class="clip-text clip-text_ten">TEST</div>
<div class="clip-text clip-text_thirteen clip-text--cover">TEST</div>
</div>
Let me know if you have any questions! The implementation is fairly straight forward I made sure to include options for you to look at, here is a link for more information:
https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip

How to use css sprite correctly?

I am trying to move a piece of text on a specific part of image using css sprites.But the background position I am applying doesn't seem to work. I have tried changing the background position but the text part(i.e. twitter, facebook) doesn't move to the correct place.
#fixedsocial {
background:url("../img/socials/icon.png") no-repeat;
top:40%;
width:50px;
height: 100px;
position:fixed;
left: 0;
display: block;
z-index: 1000;
background-color: #eee;
text-indent:-9999px;
}
.facebookflat {
background-position: -200px 0;
height:50px;
}
.facebookflat:hover {
cursor: pointer;
}
.twitterflat {
height:50px;
background-position: -400px 0;
}
.twitterflat:hover {
cursor: pointer;
}
<div id="fixedsocial">
<div class="facebookflat" id="shareBtn"></div>
<div class="twitterflat"> </div>
</div>
The reason it's not working is that you assign the background image to the container-div (#fixedsocial), and try to adjust the positioning on the inner-divs, which has no background to position. You need to rethink your CSS a bit, for one you have to assign the background where you actually want to use it.
Here's a fiddle: https://jsfiddle.net/j7kyhgmc/
#fixedsocial {
top:40%;
width:50px;
height: 100px;
position:fixed;
left: 0;
display: block;
z-index: 1000;
background-color: #eee;
}
.facebookflat {
background: url("https://i.stack.imgur.com/LR6bK.png") no-repeat;
background-position: -168px -161px;
background-size: 1430% 1430%;
height:50px;
width: 50px;
}
.facebookflat:hover {
cursor: pointer;
}
.twitterflat {
background: url("https://i.stack.imgur.com/LR6bK.png") no-repeat;
background-position: -430px -161px;
background-size: 1420% 1420%;
height:50px;
width: 50px;
}
.twitterflat:hover {
cursor: pointer;
}
<div id="fixedsocial">
<div class="facebookflat" id="shareBtn"></div>
<div class="twitterflat">
</div>
</div>

Transparent triangle with fixed background

When pages have fixed background scrolling (not parallax), is there a way to implement a triangle in the second row but have it transparent so that it shows the fixed background?
i.e.
-------------------------
this is fixed background
----------- -----------
\ /
second row solid colour
-------------------------
The second row would have a solid colour except the triangle where the background is the fixed image background. is there a way to do this?
DEMO: http://jsbin.com/tidir/1/
http://jsbin.com/tidir/1/edit
HTML
<section class="featurette"></section>
CSS:
body,html {background:#222;height:100%;padding:0;margin:0;}
.featurette {
background: url(http://lorempixel.com/700/400/cats/);
background-size: cover;
background-attachment: fixed;
background-position: center center;
width: 100%;
height:400px;
position: relative;
overflow:hidden;
}
.featurette:before,
.featurette:after {
content: '';
display: block;
position: absolute;
bottom: 0;
height: 70px;
margin: 0 0 0 -40px;
transform: skew(40deg);
background: #222;
}
.featurette:after {
left: 50%;
right: 0;
margin: 0 -40px 0 0;
transform: skew(-40deg);
}
.featurette:before {
left: 0;
right: 50%;
}
/* demo only */
body {height:2000px;}

Arranging buttons side by side

please help me arrange these 3 buttons horizontally like this photoshopped image:
Please take a look at the code here.
CSS:
a.facebookbt {
background: url(http://i1068.photobucket.com/albums/u445/neobx/bonus5.png) no-repeat 0 0;
width: 132px;
height: 52px;
display: block;
}
a.facebookbt:hover { background-position: 0 -52px; }
a.facebookbt:active { background-position: 0 -104px; }
a.twitterbt {
background: url(http://i1068.photobucket.com/albums/u445/neobx/bonus5.png) no-repeat -132px 0;
width: 132px;
height: 52px;
display: block;
}
a.twitterbt:hover { background-position: -132px -52px; }
a.twitterbt:active { background-position: -132px -104px; }
a.abpbt {
background: url(http://i1068.photobucket.com/albums/u445/neobx/bonus5.png) no-repeat -265px 0;
width: 286px;
height: 50px;
display: block;
}
a.abpbt:hover { background-position: -265px -52px; }
a.abpbt:active { background-position: -265px -104px; }​
HTML:
<a class="facebookbt" href="javascript:;"></a>
<a class="twitterbt" href="javascript:;"></a>
<a class="abpbt" href="javascript:;"></a>​
a { float:right; margin-left: 5px;}
and change the html markup to this way:
<a class="abpbt" href="javascript:;"></a>
<a class="twitterbt" href="javascript:;"></a>
<a class="facebookbt" href="javascript:;"></a>
This way the layout will be aligned to the right, and in the correct order like in your screenshot.
Demo: http://jsfiddle.net/FzYkJ/12/
How about:
a {
float:left;
}
​
or
a {
display:inline-block !important;
}
You can add
float:left;
to each button
http://jsfiddle.net/FzYkJ/2/

background-image css, image wont show up

I am making a yatzy game, and trying to put the dices in each div box but it wont show up.
You can wath my code at jsfiddle: http://jsfiddle.net/bneRe/
OR
Here is the HTML:
<div id="dice" title="Click on a dice to lock or unlock it">
<div class="dice1"><div></div></div>
<div class="dice2"><div></div></div>
<div class="dice3"><div></div></div>
<div class="dice4"><div></div></div>
<div class="dice5"><div></div></div>
<div class="dice6"><div></div></div>
</div>
And here is the CSS:
#dice {
height: 40px;
width: 240px;
}
#dice div {
height: 40px;
width: 40px;
float: left;
}
.dice1 { background-image:url('http://imm.io/9YdM.jpg');
background-position: 0 0;
}
.dice2 { background-image:url('http://imm.io/9YdY.jpg');
background-position: 0 0;
}
.dice3 { background-image:url('http://imm.io/9Ye4.jpg');
background-position: 0 0;
}
.dice4 { background-image:url('http://imm.io/9Ye9.jpg');
background-position: 0 0;
}
.dice5 { background-image:url('http://imm.io/9Yed.jpg');
background-position: 0 0;
}
.dice6 { background-image:url('http://imm.io/9Yef.jpg');
background-position: 0 0;
}
The URLs you are providing aren't actual images. They are pages with images. Use: http://i.imm.io/9YdY.jpeg, ..., etc instead:
#dice {
height: 40px;
width: 240px;
}
#dice div {
height: 40px;
width: 40px;
float: left;
}
.dice1 { background-image:url('http://i.imm.io/9YdM.jpeg');
background-position: 0 0;
}
.dice2 { background-image:url('http://i.imm.io/9YdY.jpeg');
background-position: 0 0;
}
.dice3 { background-image:url('http://i.imm.io/9Ye4.jpeg');
background-position: 0 0;
}
.dice4 { background-image:url('http://i.imm.io/9Ye9.jpeg');
background-position: 0 0;
}
.dice5 { background-image:url('http://i.imm.io/9Yed.jpeg');
background-position: 0 0;
}
.dice6 { background-image:url('http://i.imm.io/9Yef.jpeg');
background-position: 0 0;
}
After you make that change, note that each div isn't big enough to hold a full die's image. The images are 111x111 pixels, and your divs are 40x40 pixels.