简体   繁体   English

CSS:垂直对齐文本?

[英]CSS: Vertical-Align text?

I am using the following HTML: 我使用以下HTML:

<p><a href="http://www.example.com/">&larr; Back</a></p>

To create the following: 要创建以下内容:

← Back ←返回

Problem is, the left arrow is not vertically aligned in the middle. 问题是,左箭头没有在中间垂直对齐。 It appears to be at the lower 3rd. 它似乎处于较低的第3位。

Question : how do I get the left arrow to be aligned vertically in the middle (of the letter "B") using CSS? 问题 :如何使用CSS使左箭头在中间(字母“B”)垂直对齐?


UPDATE : 更新

Is it possible for me to vertically adjust/align this: 我可以垂直调整/对齐这个:

  1. Without modifying my HTML, and 不修改我的HTML,和
  2. Without using an image? 没有使用图像?

The arrow is a simple character, so it's aligned like the others (it is in the "middle", the creator of the font wants it to be where it is... maybe that's the middle of lower-case character). 箭头是一个简单的人物,所以它的人(这在“中间”,字体的创建者希望它是它在哪里......也许这是小写字符的中间)像对齐。 Maybe it looks different using another font, maybe not. 也许它使用另一种字体看起来不同,也许不是。 If you have a fixed font and that one looks messy, you could try to use the :first-letter selector (or wrap the arrow in a span or something) to move it up 1 or 2 px ( position:relative: top:-2px; ). 如果你有一个固定的字体而且看起来很乱,你可以尝试使用:第一个字母选择器(或在一个span或其他东西中包裹箭头)将它向上移动1或2像素( position:relative: top:-2px; )。

Another solution would be to use an image for this, like most websites do (and there are many free icon sets out there — my favourite is famfamfam ) 另一种解决方案是使用图像,就像大多数网站一样(并且有许多免费图标集 - 我最喜欢的是famfamfam

您可以将箭头包装在SPAN标记中,然后使用line-height和vertical-align CSS属性进行播放。

Generally you should not do this, you should let it as the font was conceived by its author. 一般情况下你不应该这样做,你应该让它作为字体是由作者构思的。

But it you want to change it you can do it like this: 但是你要改变它你可以这样做:

<p><a href="http://www.example.com/">
    <span style="position:relative;top:-3px;">&larr;</span> 
    Back
</a></p>

Note: Use what you need instead of -3px , I used that just to illustrate how the position can be changed. 注意:使用你需要的而不是-3px ,我用它来说明如何改变位置。

I think you have to use a image for the left arrow than &larr . 我认为你必须使用一个图像比左箭头&larr
It IS possible to have the &larr in a separate span , have some specific padding to bring the arrow to the right position , or use a specific font that has the arrow at the center, but this will have side effects. 可以将&larr放在一个单独的span ,使用一些特定的填充将箭头移到正确的位置 ,或者使用在中心有箭头的特定字体,但这会产生副作用。

I suggest you use an image. 我建议你使用一个图像。

There are two possible answers to this. 这有两种可能的答案。

  1. The way you're writing it, this is not a graphical element (arrow) followed by a label ("Back"), but a line of text (inside a paragraph) containing a single character followed by a letter string. 你写它的方式,这不是一个图形元素(箭头)后跟一个标签(“后面”),而是一行文字(在一个段落内),包含一个字符后跟一个字母字符串。 So alignment is a purely typographical problem and determined by the font you're choosing. 对齐是一个纯粹的排版问题,并由您选择的字体决定。 Choose a different font and see if it's more typographically pleasing. 选择一种不同的字体,看它是否更符合印象。
  2. What you want is really not a line of text but two independently placeable graphical elements. 你想要的实际上不是一行文字而是两个可独立放置的图形元素。 Put each inside its own span , give it display: inline-block and position: relative and play with vertical paddings, margins and line-heights until you're satisfied. 将每个放在自己的span ,给它display: inline-blockposition: relative并使用垂直填充,边距和线高,直到您满意为止。

You have some options: 1. Put the arrow between span tags before the word Back, add an id to this span object and then assign the style in the css file playing with: padding-top or bottom and also vertical-align or position relative . 您有一些选项:1。在单词Back之前的span标签之间放置箭头,向此span对象添加id,然后在css文件中指定样式: padding-topbottom以及vertical-alignposition relative 2. The second option is using the image as background and then you have to create the style for this link: 2.第二个选项是使用图像作为背景,然后您必须为此链接创建样式:

  li a#link,#link_conten{
      background-image: url(../../../img/arrow.gif);
      background-position: left top;
      background-repeat: no-repeat;
     }

In addition, it is not common (from the semantic point of view) to put just the link (tag a) inside a paragraph (tag p). 另外,从语义的角度来看,仅将链接(标记a)放在段落(标记p)中并不常见。 Then you have to deal with the default css rules for tag a and p but of course depends of your design 然后你必须处理标签a和p的默认css规则,但当然取决于你的设计

You could use CSS generated content. 您可以使用CSS生成的内容。 This will mean editing your HTML - to remove the arrow. 这将意味着编辑您的HTML - 删除箭头。 Essentially you're creating a pseudo-element that sits in front of the link, and you can style it however you like, eg 基本上你是在创建一个位于链接前面的伪元素,你可以随意设置它,例如

a.back:before {
    content: "\2190 "; /* Unicode equivalent of &larr; */
    display: inline-block;
    padding: 5px;
    background-color: aqua;
}  

On the downside this won't work in IE 6 or 7. You might be able to work around that with some targeted javascript. 在缺点方面,这在IE 6或7中不起作用。您可以通过一些有针对性的JavaScript来解决这个问题。

If you don't want to edit your HTML, you could give :first-letter a try. 如果您不想编辑HTML,可以尝试:first-letter It only works on block-level elements, so you'll need to work accordingly, eg 它只适用于块级元素,因此您需要相应地工作,例如

a.back {
    display: inline-block;
}
a.back:first-letter {
    background-color: aqua;
    padding: 5px;
}

I've had trouble getting this to display consistently cross-browser though. 我很难让它一直显示跨浏览器。 IE8 and FF3.6 do rather different things with the code. IE8和FF3.6对代码做了不同的事情。

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

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