CSS 锚标记元素之间不需要的间距

发布于 2024-11-27 03:37:58 字数 411 浏览 1 评论 0原文

我有这个样式表:

*{
    padding: 0px;
    margin: 0px;
}

a{

  background:yellow;

}

和这个网页:

<a href="/blog/">Home</a>
<a href="/about/">About</a>
<a href="/contact/">Contact</a>    

结果:

在此处输入图像描述

如何使这些锚标记“触摸” “彼此之间,消除中间不需要的空间?

谢谢 卢卡

I have this stylesheet:

*{
    padding: 0px;
    margin: 0px;
}

a{

  background:yellow;

}

and this webpage:

<a href="/blog/">Home</a>
<a href="/about/">About</a>
<a href="/contact/">Contact</a>    

Results in:

enter image description here

How do I make those anchor tag to "touch" each other,removing that unwanted space in-between?

thanks
Luca

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

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

发布评论

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

评论(7

安人多梦 2024-12-04 03:37:58

您需要删除标签之间的空格(在本例中为换行符)。有些浏览器将其呈现为空格。

You need to remove the whitespace (in this case the newline) between your tags. Some browsers render it as a space.

秋心╮凉 2024-12-04 03:37:58

您可以使用此技巧来消除空格:

HTML:

<div id="test">
    <a href="/blog/">Home</a>
    <a href="/about/">About</a>
    <a href="/contact/">Contact</a>   
</div>

CSS:

#test { font-size:0; }
#test a { font-size:16px; background:yellow; }

Live demo: http: //jsfiddle.net/qucy/

You can use this trick to get rid of the space:

HTML:

<div id="test">
    <a href="/blog/">Home</a>
    <a href="/about/">About</a>
    <a href="/contact/">Contact</a>   
</div>

CSS:

#test { font-size:0; }
#test a { font-size:16px; background:yellow; }

Live demo: http://jsfiddle.net/quucy/

楠木可依 2024-12-04 03:37:58

我想我可能会找到一个很酷的方法来解决它:-)。我首先使用 来填充空的 <因此

,如果您想保留新行锚点结构并且不希望它们之间有空格...只需在行尾打开一个块注释并在 new < 之前的新行上结束它锚 >

像这样:

<div id="test">
    <a href="/blog/">Home</a><!--
    --><a href="/about/">About</a><!--
    --><a href="/contact/">Contact</a>   
</div>

和演示:http://jsfiddle.net/Lukis/reZG2/1/

I think I might find a pretty cool way to solve it :-). I started with the fact of using <!-- comments --> to fill empty < span >s etc.

So if you want to keep your anchor-on-a-new-line structure and do not want the spaces between them... simply open a block comment on the end of the line and end it on the new line just before new < anchor >

Like this:

<div id="test">
    <a href="/blog/">Home</a><!--
    --><a href="/about/">About</a><!--
    --><a href="/contact/">Contact</a>   
</div>

and DEMO: http://jsfiddle.net/Lukis/reZG2/1/

旧伤还要旧人安 2024-12-04 03:37:58

链接之间的空格可能是由代码中的换行符产生的,但这实际上取决于您在哪个浏览器中获得此行为(有些浏览器会忽略这些字符,有些则不会)。

尝试将所有三个标签放在一行中,并且它们之间没有空格。

<a href="/blog/">Home</a><a href="/about/">About</a><a href="/contact/">Contact</a>

The space between the links might be produced by newline characters you have in your code but it really depends in which browser you get this behavior (some browser ignore these characters some do not).

Try putting all three tags in a single line and without spaces between them.

<a href="/blog/">Home</a><a href="/about/">About</a><a href="/contact/">Contact</a>
逆光飞翔i 2024-12-04 03:37:58

将它们放入 ul/li 结构怎么样?

#test li {
    background:yellow; 
    float: left;
    list-style: none;
}
<ul id="test">
  <li><a href="/blog/">Home</a></li>
  <li><a href="/about/">About</a></li>
  <li><a href="/contact/">Contact</a></li>
</ul>

How about puting them in ul/li structure?

#test li {
    background:yellow; 
    float: left;
    list-style: none;
}
<ul id="test">
  <li><a href="/blog/">Home</a></li>
  <li><a href="/about/">About</a></li>
  <li><a href="/contact/">Contact</a></li>
</ul>

烈酒灼喉 2024-12-04 03:37:58

上面所有的答案都展示了一些消除不需要的空白的巧妙方法,但我没有看到我已经使用了近十年的方法;因此,对于那些仍在与空白作斗争的人来说,这是另一个简单的解决方案,可以解决您的老问题 - 使用 float!

HTML:

<div id="test">
  <a href="/blog/">Home</a>
  <a href="/about/">About</a>
  <a href="/contact/">Contact</a>   
</div>

CSS:

#test { 
  overflow:hidden; 
 /* this isn't really required here but helps; 
 or use your preferred method for clearfix  */
}

#test a { 
  float:left; 
  background: yellow;
}

jsfiddle:
http://jsfiddle.net/fjj7dsyx/2/

All the above answers show some neat ways of getting rid of that unwanted whitespace but I don't see the one I've been using for almost a decade; so here's another simple solution to your very old problem for people who still wrestle with that whitespace -- use float!

HTML:

<div id="test">
  <a href="/blog/">Home</a>
  <a href="/about/">About</a>
  <a href="/contact/">Contact</a>   
</div>

CSS:

#test { 
  overflow:hidden; 
 /* this isn't really required here but helps; 
 or use your preferred method for clearfix  */
}

#test a { 
  float:left; 
  background: yellow;
}

jsfiddle:
http://jsfiddle.net/fjj7dsyx/2/

没有心的人 2024-12-04 03:37:58

您可以使用弹性盒:

div {
    display: flex;
}
<div>
    <a href="/blog/">Home</a>
    <a href="/about/">About</a>
    <a href="/contact/">Contact</a>   
</div>

或者如果您使用 Bootstrap:

<div class="d-flex">
    <a href="/blog/">Home</a>
    <a href="/about/">About</a>
    <a href="/contact/">Contact</a>   
</div>

You can use flexbox:

div {
    display: flex;
}
<div>
    <a href="/blog/">Home</a>
    <a href="/about/">About</a>
    <a href="/contact/">Contact</a>   
</div>

Or if you use Bootstrap:

<div class="d-flex">
    <a href="/blog/">Home</a>
    <a href="/about/">About</a>
    <a href="/contact/">Contact</a>   
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文