\n\n\n```\n```css\n@charset \"UTF-8\";\n/* CSS Document */\nbody{\n\tfont-size: 1rem;\n\tmargin-bottom: 500px;\n}\n.wrapper{\n\twidth: 1300px;\n\tmargin: 0 auto;\n\tbackground-color: pink;\n}\nh1{\n\tfont-size: 1.2rem;\n\tpadding: 15px 0 15px 0;\n}\n\n.kontena{\n\tdisplay: grid;\n\twidth: 100%;\n\theight: auto;\n\tgrid-template-columns: auto auto auto auto;\n\tgrid-auto-rows: auto auto;\n\tgap: 35px;\n\tpadding: 0;\n\tpadding-top: 100px;\n}\n.item{\n\twidth: 100%;\n\tmargin-bottom: 5px;\n}\nli{\n\tlist-style: none;\n}\n#photo p{\n\tfont-size: 0.75rem;\n\tmargin: 0;\n}\na{\n\ttext-decoration: none;\n\tcolor: black;\n\ttransition: all 0.5s;\n}\na:hover {\n opacity: 0.7;\n}\n/*フッター*/\nfooter{\n\tmax-width: 100%;\n\tdisplay: flex;\n\tjustify-content: space-between;\n}\nfooter li{\n\tmargin-right: 20px;\n}\n\nfooter ul{\n\tdisplay: flex;\n\tpadding: 0;\n\tmargin-bottom: 5px;\n}\nfooter a{\n\ttext-decoration: none;\n\tfont-size: 0.75rem;\n}\n.copyright{\n\tpadding: 0;\n\tfont-size: 0.5rem;\n}\n\n\n/*ハンバーガーメニュー*/\nheader{\n\tdisplay: flex;\n\tjustify-content: space-between;\n\tposition: fixed;\n\tz-index: 999;\n\tpadding: 0;\n\tmargin: 0;\n\tbackground: rgba(200,255,255,1);\n}\n\n.sp-menu #open{\n\tpadding: 15px 0 15px 0;\n\tfont-size: 2rem;\n\tcursor: pointer;\n}\n\n.overlay{\n\tposition: fixed;\n\ttop: 0;\n\tbottom: 0;\n\tright: 0;\n\tleft: 0;\n\tbackground: rgba(255,255,255,0.95);\n\tmargin-top:20px;\n\topacity: 0;\n\tpointer-events: none;\n}\n.overlay #close\n{\n\tfont-size: 2rem;\n\tdisplay: block;\n\ttext-align: right;\n\tcursor: pointer;\n}\n.overlay ul{\n\ttext-align: center;\n\tmargin-top: 50px;\n}\n.overlay li{\n\tmargin-top: 30px;\n}\n\n.overlay.show{\n\topacity: 1;\n\tpointer-events: auto;\n}\n.sp-menu #open.hide{\n\tdisplay: none;\n}\n\n```\n```javascript\n'use strict';\n\n{\n\tconst open = document.getElementById('open');\n\tconst overlay = document.querySelector('.overlay');\n\tconst close = document.getElementById('close');\n\t\n\t\n\topen.addEventListener('click', () => {\n\toverlay.classList.add('show');\n\topen.classList.add('hide');\n\n\t\t\n\t});\n\t\n\tclose.addEventListener('click', () => {\n\toverlay.classList.remove('show');\n\topen.classList.remove('hide');\n\n\t\t\n\t});\n}\n```","answerCount":1,"upvoteCount":1,"datePublished":"2022-08-10T04:35:46.431Z","dateModified":"2022-08-11T22:31:48.000Z","acceptedAnswer":{"@type":"Answer","text":" 中央に寄せることができれば解決であれば、以下のCSSを追加してみるとどうでしょうか?\n\n```css \nheader {\n\tdisplay: flex;\n\tjustify-content: space-between;\n\tposition: fixed;\n\tz-index: 999;\n\tpadding: 0;\n\tmargin: 0;\n\tbackground: rgba(200, 255, 255, 1);\n\tinset: auto 0; /* ←これ */\n}\n```\n\n メインコンテンツにヘッダーがかぶっているのも気になるのですが、そこは大丈夫ですか?\n\n---\n\n なお、固定配置をするとマージンが効かなくなる理由は、固定配置や絶対配置の時に`left` `right`が`auto`だと、`margin-left: auto` `margin-right: auto`は`margin-left: 0` `margin-right: 0`として扱われる、という仕様によるものです。\n\n(↓こちらの表の「✔ \t✘ \t✔ \tany」の欄)\n[https://www.w3.org/TR/css-position-3/#abspos-auto](https://www.w3.org/TR/css-position-3/#abspos-auto)\n\n---\n\n参考:\n[inset - CSS: カスケーディングスタイルシート | MDN](https://developer.mozilla.org/ja/docs/Web/CSS/inset)\n","dateModified":"2022-08-11T22:27:35.000Z","datePublished":"2022-08-10T05:11:56.439Z","upvoteCount":1,"url":"https://teratail.com/questions/8dirqzkan3ffjm#reply-8ddvzperb6nx64"},"suggestedAnswer":[],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/CSS3","name":"CSS3に関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/8dirqzkan3ffjm","name":"ヘッダーを固定したら wrapperが効かなくなってしまいました。なぜでしょうか?"}}]}}}
質問するログイン新規登録

Q&A

解決済

1回答

1701閲覧

ヘッダーを固定したら wrapperが効かなくなってしまいました。なぜでしょうか?

negiremi

総合スコア13

CSS3

CSS(Cascading Style Sheet)の第3版です。CSS3と略されることが多いです。色やデザインを柔軟に変更することが可能になります。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

1グッド

0クリップ

投稿2022/08/10 04:35

編集2022/08/10 04:48

1

0

position : fixed;でヘッダーを固定したところ
class="wrapper"で左右に余白を作っていましたが、左端に寄ってしまいました。
誤字がないか確認したり、コードを入力し直したりしました。半日試行錯誤しましたが、どうしてもwrapperが効きません、、なぜだか分かる方いらっしゃいますか?
イメージ説明

html

1<!doctype html> 2<html> 3<head> 4<link rel="stylesheet" href="css/style.css"> 5<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 6<meta charset="UTF-8"> 7<title>grid design</title> 8</head> 9<body> 10 <header class="wrapper"> 11 <h1 class="wrapper"><strong>Grid Design</strong></h1> 12 <div class="sp-menu"> 13 <span class="material-icons" id="open">menu</span> 14 </div> 15 <div class="overlay"> 16 <span class="material-icons" id="close">close</span> 17 <nav> 18 <ul class="wrapper"> 19 <li><a href="#">PRODUCT</a></li> 20 <li><a href="#">ABOUT</a></li> 21 <li><a href="#">COMPANY</a></li> 22 <li><a href="#">CONTACT</a></li> 23 </ul> 24 </nav> 25 </div> 26 27 </header> 28 29 <section id="photo" class="wrapper"> 30 <ul class="kontena"> 31 <li><a href=""><img src="img/item1.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 32 <li><a href=""><img src="img/item2.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 33 <li><a href=""><img src="img/item3.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 34 <li><a href=""><img src="img/item4.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 35 <li><a href=""><img src="img/item5.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 36 <li><a href=""><img src="img/item6.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 37 <li><a href=""><img src="img/item7.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 38 <li><a href=""><img src="img/item8.jpeg" class="item"><p>プロダクトタイトルプロダクトタイトル<br>¥99,999 +tax</p></a></li> 39 </ul> 40 </section> 41 <footer class="wrapper"> 42 <ul> 43 <li><a href="" target="blank">INSTAGRAM</a></li> 44 <li><a href="" target="blank">TWITTER</a></li> 45 <li><a href="" target="blank">FACEBOOK</a></li> 46 </ul> 47 <p class="copyright">©︎Grid design</p> 48 </footer> 49 <script src="js/main.js"></script> 50</body> 51</html>

css

1@charset "UTF-8"; 2/* CSS Document */ 3body{ 4 font-size: 1rem; 5 margin-bottom: 500px; 6} 7.wrapper{ 8 width: 1300px; 9 margin: 0 auto; 10 background-color: pink; 11} 12h1{ 13 font-size: 1.2rem; 14 padding: 15px 0 15px 0; 15} 16 17.kontena{ 18 display: grid; 19 width: 100%; 20 height: auto; 21 grid-template-columns: auto auto auto auto; 22 grid-auto-rows: auto auto; 23 gap: 35px; 24 padding: 0; 25 padding-top: 100px; 26} 27.item{ 28 width: 100%; 29 margin-bottom: 5px; 30} 31li{ 32 list-style: none; 33} 34#photo p{ 35 font-size: 0.75rem; 36 margin: 0; 37} 38a{ 39 text-decoration: none; 40 color: black; 41 transition: all 0.5s; 42} 43a:hover { 44 opacity: 0.7; 45} 46/*フッター*/ 47footer{ 48 max-width: 100%; 49 display: flex; 50 justify-content: space-between; 51} 52footer li{ 53 margin-right: 20px; 54} 55 56footer ul{ 57 display: flex; 58 padding: 0; 59 margin-bottom: 5px; 60} 61footer a{ 62 text-decoration: none; 63 font-size: 0.75rem; 64} 65.copyright{ 66 padding: 0; 67 font-size: 0.5rem; 68} 69 70 71/*ハンバーガーメニュー*/ 72header{ 73 display: flex; 74 justify-content: space-between; 75 position: fixed; 76 z-index: 999; 77 padding: 0; 78 margin: 0; 79 background: rgba(200,255,255,1); 80} 81 82.sp-menu #open{ 83 padding: 15px 0 15px 0; 84 font-size: 2rem; 85 cursor: pointer; 86} 87 88.overlay{ 89 position: fixed; 90 top: 0; 91 bottom: 0; 92 right: 0; 93 left: 0; 94 background: rgba(255,255,255,0.95); 95 margin-top:20px; 96 opacity: 0; 97 pointer-events: none; 98} 99.overlay #close 100{ 101 font-size: 2rem; 102 display: block; 103 text-align: right; 104 cursor: pointer; 105} 106.overlay ul{ 107 text-align: center; 108 margin-top: 50px; 109} 110.overlay li{ 111 margin-top: 30px; 112} 113 114.overlay.show{ 115 opacity: 1; 116 pointer-events: auto; 117} 118.sp-menu #open.hide{ 119 display: none; 120} 121

javascript

1'use strict'; 2 3{ 4 const open = document.getElementById('open'); 5 const overlay = document.querySelector('.overlay'); 6 const close = document.getElementById('close'); 7 8 9 open.addEventListener('click', () => { 10 overlay.classList.add('show'); 11 open.classList.add('hide'); 12 13 14 }); 15 16 close.addEventListener('click', () => { 17 overlay.classList.remove('show'); 18 open.classList.remove('hide'); 19 20 21 }); 22}
Lhankor_Mhy👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

slemntqe

2022/08/10 04:39

「効か亡くなって」「半バーガーメニュー」誤字が目立ちますので訂正して下さい。
slemntqe

2022/08/10 04:40

「色々試しましたが」具体的に何を試しましたか?明示して下さい。
slemntqe

2022/08/10 04:42

main.jsが質問本文に含まれてないのは何故でしょうか。HTMLで参照されている以上質問本文に加えるべきです。main.jsが今回の問題に関係があるかどうかを判断するのは回答者です。main.jsが提示されない限りmain.jsが問題の原因である可能性が消えません。
negiremi

2022/08/10 04:49

お返事ありがとうございます。失礼いたしました。内容修正させて頂きました。
guest

回答1

0

ベストアンサー

 中央に寄せることができれば解決であれば、以下のCSSを追加してみるとどうでしょうか?

css

1header { 2 display: flex; 3 justify-content: space-between; 4 position: fixed; 5 z-index: 999; 6 padding: 0; 7 margin: 0; 8 background: rgba(200, 255, 255, 1); 9 inset: auto 0; /* ←これ */ 10}

 メインコンテンツにヘッダーがかぶっているのも気になるのですが、そこは大丈夫ですか?


 なお、固定配置をするとマージンが効かなくなる理由は、固定配置や絶対配置の時にleft rightautoだと、margin-left: auto margin-right: automargin-left: 0 margin-right: 0として扱われる、という仕様によるものです。

(↓こちらの表の「✔ ✘ ✔ any」の欄)
https://www.w3.org/TR/css-position-3/#abspos-auto


参考:
inset - CSS: カスケーディングスタイルシート | MDN

投稿2022/08/10 05:11

編集2022/08/10 05:37
Lhankor_Mhy

総合スコア37634

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

negiremi

2022/08/11 13:31

回答ありがとうございます。 inset: auto 0; で出来ました! 固定配置するとマージンが効かなくなるのですね!勉強になりました。ありがとうございます! メインコンテンツにヘッダーが被っているとのことですが、こちらのブラウザでは問題がないです。 確認させて頂きます。ご指摘ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問