回答編集履歴

2

2019/10/18 10:03

投稿

azuapricot
azuapricot

スコア2341

test CHANGED
@@ -47,3 +47,193 @@
47
47
 
48
48
 
49
49
  [position系の参考サイト](https://saruwakakun.com/html-css/basic/relative-absolute-fixed)
50
+
51
+
52
+
53
+
54
+
55
+ ---
56
+
57
+
58
+
59
+ ### コメントから追記
60
+
61
+
62
+
63
+ ```CSS
64
+
65
+
66
+
67
+ .profile {
68
+
69
+ position:relative;
70
+
71
+ }
72
+
73
+
74
+
75
+ .profile h3 {
76
+
77
+ position: absolute;
78
+
79
+ top: 30px;
80
+
81
+ left:140px;
82
+
83
+ }
84
+
85
+
86
+
87
+ .profile img {
88
+
89
+ border-radius: 50%;
90
+
91
+ width: 100px;
92
+
93
+ height: 100px;
94
+
95
+ top: 10000px;
96
+
97
+ left: 100000px;  /* 別に効いてない */
98
+
99
+ }
100
+
101
+ ```
102
+
103
+
104
+
105
+ さきほどの回答したコードは手抜きでtop,leftを消してなかっただけです。
106
+
107
+
108
+
109
+ imgタグにもし position:absoluteをつけるとどうなるかわかりますでしょうか。
110
+
111
+
112
+
113
+ position:absolute をすると、その要素の高さが無視されるので、こうなります。
114
+
115
+
116
+
117
+ ```CSS
118
+
119
+ .profile {
120
+
121
+ position:relative;
122
+
123
+ }
124
+
125
+
126
+
127
+ .profile h3 {
128
+
129
+ position: absolute;
130
+
131
+ top: 30px;
132
+
133
+ left:140px;
134
+
135
+ }
136
+
137
+
138
+
139
+ .profile img {
140
+
141
+ position:absolute; /* 絶対位置を指定してみた*/
142
+
143
+ border-radius: 50%;
144
+
145
+ width: 100px;
146
+
147
+ height: 100px;
148
+
149
+ top: 0px;
150
+
151
+ left: 00px;
152
+
153
+ }
154
+
155
+
156
+
157
+ ```
158
+
159
+
160
+
161
+ ![イメージ説明](8fc4b4c8b8b5ada83daa69b4bb85f425.png)
162
+
163
+
164
+
165
+ ---
166
+
167
+
168
+
169
+ こうなってしまう理由は、position:absoluteを使うと他の要素は absoluteを指定した要素の高さの影響を受けなくなるからです。
170
+
171
+ ⇒だから横に重ねられるんですねー。 まぁabsoluteを使うと要素が『浮く』んだと思ってもらうのがわかりやすいかもしれません。
172
+
173
+
174
+
175
+
176
+
177
+ では、逆に今回質問者さんが困っていた position:relativeの隙間問題を応用するとこんなこともできます。
178
+
179
+
180
+
181
+ ```CSS
182
+
183
+ .profile {
184
+
185
+ position:relative;
186
+
187
+ }
188
+
189
+
190
+
191
+ .profile h3 {
192
+
193
+ position: absolute;
194
+
195
+ top: 30px;
196
+
197
+ left:140px;
198
+
199
+ }
200
+
201
+
202
+
203
+ .profile img {
204
+
205
+ position:relative;  /* relativeを指定してみた */
206
+
207
+ border-radius: 50%;
208
+
209
+ width: 100px;
210
+
211
+ height: 100px;
212
+
213
+ top: 150px; /* 元居た位置から150pxずらす */
214
+
215
+ left: 150px; /* 元居た位置から150pxずらす */
216
+
217
+ }
218
+
219
+ ```
220
+
221
+
222
+
223
+
224
+
225
+ ![イメージ説明](fca01693e42b9fa47ef072cd21bcdffc.png)
226
+
227
+
228
+
229
+
230
+
231
+ 要素(imgタグ)の高さを維持したまま場所が移動してうさちゃんが移動しましたーカワイイー
232
+
233
+
234
+
235
+ 自分でいろいろ変えてみると理解が深まると思います。
236
+
237
+
238
+
239
+ 興味があるうちに色々いじってみましょうー

1

2019/10/18 10:03

投稿

azuapricot
azuapricot

スコア2341

test CHANGED
@@ -43,3 +43,7 @@
43
43
  }
44
44
 
45
45
  ```
46
+
47
+
48
+
49
+ [position系の参考サイト](https://saruwakakun.com/html-css/basic/relative-absolute-fixed)