0
3

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.

Google code prettifyでスクロールバーを出す方法

Posted at

ブログにソースコードを載せるとき、もともとはGoogle code prettifyを使っていたんですが、1行が長い場合(ワンライナーとかだとよくある。。。)改行されちゃうんですよね。

で、代替シンタックスハイライターとしてGistを使っていたんですが、ふたつ不満があります。

1.行数の多いコードが記事そのものを長くする
2.見た目の統一感を出すためにわずか数行のコードでGistを作るのダサい

2番目のは<code>タグで対応してもいいんですが、ハイライト効かないし、やっぱ見た目の統一感って大事だし。ということで…。

Google code prettifyで横スクロールバーを出す

よくよく調べてみたらGoogle code prettifyのスタイルシートではword-wrap:break-wordになっています。要はわざわざ折り返しするように指定してある。

そこでこれをword-wrap:normal指定に戻すと折り返しをしなくなります。で、折り返しをしなくなると自動的にオーバーフロー分に対応して横スクロールバーが出てきます。なんて簡単!

Google code prettifyで縦スクロールバーを出す

こちらはもっと簡単。ソースコードを表示している<pre class="prettyprint">ボックスに対し高さ制限を追加します。

たとえば50行もあるソースコードのうち10行以降を枠外に追いやっておきたいのであれば、height:10em指定をしておけばOK。はみ出し分は縦スクロールバーでスクロール表示できるようになります。(実際にはpaddingがあるので少し表示量は減るかも)

まとめ

Google code prettifyスクリプト読み込み指定のついでに以下のようなスタイルを追記しておきましょう。

<!-- Google code prettify start -->
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<style type="text/css">
pre.prettyprint {
  font-size:12px;
  word-wrap:normal;
}
pre.high {
  height:12em;
}
</style>
<!-- Google code prettify end -->
0
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?