Friday, March 20, 2015

How to place text inside a circle using css

We often require some text to be placed inside a circle. I am gonna show you how to do it in very simple steps.

Monday, March 16, 2015

Centering a div inside a div - Responsive Design

When I was creating a simple html theme, I wanted to create a basic page that will be good for both desktops and mobiles. So, I created this simple div structure to put my contents in the center of the screen.

Wednesday, March 4, 2015

A Simple Jquery ajax example - HTML form submission made easy


You might be wondering about using jquery ajax method. Here is a simple Jquery ajax post example that explains easy way to send data to server and fetch data from server asynchronously.

Jquery ajax tutorial for beginners


1. Include Jquery in your html header 
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

2. Create a html form to submit sample data asynchronously 
    <form id ="myForm">
      ENTER YOUR NAME   <input id ="myInput" name="myInput"/>
      <button type="submit" id="submit">SUBMIT</button>
   </form>

3. Create jquery method $.ajax to submit data to server

     In your javascript add function like this
      $.ajax();   // primary method
     It takes some arguments like
        a) Jquery ajax action-type (type), eg: post, get etc..
        b) file to call (url ),
        c) data to send (data),
        d) what to do upon success ajax call (success)
        e) what to do upon server error (error)

      So, adding up the arguments, it finally looks like this
      $.ajax(
           {
             type: "post",
             url :   "/path/to/my.php",
             data : $("#myForm").serialize();
             success : function(data) {
                      // alert(data);
                            } ,
              error : function(data) {
                   // alert('Report Server Error');
                         }
           }
         );
    
4. Call ajax on clicking the submit button 

   So, our jquery ajax method is ready to send data to server.
   NOTE : $("#myForm").serialize() method will send all the form elements to server.

      Create handler to submit button 
      $("#submit").click(function () {
                // add ajax function here.
        });
      It should look like 

      $("#submit").click(function () {
           $.ajax(
           {
             type: "post",
             url :   "/path/to/my.php",
             data : $("#myForm").serialize();
             success : function(data) {
                       // alert(data);
                            } ,
              error : function(data) {
                       // alert('Report Server Error');
                         }
           }
         );
        });
 
5. Handling form elements on the server side

  On your server side PHP or servlet get the form elements

   In our example we passed our server file as "/path/to/my.php". Jquery ajax will send form to that location.

 if it is PHP, use
                     $_POST["myInput"];
 if it is jsp/servlet use
                     request.getParameter("myInput");

 do what ever you want on the server side and you can send signal saying either it is good or bad or you can throw any exception.

   Throwing exception will be treated as error by $.ajax();

So, what I have done in my example php file is
       echo $_POST["myInput"];

6. Once it is success (Jquery ajax success)
    

  It is all happy on the server side, the $.ajax parameter success function is invoked.

  There is another important thing is to block default form submission.

  Because, html form is submitted to server when you click on submit button.
   In order to avoid such form submissions, you can say event.preventDefault(); Use this on button click

     So, our final form submission using jquery will look like this.
  $("#submit").click(function (e) {
           e.preventDefault();
           $.ajax(
           {
             type: "post",
             url :   "/path/to/my.php",
             data : $("#myForm").serialize();
             success : function(data){
                      // alert(data);
                            } ,
              error : function(data) {
                     // alert('Report Server Error');
                         }
           }
         );
        });
 

Sunday, September 28, 2014

Fixed header on scroll

Having fixed header is very user friendly to any website and these below steps will show you how to have a fixed header with minimum steps. And there is an fiddle example at the bottom of this post.

fixed navigation header on page scroll


For this example, I am creating navigation header and a table below it in a html file.

Note: Bootstrap css has been used for navigation and datatable.

 Bootstrap CDN available here :

//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css


Step 1:
  Create a html file and add simple navigation header and wrap it in a div block with a class name as 'navigation'
<ul class="nav nav-pills">
  <li class="active"><a href="#">Home</a></li>
  <li><a href="#">Profile</a></li>
  <li><a href="#">Messages</a></li>
</ul>

Step 2:
   Create a simple datatable with the below code and put it in a div with class name as 'datatable'
<table class="table table-condensed table-bordered">
       <thead>
           <tr><th>Head1</th><th>Head2</th></tr>
       </thead>
       <tbody>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
           <tr><td>Col1</td><td>Col2</td></tr>
       </tbody>
</table>

Step 3:
   Now, add custom styles to your html code either in the same file or add it as an external css file

// Style to Navigation block.
.navigation {
    padding:8px; /* Some space around navigation pills */
    position:fixed; /* IMP.This makes the div fixed and cannot to moved*/
    top:0;          /* This will set the top edge position*/
    border:1px solid green; /* I Just gave it to differentiate between table and header*/
    background:#fff; 
    width:100%;    /* Occupies 100 percent of the page width*/
}
// Style to Datatable block
.datatable {
    margin-top:60px; // Top edge margin
}


At the end of step 3, you should be able to scroll the table with header as fixed (Note: For scroll bar to appear, the height of the browser should be less than the height of the page content. In our example browsers height should be less than table's height.)

 Fiddle available for this example here : http://jsfiddle.net/94hnes36/


Friday, June 6, 2014

Simple Sign In Form using html and css


The first and foremost graphical interface for any application is a login form or sign up form. It is very important that we make sign in forms attractive. You can create them easily with html and css.

Here is one simple example login in form using html and css

Simple sign in form with username and password fields
Simple sign in form with username and password




The html part :
<div id = "signUpForm" class = "center">
    <div id = "avatarHolder" class= "center">
      <img class = "avatar" src = "http://imagizer.imageshack.us/v2/100x100q90/837/ota1.jpg" />
    </div>
   
    <div id = "form" class = "center">
        <input id = "email"  placeholder= "Email"/>
        <input id = "password" type = "password"  placeholder = "password"/>
        <button id = "signIn">Sign in </button>
    </div>
</div>



The CSS style part


#signUpForm {
    width : 100%;
    max-width : 300px;
    height : auto;
    background-color : #ddd;
    padding: 20px;
    box-shadow : 0 3px 4px #aaa;
}
#avatarHolder {
    width : 90px;
}
.avatar {
    height : 90px;
    border : 1px solid #bbb;
    width : 90px;
    border-radius : 90px;
}
#form {
    width : 200px;
   padding: 10px;
}
#form input {
    line-height : 2;
    width : 100%;
    border-radius : 5px;
    border-style : none;
}
#form button {
  background-image:linear-gradient(#6D94BF, #446E9B 50%, #3E648D);
  border:1px solid #345578;
  padding:8px 12px;
  text-align:center;
  vertical-align:middle;
  white-space:nowrap;
  color : #FFF;
  width : 100%;
  border-radius : 5px;
  margin-top: 10px;
}

.center {
    margin-left : auto;
    margin-right : auto;
}


Place the html part between the body tags and place the css in your css file or in between style tags and your html should look like as shown in the picture above


There is fiddle available for this html sign-in form here





Wednesday, May 14, 2014

How to call jquery function from JSP page conditionally

Sometimes we might need to write a jquery function in jsp page and we might need to call it only when it is required or based on certain conditions.

Say we have a boolean attribute based on which we have to call jquery function.

Setting an attribute on server side


We set an attribute on server side using
 req.setAttribute("myAttribute", true);  // On Server Side

on jsp page use <% % > tags to write java code and get that attribute.

Getting attribute in JSP


<%  boolean myAttribute = (Boolean)req.getAttribute("MyAttribute") %>


Now, if that boolean is true write your jquery script.

Calling Jquery function in JSP


<%  if( myAttribute) {%>
<script>
   $(function () {
      alert("Called Jquery from JSP conditionally")
})
</script>
<%}%>


Or you can use taglib library

Example for calling jquery in JSP:


 <c:set var="myAttribute"  value="${5}"/>
<c:if test="${myAttribute< 10}">
  <script>
   $(function () {

       alert("Called Jquery from JSP conditionally");
   });
  </script>
   Variable <c:out value="${myAttribute}"/>

</c:if>




How to make divs responsive-Creating simple responsive design

It is now became necessary for every website to be responsive to different platforms like mobile, tablet and desktop. Creating fluid or responsive design is made easy CSS3 media queries.

In this post we will see how to create a simple responsive design using media query.


1. Lets create two div blocks in a simple html page


<!DOCTYPE html>
<html>
<style>

#container {
  width : 610px;
}

.holder {
  width : 300px;
  height : 300px;
  border : 1px solid red;
}

</style>
<body>
    <div id = "container"> 
    <div class = "holder">
     MY CONTENT IN DIV 1
    </div>
    <div class = "holder">
     MY CONTENT IN DIV 2
    </div>
   </div>
</body>
</html> 


If you execute the above code you will see something like below.


Simple Responsive Design Image 1
Fig.1 : Shows general div blocks with defined width and height of 300px

2. We need these div blocks to be side by side intially. So we will add some css properties to place them in one row.

Replace the holder class style with the below one. 


.holder {
width : 300px;
height : 300px;
border : 1px solid red;
display : inline-table;
float : left;
}


Now our divs should look like this

Simple Responsive Design Image 2
Fig.2 : Shows div blocks aligned in one row(aligned side by side)

3. Now, the two divs total width is 600px(i.e 300px + 300px). Lets say any device/browser width which is less than that, we want them to be in one column 

So, now using media query in our css we will change the css properties of the divs when the browser width is less than 600px.

     a) Change container width to 100%
     b) Change holder width to 100%
     

@media (max-width: 600px) 
{
 #container {
     width : 100%;
  }
 .holder {
     width : 100%;
  }
}
Thats it, if you include media query section in your style sheet, the result div blocks willl look like below for devices/browsers that are less than 600px width.



If you have any questions regarding this post, please post them in the comments section. I will reply as soon as possible.