Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Tuesday, September 2, 2014

12 Handy CSS Snippets for Developers

Find a complied list of 12 handy CSS Snippets for developers or designers. These CSS Snippets addresses most common use cases that are must for websites. These snippets includes like centering a div, rounding corners, targeting chrome and firefox, alternate table row, sticky footer, media queries and many more.


1. Center a DIV with CSS

#idOfTheDiv {
   width: 400px; /* here you put the width that you need */
   height: 200px; /* here you put the height that you need */
   position:absolute;
   left:50%;
   top:50%;
   margin-left:-200px; /* this number always to be the width divided two in negative */
   margin-top:-100px; /* this number always to be the height divided two in negative */
}
Source Link

2. Common @media queries

/* Smartphones (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
@media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
@media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
@media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
@media only screen
and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

Source Link

3. Print the url after your links

@media print  
{  
  a:after {  
    content: " [" attr(href) "] ";  
  }  
}
Source Link

You may also like:

4. Rounded Corners using CSS

#idOfTheDiv {
    border-radius: 5px;
   -moz-border-radius: 5px;
   -webkit-border-radius: 5px;
}
Source Link

5. Transparency for ie, firefox and safari

#element {
    filter:alpha(opacity=50); //For IE
    opacity: 0.5; //Safari
    -moz-opacity:0.5; //Mozilla & Firefox
}
Source Link

6. Sticky Footer

html, body {height: 100%;}
     
    #wrap {min-height: 100%;}
     
    #main {overflow:auto;
    padding-bottom: 150px;} /* must be same height as the footer */
     
    #footer {position: relative;
    margin-top: -150px; /* negative value of footer height */
    height: 150px;
    clear:both;}
     
    /*Opera Fix*/
    body:before {
    content:"";
    height:100%;
    float:left;
    width:0;
    margin-top:-32767px;/
    }
     
     
    
Source Link

7. Prevent Long URL's From Breaking Out with CSS

.break {
    -ms-word-break: break-all;
    word-break: break-all;
    word-break: break-word; 
    -webkit-hyphens: auto;
    -moz-hyphens: auto;
    hyphens: auto;
}
 
.ellipsis {
    width: 250px;
    white-space: nowrap;
    overflow: hidden;
    -ms-text-overflow: ellipsis; /* Required for IE8 */
    -o-text-overflow: ellipsis; /* Required for Opera */
    text-overflow: ellipsis;
}
Source Link

8. Removing Arrows From <SELECT> Tag with CSS

SELECT.no_arrows {
    width:90px; padding-top:0px; margin:-5px 0 0 5px; border:0px;
    background:transparent; -webkit-appearance:none;
    text-indent:0.01px; text-overflow:""; color:#444;
}
SELECT.no_arrows:focus {
    box-shadow:none;
}
SELECT.no_arrows::-ms-expand{
    /* for IE 10+ */
    display:none; 
}
@-moz-document url-prefix(){
    /* for FF */
    SELECT.no_arrows {
        width:auto; padding-top:2px; margin:0 0 0 5px;
        -webkit-appearance: none; -moz-appearance: none;
    }
}
Source Link

9. Targeting Chrome With CSS

@media screen and (-webkit-min-device-pixel-ratio:0) {
    H5 { color:red; }
    P { margin-left:20px; }
    /* other special styles for Chrome here */
}
Source Link

10. Targeting Firefox With CSS

@-moz-document url-prefix(){
    H5 { color:red; }
    P { margin-left:20px; }
    /* other special styles for FireFox here */
}
Source Link

11. Center Website Content with CSS

<style type="text/css">
/* Center your website horizontally */
.wrapper{
   width:960px;
   display:table;
   margin:auto;
}
 
/* Center certain content vertically */
.container{
   min-height: 10em;
   display: table-cell;
   vertical-align: middle;
}
</style>
 
<div class="wrapper">
    <div class="container">
        <p>Content goes here</p>
    </div>
</div>
Source Link

12. Alternating Table Color Rows in CSS

<style type="text/css">
/* technique 1 */
tbody tr:nth-child(odd){ background-color:#ccc; }
/* technique 2 */
TBODY TR.odd { background-color:#78a5d1; }
</style>
Source Link

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Thursday, July 12, 2012

Magnifying glass for image zoom using jQuery and CSS3

"The code uses CSS3 box-shadow and border-radius properties to create the magnifying glass. Jquery is used to position it at the cursor coordinates and change the background position accordingly.

Moving the cursor away from the image gently fades out the magnifying glass bringing the image back to the default state.
"

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Wednesday, July 11, 2012

Load CSS files using jQuery

Today I got into a situation where I need to load CSS file dynamically using jQuery. My code was working in all the browsers except IE. But after spending couple of minute I was able to make proper cross browser solution.

Here is a piece of code that will load CSS using jQuery.
$(document).ready(function(){
   $("head").append("<link>");
   var css = $("head").children(":last");
   css.attr({
     rel:  "stylesheet",
     type: "text/css",
     href: "CSS/Demo.css"
  });
});
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Wednesday, June 27, 2012

CSS Class, Attribute, Property and jQuery

In this short post, I will show you how to add/remove attributes or properties to DOM elements and how to add/remove CSS classes from DOM elements using jQuery. Also, how attribute and properties are different in jQuery.

Before jQuery 1.6 there was no concept of property. Everything was treated as attributes. But now you must be wondering about how attribute and property are different in jQuery. Find out answer at below post,


Properties:


Add Property: The general syntax to add attribute to one or set of DOM elements
$("selector").prop("propertyname","value");
Add Multiple Properties: You can also add multiple properties together using prop().
$("selector").prop({property1: "value", property2: "value"})
Remove Property:
$("selector").removeProp("PropertyName");
 Note: prop and removeProp are available with jQuery 1.6 and its higher version.  

Attributes:


Add Attribute: The general syntax to add attribute to one or set of DOM elements
$("selector").attr("attributeName","value");
Add Multiple Attributes: You can also add multiple attribute together using attr().
$("selector").attr({attribute1: "value", attribute2: "value"})
Remove Attribute:
$("selector").removeAttr("attributeName");

CSS Classes:


Add CSS Class: The general syntax to add css class to one or set of DOM elements
$("selector").addClass("classname");
Add Multiple Classes: You can also add multiple CSS classes together using addClass().
$("selector").addClass('class1 class2');
Remove CSS Class:
$("selector").removeClass("classname");
Remove All CSS Class: To remove all associated CSS classes from element, then don't pass the class name while making a call to removeClass() method.
$("selector").removeClass();
Element has the cssClass associated with it:
$("selector").hasClass("classname");
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Monday, June 11, 2012

Fix for setting CSS background image using jQuery

Yesterday I got into a very silly problem. I need to change background image of the div element using jQuery. And to change the background image, I was using jQuery CSS method which takes key as CSS attribute and value as value of that attribute.

So, to change the background image I was using below code.
$('#dvBackGround').css('background','path/to/image.jpg');
From initial look, I don't see any problem with the code but it was not working. The background image was not getting set. Even firebug showing that no CSS background properties is set. I was wondering what was the problem with the code. The code seems correct to me but it wasn't working. After 5-10 minutes, I realized the my mistake. The CSS background property takes value of the image path within url() keyword.

So the correct jQuery code is,
$('#dvBackGround').css('background','url(path/to/image.jpg)');
See result below.



Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Thursday, May 10, 2012

Make your input controls look stylish using jQuery

STYLE matters whether it comes to clothes, house or web pages because it attract others. So in this post, I will show you how to make your input control looks stylish using jQuery. There is a jQuery plugin called "jqTransform" which makes input control look stylish.

This plugin allows you to create custom form elements. This plugin can customize all types of form inputs, textarea, buttons and it works with all major browsers.


Download the plugin and add reference of jQuery library and plugin. Also don't forget to include the required CSS that comes with the plugin.
//Code Starts
<link href="/css/jqtransform.css" rel="stylesheet"/>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.jqtransform.min.js"></script>
//Code Ends
Now to apply transform effect, assign a CSS class "jqtransform" form so that all the controls within the form gets transformed.
//Code Starts
<form class="jqtransform">
<input type="checkbox" name="checkbox" />
<input type="radio" name="radio-btn" value="1" checked="checked"/>
<input type="radio" name="radio-btn" value="2" />
<select>
<option>Option 1</option>
<option>Option 2</option>
<select>
</form>
//Code Ends
Now, all you need to call is jQTransform() function using CSS selector.
//Code Starts
$(document).ready(function() {
  $('.jqtransform').jqTransform();
});
//Code Ends
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Wednesday, May 9, 2012

How to change image opacity on mouseover using jQuery

Images are undoubtedly the most attractive element of the page. And images with some cool effects make them more powerful and attractive. One of the effect on the image, is to make it transparent and play with transparency with mouse gestures. So in this post, I will show you how to change image opacity on mouseover and reset it on mouseout using jQuery.


First when image gets load on the page, set its opacity to 0.5 so that it looks transparent on load.
//Code Starts
$("#imgDemo").css("opacity", 0.5);
//Code Ends
Now using "hover" event, just change the opacity of image to 1.0 on mouseover and 0.5 on mouseout. Read more about "hover" here.

You can set the opacity property directly, but I have use animate method. The jQuery animate method allows create animation effects on any numeric CSS property. It also takes duration (in millisecond) as parameter. Thus the below jQuery code will apply opacity property in 5 milliseconds.
//Code Starts
$("#imgDemo").hover(function() {
        $(this).animate({opacity: 1.0}, 500);
    }, function() {
        $(this).animate({opacity: 0.5}, 500);
    });
//Code Ends
So, the complete code looks like,
//Code Starts
$(document).ready(function() {
    $("#imgDemo").css("opacity", 0.5);
    $("#imgDemo").hover(function() {
        $(this).animate({opacity: 1.0}, 500);
    }, function() {
        $(this).animate({opacity: 0.5}, 500);
    });
});​
//Code Ends
See result below.


See Complete Code
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Friday, May 4, 2012

How to use jQuery css method

jQuery provides .css() method to get and set the various CSS properties for the element. This method can be used to set one or multiple CSS properties for any element. You can also get the CSS property value using this method. In this post, I will show you all the 3 operations.

Set single CSS Property
//Code Starts
$("p").css("background-color", "LightGrey");
//Code Ends
Set multiple CSS Properties
//Code Starts
$("span").css({"background-color": "Green","color":"Yellow"});
//Code Ends
Get value of CSS Property
//Code Starts
$("div").html($('span' ).css("font-size"));
//Code Ends
See result below.



Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Saturday, December 24, 2011

Instant documentation for jQuey, CSS, HTML and JavaScript

It is always a good feeling to find everything what you need under a single roof. How about having instant documentation stuff.

Dochub is collection of instant documentation of CSS, HTML, JavaScript and jQuery. But this documentation is not written by someone. Instead it fetches the CSS, HTML and JavaScript collection based on the user request from Mozilla developer network and jQuery documentation from jQuery API website.
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Friday, August 26, 2011

addClass, removeClass, hasClass and toggleClass in jQuery

In this post, I will explain about some of the basic but useful jQuery css selectors. These CSS selectors are used to add or remove CSS class, to check whether element has specific css class associated with it or not. Believe me these are basic, but pretty useful.

Add cssClass to specific element
$('#element').addClass('myclass');
Remove cssClass from specific element
$('#element').removeClass('myclass');
Check whether element has the cssClass associated with it
$('#element').hasClass('myclass');
Add or remove cssClass using single css Selector
$('#element').toggleClass('myclass');
.toggleClass() is combination of .addClass() and .removeClass(). It first checks, whether specified cssClass is associated with element or not. if not then it adds it, otherwise it removes it. This is very useful when on button click you want to add and remove css class for any element.

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...