如何使 HTML 中的链接居中

发布于 2024-10-04 12:52:25 字数 551 浏览 9 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(8

峩卟喜欢 2024-10-11 12:52:25

您的代码中有一些错误 - 第一个:您还没有关闭 p -tag:

<a href="http//www.google.com"><p style="text-align:center">Search</p></a>

next: p 代表“段落”并且是一个块元素(所以它会导致换行)。您想要使用的是 span,它只是用于格式化的内联元素:

<a href="http//www.google.com"><span style="text-align:center">Search</span></a>

但如果您只想向链接添加样式,为什么不为其设置样式直接链接:

<a href="http//www.google.com" style="text-align:center">Search</a>

最后,这至少是正确的 html,但仍然不完全是您想要的,因为 text-align:center 将文本在该元素中居中,所以你必须为包含这个链接的元素设置它(这段html没有发布,所以我无法纠正你,但我希望你理解) - 要显示这一点,我将使用一个简单的 div

<div style="text-align:center">    
  <a href="http//www.google.com">Search</a>
  <!-- more links here -->
</div>

编辑: 对您的问题进行一些补充:

  • p 不是“函数”,但您是是的,这导致了问题(因为它是一个块元素)
  • 您尝试使用的 is css - 它只是内联而不是放置在单独的文件中,但您没有这样做此处为“仅 HTML”

there are some mistakes in your code - the first: you havn't closed you p-tag:

<a href="http//www.google.com"><p style="text-align:center">Search</p></a>

next: p stands for 'paragraph' and is a block-element (so it's causing a line-break). what you wanted to use there is a span, wich is just an inline-element for formatting:

<a href="http//www.google.com"><span style="text-align:center">Search</span></a>

but if you just want to add a style to your link, why don't you set the style for that link directly:

<a href="http//www.google.com" style="text-align:center">Search</a>

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 simple div:

<div style="text-align:center">    
  <a href="http//www.google.com">Search</a>
  <!-- more links here -->
</div>

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)
  • what you're trying to use is css - it's just inline instead of being placed in a seperate file, but you aren't doing 'just HTML' here
青芜 2024-10-11 12:52:25

由于您有一个链接列表,因此您应该将它们标记为列表(而不是段落)。

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.

眼泪淡了忧伤 2024-10-11 12:52:25

您可以将它们放入

<p style="text-align:center">
    <a href="http//www.google.com">Search</a> 
    <a href="Contact Us">Contact Us</a>
</p>

示例中:http://jsfiddle.net/X8HM4/1/

you would put them inside a <p> or a <div>

<p style="text-align:center">
    <a href="http//www.google.com">Search</a> 
    <a href="Contact Us">Contact Us</a>
</p>

sample: http://jsfiddle.net/X8HM4/1/

╭ゆ眷念 2024-10-11 12:52:25

将显示在新行上。尝试将所有链接包装在一个

中标签:

<p style="text-align:center;"><a href="http//www.google.com">Search</a><a href="Contact Us">Contact Us</a></p>

The <p> will show up on a new line. Try wrapping all of your links in one single <p> tag:

<p style="text-align:center;"><a href="http//www.google.com">Search</a><a href="Contact Us">Contact Us</a></p>
随梦而飞# 2024-10-11 12:52:25

一种解决方案是将它们放在

中,如下所示:

<center>
<a href="http//www.google.com">Search</a>
<a href="Contact Us">Contact Us</a>
</center>

我还为您创建了一个 jsfiddle:https://jsfiddle.net/9acgLf8e/

One solution is to put them inside <center>, like this:

<center>
<a href="http//www.google.com">Search</a>
<a href="Contact Us">Contact Us</a>
</center>

I've also created a jsfiddle for you: https://jsfiddle.net/9acgLf8e/

孤千羽 2024-10-11 12:52:25

p 并不是将文本放入 a 中的方式。这就是问题所在。唯一的解决方案是将文本放在 之间。例如:

<a href="https://stackoverflow.com/posts/64201994/edit" style="text-align:center;">Stack Overflow</a>

p is not how you put text in a. That is the problem. The only solution is to put the text between <a> and </a>. For example:

<a href="https://stackoverflow.com/posts/64201994/edit" style="text-align:center;">Stack Overflow</a>
忆伤 2024-10-11 12:52:25

好吧,一个非常简单的方法就是采用老式方法并通过使用来省去麻烦。
您可以按照您喜欢的方式更改左边距或右边距以及与像素的距离。

<a href:"#" style:"margin-left:"260px">Link</a>

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.

<a href:"#" style:"margin-left:"260px">Link</a>
囍孤女 2024-10-11 12:52:25

尝试使用 nav 元素和 ul 元素进行操作。我的上面有一个 main 但我认为你不需要它。

<main>
<nav>
<ul><li><a href="http//www.google.com">search</a>
<li><a href="http//www.google.com">search</a>
<li><a href="http//www.google.com">search</a>

代码是这样的。
当我输入代码时,它无法正常工作,因此您需要填写空白,
然后将其居中。

main
nav
ul> li> a>: href="link of choice":name of link:/a>

Try doing a nav element with a ul element. Mine has a main above but I don't think you need it.

<main>
<nav>
<ul><li><a href="http//www.google.com">search</a>
<li><a href="http//www.google.com">search</a>
<li><a href="http//www.google.com">search</a>

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.

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