html - 无法在容器内垂直居中文本

标签 html css wordpress

我在容器中垂直居中文本时遇到了一些问题。我试过设置 display: inline-block;和垂直对齐:中间;但它似乎不起作用。非常感谢任何帮助!

enter image description here

HTML:

<div class="col2" data-equal="div">
    <h1>Upcoming Events</h1>

    <?php query_posts('post_type=event&posts_per_page=4'); ?>
    <?php while (have_posts()) : the_post(); ?>

    <a href="<?php the_permalink(); ?>">
      <div class="event-container group">
        <div class="col1">
          <p>NOV</p>
          <p>17</p>
          <p>TUE</p>
        </div>

        <div class="col2">
          <h2><?php the_title(); ?></h2>
          <p><?php the_field('time'); ?></p>
        </div>
      </div>
    </a>

    <?php endwhile; wp_reset_query(); ?>
  </div>

CSS:

.news-events .col2 .event-container {
  background-color: #ECF0F1;
  border-radius: 3px;
  padding: 5px 15px;
  margin-bottom: 25px;
  font-family: Verdana;
}

.news-events .col2 .event-container .col1 {
  width: 15%;
  display: inline-block;
  vertical-align: middle;
  margin: 0 auto;
}

.news-events .col2 .event-container .col1 p {
  vertical-align: middle;
}

.news-events .col2 .event-container .col2 {
  width: 85%;
  display: inline-block;
  margin: 0 auto;
  vertical-align: middle;
}

.news-events .col2 .event-container h2 {
  font-weight: 400;
  font-family: 'Merriweather';
  margin-top: 10px;
  margin-bottom: 5px; 
}

.news-events .col2 .event-container p {
  margin-top: 5px;
  margin-bottom: 5px;
}

最佳答案

vertical-align: middle 属性与 display:table-cell 一起使用。一起尝试。

关于html - 无法在容器内垂直居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34141046/

相关文章:

html - flexbox - 如何通过命题根据列的宽度调整居中椭圆的大小?

javascript - 获取不为空的文本字段

javascript - 如何从数组创建多个 HTML 元素?

css - 动画关键帧的 scss 函数

php - Wordpress 插件处于事件状态但未在管理工具中检查

php - 从 functions.php 或 style.css 设置 'Update' 或 'Publish' WP 按钮的样式

javascript - 可以将 Wordpress 插件转换为在非 WP 网站上使用吗?

html - 在 Bootstrap 上调整大小时如何将页脚保持在页面底部

javascript - 基于 Chromium 的浏览器中奇怪的 JavaScript 错误

html - 如何强制父 div 水平拉伸(stretch)以容纳 float 的子 div?