teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コード追記

2020/08/13 14:54

投稿

hatena19
hatena19

スコア34367

answer CHANGED
@@ -11,4 +11,122 @@
11
11
 
12
12
  [もう迷わない!CSS Flexboxの使い方を徹底解説 | Web Design Trends](https://webdesign-trends.net/entry/8148)
13
13
 
14
- [サクッとグリッドCSSを理解して、クライアントワークに使う | 新宿のホームページ制作会社 ITTI(イッティ)](https://www.itti.jp/web-design/quick-css-grid/)
14
+ [サクッとグリッドCSSを理解して、クライアントワークに使う | 新宿のホームページ制作会社 ITTI(イッティ)](https://www.itti.jp/web-design/quick-css-grid/)
15
+
16
+ ---
17
+ とりあえず理想のレイアウト画像に近いレイアウトのものを作成してみました。
18
+ 参考にしてください。
19
+
20
+ ```html
21
+ <section class="main__section">
22
+ <div class="left_col">
23
+ <h2 class="section__header">「stay Iroha as Live」</h2>
24
+ <div class="section__paragraph">
25
+ <p class="paragraph">
26
+ テキストテキストテキスト<br>
27
+ 屋久島の地で、、、いろはで、感じてほしいこと。<br>
28
+ <br>
29
+ 作っていきたい空間。<br>
30
+ 丸さんたちの想い。<br>
31
+ 暮らしの体験宿、いろは<br>
32
+ <br>
33
+ English version About iroha.
34
+ </p>
35
+ </div>
36
+ <button class="section__button">
37
+ Read more
38
+ </button>
39
+ </div>
40
+ <div class="right_col">
41
+ <img src="https://placehold.jp/600x400.png" alt="" class="section__photo">
42
+ <img src="https://placehold.jp/600x400.png" alt="" class="section__photo">
43
+ </div>
44
+ </section>
45
+ ```
46
+
47
+ ```css
48
+ .main__section {
49
+ display: flex;
50
+ position: relative;
51
+ padding: 1px;
52
+ max-width: 1200px;
53
+ margin: 0 auto;
54
+ }
55
+ .left_col {
56
+ width: 60%;
57
+ }
58
+ .right_col {
59
+ width: 38%;
60
+ }
61
+
62
+ .section__header{
63
+ font-size: 40px;
64
+ font-family: 'Toppan Bunkyu Midashi Mincho';
65
+ font-weight: bold;
66
+ letter-spacing: 0.05em;
67
+ margin-top: 20px;
68
+ margin-left: 20px;
69
+ }
70
+ .section__paragraph{
71
+ margin-top: 76px;
72
+ }
73
+ .paragraph{
74
+ font-size: 25px;
75
+ font-family: 'Toppan Bunkyu Mincho'; font-weight: normal;
76
+ line-height: 1.4;
77
+ text-shadow: 0px 3px 6px #707070;
78
+ }
79
+ .section__button{
80
+ display: block;
81
+ width: 200px;
82
+ height: 60px;
83
+ border: 1px solid;
84
+ margin: 50px auto 0;
85
+ background-color: rgba(0, 0, 0, 0);
86
+ font-family: 'Toppan Bunkyu Midashi Mincho'; font-weight: bold;
87
+ font-size: 20px;
88
+ letter-spacing: 0.1em;
89
+ }
90
+
91
+ .section__photo{
92
+ width: 100%;
93
+ height: auto;
94
+ margin-bottom: 20px;
95
+ }
96
+
97
+ .main__section::before {
98
+ content: "";
99
+ display: block;
100
+ position: absolute;
101
+ top: 20%;
102
+ bottom: 20%;
103
+ left: 20%;
104
+ right: 20%;
105
+ background-color: #F8F8F8;
106
+ z-index: -1;
107
+ }
108
+ ```
109
+ [Codepenサンプル](https://codepen.io/hatena19/pen/XWdXVQw)
110
+
111
+ こまかい位置やサイズは適当ですのでお好みで修正してください。
112
+
113
+ ** HTML **
114
+ `main__section`クラスの子要素に、`left_col` `right_col`クラス要素を追加した。2カラムになるように`main__section`クラスに display: flex; を設定した。
115
+
116
+ `left_col`内には、テキストの要素群を配置し、`right_col`内には画像(img要素)を配置した。
117
+
118
+ `section__graybox`は装飾のみの要素であり、コンテンツとしては無意味なものなので削除しました。
119
+ この装飾はCSSの方で実装します。
120
+
121
+ ** CSS **
122
+ 全体のコンテナである `.main__section` の幅は、`max-width` で最大幅のみ設定します。
123
+ `margin: 0 auto;`で左右中央寄せします。
124
+
125
+ 全体のレイアウトを決める幅などは % で指定します。
126
+ 各要素間の余白はmarginで固定値で設定します。
127
+ こうすることで、レスポンシブ対応(画面幅が変わってもおおきくレイアウトがくずれない)できます。
128
+
129
+ 背面のグレーのブロック(`section__graybox`で実装されていたもの)は、`::before`疑似要素で実装しました。このような他の要素に重ねるものは`position: absolute;`で配置します。
130
+
131
+ スマホ用には別にメディアクエリを使って1カラムのレイアウトにするといいと思います。
132
+ スマホ用にどのようなレイアウトをお望みかは不明なので、今回はこれは実装してません。

1

説明の改善

2020/08/13 14:54

投稿

hatena19
hatena19

スコア34367

answer CHANGED
@@ -1,4 +1,5 @@
1
1
  margin-top margin-left に絶対値でレイアウトの位置決めしていますが、これはやめましょう。
2
+ (各要素間の余白のためにmarginを使うのはいいですが、レイアウトのために使うのはNGです。)
2
3
  同様に、position: absolute; での位置決めもやめましょう。
3
4
  これらを使うと、レスポンシブに対応させるのは困難になります。
4
5