Snippets

Adjust li width fit with ul width of the outer div/body

HTML:-

<nav role='navigation'>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Clients</a></li>
    <li><a href="#">Contact Us</a></li>
  </ul>
</nav>

CSS:-

* {
  box-sizing:border-box;
}
ul {
  width:700px;
  margin:10px auto;
  text-align:center;
}
li {
  list-style:none;
  float:left;
  border:1px solid red;
  padding:10px 0;
}
a {
  background:lightgrey;
  display:block;
}

JavaScript:-

(function( $ ){
  $.fn.autowidth = function() {
    return this.each(function() {        
        $('li', this).css({'width' : (100 / $('li', this).length) + '%'})
    });
  };
})( jQuery );

$(document).ready(function(){
    $('nav > ul').autowidth();    
});

 

Share
Tags: cssformliul

Recent Posts

Selecting Elements in jQuery (jQuery Selectors)

To collect a group of elements, we pass the selector to the jQuery function using…

5 years ago

jQuery library Introduction

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is…

5 years ago

Customize Input Box using jQuery

Prevent users typing special characters in text box, textarea, etc. [crayon-6637a2802984d871120590/] ^[a-zA-Z]+$ Alphabets ^[a-zA-Z\s]+$ Alphabets…

5 years ago

JavaScript simple form validation with example

Any interactive web site has form input - a place where the users input different…

5 years ago

Clear the browser cache of CSS or JavaScript Using Javascript

[crayon-6637a28029d33190849605/]  

5 years ago

Simple Ajax Call

[crayon-6637a28029e85386180194/]  

5 years ago