html - float 数字旁边的 CSS/html 中心文本

标签 html css sass

我有一个元素列表,其中的数字和文本相互堆叠 - 我正在努力尝试将文本与它并排放置的数字旁边垂直对齐。我的数字和文本只是 float 在一个 div 中 - 可能我需要采取不同的方法?请在此处查看工作示例 - https://codepen.io/ajmajma/pen/gRpxrQ?editors=1100

我的元素 html 结构如下:

  <div class="featured-posts wrapper">
  <div class="featured-posts--posts row">
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">1</span><a href="/blog/1">10 Tips to Avoid Dry Flaky Winter Skin, Safely</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">2</span><a href="/blog/2">10 Ways to Spa at Home</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">3</span><a href="/blog/3">13 Healthy Ways Winterize Your Routine</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">4</span><a href="/blog/4">18 Things to Love About Our Follain Soap</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">5</span><a href="/blog/5">&lt;3 at First Site</a></div>
      </div>
    </div>
  </div>
</div>

CSS (SCSS) 看起来像这样:

.featured-posts {
   counter-reset: section;

   .featured-posts--posts {
     .featured-post {
       margin-bottom: 40px;
       width: 100%;

       .featured-post--wrapper {
         margin: 0 auto;
       }

       .featured-post--count {
         width: 45px;
         float: left;
         color: red;
         height: 100%;
         display: block;
          font-size: 30px;
       }

       .featured-post--body {
         width: 350px;
         max-width: 100%;
         margin: 0 auto;
         color: black;

         a {
           cursor: pointer;
           text-decoration: none;
           color: black;
           transition: color 0.2s ease-in;


           &:hover {
             color: red;
           }
         }
       }
     }
   }
 }

无论文本的长度是多少,我都试图让它与“计数”(左边的数字)垂直对齐。欢迎任何意见!谢谢。

最佳答案

我会使用 display: flex; align-items: center; 在父元素上

.wrapper {
  margin: 0 auto;
  max-width: 600px;
}

.featured-posts {
  counter-reset: section;
}
.featured-posts .featured-posts--posts .featured-post {
  margin-bottom: 40px;
  width: 100%;
}
.featured-posts .featured-posts--posts .featured-post .featured-post--wrapper {
  margin: 0 auto;
}
.featured-posts .featured-posts--posts .featured-post .featured-post--count {
  width: 45px;
  color: red;
  height: 100%;
  display: block;
  font-size: 30px;
}
.featured-posts .featured-posts--posts .featured-post .featured-post--body {
  width: 350px;
  max-width: 100%;
  margin: 0 auto;
  color: black;
  display: flex;
  align-items: center;
}
.featured-posts .featured-posts--posts .featured-post .featured-post--body a {
  cursor: pointer;
  text-decoration: none;
  color: black;
  transition: color 0.2s ease-in;
}
.featured-posts .featured-posts--posts .featured-post .featured-post--body a:hover {
  color: red;
}
<div class="featured-posts wrapper">
  <div class="featured-posts--posts row">
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">1</span><a href="/blog/1">10 Tips to Avoid Dry Flaky Winter Skin, Safely</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">2</span><a href="/blog/2">10 Ways to Spa at Home</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">3</span><a href="/blog/3">13 Healthy Ways Winterize Your Routine</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">4</span><a href="/blog/4">18 Things to Love About Our Follain Soap</a></div>
      </div>
    </div>
    <div class="featured-post col-12">
      <div class="featured-post--wrapper">
        <div class="featured-post--body"><span class="featured-post--count">5</span><a href="/blog/5">&lt;3 at First Site</a></div>
      </div>
    </div>
  </div>
</div>

关于html - float 数字旁边的 CSS/html 中心文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44398752/

相关文章:

javascript - 延迟突出显示文本区域中的文本

css - 如何使用 CSS 隐藏闪烁的文本字段光标?

css - 使用 Button React 更改 .scss

css - 缩小 css + Gulp

css - 如何更改 SASS 的 CSS 输出格式样式

Javascript,语法返回两个不同的值?

javascript - 除了 Window.location.href 之外还有其他方法吗?

html - 使边框宽度响应

javascript - 在 html 类中使用函数的 Svelte 错误

php - CSS 不适用于 PHP 脚本