简体   繁体   English

一个<a>标签</a> <a><img></a> <a>内部标记不同于</a> <a>标记只有文本。</a> <a>我怎样才能解决这个问题?</a>

[英]An <a> tag with an <img> tag inside lines up differently that an <a> tag with just text. How can I fix this?

I am working on a website, and I am trying to make a top navigation bar. 我正在一个网站上工作,我正在尝试制作一个顶部导航栏。 The far left link should be an image with my logo, and the other ones should navigate to different parts of the site. 最左边的链接应该是带有我的徽标的图像,其他的应该导航到网站的不同部分。 The button with the image lines up differently that the other ones. 带有图像的按钮与其他按钮的排列方式不同。 How can I fix that? 我该如何解决这个问题?

 <div id='topbar'> <a href='index.html' class='topbar-button-home'> <img src='camelCaseCo_centered_cropped_notext_bold_small.png'> </a> <a href='index.html' class='topbar-button'>Home</a> <a href='index.html' class='topbar-button'>2</a> <a href='index.html' class='topbar-button'>3</a> <a href='index.html' class='topbar-button'>4</a> </div> <style> #topbar { padding: 0; margin: 0; display: inline-block; font-size: 0; } .topbar-button { width: 200px; margin: 0px; padding: 20px; display: inline-block; border: solid black 1px; border-radius: 0px; text-decoration: none; color: black; text-align: center; font-family: "Google Sans", sans-serif; font-size: 14px; } .topbar-button-home { width: 50px; margin: 0px; padding: 8px; border: solid black 1px; display: inline-block; text-decoration: none; color: black; text-align: center; font-family: "Google Sans", sans-serif; font-size: 14px; } </style> 

Thanks in advance! 提前致谢!

What about Flexbox? 那么Flexbox呢?

 #topbar { padding: 0; margin: 0; display: flex; } .topbar-button { width: 200px; margin: 0px; padding: 20px; display: inline-block; border: solid black 1px; border-radius: 0px; text-decoration: none; color: black; text-align: center; font-family: "Google Sans", sans-serif; font-size: 14px; } .topbar-button-home { width: 50px; margin: 0px; padding: 8px; border: solid black 1px; display: inline-block; text-decoration: none; color: black; text-align: center; font-family: "Google Sans", sans-serif; font-size: 14px; } 
 <div id='topbar'> <a href='index.html' class='topbar-button-home'><img src='camelCaseCo_centered_cropped_notext_bold_small.png'></a> <a href='index.html' class='topbar-button'>Home</a> <a href='index.html' class='topbar-button'>2</a> <a href='index.html' class='topbar-button'>3</a> <a href='index.html' class='topbar-button'>4</a> <a href='index.html' class='topbar-button'><img src='camelCaseCo_centered_cropped_notext_bold_small.png'></a> </div> 

In the example above, all flex children will have the same height regardless of their content. 在上面的示例中,无论内容如何,​​所有弹性子级都将具有相同的高度。

Remember, images are inline elements and so they are affected by line height. 请记住,图像是内联元素,因此它们受行高影响。 Try setting your image to diasplay block or inline-block. 尝试将图像设置为diasplay块或内联块。 I see you have it on the link— move it to the image. 我看到你在链接上有它 - 将它移动到图像上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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