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

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

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

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

CSS

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

Q&A

解決済

1回答

1648閲覧

Bootstrapでメディアクエリが効かない

teltel07

総合スコア42

HTML5

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

CSS

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

0グッド

0クリップ

投稿2020/12/14 15:26

Bootstrapのcardの列数を画面の幅に応じて3個、2個、1個としたく、
公式ドキュメントのカードの最下部にある下記のような書き方を試してみました。

イメージ説明

.card-columns { @include media-breakpoint-only(md) { column-count: 2; } @include media-breakpoint-only(xl) { column-count: 5; } }

が、少しづつ画面を狭くしても3個→1個になってしまいます!
イメージ説明
↓↓↓↓↓
イメージ説明

ソースコードの全体は以下の通りです。
BootstrapのテンプレートにCard要素を丸コピペしています。

index.html

1<!doctype html> 2<html lang="ja"> 3 <head> 4 <!-- Required meta tags --> 5 <meta charset="utf-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 7 8 <!-- Bootstrap CSS --> 9 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> 10 11 <title>Hello, world!</title> 12 </head> 13 <body> 14 <h1>Hello, world!</h1> 15 <div class="card-columns"> 16 <div class="card"> 17 <svg class="bd-placeholder-img card-img-top" width="100%" height="160" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Image cap"><title>Placeholder</title><rect width="100%" height="100%" fill="#868e96"/><text x="50%" y="50%" fill="#dee2e6" dy=".3em">Image cap</text></svg> 18 <div class="card-body"> 19 <h5 class="card-title">Card title that wraps to a new line</h5> 20 <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p> 21 </div> 22 </div> 23 <div class="card p-3"> 24 <blockquote class="blockquote mb-0 card-body"> 25 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p> 26 <footer class="blockquote-footer"> 27 <small class="text-muted"> 28 Someone famous in <cite title="Source Title">Source Title</cite> 29 </small> 30 </footer> 31 </blockquote> 32 </div> 33 <div class="card"> 34 <svg class="bd-placeholder-img card-img-top" width="100%" height="160" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Image cap"><title>Placeholder</title><rect width="100%" height="100%" fill="#868e96"/><text x="50%" y="50%" fill="#dee2e6" dy=".3em">Image cap</text></svg> 35 <div class="card-body"> 36 <h5 class="card-title">Card title</h5> 37 <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p> 38 <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> 39 </div> 40 </div> 41 <div class="card bg-primary text-white text-center p-3"> 42 <blockquote class="blockquote mb-0"> 43 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat.</p> 44 <footer class="blockquote-footer text-white"> 45 <small> 46 Someone famous in <cite title="Source Title">Source Title</cite> 47 </small> 48 </footer> 49 </blockquote> 50 </div> 51 <div class="card text-center"> 52 <div class="card-body"> 53 <h5 class="card-title">Card title</h5> 54 <p class="card-text">This card has a regular title and short paragraphy of text below it.</p> 55 <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> 56 </div> 57 </div> 58 <div class="card"> 59 <svg class="bd-placeholder-img card-img" width="100%" height="260" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Card image"><title>Placeholder</title><rect width="100%" height="100%" fill="#868e96"/><text x="50%" y="50%" fill="#dee2e6" dy=".3em">Card image</text></svg> 60 </div> 61 <div class="card p-3 text-right"> 62 <blockquote class="blockquote mb-0"> 63 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p> 64 <footer class="blockquote-footer"> 65 <small class="text-muted"> 66 Someone famous in <cite title="Source Title">Source Title</cite> 67 </small> 68 </footer> 69 </blockquote> 70 </div> 71 <div class="card"> 72 <div class="card-body"> 73 <h5 class="card-title">Card title</h5> 74 <p class="card-text">This is another card with title and supporting text below. This card has some additional content to make it slightly taller overall.</p> 75 <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> 76 </div> 77 </div> 78 </div> 79 <!-- Optional JavaScript --> 80 <!-- jQuery first, then Popper.js, then Bootstrap JS --> 81 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> 82 <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> 83 <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script> 84 </body> 85 <style> 86.card-columns { 87 @include media-breakpoint-only(md) { 88 column-count: 2; 89 } 90 @include media-breakpoint-only(xl) { 91 column-count: 5; 92 } 93} 94 </style> 95</html> 96

試したこと

メディアクエリにあまり詳しくありませんが、調べているとCASSやSCSSの書き方なのかな?と思い、
以下を試したりしました。

<style lang='scss'>
<style lang='sass'>

よろしくお願い致します。

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

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

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

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

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

guest

回答1

0

ベストアンサー

SCSSはブラウザでは実行できませんので、CSSにコンパイルするところから始めてください。
Bootstrap 4のCSSコンパイルをGulpで管理する方法 - Qiita

または、SCSSでの記述をやめて、普通にCSSのメディアクエリで書くのもいいかと思います。


なお、Bootstrap5では削除されています。

In v4 we used a CSS-only technique to mimic the behavior of Masonry-like columns, but this technique came with lots of unpleasant side effects. If you want to have this type of layout in v5, you can just make use of Masonry plugin. Masonry is not included in Bootstrap, but we’ve made a demo example to help you get started.

Cards · Bootstrap v5.0

投稿2020/12/14 16:40

編集2020/12/14 16:44
Lhankor_Mhy

総合スコア36149

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

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

teltel07

2020/12/15 11:46

CSSにコンパイルが必要なのですね!ありがとうございます! Gulpインストールしようとしたのですが、そこでもエラーとなり、 沼にハマりそうだったので、CSSでメディアクエリ書いて動かしました!! ご回答ありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問