LoginSignup
1
1

More than 3 years have passed since last update.

【CSS】z-index ではみ出しを防ぐ (重ねる)

Last updated at Posted at 2020-04-16

制作中アプリにて、
写真をアップし、その場でプレビューする機能を実装中に、
z-indexを久しぶりに使ったので、
備忘録も兼ねて、ざくっとご紹介します。

解決したい事

下記のように、写真が来ると文字が枠外にはみ出てしまいます。
Image from Gyazo

< 完成形 >

写真はテキストの上に乗ってもらいます。
Image from Gyazo

z-index プロパティ

ボックスの重なる順番(優先順位)を決めれるプロパティ。
値は整数で、大きいものが手前に表示される。


cssを記述する。

まず、テキストは写真の下に入って欲しいので、
z-indexの値は 0 にします。

sample.scss
.text {      // ⬅️テキストが入っているクラス
  z-index: 0;   
  font-size: 16px;
}

次に、写真はテキスト上に重ねるので
z-indexの値を 1 にします。

sample.scss
.img {      // ⬅️写真
  z-index: 1;   
  height: 145px;

}

終了です。

写真下部にある「削除」の要素は今回の記事とは関係ありません。
ご覧いただきありがとうございました。


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