質問をすることでしか得られない、回答やアドバイスがある。

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

新規登録して質問してみよう
ただいま回答率
85.31%
HTML5

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

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Q&A

解決済

1回答

1340閲覧

HTMLで親要素の高さが子要素に合わせることができない。

shihominorth

総合スコア46

HTML5

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

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

0グッド

0クリップ

投稿2022/03/15 05:30

前提・実現したいこと

responsive対応の時HTML/CSSで子要素に合わせて親要素の高さを可変にしたい。

  • #topを可変にしたい。
  • できれば.top-contentも

発生している問題・エラーメッセージ

子要素の高さを指定しているのにも関わらず、親要素の高さが0になる

該当のソースコード

HTML

1 <main> 2 <div id="top"> 3 <div class="top-content"> 4 <h1>Tastybox</h1> 5 <p> 6 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vitae 7 molestie dolor. Donec vel pretium velit. Pellentesque consectetur 8 dictum urna et mattis. Mauris vel tempor tortor, id viverra ipsum. 9 Nunc in molestie leo. Etiam fermentum, leo consequat posuere 10 ultricies, felis ante placerat ligula, vitae sodales turpis sapien 11 non lacus. Fusce vitae nunc quis elit malesuada sodales. Cras lorem 12 lacus, tincidunt non aliquam quis, eleifend eget nisl. Aenean 13 blandit tortor sit amet odio porttitor, id posuere felis laoreet. 14 Nullam venenatis turpis eros. Mauris egestas luctus finibus. Donec 15 semper varius fermentum. 16 </p> 17 <div class="top-buttons"> 18 <button 19 type="button" 20 class="btn btn-outline-primary btn-lg login-button" 21 onclick="location.href='login.html'" 22 > 23 Login 24 </button> 25 <button 26 type="button" 27 class="btn btn-outline-primary btn-lg new-account-button" 28 > 29 Create new account 30 </button> 31 </div> 32 </div> 33 </div> 34 35<省略> 36 37<main>

CSS

1@media screen and (max-width: 800px) { 2 main { 3 height: 100%; 4 width: 100%; 5 } 6 7 #top { 8 display: flex; 9 flex-direction: column; 10 height: 100%; 11 width: 100%; 12 } 13 14 #top .top-content { 15 /* padding: 10px; */ 16 height: 2000px; 17 } 18 19 #top .top-content h1 { 20 height: 50px; 21 margin-top: 30px; 22 margin-bottom: 15px; 23 } 24 25 #top .top-content p { 26 height: 500px; 27 margin-top: 15px; 28 } 29 30 #top .top-content .top-buttons button { 31 width: 100%; 32 height: 40px; 33 margin: 0; 34 padding: 0; 35 margin-bottom: 10px; 36 } 37} 38 39main { 40 width: 100%; 41 height: 3646px; 42 /* height: calc(166px + 700px + 500px); */ 43 position: relative; 44 padding-bottom: 10px; 45} 46 47#top { 48 background-image: url("../images/background-img.png"); 49 width: auto; 50 height: 700px; 51 position: relative; 52} 53 54#top::after { 55 background-color: rgba(254, 216, 177, 0.5); 56 content: ""; 57 position: absolute; 58 left: 0; 59 right: 0; 60 top: 0; 61 bottom: 0; 62} 63 64#top .top-content { 65 position: absolute; 66 top: 0; 67 right: 0; 68 bottom: 0; 69 left: 0; 70 margin: auto; 71 max-width: 80vw; 72 height: 400px; 73 text-align: center; 74 color: white; 75 z-index: 1; 76} 77 78#top .top-content h1 { 79 height: 100px; 80 font-family: "Balsamiq Sans", cursive; 81} 82

試したこと

  • #top, .top-contentのheightをautoまたは100%

補足情報(FW/ツールのバージョンなど)

  • Mac , VSCode

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

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

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

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

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

guest

回答1

0

ベストアンサー

CSS

1#top .top-content { 2 position: absolute;

↑この部分で絶対配置にしているのが原因です。
絶対配置にすると通常のフローから外れるため、内容物とみなされなくなり、親要素はその分の高さを失います。
CSSでの回避方法はないです。

解決方法としては、position: absoluteを削除して、別の方法で中央配置をすることです。

absolute は使うな | 初心者向けCSSべからず集・すべし集 - Qiita

絶対配置の子要素サイズに親要素のサイズを合わせる | 初心者向けCSSべからず集・すべし集 - Qiita

投稿2022/03/15 05:59

Lhankor_Mhy

総合スコア37426

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

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

shihominorth

2022/03/16 02:56

ご回答ありがとうございます! 絶対配置にすると通常のフローから外れるため、内容物とみなされなくなり、親要素はその分の高さを失います。 CSSでの回避方法はないです。 こちら知りませんでした... 参考になるURLもご教示いただきありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問