1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[HTML/CSS] 画像の上に文字を重ねる方法

Posted at

画像と文字を表示する

今回はclass名を box としました。
box classの中に、表示させたい画像と文字を入れます。

html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/main.css">
    <title>画像の上に文字を重ねる方法</title>
</head>
<body>
    <div class="box">
        <img src="./img/dog.png">
        <p>Hello!</p>
    </div>
</body>
</html>

CSSで画像の上に文字を重ねる

以下を追記します。
absoluteは、親要素を基準に、絶対的な位置を決めるので、top: 0; left: 0; を指定すると、文字は box classの左上に配置されます。
例えば、top: 50px; left: 100px; とすれば、文字は box classの上から50px、左から100pxの位置に配置されます。

CSS
.box {
   position: relative;
}

.box p {
   position: absolute;
   top: 0;
   left: 0;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?