0

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>
gag
  • 325
  • 7
  • 16
  • It's advisable that you provide us with a http://www.jsfiddle.net with your relevant code. – Itay Sep 07 '13 at 07:52
  • 2
    you should use descriptive variable names, not the alphabet – omma2289 Sep 07 '13 at 07:53
  • Here's a [demo](http://jsfiddle.net/9TqBD/). Your script doesn't spread the `
  • `-elements over the 960px width in Safari. It's close in Forefox but there's still a gap. Both tested on MAC, latest browsers.
  • – insertusernamehere Sep 07 '13 at 08:43