图像链接之间的 CSS 无缘无故出现间隙

发布于 2024-08-29 22:53:16 字数 664 浏览 8 评论 0原文

在过去的几个小时里,我一直在尝试对这个水平导航进行排序,但没有任何效果。我尝试过reset.css样式表,*{padding: 0; margin: 0) 等,我的图像链接之间仍然有间隙。

您会看到,导航由内联显示的无序列表的图像链接组成,但每个图像之间(左、右、上、下)都有间隙,我不明白为什么。在所有浏览器中都是一样的。

这是该页面的链接,因此来源:Beansheaf 临时

链接到 css: < a href="http://pentathlongb-yorkshire.co.uk/tomsmith/Beansheaf/styles/fund2.css" rel="noreferrer">http://pentathlongb-yorkshire.co.uk/tomsmith/Beansheaf/styles/ fund2.css

网站的其余部分显然还没有完成,我现在担心的只是导航。

I've been trying to get this horizontal navigation sorted for the past few hours now and nothing is working. I've tried reset.css stylesheets, *{padding: 0; margin: 0) etc. and I still have gaps inbetween my image links.

You see, the navigation is made up of an unordered list of image links displayed inline, but there are gaps in between each image, left, right, top and bottom and I can't see why. It's the same in all browsers.

Here is a link to the page, and so source: Beansheaf Temporary

Link to css: http://pentathlongb-yorkshire.co.uk/tomsmith/Beansheaf/styles/fund2.css

The rest of the site is obviously still not done, it's just the navigation I'm worried about right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

软糯酥胸 2024-09-05 22:53:16

为了避免浮动导航li,您至少有两个选项来删除内联元素之间的空格。

<ul> 
  <li><a href="#"><img src="../hotel.jpg" /></a></li 
  ><li><a href="#"><img src="../foodDrink.jpg" /></a></li
  ><li><a href="#"><img src="../meetingsConferences.jpg" /></a></li>
</ul>

请注意,结束 标记在后续行中关闭(最后一行除外),这是有效的并保持可读性(至少对我来说)。

另一个选项稍微混乱一些

<ul> 
  <li><a href="#"><img src="../hotel.jpg" /></a></li><!-- 
  --><li><a href="#"><img src="../foodDrink.jpg" /></a></li><!--
  --><li><a href="#"><img src="../meetingsConferences.jpg" /></a></li>
</ul>

,只使用 html 注释 来注释掉原本会折叠成单个空格的空格。

我确实希望有某种方式来指定(例如):

ul li img {whitespace: none-between; }

To avoid floating the navigation lis, you've got -at least- two options to remove the spaces between inline elements.

<ul> 
  <li><a href="#"><img src="../hotel.jpg" /></a></li 
  ><li><a href="#"><img src="../foodDrink.jpg" /></a></li
  ><li><a href="#"><img src="../meetingsConferences.jpg" /></a></li>
</ul>

Note that the closing </li> tag is closed on the subsequent line (except for the last one), which is valid and maintains readability (for me, at least).

The other option is slightly messier

<ul> 
  <li><a href="#"><img src="../hotel.jpg" /></a></li><!-- 
  --><li><a href="#"><img src="../foodDrink.jpg" /></a></li><!--
  --><li><a href="#"><img src="../meetingsConferences.jpg" /></a></li>
</ul>

And just uses html comments <!-- ... --> to comment-out the spaces that would otherwise be collapsed into a single space.

I do wish there was some way of specifying (for example):

ul li img {whitespace: none-between; }
薄暮涼年 2024-09-05 22:53:16

另一种避免浮动的方法是将容器上的字体大小设置为 0,然后为 LI 重新设置它,如下所示:

#mainNav
{    font-size: 0}

#mainNav li
{
    display: inline;
    list-style-type: none;
    font-size: 1em
}

Another approach, avoiding floats, is to set the font-size on the container to 0, and then re-set it back up for the LIs, like this:

#mainNav
{    font-size: 0}

#mainNav li
{
    display: inline;
    list-style-type: none;
    font-size: 1em
}
爱的那么颓废 2024-09-05 22:53:16

图像链接下方的间隙是由于默认情况下图像与基本文本行对齐所致,您只需声明

li img {
    vertical-align:bottom;
}

魔法即可解决它!

the gap below images links is due to the image being aligned with base text line by default, you can solve it simply declaring

li img {
    vertical-align:bottom;
}

magic!

宫墨修音 2024-09-05 22:53:16

尝试删除 li 元素之间的所有空格和换行符。

由于您是内联显示它们,因此 HTML 中的空格将充当内联文本中的空格,并导致渲染时留下间隙。

Try removing all spaces and line-breaks between the li elements.

Because you are displaying them inline, spaces in the HTML will act as if they were a space in inline text and cause a gap to be left when rendering.

够运 2024-09-05 22:53:16

添加

#mainNav li { 
  float:left;
}

到您的 CSS。

Add

#mainNav li { 
  float:left;
}

to your CSS.

携君以终年 2024-09-05 22:53:16

这是因为 HTML 文档中的新行将被解释为打印内容中的空格。由于所有“li”元素都位于新行上,因此它会在每个元素之间添加一个空格。确保将它们显示为块元素并将它们浮动到左侧,以便它们均匀地运行在一起。

It is because a new line in an HTML document will be interpreted as a space in the printed content. Since all of your 'li' elements are on new lines, it is adding a space between each of them. Make sure you display them as block elements and float them to the left so they run evenly together.

携君以终年 2024-09-05 22:53:16

您可以浮动列表元素,那么空格就不会干扰:

#mainNav li
{
 float: left;
 list-style-type: none;
}

You can float the list elements, then the white space doesn't interfer:

#mainNav li
{
 float: left;
 list-style-type: none;
}
ゃ懵逼小萝莉 2024-09-05 22:53:16

我使用 li 标签上的 line-height 属性来解决这个问题。

ul li { line-height:0; }

I use the line-height attribute on the li tag to fix this.

ul li { line-height:0; }
迷迭香的记忆 2024-09-05 22:53:16

最佳解决方案来自 http://www.cssplay.co.uk/menus/ centered.html。并引用:

我们需要做的就是将 ul 标签封装在宽度为 100% 并将溢出设置为隐藏的外部容器中。

然后使用相对位置设置

    标记的样式,并向左浮动 50% 的左侧位置。

    最后,

  • 标记也使用相对位置进行样式设置,向左浮动,但这次的右侧位置为 50%。

    ...正如他们所说,这就是所需要的。

Best solution to this comes from http://www.cssplay.co.uk/menus/centered.html. And quoting:

All we need to do is enclose the ul tag in an outer container that has a width of 100% and overflow set to hidden.

The <ul> tag is then styled with a relative position and floated left with a left position of 50%.

Finally the <li> tag is also styled with a relative position, floated left but this time with a right position of 50%.

...and that as they say is all that is needed.

扛刀软妹 2024-09-05 22:53:16

如果您使用 xslt 来显示这些元素,您应该进行以下操作:

<xsl:template match=".//text()">  
    <xsl:value-of select="normalize-space(.)" />
</xsl:template>

if you are using xslt to show these element, you should make the following:

<xsl:template match=".//text()">  
    <xsl:value-of select="normalize-space(.)" />
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文