CSS Sprite: Setting sprite image for several categories - css

I am currently working with css sprite images for my site. But I am running into css styling issues. I have 6 categories with each having an image beside. When applying the image sprite I get repeated images of the same object.
How can I get each category with their respective image through css & sprite? If you like to look at the live examples below are the links.(ps sorry for the horrible html structure)
Thank you
How it looks:
Sprite Images: Click Here
Individual Images: Click Here
Desired Outcome:
CSS part related
<style>
#index_categories {
background: #fff;
float: left;
width: 255px;
height: 125px;
margin: 10px 15px 10px 40px;
padding: 5px;
font-size: 97%;
vertical-align: middle;
background: url( http://webprolearner2346.zxq.net/css-test/images/categories.png) no-repeat top left;
}
.index_thumb {
padding: 5px;
}
.index_categories_list {
text-indent: 5px;
padding-left: 10px;
}
.index_cat {
list-style-type: none;
}
#listing {
background: #fff;
width: 95%;
height: 115px;
padding: 10px;
margin-bottom: 10px;
margin-right: 5px;
margin-left: 5px;
margin-top: 5px;
}
.listing_pic {
float: left;
padding: 10px;
}
#whitebox {
background: #fff;
width: 95%;
min-height: 200px;
padding: 10px;
margin-bottom: 10px;
margin-right: 5px;
margin-left: 5px;
margin-top: 5px;
}
.sprite-books{ background-position: 0 0; width: 87px; height: 87px; }
.sprite-tshirt{ background-position: 0 -137px; width: 87px; height: 87px; }
.sprite-stereo{ background-position: 0 -274px; width: 83px; height: 83px; }
.sprite-chair{ background-position: 0 -407px; width: 87px; height: 87px; }
.sprite-key{ background-position: 0 -544px; width: 83px; height: 83px; }
.sprite-cake{ background-position: 0 -677px; width: 87px; height: 87px; }
</style>
HTML snippet
<div class="contentPost" style="height:900px;">
<div id="index_categories" class="sprite-books">
<h3><a class="frontLinks" href="#">Category 1</a></h3>
<ul class="index_cat">
<li>Books</li>
</ul>
</div>
<div id="index_categories " class="sprite-stereo">
<h3><a class="frontLinks" href="#">Category 2</a></h3>
<ul class="index_cat">
<li>Stereo</li>
</ul>
</div>
<div id="index_categories" class="sprite-chair">
<h3><a class="frontLinks" href="#">Category 3</a></h3>
<ul class="index_cat">
<li>Chair</li>
</ul>
</div>
</div>

CSS Sprite (Google) is a big image (most probably .png) consisting out all of your required images(6 in yoyr case), in a grid-like structure. Benefit is that you will need only One HTTP request instead of NHTTP requests (where n is your number of images).
Your problem looks like the CSS Styling. But if you want to use sprites, you need one master image (sprite.png) having transparent background and having width of x*n (where X= width of your one div/category, as you shown in Desired Outcome in your question. N= number of total images, 6 here.) The height of sprite.png should be equal to the tallest of your images.
So, copy all your images stero.png, books.png into a new sprite.png as described above.
Then you could use following HTML:
<div id="wrapper">
<div class="row">
<div id="books" class="cell">
</div><!--//books-->
<div id="stereo" class="cell">
</div><!--//stereo-->
<div id="pen" class="cell">
</div><!--//pen-->
</div><!--//row-->
<div class="row">
<div id="mag" class="cell">
</div><!--//mag-->
<div id="bag" class="cell">
</div><!--//bag-->
<div id="paper" class="cell">
</div><!--//paper-->
</div><!--//row-->
</div><!--//wrapper-->
CSS:
#wrapper{
margin: 5px auto; // to center the div on page
width: 80%;
}
.row{ //stack'em in 3x2 grid
clear:both;
}
.cell{
background-color: #fefefe;
background-url: images/sprite.png; //Set common background UR
float: left; //stack each other to left
height: 15px; //desired height
width: 30px; //desired width
border: 3px solid #c3c3c3; //set border
}
//set specific background-position for each div
//(Remember we are using same image for all these divs?
#books{ background-position: -0px -0px; } //image starts at top:0, left:0px
#stereo{ background-position: -0px -30px; } //image starts at top:0, left:30px
#pen{ background-position: -0px -60px; } //image starts at top:0, left:60px
#mag{ background-position: -0px -90px; }
#bag{ background-position: -0px -120px; }
#paper{ background-position: -0px -150px; }
You could also use your existing 6 images to achieve your desired result. Just set a required height,width of the div. Set background-position:top-left;. If the image is smaller that the div's dimensions, the image will be placed in top-left corner.
EDIT:
Ok, saw your live-page's code. You already are using a sprite file.
From seeing your How It looks & Desired pages, looks like you want to add that extra white-space around the right-bottom side of image. Right?? If yes, you could add the required white-space in the categories.png and use background-position:top-left; in CSS. It will prevent the image to repeat in the horizontal side.

Related

Icons separation and horizontal line behind

I search any problem but without results. Have big problem with separation of icons in inline-block after add css spearation ::after and ::before in the middle icon and with rwd of it.here's my code.
HTML:
<div class="icon-box">
<div class="icon_1"></div>
<div class="icon_2"></div>
<div class="icon_3"></div>
</div>
CSS:
.icon_1 {
float: left;
display: inline-block;
height: 66px;
width: 66px;
background-image: url("../img/timing_icon.png");
background-color: #97735e;
background-position: center;
background-repeat: no-repeat;
}
.icon_2 {
float: left;
display: inline-block;
height: 66px;
width: 66px;
background-image: url("../img/presentation_icon.png");
background-color: #97735e;
background-position: center;
background-repeat: no-repeat;
}
.icon_3 {
float: left;
display: inline-block;
height: 66px;
width: 66px;
background-image: url("../img/accurate_icon.png");
background-color: #97735e;
background-position: center;
background-repeat: no-repeat;
}
.icon_2::before,
.icon_2::after {
display: inline-block;
content: "";
height: 10px;
border-bottom: 1px solid #97735e;
width: 100%;
}
.icon_2::before {
right: 100%;
margin-right: 10%;
}
.icon_2::after {
left: 100%;
margin-left: 200%;
}
I think my code is very messy and got to fix.
effect,what I want get enter image description here
first. you have common styles for all the .icon_1,.icon_2,.icon_3 divs except the background-image . so, give a common class icon for example, to all those divs, or, if you can't change the HTML , use .icon_1,.icon_2,.icon_3 instead of .icon in my example below. but don't write the same styles 3 times.
second. either use display:inline-block either use float:left . i chose to use float:left; because inline-block adds additional whitespaces between elements. ( if you want to use inline-block let me know . there are ways to override the whitespaces . )
third. add margin:0 15px to .icon_2 which adds margin-left and margin-right of 15px
fourth. add the pseudo-element ( :before in my example ) to the div that wraps the three icons ,respectively .icon-box . and style it as you want.
i added z-index:-1 so that the line ( :before ) goes under the icons.
added background:red to it so you can see that it goes under the icons.
.icon {
float:left;
height: 66px;
width: 66px;
background-color: #97735e;
background-position: center;
background-repeat: no-repeat;
position:relative;
}
.icon_1 {
background-image: url("../img/timing_icon.png");
}
.icon_2 {
background-image: url("../img/presentation_icon.png");
margin:0 15px;
}
.icon_3 {
background-image: url("../img/accurate_icon.png");
}
.icon-box { position:relative;width:auto;float:left;}
.icon-box:before {
content:"";
position:absolute;
height:1px;
background:red;
top:50%;
width:100%;
left:0;
z-index:-1;
}
<div class="icon-box">
<div class="icon icon_1"></div>
<div class="icon icon_2"></div>
<div class="icon icon_3"></div>
</div>

Can you make a layer clickable? Going to a URL?

Can you make a layer clickable, going to a url?
Here's my layer, with a background picture. I want my users to click it to navigate.
CSS:
#content-2-2
{
float: left;
width: 269px;
height: 103px;
margin:0px 20px 0px 0px;
padding: 0px;
background: url('../images/Guided-tour-logo-front.png') no-repeat left top;
}
HTML:
<div id="content-2-2"></div>
Regards,
Tea
You could add an anchor:
<div id="content-2-2"></div>
#content-2-2
{
float: left;
width: 269px;
height: 103px;
margin:0px 20px 0px 0px;
padding: 0px;
background: url('../images/Guided-tour-logo-front.png') no-repeat left top;
}
#content-2-2 a
{
width: 269px;
height: 103px;
display:block;
}
You can't via css...try using something like:
<div id="content-2-2" onclick="location.href='http://www.example.com'"></div>
It's javascript :)
You can try this:
For example:
<div id="facebook" onclick="location.href='your_link.html'">
and then in your CSS:
#facebook{
cursor: pointer;
/* The rest of your styling to that div */
}
HTML5 Allows for anchors to wrap block elements. This should work if you are using HTML5:
<a href="link.html">
<div id="content-2-2">my content</div>
</a>

Div overlap not correct

I'm having a problem with making one div overlap the rest of the page.
I just need one image to overlap one section. I have kinda got it to work but once you resize the window or look at it on a different resolution the image doesn't appear where it should.
I'm using an position:absolute; and z-index. It is working to some extent. but it won't stay in that position, for example, if you resize your browser window (it moves from where I'd like it to stay).
Here is this website
I need it to overlap the yellow box like this.
Edit: Just a quick follow up: I think your solution has put me a bit of bother. I am unable to place another div directly under it as can be seen here
Move
<div id="medal"><img src="images/star2012medal.png" width="220" height="277"></div>
inside
<div id="box"><img src="images/boxheading.png"></div>
just before the image.
Change the CSS to
#medal {
position: relative;
top: -240px;
right: -80px;
z-index: 50;
}
and apply the following to the boxheading.png image
{
position: relative;
top: -280px;
}
EDIT:
From what I feel you are trying to achieve, you should be looking at a 2-column layout. There's too many good-practice resources online to learn how to do it.
To add another box below the first one, you will need to do the following changes to html:
<div id="box-container">
<div id="box">
<div id="medal">
<img src="images/star2012medal.png" width="220" height="277">
</div>
<img src="images/boxheading.png" width="291px" height="240px" style="position: relative; top: -280px; ">
</div>
<div id="box2">testing</div>
</div>
then add the following css:
#box-container {
float: right;
}
#box {
float: left;
color: #333;
background: #fff;
height: 240px;
width: 291px;
display: inline;
border-style: solid;
border-color: #fff100;
-moz-border-radius: 10px;
border-radius: 10px;
clear: both;
}
#box2 {
float: left;
color: #333;
background: #fff;
height: 240px;
width: 291px;
display: inline-block;
border-style: solid;
border-color: #fff100;
-moz-border-radius: 10px;
border-radius: 10px;
clear: both;
margin-top: 10px;
}
tested only in Chrome. Remember to test it in other browsers!

CSS problem, creating tabs

I have a CSS problem that I'm not able to figure out. I'm not even sure it is possible. What I want is the following:
I have three buttons/tabs like this http://sv.tinypic.com/r/21cf85t/6 and when you click one tab a different div should show for each tab like this http://sv.tinypic.com/r/21l5y85/6 or http://sv.tinypic.com/r/2dbrv5u/6.
I know how to show/hide the divs with jQuery but the problem is that the divs will increase in height http://sv.tinypic.com/r/k2xxfb/6 and then they will push the other tabs and divs down. Is there a way to create what I am trying to do?
I'm not a guru in CSS so if you have an example to look at or can post code here I would be very very thankful!
This is the HTML I'm using for my tabs:
<div class="MainContent">Content</div>
<div class="TabsHolder">
<div id="Tab1">
<div style="width:200px">
Content Tab 1
</div>
</div>
<a class="Button1" href="#Tab1"></a>
<div class="clearer"></div>
<div id="Tab2">
<div style="width:200px">
Content Tab 2
</div>
</div>
<a class="Button2" href="#Tab2"></a>
</div>
CSS:
.MainContent {
float: left;
}
.TabsHolder
{
float: left;
}
.Button1
{
float: left;
margin: 100px 0px 20px 0px;
background: url(images/Button1.png) no-repeat 0 0;
height: 79px;
width: 27px;
}
#Tab1
{
width: 200px;
margin: 80px 0px 20px 0px;
border: solid 1px #ACCD45;
position: relative;
float: left;
overflow: hidden;
padding: 20px;
}
.Button2
{
float: left;
margin: 0px 0px 20px 0px;
background: url(images/Button2.png) no-repeat 0 0;
height: 97px;
width: 27px;
}
#Tab2
{
width: 200px;
margin: 0px 0px 20px 0px;
border: solid 1px #ACCD45;
position: relative;
float: left;
overflow: hidden;
padding: 20px;
}
div.clearer
{
clear: both;
margin: 0px;
margin-bottom: 0px;
margin-top: 0px;
padding: 0px;
line-height: 0px;
height: 0px;
width: 0px;
overflow: hidden;
}
Here is what I put together using pure CSS - Tested in Firefox, IE8 and Chrome (not sure about others). Try out a demo here.
Note: I wanted to make a comment about one thing in your original HTML - you can't add a background image to a link <a> tag.
CSS
.MainContent {
float: left;
width: 400px;
height: 400px;
background: #444;
}
.buttons {
float: left;
margin: 10px 0 10px 0;
width: 27px;
clear: both;
}
.Button1 {
background: #555 url(images/Button1.png) no-repeat 0 0;
height: 79px;
}
.Button2 {
background: #555 url(images/Button2.png) no-repeat 0 0;
height: 97px;
}
.Button3 {
background: #555 url(images/Button3.png) no-repeat 0 0;
height: 127px;
}
.tabsHolder {
float: left;
position: relative;
}
.tabs {
width: 200px;
height: 200px;
margin: 0 0 20px 0;
border: solid 1px #ACCD45;
background: #444;
overflow: hidden;
padding: 20px;
display: none;
position: absolute;
left: 0;
}
#tab1 { top: 0; }
#tab2 { top: 98px; }
#tab3 { top: 215px; }
a:hover .tabs {display: block;}
HTML
<div class="MainContent">Content</div>
<div class="tabsHolder">
<a href="#tab1"><div class="buttons Button1">1</div>
<div id="tab1" class="tabs">
Content tab 1
</div>
</a>
<a href="#tab2"><div class="buttons Button2">2</div>
<div id="tab2" class="tabs">
Content tab 2
</div>
</a>
<a href="#tab3"><div class="buttons Button3">3</div>
<div id="tab3" class="tabs">
Content tab 3
</div>
</a>
</div>
</div>
You will need to define the pages (divs to hide/show) and tabs in two separate divs.
These will want to be floated next to each other, so you will have something like
<div class="pages">
<div class="page" id="tab1">....</div>
<div class="page" id="tab2">....</div>
</div>
<div class="tabs">
<div class="tab">Tab 1</div>
<div class="tab">Tab 2</div>
</div>
You can then set a min-height on pages (height for IE6, put into a conditional stylesheet), set pages and tabs to both float left, both with fixed widths.
Finally when you attach your event to $('#tab a'), make sure you iterate over all the pages hiding the non-relevant ones.
Without JavaScript, you cannot hide one of your divs, you can only have an HTML page per tab (like this or this).
If you want something more dynamic, you should use JavaScript. The tabs system is a built-in component of jQuery, for instance. (Homepage, live demo).
Hope that'll help you.

Dynamically sizing tab made of images

I'm trying add a tab to my web page that looks like this:
Using this example as a basis, I've gotten it partially working. My case differs because I want the text section to be a fixed with, but the tail section to dynamically resize to take up the rest of the tab's container.
It looks good in IE 6, but doesn't really take up the full width of the container.
In Firefox 3 it doesn't render well at all: (the red is a blank area between the spans).
How do I get this to render properly in both IE6 and Firefox to take up the full width specified for #Tab? #Tab4 is the area I'd like to size to take up as much room as possible.
<style type="text/css">
#Tab
{
width: 300px;
}
#Tab1
{
background: #000 url(BlueTabSprite.png) no-repeat 0 -136px;
display: inline-block;
height: 23px;
padding-left: 4px;
}
#Tab2
{
background: #000 url(BlueTabSprite.png) repeat-x 0 -242px;
display: inline-block;
overflow: hidden;
padding-top: 4px;
height: 19px;
width: 100px;
}
#Tab3
{
background: #000 url(BlueTabSprite.png) no-repeat right -30px;
display: inline-block;
height: 23px;
padding-right: 6px;
}
#Tab4
{
background: #000 url(BlueTabSprite.png) repeat-x 0 -83px;
display: inline-block;
height: 23px;
width:60%
}
#Tab5
{
background: #000 url(BlueTabSprite.png) no-repeat right -189px;
display: inline-block;
height: 23px;
padding-right:6px;
}
</style>
<div id="Tab">
<span id="Tab1">
<span id="Tab3">
<span id="Tab2">Test Tab</span>
</span>
</span>
<span id="Tab5">
<span id="Tab4"></span>
</span>
</div>
I think the Sliding Doors Technique may be what you're looking for.
This is a simplified version that works:
<div style="background: url('BlueTabSprite.png') no-repeat; width: 290px; min-width: 120px; max-width: 290px; height: 23px;">
<div style="float: right; background: url('BlueTabSprite.png') top right no-repeat; width: 10px; height: 23px;"></div>
Test
</div>
This tool may be a help.

Resources