I am trying to evenly spread navigation items within a div tag. My code goes below. It makes some calculation and gives the results.
On Chrome, Opera and Safari it works all right, but on IE8 and Firefox it does have some trouble. What I want is to convert all the results of calculations (li width, container width, padding) into percents instead of pixels to work on all browsers correctly.
Please, let me know if someone has the solution.
And my code sometimes makes also trouble after refresh, what can be the reason for this?
Thank you!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
width: 960px;
margin: 0 auto;
}
.container {
width: 100%;
}
.menu {
width: 100%;
list-style: none;
}
.parent {
float: left;
background: red;
margin-right: 1px;
padding: 0;
}
.parent:last-child {
margin-right: 0px;
}
</style>
</head>
<body>
<div class="container">
<ul class="menu">
<li class="parent"><a href="#">О Фонде</a> </li>
<li class="parent"><a href="#">Дети</a> </li>
<li class="parent"><a href="#">Проекты</a> </li>
<li class="parent"><a href="#">Новости</a> </li>
<li class="parent"><a href="#">Как помочь</a> </li>
<li class="parent"><a href="#">Вам нужна помощь?</a> </li>
<li class="parent"><a href="#">Специалистам</a> </li>
<li class="parent"><a href="#">Волонтерство</a> </li>
</ul>
</div>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script>
$(document).ready(function() {
var a = $(".menu li.parent").length;
var b = 0;
$(".menu li.parent").each(function(){
b = b + $(this).width()
});
var c = ($(".container").width() - b - (a-1))/a ;
var d = c/2;
$(".parent").css("padding", d)
});
</script>
</body>
</html>