解決方法から言うとstyle.css
で
css
1header {
2 ...
3 top: 0px; /* 追加 */
4}
5
6#img-box {
7 margin-top: 90px;
8 margin-bottom: 10px;
9 margin-left: auto; /* 追加 */
10 margin-right: auto; /* 追加 */
11 /*margin: 90px auto 10px; あるいはこう書き直す */
12 /*display: inline-block; 削除 */
13 ...
14}
とすると問題なく動作すると思います。
他にわかる方がいれば追記していただきたいのですが
なんでinline-block
にすると意図しない動作が発生するんでしょう?
#img-box
のdisplay: inline-block;
を有効にしてボタンをクリックすると<div id="img-box">
の右下に位置していた<br>
が<div id="img-box">
の右に移動する(結果として「ページ下部の表示が少し上にずれ」る)
タグcss
つけて質問し直した方が良い気もしますが……
以下質問者のコードを必要分までざっくりと削ったものです
html
1<!DOCTYPE html>
2<html lang="ja">
3<head>
4 <meta charset="UTF-8">
5 <link rel="stylesheet" href="style.css">
6<body>
7 <header>
8 <h1>タイトル</h1>
9 </header>
10 <div class="main">
11 <div id="img-box"></div>
12 <br>
13 <button id="startbutton">ボタン</button>
14
15 <article>
16 <p>テキスト</p>
17 </article>
18 </div>
19 <script src="script.js"></script>
20</body>
21</html>
css
1body {
2 background-image: url("単色じゃなくてなにかしらあると分かりやすい");
3 background-position: 50%;
4 margin: 0;
5 min-width: 700px;
6}
7
8header {
9 padding: 0;
10 background-color: #1eff005e;
11 text-align: center;
12 width: 100%;
13 position: fixed;
14 top: 0px;
15}
16
17header h1 {
18 margin: 0em;
19 font-size: 45px;
20}
21
22.main {
23 text-align: center;
24}
25
26#img-box {
27 margin: 90px auto 10px;
28 /*display: inline-block;*/
29 height: 500px;
30 width: 700px;
31 background-color: #ffffff;
32}
33
34#img-box p {
35 margin: 0;
36 font-size: 200px;
37}
javascript
1var isStarted = false;
2function main() {
3 if (isStarted) {
4 return;
5 }
6
7 var counter = document.getElementById("img-box").appendChild(document.createElement("p"));
8
9 isStarted = true;
10 counter.textContent = "hoge";
11}
12
13document.getElementById("startbutton").onclick = main;