如何使 HTML 中的链接居中
我对 HTML 很陌生,想知道如何用 html 居中多个链接?
我一直在尝试:
<a href="http//www.google.com"><p style="text-align:center">Search</a>
但问题是当我尝试将其他链接放在它后面时,例如:
<a href="http//www.google.com"><p style="text-align:center">Search</a><a href="Contact Us"><p style="text-align:center">Contact Us</a></p>
它只是将它们放在单独的行上。我相信这是因为
函数而发生的...但我只知道 HTML,我知道你可以在 CSS 中完成它,但我想知道是否可以仅使用 HTML 来完成。
感谢您的帮助!
I am really new to HTML and was wondering how you center multiple links with html?
I have been trying :
<a href="http//www.google.com"><p style="text-align:center">Search</a>
But the problem is when I try to put other links behind it for example:
<a href="http//www.google.com"><p style="text-align:center">Search</a><a href="Contact Us"><p style="text-align:center">Contact Us</a></p>
It just places them on separate lines. I believe this is happening because of the <p>
function...but I only know HTML, I know you can do it in CSS but I was wondering if it can be done using just HTML.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您的代码中有一些错误 - 第一个:您还没有关闭
p
-tag:next:
p
代表“段落”并且是一个块元素(所以它会导致换行)。您想要使用的是span
,它只是用于格式化的内联元素:但如果您只想向链接添加样式,为什么不为其设置样式直接链接:
最后,这至少是正确的 html,但仍然不完全是您想要的,因为
text-align:center
将文本在该元素中居中,所以你必须为包含这个链接的元素设置它(这段html没有发布,所以我无法纠正你,但我希望你理解) - 要显示这一点,我将使用一个简单的div
:编辑: 对您的问题进行一些补充:
p
不是“函数”,但您是是的,这导致了问题(因为它是一个块元素)there are some mistakes in your code - the first: you havn't closed you
p
-tag:next:
p
stands for 'paragraph' and is a block-element (so it's causing a line-break). what you wanted to use there is aspan
, wich is just an inline-element for formatting:but if you just want to add a style to your link, why don't you set the style for that link directly:
in the end, this would at least be correct html, but still not exactly what you want, because
text-align:center
centers the text in that element, so you would have to set that for the element that contains this links (this piece of html isn't posted, so i can't correct you, but i hope you understand) - to show this, i'll use a simplediv
:EDIT: some more additions to your question:
p
is not a 'function', but you're right, this is causing the problem (because it's a block-element)由于您有一个链接列表,因此您应该将它们标记为列表(而不是段落)。
Listamatic 有一堆关于如何设置链接列表样式的示例,其中包括一些垂直列表,每个链接都居中(这就是您所看到的)。它还具有解释原理的教程。
样式的这一部分本质上可以归结为“在显示为包含链接文本的块的元素上设置
text-align: center
”(这可能是锚点本身(如果您将其设置为显示为块)或包含它的列表项。Since you have a list of links, you should be marking them up as a list (and not as paragraphs).
Listamatic has a bunch of examples of how you can style lists of links, including a number that are vertical lists with each link being centred (which is what you appear to be after). It also has a tutorial which explains the principles.
That part of the styling essentially boils down to "Set
text-align: center
on an element that is displaying as a block which contains the link text" (that could be the anchor itself (if you make it display as a block) or the list item containing it.您可以将它们放入
或
示例中:http://jsfiddle.net/X8HM4/1/
you would put them inside a
<p>
or a<div>
sample: http://jsfiddle.net/X8HM4/1/
将显示在新行上。尝试将所有链接包装在一个
中标签:
The <p> will show up on a new line. Try wrapping all of your links in one single <p> tag:
一种解决方案是将它们放在
中,如下所示:
我还为您创建了一个 jsfiddle:https://jsfiddle.net/9acgLf8e/
One solution is to put them inside
<center>
, like this:I've also created a jsfiddle for you: https://jsfiddle.net/9acgLf8e/
p
并不是将文本放入a
中的方式。这就是问题所在。唯一的解决方案是将文本放在和
之间。例如:
p
is not how you put text ina
. That is the problem. The only solution is to put the text between<a>
and</a>
. For example:好吧,一个非常简单的方法就是采用老式方法并通过使用来省去麻烦。
您可以按照您喜欢的方式更改左边距或右边距以及与像素的距离。
well, one really easy way is to just do the old-fashioned way and save the hassle by using.
You can just change the margin-left or margin-right and the distance with the pixels in whichever way you prefer.
尝试使用
nav
元素和ul
元素进行操作。我的上面有一个main
但我认为你不需要它。代码是这样的。
当我输入代码时,它无法正常工作,因此您需要填写空白,
然后将其居中。
Try doing a
nav
element with aul
element. Mine has amain
above but I don't think you need it.The code is something like this.
When ever I put in the code it wouldn't work right so you need to fill in the blank,
then center it.