Reduce sprite CSS with LESS? - css

The repition of code needed to produce CSS sprite effects seems to be a perfect case for the use of LESS.
But other than standardising the base ,hover and active increments (below) I can't see any further way to reduce the lines of code needed.
Can anyone suggest futher improvements:
#sprite-base:0px;
#sprite-hover:20px;
#sprite-active:40px;
.zone-user .region-user-second ul.text-size li.one a {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px #sprite-base transparent;
}
.zone-user .region-user-second ul.text-size li.two a {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px #sprite-base transparent;
}
etc ...
.zone-user .region-user-second ul.text-size li.one a:hover {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px #sprite-base - #sprite-hover transparent;
}
.zone-user .region-user-second ul.text-size li.two a:hover {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px #sprite-base - #sprite-hover transparent;
}
.zone-user .region-user-second ul.text-size li.one a.active {
background: url("../img/sprite-accessibility.gif") no-repeat scroll 0px #sprite-base - #sprite-active transparent;
}
.zone-user .region-user-second ul.text-size li.two a.active {
background: url("../img/sprite-accessibility.gif") no-repeat scroll -25px #sprite-base - #sprite-active transparent;
}
etc ...

You are failing to take advantage of one of the greatest features of LESS which is nesting your rules. It makes it easier to identify what is going on and in my opinion it is easier to read. Also you do write considerably less code. Here is how I would nest the rules you have shown in your question:
#sprite-base:0px;
#sprite-hover:20px;
#sprite-active:40px;
.zone-user .region-user-second ul.text-size li a {
background: url("../img/sprite-accessibility.gif") no-repeat scroll transparent;
&.one {
background-position: 0px #sprite-base;
&:hover {
background-position:0px (#sprite-base - #sprite-hover);
}
&.active {
background-position:0px (#sprite-base - #sprite-active);
}
}
&.two {
background-position:-25px #sprite-base;
&:hover {
background-position:-25px (#sprite-base - #sprite-hover);
}
&.active {
background-position:-25px (#sprite-base - #sprite-active);
}
}
}

Well this is what i would do. ( without LESS ..or More.. )
Working example: http://jsfiddle.net/lollero/gE7df/
CSS:
.Element {
background-image: url('path/to/img.jpg');
background-repeat: no-repeat;
}
.E-1 { background-position: 0px 0px; }
.E-1:hover { background-position: -20px 0px; }
.E-2 { background-position: 0px -25px; }
.E-2:hover { background-position: -20px -25px; }
.E-3 { background-position: 0px -55px; }
.E-3:hover { background-position: -20px -55px; }
HTML:
<div class="Element E-1"></div>
<div class="Element E-2"></div>
<div class="Element E-3"></div>
Edit: I think I had a brain fart or something.. I fixed a huge error in my css example.

Related

trying to alter a sites color with Stylish but some elements refuse to change

the element that won't change is in this page (http://www.animefansftw.org/community/threads/fate-kaleid-liner-prisma%E2%98%86illya-3rei.641/add-reply)
the code that doesn't seem to take effect is
body[style="overflow-y: hidden; min-height: 259px; background: black none repeat scroll 0% 0%;"]
{
background: blue !important;
}
my real questunion is why it doesn't work & how to make it work
my Style(FF v47.0.1)
#namespace url(http://www.w3.org/1999/xhtml);
#-moz-document domain("animefansftw.org")
{
a:link{color: #b7e2fb !important;}
a:visited{color: #1eadff !important;}
a:hover{color: green !important;}
a:active{color: red !important;}
*, body, #content-wrapper, #footer-widgets,
single-post,.discussionList .discussionListItem.sticky .posterAvatar,
.discussionList .discussionListItem.sticky .stats,
.discussionList.discussionListItem.moderated .listBlock,
.discussionListItem.InlineModChecked .posterAvatar,
.discussionListItem.InlineModChecked .main,
.discussionListItem.InlineModChecked .stats,
.discussionListItem.InlineModChecked .lastPost,
.discussionListItem.moderated.InlineModChecked,
.discussionListItem.deleted .posterAvatar
{
background: black;
color: white !important;
}
.footer-widgets-border-left, #DiscussionListOptionsHandle a
{
background: #00004d;
}
.footer-widgets-border-right
{
background: #000080;
}
img[alt="default"]
{
-webkit-filter: invert(100%);
filter: invert(100%);
}
.cat-list li.ep-title:hover
{
background: gray !important;
border-left: 5px solid #f5f211;
border-right: 5px solid #1143f2;
}
li.ep-title
{
margin: 0 !important;
background: black !important;
border-bottom: 3px solid #00004d;
border-left: 0px solid transparent !important;
border-right: 0px solid transparent !important;
}
.dl-box
{
background: black;
border: 1px solid black;
box-shadow: -10px -10px 5px 0px #00004d;
}
.dl-title
{
background: -moz-linear-gradient(45deg, #00004d, blue, blue, #00004d);
}
.post-date .date, #social-wrapper, #nav, h3.newanimetitle,
#footer-bottom, .hasFlexbox .nodeList .categoryStrip,
.hasFlexbox .sidebar .visitorPanel .secondaryContent,
.sidebar .section .secondaryContent,
.threadListSeparator,.discussionList .sectionFooter
{
background: -moz-linear-gradient(top, #00004d, #000080, #000080, #00004d) no-repeat !important;
}
.dl-item
{
background: #1a1a1a;
}
#header-bottom
{
float: left;
background: url("http://puu.sh/2b5au.jpeg") #000000 !important;
width: 980px;
height: 165px;
}
#respond textarea
{
font-family: "Times New Roman", Times, serif;
font-size: large !important;
background: #00001a !important;
border: 1px solid #00004d !important;
}
.nodeList .node.level_1
{
background: #00001a !important;
box-shadow: 1px 2px 3px #00001a;
}
.nodeList .categoryStrip
{
border-top: 2px solid blue;
}
.hasFlexbox .node.level_2 .nodeInfo, .visitorPanel .stats dl,
.blendedEditor .redactor_box .redactor_toolbar
{
background: 0;
}
.hasFlexbox .message .messageUserInfo
{
background: #00001a !important;
}
div[style="background: #eeeeee; width: 620px; margin: 0 auto; padding:15px; font: 10pt italic; border: 1px solid #333333;"],
.message .dark_postrating.likesSummary, .dark_postrating
{
background: rgb(0, 0, 26) none repeat scroll 0% 0% !important;
}
.quickReply,.mainContainer .mainContent, .mainContainer_noSidebar
{
background: #00001a;
border-top: 2px solid #000080;
}
.blendedEditor .redactor_box .redactor_toolbar, .messageList .message
{
border-top: 2px solid #000080;
}
body[style="overflow-y: hidden; min-height: 99px;"],
.submitUnit .button, .redactor_toolbar
{
background: rgb(0, 0, 26) none repeat scroll 0% 0% !important;
}
element, body[style="overflow-y: hidden; min-height: 259px; background: black none repeat scroll 0% 0%;"]
{
background: blue !important;
}
}
Remember the basic rule of css hierarchy for implication and importance:
Inline - highest importance
Internal
External
To my understanding of css, and Whatever I am able to get from your code is you have first implied a css property as !important to a parent element, and then might have used something on child.
Though remember it will not show its impact even on implying !important, as it is under that parent property. So, you need to write inline css for it, though not recommended.
e.g: if font-size is set as 15px!important to parent itself, then an inline css is used to change font-size of a child.
The best rule I follow is never imply !important property until it is the last possible solution. You can use better and unique names for your classes and ids, if you are worried it will get disturbed with some other piece of code.

Kendo legacy theme's problems

This is a self-answered question
I'd like to use Kendo legacy themes for my app, since default themes are extremely ugly. I chose Office 2007 theme. I've included the following files:
<link href="~/Content/kendo/legacy/telerik.common.css" rel="stylesheet" />
<link href="~/Content/kendo/legacy/telerik.office2007.min.css" rel="stylesheet" />
I came across several problems with this themes that I'm not able to get ride of them and any help is appreciated.
Aligning buttons. Custom buttons are higher in the grid than the Grid's buttons.
Paging. Current page's number is shown in the corner. Also pagination's buttons are reverse, and page number's box is too big.
Multi select's wheel doesn't stop, even after loading items!
And finally, window's minimize button isn't visible.
This is the CSS I have used for buttons:
.k-grid .k-button{
width: 10px;
min-width: 10px !important;
}
.k-grid-custom,
.k-grid-custom:hover{
background-image: url('/Content/Images/Icon/icon.png');
height: 20px;
background-position: center;
background-repeat: no-repeat;
}
.k-edit{
background-image: url('/Content/Images/Icon/Edit.png');
background-position: 0 0 ;
}
Well, I worked around the multiselect's problem with robbing some of CSS from kendo.common.css.
.k-multiselect-wrap {
position: relative;
border-width: 0;
border-style: solid;
border-radius: 4px;
border-color: #c5c5c5;
background-color: #FFF;
min-height: 2.04em;
}
.k-multiselect-wrap .k-input {
background-color: transparent;
height: 1.31em;
line-height: 1.31em;
padding: .18em 0;
text-indent: .33em;
border: 0;
margin: 1px 0 0;
float: left;
}
.k-multiselect-wrap li {
margin: 1px 0 1px 1px;
padding: .1em 1.6em .1em .4em;
line-height: 1.5em;
float: left;
position: relative;
}
.k-autocomplete .k-loading, .k-multiselect .k-loading {
position: absolute;
right: 3px;
bottom: 4px;
}
.k-multiselect .k-loading-hidden {
visibility: hidden;
}
.k-multiselect-wrap .k-select {
position: absolute;
top: 0;
bottom: 0;
right: 0;
padding: .1em .2em;
}
You can hide current page's number:
.k-pager-numbers .k-current-page {
display: none;
}
Page numbers' size:
.k-pager-wrap .k-dropdown {
width: 4.500em;
}
For pagination's arrows, if you're using left-to-right you won't have any problem. In case you're using right-to-left, k-rtl, replace following CSS in telerik.common.css, line #230:
.k-i-seek-w { background-position: -48px -192px; }
.k-i-arrow-w { background-position: -32px -192px; }
.k-i-arrow-e { background-position: -16px -192px; }
.k-i-seek-e { background-position: 0 -192px; }
.k-state-disabled .k-i-seek-w { background-position: -48px -208px; }
.k-state-disabled .k-i-arrow-w { background-position: -32px -208px; }
.k-state-disabled .k-i-arrow-e { background-position: -16px -208px; }
.k-state-disabled .k-i-seek-e { background-position: 0 -208px; }
.k-state-hover .k-i-seek-w { background-position: -48px -224px; }
.k-state-hover .k-i-arrow-w { background-position: -32px -224px; }
.k-state-hover .k-i-arrow-e { background-position: -16px -224px; }
.k-state-hover .k-i-seek-e { background-position: 0 -224px; }
For the minimize button in Kendo Window, it seems they forgot to add the CSS!
.k-i-minimize {background-position: -32px -368px; }
Use the following CSS to align custom buttons with grid's buttons.
.k-grid .k-button {
vertical-align: bottom;
}

CSS seperator using image or plain css?

The separator image isn't displaying on the right side of the input field. I tried using a plain border and with different positioning, as well as with an image as in my example, but it doesn't show.
PS. This is the seperator image:
jsfiddle
.ui-topbanner {
color:#000;
height: 31px;
background-color: #f2e9da;
border-bottom: 1px solid #d9cebc;
}
.ui-topbanner-title {
position:absolute;
top:5px;
left:4px;
height:26px;
line-height:26px;
padding:0 10px 0 25px;
display:inline-block;
color:#000;
border:1px solid #d9cebc;
border-bottom:none;
background-color:#fff;
font-size:.7rem;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
background-image: url(http://hardist.eu/public/img/icon_folderyellow3.gif);
background-position: 5px 5px;
background-repeat: no-repeat;
}
.ui-topsubbanner {
position:relative;
color:#000;
height: 30px;
background: -webkit-linear-gradient(top, #f7f1e8 0%, #f4ecdf 100%);
border-top: 1px solid #fff;
border-bottom: 1px solid #efe8da;
}
.ui-topsubbanner-wrapper {
padding:2px 3px 0 5px;
}
.ui-seperator {
width:2px;
height:17px;
display:inline-block;
background-image: url(../images/ui-seperator.png);
background-position: 0 6px;
background-repeat: no-repeat;
}
<div class="ui-topbanner"><div class="ui-topbanner-title">Test</div></div>
<div class="ui-topsubbanner">
<div class="ui-topsubbanner-wrapper">
<input type="text" class="input-search" placeholder="Search...">
</div>
<div class="ui-seperator"></div>
</div>
Check setting float property. It may help you.
.ui-topsubbanner-wrapper {
padding:2px 3px 0 5px;
float: left;
}
.ui-seperator {
width:2px;
height:17px;
display:inline-block;
background-image: url(http://i.imgur.com/ovVm6fW.png);
background-position: 0 6px;
background-repeat: no-repeat;
float: left;
}
The image is actually there, but it's hard to see because it's so similar in color to background and not where you expect. Because the topsubbanner-wrapper div is a block element by default, the separator was getting bumped to the beginning of the next line. I couldn't see it at all until I changed the background of the separator div to red for diagnostic purposes.
You can fix the problem by setting the display of the topsubbanner-wrapper div to inline-block, as you did with the separator, or by setting its float to left.

Reusing nested classes in LESS for sprite icon reuse

I have a .less file that contains classes to manage my sprites, such as:
.icon-16 {
background: url('/Content/images/app/icons-16.png') no-repeat top left;
width: 16px;
height: 16px;
&.about { background-position: 0px 0px; }
&.add { background-position: 0px -16px; }
&.add2 { background-position: 0px -32px; }
}
Later on in another .less file that is being linked, I need to set background images using these sprites.
I know it is possible to reuse a class like this:
.myClass {
.mySharedClass;
}
However I'm unable to work out the correct syntax to reuse a class in a nested hierarchy.
What I would like to do is something like the following:
.myClass {
.icon-16.about;
}
Giving the following output (from both .icon-16 and .about):
.myClass {
background: url('/Content/images/app/icons-16.png') no-repeat top left;
width: 16px;
height: 16px;
background-position: 0px 0px;
}
However this doesn't compile.
How can I achieve this nested class reuse?
If what I'm trying to do is not possible, what would be the best alternative to allow my
sprites to be used in other classes?
Specific Solution in This Case
I recommend making a master mixin to generate what you need, as you need it. Here is a solution that works with your current version of LESS 1.3.1:
LESS
.make-icon-16(#form: all) {
background: url('/Content/images/app/icons-16.png') no-repeat top left;
width: 16px;
height: 16px;
#about: 0px 0px;
#add: 0px -16px;
#add2: 0px -32px;
.makePositioning() when (#form = all) {
&.about { background-position: #about; }
&.add { background-position: #add; }
&.add2 { background-position: #add2; }
}
.makePositioning() when (#form = about) {
background-position: #about;
}
.makePositioning() when (#form = add) {
background-position: #add;
}
.makePositioning() when (#form = add2) {
background-position: #about;
}
.makePositioning();
}
//to generate icon classes
.icon-16 {
.make-icon-16;
}
//to include as you want in your class
.myClass {
.make-icon-16(about);
}
CSS Output
.icon-16 {
background: url('/Content/images/app/icons-16.png') no-repeat top left;
width: 16px;
height: 16px;
}
.icon-16.about {
background-position: 0px 0px;
}
.icon-16.add {
background-position: 0px -16px;
}
.icon-16.add2 {
background-position: 0px -32px;
}
.myClass {
background: url('/Content/images/app/icons-16.png') no-repeat top left;
width: 16px;
height: 16px;
background-position: 0px 0px;
}
If you don't need or want the actual .icon-16 classes, skip the step that makes those as it is not necessary for you to do that.
More General Solution?
If your icons have the right logic to them, then this can be more generalized to accommodate any size icon.
LESS
.make-icon(#form: all, #size: 16) {
background: url('/Content/images/app/icons-#{size}.png') no-repeat top left;
width: #size*1px;
height: #size*1px;
#about: 0px 0px;
#add: 0px (#size*-1px);
#add2: 0px (#size*-2px);
.makePositioning() when (#form = all) {
&.about { background-position: #about; }
&.add { background-position: #add; }
&.add2 { background-position: #add2; }
}
.makePositioning() when (#form = about) {
background-position: #about;
}
.makePositioning() when (#form = add) {
background-position: #add;
}
.makePositioning() when (#form = add2) {
background-position: #about;
}
.makePositioning();
}
//to generate icon classes for 32px size
.icon-32 {
.make-icon(all, 32);
}
CSS Output
.icon-32 {
background: url('/Content/images/app/icons-32.png') no-repeat top left;
width: 32px;
height: 32px;
}
.icon-32.about {
background-position: 0px 0px;
}
.icon-32.add {
background-position: 0px -32px;
}
.icon-32.add2 {
background-position: 0px -64px;
}

CSS Positioning help ( sprite )

I have created a fiddle here:
http://jsfiddle.net/ozzy/77dJz/
Somehow ( mind you its 4:30am ) I have cocked up the hover effect on facebook link..
Twitter one works... just not sure what I have done wrong.
Any help appreciated, I am tired lol.
To Iterate:
CSS:
#facebook {
background:url('../images/socialsprite.png') no-repeat scroll 0px 0px transparent;
height:40px;
width:180px;
margin-left:10px;
margin-top:30px;
}
#facebook:hover {
background:url('../images/socialsprite.png') no-repeat scroll 0px -40px transparent;
height:40px;
width:180px;
}
#twitter {
background:url('../images/socialsprite.png') no-repeat scroll -180px 0px transparent;
height:40px;
width:180px;
margin-left:210px;
margin-top:-40px;
}
#twitter:hover {
background:url('../images/socialsprite.png') no-repeat scroll -180px -40px transparent;
height:40px;
width:180px;
}
Cheers
Need twitter and facebook sprite to sit side by side horizontally centered in the master div
What you need to do is float your divs so they don't cover each other.
Here is some simplified CSS. Edit the margins as needed.
#facebook {
background:url('http://sitehelp.com.au/images/socialsprite.png')
no-repeat scroll 0px 0px transparent;
height:40px;
width:180px;
float:left;
margin:25px 10px;
}
#facebook:hover {
background:url('http://sitehelp.com.au/images/socialsprite.png')
no-repeat scroll 0px -40px transparent;
}
#twitter {
background:url('http://sitehelp.com.au/images/socialsprite.png')
no-repeat scroll -180px 0px transparent;
height:40px;
width:180px;
float:right;
margin:25px 10px;
}
#twitter:hover {
background:url('http://sitehelp.com.au/images/socialsprite.png')
no-repeat scroll -180px -40px transparent;
}
http://jsfiddle.net/jasongennaro/77dJz/3/
EDIT
You could further simplify your CSS as follows:
#facebook, #twitter {
background:url('http://sitehelp.com.au/images/socialsprite.png') no-repeat scroll transparent;
height:40px;
width:180px;
float:left;
margin:25px 10px;
}
#facebook:hover {
background-position:0px -40px;
}
#twitter {
background-position:-180px 0px;
float:right;
}
#twitter:hover {
background-position: -180px -40px;
}
http://jsfiddle.net/jasongennaro/77dJz/4/
Quote: "...just not sure what I have done wrong."
The problem is that your Twitter <div> is covering your Facebook <div>.
Simply deleting the Twitter <div> gets your Facebook <div> working again...
And if you want side by side <div>, you need to float them.
this is working...
http://jsfiddle.net/77dJz/2/

Resources