I wanted to make a little game with a div#field containing 100 div's with the shape of a square. I also want the div's to be responsive.
I tried to give them a width in percentage with css.
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
border: 1px solid black;
margin: auto;
width: 10%;
}
#field > div{
float: left;
width: 10%;
}
</style>
But it looked like:
Then I erased the float: left; and placed display: inline;
But it looked like:
My next attempt to succeed was using jQuery to make to make my div's responsive, and my code looked like.
<!DOCTYPE html>
<html>
<head>
<title>100 div's field</title>
<meta charset="utf-8">
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
border: 1px solid black;
}
#field > div{
float: left;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
for (var i = 0; i < 100; i++) {
$('#field').append('<div></div>');
};
resizeWindow();
$(window).resize(function(){
resizeWindow();
});
function resizeWindow () {
var windowHeight = $(window).height();
var windowWidth = $(window).width();
if(windowHeight < windowWidth){
var windowsize = windowHeight;
}else{
var windowsize = windowWidth;
}
$('#field').css("width", windowsize - windowsize * 0.1 + "px");
$('#field').css("height", windowsize - windowsize * 0.1 + "px");
$('#field > div').css("width", windowsize * 0.1 - windowsize * 0.015 + "px");
$('#field > div').css("height", windowsize * 0.1 - windowsize * 0.015 + "px");
}
});
</script>
</head>
<body>
<div id="field"></div>
</body>
</html>
I decreased the sizes with a 10th because I don't want the div's to be too big for my #field div. I thought my problem was solved now but, when I resize the browser window I had to many space in my #field div, or there wasn't enough space and my div's popped out.
Also:
-Trying other browser didn't help.
-Is the only option to make a table?
-Or is it possible to add a lot of CSS Media Queries