3
2

More than 3 years have passed since last update.

WordPress テーマ作成時に設定しておきたいネイティブ CSS

Last updated at Posted at 2021-03-17

WordPress の独自テーマで Classic Editor を導入する際に発生する

TinyMCEエディタのボタン押したけど何も変化しないんだけど?」問題。

この問題を回避するのに必要な CSS (WordPress Native CSS)を毎回忘れるのでまとめました。

リセットしたタグの設定

リセットCSS のリセット具合によって変わりますが、以下を設定しておけばそれなりに見れるはず。

css
h2,
h3,
h4,
h5,
h6 {
  margin: 2em 0 1em;
  line-height: 1.4;
}
h2 {
  font-size: 1.8em;
  border-bottom: 1px solid #ddd;
  padding-bottom: 0.2em;
}
h3 {
  font-size: 1.6em;
}
h4 {
  font-size: 1.4em;
}
h5 {
  font-size: 1.2em;
}
h6 {
  font-size: 1em;
}
p {
  line-height: 1.8;
}
p + p {
  margin-top: 1.5em;
}
em {
  font-style: italic;
}
blockquote {
  margin: 1.5em 0;
  border-left: 5px solid #ddd;
  padding: 1em 0 1em 10px;
  color: #777;
}
blockquote p + p {
  margin-top: 0.5em;
}
ul,
ol {
  margin-top: 1.5em;
}
ul {
  list-style-type: disc;
}
ol {
  list-style-type: decimal;
}
li {
  margin-left: 1.5em;
}

WordPress が出力する class の設定

css
.aligncenter {
  display: block;
  margin-right: auto;
  margin-left: auto;
}
.alignright {
  float: right;
  margin-bottom: 20px;
  margin-left: 20px;
}
.alignleft {
  float: left;
  margin-right: 20px;
  margin-bottom: 20px;
}
.wp-caption,
[class*='wp-image'] {
  display: block;
  max-width: 100% !important;
  margin-top: 1.5em;
  text-align: center;
}
.wp-caption-text {
  margin-top: 0;
}

float の解除

左寄せ、右寄せボタンを押すと float が設定されるため、
後続の要素で clear が必要です。
がしかし、後続の要素は回り込ませる必要もあるため、<?php the_content(); ?> の親要素に clear の設定をしておきます。

php
<article>
  <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
          <h2><?php the_title(); ?></h2>
          <div class="articleBody"><?php the_content(); ?></div>
      <?php endwhile; ?>
  <?php else : ?>
      Not Found
  <?php endif; ?>
</article>
css
.articleBody {
  display: flow-root;
}

/* IE11 を含む場合 */
.articleBody::after {
  display: block;
  clear: both;
  content: '';
}
3
2
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
3
2