簡體   English   中英

如何居中對齊腳本

[英]How to center align script

我使用編寫了一個簡單的游戲,然后在JavaScript代碼中創建了畫布。 我希望將其居中對齊/放置在頁面上的某個位置,盡管我不知道該怎么做。 這是游戲:

游戲

<body>
    <div>
        <script src='js/game.js'></script>
        <div class='about'>

            <h2>Controls:</h2>
            <p>Use the <span class="glyphicon glyphicon-arrow-left"></span> <span class="glyphicon glyphicon-arrow-right"></span><span class="glyphicon glyphicon-arrow-up"></span> and <span class="glyphicon glyphicon-arrow-down"></span> arrows to control the bacon eater.</p>
            <h2>Rules:</h2>
            <p>Collect the highest amount of bacon in the alloted time amount
            </p>
        </div>
    </div>


</body>

使用CSS可以使居中元素非常容易。 將元素括在div並應用以下html規則:

margin: 0 auto;
width: *some-percent-here*%;

希望這可以幫助! 干杯。

編輯:

例如,如果您想以50%的寬度將about部分居中,則可以使用:(用於可視化目的的背景顏色...)

.about {
   margin: 0 auto;
   width: 50%;

   background-color: blue;
}

注意:這將使div居中-而不是div內的內容。

對於文本居中使用:

text-align: center;

為了完整起見,應注意, 可以使用javascript所應用的樣式將div居中。 但是,在我看來,您的解決方案希望靜態放置畫布-使html成為更好的選擇...

只需將主體選擇器的CSS規則更改如下:

body {
  background-color: black;
  color: grey;
  margin: 0 auto;
  text-align: center;
  width: 80%;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM