I have a sprite-sheet with 4 social sprite buttons that I can't get to work as list items after many hours of researching.
Each sprite button should take up the same background-position as in the sprite-sheet. I have even tried setting each background-position: 0 0; without success.
Any suggestions would be appreciated.
.s-1-facebook,
.s-2-tweet,
.s-3-google,
.s-4-email {
<!--background-image: url('http://i.stack.imgur.com/tpoaF.png');-->
background-image: url('images/sprites.png');
background-repeat: no-repeat;
display: inline-block;
list-style: none;
margin: -12px 0 0 -16px;
position: relative;
}
.s-1-facebook {
display: inline-block;
list-style: none;
background-position: 0 0;
width: 32px;
height: 32px;
position: absolute;
}
.s-2-tweet {
display: inline-block;
list-style: none;
background-position: -32px 0;
width: 32px;
height: 32px;
position: absolute;
}
.s-3-google {
display: inline-block;
list-style: none;
background-position: -72px 0;
width: 32px;
height: 32px;
position: absolute;
}
.s-4-email {
display: inline-block;
list-style: none;
background-position: -108px 0;
width: 32px;
height: 32px;
position: absolute;
}
<ul>
<li class="s-1-facebook">
</li>
<li class="s-2-tweet">
</li>
<li class="s-3-google">
</li>
<li class="s-4-email">
</li>
</ul>
Tips for using sprites in CSS
Let's take the image you provided as example. The image has 140px width and 32px height.
Since we have 4 equally distributed icons in the image, we can assume that every icon will have 35px width (140 / 4) and 32px height.
In order to make the sprite work, we will have to assign those width/height values to an element of our choose, in your case, we will use the tag a inside every li.
To change the icon shown in every class, we will use the CSS property background-position.
Since we already know that our sprite have 4 columns and 1 row, we will only need to use the x position (using factors of 35).
Example
ul.social {
list-style: none;
margin: 0;
padding: 0;
}
ul.social li {
float: left;
}
ul.social a {
background-image: url('http://i.stack.imgur.com/tpoaF.png');
width: 35px;
height: 32px;
display: inline-block;
}
ul.social li.s-2-tweet a {
background-position: -35px 0;
}
ul.social li.s-3-google a {
background-position: -70px 0;
}
ul.social li.s-4-email a {
background-position: -105px 0;
}
<ul class="social">
<li class="s-1-facebook">
</li>
<li class="s-2-tweet">
</li>
<li class="s-3-google">
</li>
<li class="s-4-email">
</li>
</ul>
Your comment for the background image is using HTML comment tags which are invalid in CSS and causing your sprite image to fail. Remove that line and you can be on your way.
Related
I have a nav bar wit several items.
<ul>
<li class="fixedLeft"><a class="links-main" href="#">Left1</a></li>
<li class="left"><a class="links-main" href="#">Left2</a></li>
<li class="left"><a class="links-main" href="#">Left3</a></li>
<li class="right"><a class="links-main" href="#">Right1</a></li>
<li class="right"><a class="links-main" href="#">Right2</a></li>
<li class="fixedRight"><a class="links-main" href="#">Right3</a></li>
</ul>
I don't know how to fix the "fixedLeft" class to the left, and "fixedRight" class to the right, so that if i change the window size, or use it on a smaller window, those li are allways visible, the others are not important, if i can't see them i'll deal with that later.
(just to clear things up, i don't mean "float:left/right", i'm already using those.)
Right now when i change the size of the screen, the li start dissapearing right to left, How can i avoid the "fixedRight" item to dissapear.
(if possible, i'd like to implement the solution only with html and css)
I think the only way by using only pure CSS is with inline-block mode, like this example:
li {
width: 80px;
height: 20px;
list-style-type: none;
display: inline-block;
margin: 0;
padding: 0;
z-index: 100;
vertical-align: top;
font-size: 0;
position: relative;
margin-right: -4px;
}
a {
padding: 0;
margin: 0;
display: block;
font-size: 12px;
text-align: center;
text-decoration: none;
}
ul li:nth-child(1) {
position: absolute;
left: 0;
top: 0;
z-index: 200;
background-color: red;
}
ul li:nth-last-child(1) {
position: absolute;
right: 0;
top: 0;
z-index: 200;
background-color: red;
}
ul {
position: relative;
margin: 0;
padding: 0 80px;
width: calc(100% - 160px);
overflow: hidden;
white-space: nowrap;
background-color: yellow;
}
Here is the jsfiddle: https://jsfiddle.net/fabio1983/oc398jx9/
I am trying to center nav tag within the main-container div, but it seems to have a bit too much space on the right. I tried margin: 0 auto method as well as display: inline/inline-block and text-align on the child element - but it does not work.
body {
background: #000;
}
.main-container {
margin: 0 auto;
width: 90%;
overflow: hidden;
background: #fff;
}
.main-container nav {
width: 100%;
display: block;
margin: 10px 0;
}
.main-container nav ul {
list-style-type: none;
}
.main-container nav ul li {
width: 48%;
display: block;
float: left;
height: 300px;
margin: auto auto 10px 10px;
}
.main-container nav ul li.one {
background: url('http://media.npr.org/images/picture-show-flickr-promo.jpg');
background-size: cover;
overflow: hidden;
}
a {
display: block;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
text-align: center;
}
a span {
position: relative;
top: 40%;
font-size: 2em;
z-index: 1000;
}
<div class="main-container">
<nav>
<ul>
<li class="fruits"><span> One </span></li>
<li class="vegetables"><span> Two </span></li>
<li class="carbohydrates"><span> Three </span></li>
<li class="proteins"><span> Four </span></li>
<li class="junk-food"><span> Five </span></li>
<li class="health-tips"><span> Six </span></li>
</ul>
</nav>
</div>
Hi now used to this
.main-container nav ul{padding:0;}
or used to always css reset
Demo
The problem:
ul element has padding and margin by default. You have to set those property to 0.
Jsfiddle
.main-container nav ul {
list-style-type: none;
padding: 0;
margin: 0;
}
I am trying to implement background image sprite hover effect from this CSS-tricks article: http://css-tricks.com/fade-image-within-sprite/
My problem is that the images do not align completely in the following case:
HTML:
<ul class="contact">
<li class="phone"><a class="bg_hover" href="#">Call me</a>
</li>
<li class="twitter"><a class="bg_hover" href="#">Follow me on Twitter</a>
</li>
<li class="email"><a class="bg_hover" href="#">Email me</a>
</li>
</ul>
CSS:
.bg_hover {
position: relative;
}
.bg_hover:after {
content:"";
background-image: inherit;
background-position: bottom left;
background-repeat: no-repeat;
opacity: 0;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
transition: 1s;
}
.bg_hover:hover:after {
opacity: 1;
}
.contact {
margin-left: 60px;
}
.contact li {
float:left;
margin: 30px 15px 0 0;
font-style: italic;
}
.contact li a {
padding: 3px 0 0 25px;
height: 18px;
background-repeat: no-repeat;
display: block;
}
.contact .phone a {
background-image: url(http://i.imgur.com/9d9hdiL.png);
}
.contact .email a {
background-image: url(http://i.imgur.com/9d9hdiL.png);
}
.contact .twitter a {
background-image: url(http://i.imgur.com/9d9hdiL.png);
}
li {
list-style: none;
}
jsfiddle: http://jsfiddle.net/47Lngd4t/2/
Can you tell me where is the problem?
Your background-position isn't quite right. The image sprite you used is a pixel or two out. Change background-position to the following:
.bg_hover:after {
background-position: 0 -21px;
}
See demo here: http://jsfiddle.net/AndyMardell/47Lngd4t/3/
I changed
.contact li a {
padding: 3px 0 0 25px;
...
}
to
.contact li a {
padding: 2px 0 0 25px;
...
}
So, from 3px to 2px. Seems to align for me.
If You're talking about effect like here http://take.ms/a6ar2 simply add line-height for example 10px to .contact li a selector
There are also some other ways to get effect:
You can change padding of <a> elements
You can change background-position of image
I have a navigation which holds either text or images for links. I want the image to change on hover, so am using CSS backgrounds inside an empty div. However, I am looking for a way of doing this without using "position: absolute;" as the containing a tag will not expand to fill its dimensions. I would also like to do this without using a transparent placeholder image as I want to find a more elegant solution.
Here's the jsfiddle and the code:
http://jsfiddle.net/urhLs736/1/
<nav id="navigation">
<ul>
<li><a onclick="example1.html">PAGE 1</a></li>
<li><a onclick="example2.html">PAGE 2</a></li>
<li><div id="nav-image"></div></li>
</ul>
</nav>
and for the CSS:
#navigation {
z-index: 1;
background-color: #3A5E90;
color: #FFFFFF;
text-align: center;
width: 100%;
padding-top: 5px;
padding-bottom: 5px;
}
#navigation.fixed {
position: fixed;
top: 0px;
}
#navigation li {
display: inline;
}
#navigation a {
cursor: pointer;
padding-left: 3.5%;
padding-right: 3.5%;
color: #FFFFFF;
text-decoration: none;
}
#navigation a:hover {
background: #FFFFFF;
color: #3A5E90;
padding-top: 2%;
padding-bottom: 2%;
}
#nav-image {
display: inline;
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
margin-bottom: -6px;
height: 24px;
width: 100px;
}
#nav-image:hover {
height: 24px;
width: 100px;
background: url('https://avatars3.githubusercontent.com/u/5278945?v=2&s=96') no-repeat;
background-size: 100%;
}
I think that your layout will work as is with a minor adjustment to the CSS:
#nav-image {
display: inline-block;
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
margin-bottom: -6px;
height: 24px;
width: 100px;
border: 1px dotted yellow;
}
If you use display: inline-block, the div will take up the specified width and height and the background image will be visible, and the hover effect will work as you expect.
See demo: http://jsfiddle.net/audetwebdesign/9fd1dxn4/
In order to achieve this, you have to change both your HTML and your CSS.
First, your HTML should go like this:
<nav id="navigation">
<ul>
<li><a onclick="example1.html">PAGE 1</a>
</li>
<li><a onclick="example2.html">PAGE 2</a>
</li>
<li id="nav-image">PAGE 3
</li>
</ul>
</nav>
Note that I have added some content in your empty div. If you have an empty <li>, you'll have no background at all (just like your example) since you have a 0x0 pixels li element. I have added some content so the li displays as a general rule, which anyways won't be necessary after you see the CSS, which is the following:
#navigation {
z-index: 1;
background-color: #3A5E90;
color: #FFFFFF;
text-align: center;
width: 100%;
padding-top: 5px;
padding-bottom: 5px;
}
#navigation.fixed {
position: fixed;
top: 0px;
}
#navigation li {
display: inline-block;
height: 24px;
width: 100px;
padding-left: 3.5%;
padding-right: 3.5%;
}
#navigation ul li a {
cursor: pointer;
padding:2% 3.5%;
color: #FFFFFF;
text-decoration: none;
display:block;
height: 24px;
width: 100px;
}
#navigation a:hover {
background: #FFFFFF;
color: #3A5E90;
}
#nav-image {
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
}
#nav-image:hover {
background: url('https://avatars3.githubusercontent.com/u/5278945?v=2&s=96') no-repeat;
background-size: 100%;
}
#nav-image:hover a {
background:transparent
}
OK, now you see I have made some changes and added width and height to the li (the same you had in your sample, but you can change it to anything you want). Now, if you delete the content inside the empty DIV, you'll see how the rendering changes. While it's very easy to solve, I'll leave it to you so you can practice and understand how the whole positioning and display thing works. Also, you can add paddings, margins, et
Here you have a fiddle so you can see it in action and play around
Please take a look at this fiddle: http://jsfiddle.net/SkjHs/4/
<html>
<ul class="langBar">
<li> <img src="http://goo.gl/aIxpv"> </li>
<li> <img src="http://goo.gl/wIQob"> </li>
<li> <img class="activeLang" src="http://goo.gl/If4lA"> </li>
</ul>
</html>
html {
background-color:#000;}
.langBar{
display: block;
overflow: hidden;
float: left;
width: 125px;
}
.langBar li{
display: block;
width: 32px;
height: 40px;
float: left;
margin-left: 7px;
padding: 0px;
}
.activeLang{
-moz-border-radius: 20px;
width: 32px;
height: 32px;
border-radius: 20px;
border: 2px solid #482663;
}
.langBar li a{
display: block;
height: 100%;
width: 100%;
}
I'm trying to achieve some nice glow effect around activeLang class image. First problem is, i'm getting padding between border and image itself. Second can't get glow effect. Any suggestions?
First of all the padding of the image is because of image canvas My Fiddle
Image with canvas cropped
Add the below CSS to .activeLang for glow like effect to your image...(Ofcourse you can change colors as per your choice)
box-shadow: 0 0 3px 3px #888;
And Remove from .activeLang
width: 32px;
height: 32px;
Such a glow effect could be realized via a box-shadow:
/* pink glow effect */
box-shadow: 0 0 5px 5px #f3a;
See http://jsfiddle.net/SkjHs/8/ for an example with glow effect.
Or (as I would set it up) a solution without using img-tags:
<ul class="langBar">
<li>[AZ]</li>
<li>[EN]</li>
<li>[RU]</li>
</ul>
.langBar {
overflow: hidden;
}
.langBar li{
display: block;
float: left;
margin-left: 7px;
padding: 0px;
}
.icon {
display: inline-block;
width: 32px;
height: 32px;
margin: 5px 0;
overflow: hidden;
text-indent: 110%;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
}
.icon:focus, .icon:active, .icon:hover,
.icon.active {
-webkit-box-shadow: 0 0 5px 5px #f3a;
-moz-box-shadow: 0 0 5px 5px #f3a;
box-shadow: 0 0 5px 5px #f3a;
}
/* one may use a sprite and only set the background position here */
.icon-az {
background-image: url(http://goo.gl/aIxpv);
}
.icon-en {
background-image: url(http://goo.gl/wIQob);
}
.icon-ru {
background-image: url(http://goo.gl/If4lA);
}
http://jsfiddle.net/SkjHs/10/
Even when using border-radius, a border won't go inside the element. The few pixels at the edge of each image file force the border outside of the content of your image.
I recommend editing your image to remove the spacing, or even to add all of the effects you want on rollover.
As an example of spriting making shorter work of the issue:
<ul class="langBar">
<li><a id="az" href="?lang=az&page=main"></a></li>
<li><a id="en" href="?lang=en&page=main"></a></li>
<li><a id="ru" href="?lang=ru&page=main" class="activeLang"></a></li>
</ul>
html {
background-color:#000;}
.langBar{
display: block;
overflow: hidden;
float: left;
width: 125px;
}
.langBar li {
display: block;
width: 32px;
height: 32px;
float: left;
margin-left: 7px;
padding: 0px;
}
.langBar li a {
background: url(https://lh5.googleusercontent.com/orZ4dkDz2lBVJMB7D0Pb2fBHB8JPLcD8r2xqSYw-e3K7K2G427Ws_iqNbcYF1U2X36ju1y3eVy0) no-repeat 0 0;
display:block;
width:32px;
height:32px;
}
.langBar li a#az {
background-position:0 0;
}
.langBar li a#en {
background-position:0 -32px;
}
.langBar li a#ru {
background-position:0 -64px;
}
.langBar li a.activeLang,
.langBar li a#az.activeLang,
.langBar li a#en.activeLang,
.langBar li a#ru.activeLang {
background-position-x: -46px;
}
Thought this was a little lengthy, which is why I put this in a jsFiddle:
http://jsfiddle.net/mori57/n3Q74/
Hope this helps!