wp_enqueue_styleにメディアクエリ指定をする

wp_enqueue_styleにメディアクエリ指定をする

レスポンシブの効率的なスタイルシートには、いつまで立っても頭を悩まされている私ですが、functions.phpでwp_enqueue_style()関数を使ってCSSを読み込む際に、メディアクエリ指定したいケースがあり、調べてみました。簡単です。

wp_enqueue_style('html5blank', false, array(), false, 'all and (min-width: 1200px)');

エンキュースタイルの書式は
<?php wp_enqueue_style( $handle, $src, $deps, $ver, $media ); ?>
ですので、$mediaの箇所にメディアクエリが入ります。

エンキューと似た形式で、wordpressにはwp_register_style()という関数もあるのですが、これはwordpressにCSSを登録する関数です。予めwp_register_style()で登録したCSSは、wp_enqueue_style()にhandle名を渡すだけで、読み込まれます。