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

CSSで横スクロール、でも文章は折り返したい

Posted at

やりたかったこと

シンプルにhtml + cssで横スクロールを作りたい。
イメージとしてはこんな感じ。
ポイントはコンテンツの下に長い文章があるという点。
image2

調べたら出てきたやり方

様々な記事を参照してみると、ulタグとliタグを用いてそれぞれにoverflow-x: auto;white-space: nowrap;display: inline-block;を指定する、という手法が多く紹介されていた。

scroll-example.html
<ul class="horizontal-list">
	<li class="item">
		<div class="contents-1">コンテンツ1</div>
		<div class="long-sentence">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
	</li>
	<li class="item">
		<div class="contents-2">コンテンツ2</div>
		<div class="long-sentence">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
	</li>
	<li class="item">
		<div class="contents-3">コンテンツ3</div>
		<div class="long-sentence">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
	</li>
</ul>
stylesheet.css
.horizontal-list {
	overflow-x: auto;
	white-space: nowrap;
}

.item {
	display: inline-block;
	width: 90%;
    margin-right: 20px;
}

.contents-1 {
	height: 500px;
	background-color: #2098E3;
}

.contents-2 {
	height: 500px;
	background-color: #E3E020;
}

.contents-3 {
	height: 500px;
	background-color: #14E3C4;
}

.long-sentence {
	font-size: 16px;
	line-height: 24px;
	width: 100%;
}

しかし、これだと長い文章が折り返されない。

image01

解決策

ここで、html, cssを次のように修正する。
ulタグやliタグを用いずに、Flexboxを用いて作っていく。

scroll-example-rev.html
<div class="horizontal-list">
	<div class="item">
		<div class="contents-1">コンテンツ1</div>
		<div class="long-sentence">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
	</div>
	<div class="item">
		<div class="contents-2">コンテンツ2</div>
		<div class="long-sentence">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
	</div>
	<div class="item">
		<div class="contents-3">コンテンツ3</div>
		<div class="long-sentence">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
	</div>
</div>
stylesheet-rev.css
.horizontal-list {
	display: flex;
	overflow-x: auto;
}

.item {
	flex-shrink: 0;
	width: 90%;
    margin-right: 20px;
}

.contents-1 {
	height: 500px;
	background-color: #2098E3;
}

.contents-2 {
	height: 500px;
	background-color: #E3E020;
}

.contents-3 {
	height: 500px;
	background-color: #14E3C4;
}

.long-sentence {
	font-size: 16px;
	line-height: 24px;
	width: 100%;
}

こうするとちゃんと意図した通りのものがつくれました。めでたしめでたし。
image2

参考

日本語対応!CSS Flexboxのチートシートを作ったので配布します
CSSだけでスマホの横スクロールを実装する - 株式会社TORAT

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?