Showing posts with label css. Show all posts
Showing posts with label css. Show all posts

Tuesday, 22 September 2015

Amazing CSS attribute Selector with substring matching selection

CSS attribute Selector


If you have learned html and css, you must be aware of how to target element with id or class in css.

<p id="demo_id" class="demo_class"> </p>

above element has id and class which you can use in css to target p element as "#demo_id" and ".demo_class". But apart from these selectors you can select element with other attributes using attribute selector.


<p id="demo_id" class="demo_class" rel="name" title="name of Jon"> </p>

In above P tag we can see attributes other than class and id, like "rel", "title". So it is possible to us to user these attributes for selecting perticuler P tag.



p[rel] {
    background-color: yellow;
}

this css will target p tag which has rel attribute and change its background color to yellow.

It is also possible to use attribute with value. Let's see another example. If we have two P tags


<p rel="name">Rajesh</p>
<p rel="email">rajesh@something.com</p>

And in css if we write


  p[rel="name"]{
    background-color: yellow;
}


Then this css will be apply for the p tag with rel has value "name".


Attribute selector has lot more option to do. Let's take a look at these.

Above example is attribute with exactly match value.


[attribute~="value"] Selector.

The [attribute~="value"] selector is used to select elements with an attribute value containing a specified word.

The following example selects all elements with a title attribute that contains a space-separated list of words, one of which is "image":

<img src="image1.jpg" title="small image " width="150" height="113">
<img src="image2.jpg " title=" image " width="224" height="162">


[attribute|="value"] Selector

The [attribute|="value"] selector is used to select elements with the specified attribute starting with the specified value.

The following example selects all elements with a class attribute value that begins with "top":

Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text"!


<h1 class="top-header">Welcome</h1>
<p class="top-text">Hello world!</p>
  

[attribute^="value"] Selector

The [attribute^="value"] selector is used to select elements whose attribute value begins with a specified value.

The following example selects all elements with a class attribute value that begins with "top":

Note: The value does not have to be a whole word!

<h1 class="top-header">Welcome</h1>
<p class="top-text">Hello world!</p>
<p class="topcontent">Are you learning CSS?</p>



[attribute$="value"] Selector

The [attribute$="value"] selector is used to select elements whose attribute value ends with a specified value.

The following example selects all elements with a class attribute value that ends with "test":

Note: The value does not have to be a whole word!


<div class="first_test">The first div element.</div>
<div class="my-test">The third div element.</div>
<p class="mytest">This is some text in a paragraph.</p>


[attribute*="value"] Selector

The [attribute*="value"] selector is used to select elements whose attribute value contains a specified value.

The following example selects all elements with a class attribute value that contains "te":

Note: The value does not have to be a whole word!


<div class="first_test">The first div element.</div>
<div class="second">The second div element.</div>
<div class="my-test">The third div element.</div>
<p class="mytest">This is some text in a paragraph.</p>


Thursday, 4 December 2014

Simple popup box in Jquery

While building dynamic web pages popups are useful for showing information of inputs other than content that is shown on page. Mostly html alert popup is used to some info like 'alerts or information for website'. But when development needs popup that do multiple things with multiple formats, alert box is not enough.

Jquery popup


          In this case we need a custom pop up that satisfy our need. We can see that there are many jquery plugin that provide custom popup. But they come with external js & css files with complex format to implement. That is why I want to built my own custom popup.

          We will build this with ‘div’ in html, which will be hidden on page load.  After clicking on link this popup will be shown by jquery. The code is given below

<!DOCTYPE html>
<html lang="en">
<head>
    <title>test</title>

<style>
.popup-back {
    float: left;
    background-color: rgba(51, 51, 51, 0.71);
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    display:none;
    z-index:10000;
}

.popup-con {
    margin: auto;
    width: 600px;
    position: relative;
}

.popup-body {
    background-color: #fff;
    padding: 10px;
    position: absolute;
    width: 100%;
    top: 100px;
}

a.close-popup {
font-family: arial;
text-decoration: none;
background-color: #333;
color: #fff;
border-radius: 50%;
height: 21px;
position: absolute;
width: 20px;
text-align: center;
right: 5px;
top: 5px;

}
    </style>
   
</head>
<body>

<h1>Pop ups</h1>
<a href="#" class="link-class popup-link" rel="my_popup1">popup 1</a>
<a href="#" class="link-class popup-link" rel="my_popup2">popup 2</a>

<div class="popup-back popup-spclass" id="my_popup1">

        <div class="popup-con">
                <div class="popup-body">

                <a href="#" class="close-popup"></a>
                        <h1>this is my 1st popup</h1>

                        <p>Simple way for popup</p>
               
                </div>
        </div>


</div>


<div class="popup-back popup-spclass" id="my_popup2">

        <div class="popup-con">
                <div class="popup-body">

                <a href="#" class="close-popup"></a>
                        <h1>this is my 2nd popup</h1>

                        <p>Simple way for popup</p>
               
                </div>
        </div>


</div>


 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(".popup-link").click(function () {
            var popupId = "#" + $(this).attr("rel");

            $(popupId).fadeIn();


        });
        $(".close-popup").click(function () {

            $(".popup-spclass").fadeOut();


        });
       
    </script>
</body>
</html>




Tuesday, 19 August 2014

Simple banner image slider gallery in Jquery

If you want jquery galleries in your webpage, you will get lots of plugins out there. But these plugins are loaded with lots of other js and css files, which are mostly heavy for loading page and also difficult for modification. I was exactly don't want this for my website.




Simple banner image slider gallery in Jquery




      
       So I tried to reduce javascript for lighten the page. This slider will never do fancy thing but sufficient for show images one by one.

Let us start now. First of all we start with html.

<div class="bannerdiv">
     
      <div class="mainslider">
          
        <img src="images/img1.jpg" alt="Name1" rel="http//yourlink"/>
                <img src="images/img2.jpg" alt="Name2" rel="http//yourlink"/>
                <img src="images/img3.jpg" alt="Name3" rel="http//yourlink"/>
      </div>

<span class="bannercaption">Banner alt will displayed here</span>


       <a class="nextslide slidenav" href="#" id="hyp_NextGallery"></a>
<a class="prevslide slidenav" href="#" id="hyp_PreviousGallery" style="display: none;"></a>

</div>

Here banner div contain two sections. First is 'mainslider' which is containing images of slide show and second one is a span tag which will contain banner caption. You can do styling of it in your way. If you notice banner img tag containing alt and rel attribute, this we will use as our caption for that particulate image.

Now let's do css part. I am setting a height of my banner 500px. You can change it as your need.  


/*******************************css*******************************************//////////////////

.bannerdiv {

position: relative;
float: left;
width: 100%;
top: 0;
margin-bottom: 12px;
margin-top: 50px;
overflow: hidden;
max-height: 500px;
}

.mainslider img {
position: absolute;
width: 100%;
}
.bannercaption{
position: absolute;
bottom: 2px;
right: 2px;
z-index: 1;
font-size: 14px;
color: #fff;
background-color: rgba(0, 0, 0, 0.51);
padding: 5px;
}
a.nextslide {
position: absolute;
color: rgb(255, 255, 255);
top: 0;
font-size: 27px;
background-position: 3px center;
right: 0px;
}

a.prevslide {
position: absolute;
color: rgb(255, 255, 255);
top: 0;
font-size: 27px;
left: 0px;
background-position: -29px center;
}

a.slidenav {
width: 37px;
height: 100%;
background-image: url('../Images/slim-arrow-sprite.png');
background-repeat: no-repeat;

}


It's time for important part of an banner slider, that means Javascript. We are using library here.
Here we are using 'setInterval' for continuous image change. For next and previous we are using two 'a' tags, 'prevslide' and 'nextslide'. On their click we are performing particular task.  
/*****************************js****************************************///////////////////
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script>
$(document).ready(function () {


    $('.mainslider img:gt(0)').hide();
    setInterval(function () { $('.mainslider :first-child').fadeOut(1000).next('img').fadeIn(1000).end().appendTo('.mainslider'); }, 5000);


    $('.mainslider :first-child').fadeOut(1000).next('img').fadeIn(1000).end().appendTo('.mainslider').ready(function () { $('.bannercaption').html("<a href='" + $('.mainslider :first-child').attr('rel') + "'>" + $('.mainslider :first-child').attr('alt') + "</a>"); });




    $('.nextslide').click(function () {

        $('.mainslider :first-child').fadeOut(1000).next('img').fadeIn(1000).end().appendTo('.mainslider').ready(function () { $('.bannercaption').html("<a href='" + $('.mainslider :first-child').attr('rel') + "'>" + $('.mainslider :first-child').attr('alt') + "</a>"); }); ;

        return false;


    });


  $('.prevslide').click(function () {

        $('.mainslider :first-child').fadeOut(1000);
        $('.mainslider :last-child').fadeIn(1000).end();

        $('.mainslider').prepend($('.mainslider :last-child')).ready(function () { $('.bannerauthor').html("<a href='" + $('.mainslider :first-child').attr('rel') + "'>" + $('.mainslider :first-child').attr('alt') + "</a>"); }); 


        return false;

    });


});
</script>

So this is it. This code is simple and lighter than other heavy slider plugins and also easy for modification that you want in your page.