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

【CSS】iOS_Safariなどでコンテンツの幅がオーバーしてしまう時の対処方法

Posted at

ページ制作時にブラウザの違いで
デザインが微妙に変わってくるのは良くあります。

その中でもiOS Safariなどで
コンテンツの幅がオーバーした時に対処した方法です。
幅100指定で横スクロールが出ると結構カッコ悪いです。
※iOSに限らない課題でもあります。
haba.png

paddingやborderの幅の解釈の違い

レスポンシブを意識してコーディングしているのに
はみ出てしまう。その要因の1つとして考えられるのが
幅指定とpaddingの解釈です。
※ボックスモデルという各領域の解釈

例:

<div style="width:100%;padding:10px;">
TEXT
</div>

このような書き方ですと幅100%に加えて10pxの
内部余白を付けるという意味合いになります。

回避1: 幅指定とpadding指定のボックスは分ける

この場合は幅100%のボックスの中に、
余白10px付きのものがあるという意味合いに
なりますのではみ出ません。

<div style="width:100%;">
	<div style="padding:10px;">
	TEXT
	</div>
</div>

回避2: box-sizingプロパティを使う

これはborderとpaddingの幅も含めてその要素の
サイズですと指示するためのものです。
paddingの10pxはあくまで100%の中の幅として
認識されます。

<div style="width:100%;padding:10px;box-sizing:border-box;">
TEXT
</div>

まとめ

個人的に、この幅とposition(配置)辺りは
CSSの中でも特にわかり辛いなと思っています。
だからこそ実際に複数のパターンを書いてみて、
表示してみて覚える事をオススメします。

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?