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

回答編集履歴

2

2019/10/18 10:03

投稿

azuapricot
azuapricot

スコア2343

answer CHANGED
@@ -22,4 +22,99 @@
22
22
  }
23
23
  ```
24
24
 
25
- [position系の参考サイト](https://saruwakakun.com/html-css/basic/relative-absolute-fixed)
25
+ [position系の参考サイト](https://saruwakakun.com/html-css/basic/relative-absolute-fixed)
26
+
27
+
28
+ ---
29
+
30
+ ### コメントから追記
31
+
32
+ ```CSS
33
+
34
+ .profile {
35
+ position:relative;
36
+ }
37
+
38
+ .profile h3 {
39
+ position: absolute;
40
+ top: 30px;
41
+ left:140px;
42
+ }
43
+
44
+ .profile img {
45
+ border-radius: 50%;
46
+ width: 100px;
47
+ height: 100px;
48
+ top: 10000px;
49
+ left: 100000px;  /* 別に効いてない */
50
+ }
51
+ ```
52
+
53
+ さきほどの回答したコードは手抜きでtop,leftを消してなかっただけです。
54
+
55
+ imgタグにもし position:absoluteをつけるとどうなるかわかりますでしょうか。
56
+
57
+ position:absolute をすると、その要素の高さが無視されるので、こうなります。
58
+
59
+ ```CSS
60
+ .profile {
61
+ position:relative;
62
+ }
63
+
64
+ .profile h3 {
65
+ position: absolute;
66
+ top: 30px;
67
+ left:140px;
68
+ }
69
+
70
+ .profile img {
71
+ position:absolute; /* 絶対位置を指定してみた*/
72
+ border-radius: 50%;
73
+ width: 100px;
74
+ height: 100px;
75
+ top: 0px;
76
+ left: 00px;
77
+ }
78
+
79
+ ```
80
+
81
+ ![イメージ説明](8fc4b4c8b8b5ada83daa69b4bb85f425.png)
82
+
83
+ ---
84
+
85
+ こうなってしまう理由は、position:absoluteを使うと他の要素は absoluteを指定した要素の高さの影響を受けなくなるからです。
86
+ ⇒だから横に重ねられるんですねー。 まぁabsoluteを使うと要素が『浮く』んだと思ってもらうのがわかりやすいかもしれません。
87
+
88
+
89
+ では、逆に今回質問者さんが困っていた position:relativeの隙間問題を応用するとこんなこともできます。
90
+
91
+ ```CSS
92
+ .profile {
93
+ position:relative;
94
+ }
95
+
96
+ .profile h3 {
97
+ position: absolute;
98
+ top: 30px;
99
+ left:140px;
100
+ }
101
+
102
+ .profile img {
103
+ position:relative;  /* relativeを指定してみた */
104
+ border-radius: 50%;
105
+ width: 100px;
106
+ height: 100px;
107
+ top: 150px; /* 元居た位置から150pxずらす */
108
+ left: 150px; /* 元居た位置から150pxずらす */
109
+ }
110
+ ```
111
+
112
+
113
+ ![イメージ説明](fca01693e42b9fa47ef072cd21bcdffc.png)
114
+
115
+
116
+ 要素(imgタグ)の高さを維持したまま場所が移動してうさちゃんが移動しましたーカワイイー
117
+
118
+ 自分でいろいろ変えてみると理解が深まると思います。
119
+
120
+ 興味があるうちに色々いじってみましょうー

1

2019/10/18 10:03

投稿

azuapricot
azuapricot

スコア2343

answer CHANGED
@@ -20,4 +20,6 @@
20
20
  top: 0px;
21
21
  left: 0px;
22
22
  }
23
- ```
23
+ ```
24
+
25
+ [position系の参考サイト](https://saruwakakun.com/html-css/basic/relative-absolute-fixed)