2016-08-08 27 views
2

im Bau eines Tic-Tac-Toe-Spiel und das Brett mit css gezeichnet bewegt sich weiter auf meiner Einfügung eines 'X', als ob das 'X' eine Margin-Eigenschaft hat. Ich habe alle Rand und Padding auf 0 oder keine gesetzt und es bewegt meine Platine jedes Mal, wenn eine eingefügt wird.kann Rand und Padding nicht auf 0 setzen

HTML-Code:

<!DOCTYPE html> 
<html> 

<head> 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 

    <script> 
    $(document).ready(function(){ 
    $(".sq").click(function(){ 
     $(this).html("<em class='x'>X</em>"); 
    }); 
}); 
    </script> 

</head> 
<body> 
<center> 
    <div id="gameboard"> 
      <div class="sq" id="top-left"></div> 
      <div class="sq" id="top-mid"></div> 
      <div class="sq" id="top-right"></div> 
      <div class="sq" id="mid-left"></div> 
      <div class="sq" id="mid-mid"></div> 
      <div class="sq" id="mid-right"></div> 
      <div class="sq" id="bot-left"></div> 
      <div class="sq" id="bot-mid"></div> 
      <div class="sq" id="bot-right"></div> 
    </div> 
</center> 

</body> 
</html> 

here die CSS-Datei:

div { 
    position: static; 
} 

#gameboard { 
    width: 900px; 
} 

#top-left { 
    border-bottom: 1px solid black; 
    border-right: 1px solid black; 
} 

#top-mid { 

    border-bottom: 1px solid black; 
    border-right: 1px solid black; 
} 

#top-right { 

    border-bottom: 1px solid black; 
} 

#mid-left { 

    border-bottom: 1px solid black; 
    border-right: 1px solid black; 
} 

#mid-mid { 

    border-bottom: 1px solid black; 
    border-right: 1px solid black; 
} 

#mid-right { 

    border-bottom: 1px solid black; 
} 

#bot-left { 

    border-right: 1px solid black; 
} 

#bot-mid { 

    border-right: 1px solid black; 
} 

#bot-right { 

} 

.sq { 
    padding: none; 
    margin: -5px; 
    display: inline-block; 
    width: 300px; 
    height: 250px; 
} 

.x { 
    padding: 0; 
    margin: none; 
    font-size: 150px; 
} 

.o { 
    padding: 0; 
    margin: 0; 
    font-size: 200px; 
} 

Antwort

1

Sie müssen nur position:absolute zu <em> Elemente hinzuzufügen (in diesem Fall x);

div { 
 
    position: static; 
 
} 
 

 
#gameboard { 
 
    width: 900px; 
 
} 
 

 
#top-left { 
 
    border-bottom: 1px solid black; 
 
    border-right: 1px solid black; 
 
} 
 

 
#top-mid { 
 

 
    border-bottom: 1px solid black; 
 
    border-right: 1px solid black; 
 
} 
 

 
#top-right { 
 

 
    border-bottom: 1px solid black; 
 
} 
 

 
#mid-left { 
 

 
    border-bottom: 1px solid black; 
 
    border-right: 1px solid black; 
 
} 
 

 
#mid-mid { 
 

 
    border-bottom: 1px solid black; 
 
    border-right: 1px solid black; 
 
} 
 

 
#mid-right { 
 

 
    border-bottom: 1px solid black; 
 
} 
 

 
#bot-left { 
 

 
    border-right: 1px solid black; 
 
} 
 

 
#bot-mid { 
 

 
    border-right: 1px solid black; 
 
} 
 

 
#bot-right { 
 

 
} 
 

 
.sq { 
 
    padding: none; 
 
    margin: -5px; 
 
    display: inline-block; 
 
    width: 300px; 
 
    height: 250px; 
 
} 
 

 
.x { 
 
    padding: 0; 
 
    margin: none; 
 
    font-size: 150px; 
 
    position: absolute; 
 
} 
 

 
.o { 
 
    padding: 0; 
 
    margin: 0; 
 
    font-size: 200px; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 

 
    <script> 
 
    $(document).ready(function(){ 
 
    $(".sq").click(function(){ 
 
     $(this).html("<em class='x'>X</em>"); 
 
    }); 
 
}); 
 
    </script> 
 

 
</head> 
 
<body> 
 
<center> 
 
    <div id="gameboard"> 
 
      <div class="sq" id="top-left"></div> 
 
      <div class="sq" id="top-mid"></div> 
 
      <div class="sq" id="top-right"></div> 
 
      <div class="sq" id="mid-left"></div> 
 
      <div class="sq" id="mid-mid"></div> 
 
      <div class="sq" id="mid-right"></div> 
 
      <div class="sq" id="bot-left"></div> 
 
      <div class="sq" id="bot-mid"></div> 
 
      <div class="sq" id="bot-right"></div> 
 
    </div> 
 
</center> 
 

 
</body> 
 
</html>

+1

Vielen Dank! – tocoolforscool

+1

Wie schließe ich eine Frage? – tocoolforscool