简体   繁体   中英

<a> tag and <p> have different heights and alignments in flex container

I'm building a bottom-nav of sorts for a blog page. In the nav, you have the option to navigate to the last blog entry, the blog home/index, and the next blog post. In the event that you're at the first or last blog post, instead of rendering an <a> I'm rendering a <p> with text indicating that there are no more posts. I chose this approach because apparently it's confusing (accessibility-wise) to render a link that's inactive.

The issue I'm running into is that the text in the anchor elements (or the elements themselves) are a slightly different height than the paragraph element - a 3px difference. I'll attach a picture for reference. It seems undesirable to manually set the height(s) of the elements to make them match. I've tried playing with padding and margins, although the inspector shows no padding or margins. I've also manually set the height on the <p> element to match the height of the <a> elements but it makes the alignment funky. Any ideas why this is? I've tried some creative googling but I'm not getting anything relevant.

Photo of the issue: Note the white space above and below the first two elements (which are anchor elements) and the white space below the last element, which is a paragraph element. 水平导航包含 3 个元素,其中前两个是锚标签,分别链接到最后一篇博文和博文索引。最后一个元素是段落元素,而不是禁用的锚标记。前两个元素的上方和下方都有空格,而段落元素下方只有空格。此外,锚元素出现在同一水平轴上,而最后一个元素未在同一轴上正确居中。

Relevant code: (Please note that the <Link> elements render as <a> elements.)

<footer className='blog-post-footer'>
  <nav>
    <ul>
      <li>
        {previous ? (
          <Link to={`../${previous.frontmatter.slug}`} rel='prev'>
            ← {previous.frontmatter.title}
          </Link>
        ) : (
           <p>You're on the first post.</p>
         )}
       </li>
       <li>
         <Link to='/blog'>
           Blog Home
         </Link>
       </li>
       <li>
         {next ? (
           <Link to={`../${next.frontmatter.slug}`} rel='next'>
             {next.frontmatter.title} →
           </Link>
        ) : (
          <p>No next post...yet.</p>
        )}
      </li>
    </ul>
  </nav>
</footer>

And the CSS:

.blog-post-footer {
  box-sizing: border-box;
  text-align: center;
}

.blog-post-footer ul {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;
  padding: 0;
}

.blog-post-footer nav p {
  margin: 0;
  padding: 0;
  height: 23px;
}

.blog-post-footer a,
.blog-post-footer nav p {
  color: #65def1;
  background-color: red;
  display: inline-block;
}

And an image of how this is rendered (Firefox): 这是没有过多元素的渲染元素的更新图像。

Please notice that the code above presented is not only conditionally rendering a <p> element inside the the <li> element like this:

<li> <p>First Post!</p> </li>

but is rendering also second nested <li> element containing a <p> element inside the already existing <li> direct child element from your <ul> .

So it is rendering this structure:

<ul> <li> <li> <p>Frist post!</p> </li> <li> </ul>

This might be messing with your styles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM