0
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 5 years have passed since last update.

CSSレイアウト:ケースに応じた実装方法(メモ)

Last updated at Posted at 2020-06-27

ケース1. 画像に重ねる形で、ボタンや文字を配置。レスポンシブで画像と文字やボタンもリサイズ・移動。

  • コンテナ内にimg(picture/figure)とボタン、文字を含める。
  • 重ねる要素は、position:absolute。
  • background-imageを使うと、リサイズ・再配置の同期が難しい。
tag.html

<section>
 <img src="xxxx.svg" />
 <h1>タイトル</h2>
 <button>ボタン</button>
</section>

style.css
section {
 /* 子要素のabsoluteを有効にする為 */
 position: relative;
 width: 100vw;
}

section img {
  /* コンテナのすべての領域に画像表示 */
  width: 100%;
  height: auto;
  max-width: 100%;
  max-height: 100%;
}

section h1, button {

  /* 文字サイズ(画面の横サイズを基準とする)vwで全体のサイズに応じて変更。 */
  font-size: [任意の数字]vw; 

  /* 重ねる */
  position: absolute;

  /* 上からの位置 */
  top: [任意の数値]%;

  /* absoluteでの中央配置 */
  left: 50%;
  transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
}

0
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
0
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?