简体   繁体   English

以特定词为中心的文字

[英]Center text around a specific word

In CSS, how can I center text around a specific word? 在CSS中,如何将文字围绕特定的单词?

For instance, let's say I have the following DIV : 例如,假设我有以下DIV

<div style="text-align: center;">
  Previous Day | Navigation | Next Day
</div>

The text will technically be centered, but the word "Navigation" will NOT be in the exact middle. 从技术上讲,文本将居中,但“导航”一词将不在中间。 Rather, the middle will be exactly between the letters "v" and "i". 相反,中间将恰好在字母“ v”和“ i”之间。 This is because when centering text, the length of the entire string is taken into account. 这是因为在将文本居中时,会考虑整个字符串的长度。

How can I make the middle instead be between the "g" and the "a", using (preferably) only CSS? 我如何只使用CSS(最好是使用CSS)使中间出现在“ g”和“ a”之间? Modifying the HTML is also acceptable. 修改HTML也可以接受。 As a last resort, I'm willing to use JavaScript, although only if it's kept simple, otherwise it's not worth it to use complex JavaScript for such a simple task. 作为最后的选择,我愿意使用JavaScript,尽管只有将其保持简单即可,否则不建议将复杂的JavaScript用于如此简单的任务。

Fixing the width of the elements containing "Previous Day" and "Next Day" is probably the simplest solution: 固定包含“上一天”和“下一天”的元素的宽度可能是最简单的解决方案:

<div style="text-align: center;">
    <div style="display: inline-block; width: 12em; text-align: right;">Previous Day</div>
    <div style="display: inline-block;"> | Navigation | </div>
    <div style="display: inline-block; width: 12em; text-align: left;">Next Day</div>
</div>

Fiddle here . 在这里摆弄。

You can wrap your individual items in an HTML tag, like an anchor, and float them to achieve your desired result. 您可以将单个项目包装在HTML标记(例如锚)中,然后将其浮动以达到所需的结果。 Floating the tags places them side-by-side, and giving each item a percentage-width that collectively adds to 100% effectively centers the elements in their container. 浮动标签可将它们并排放置,并为每个项目提供一定百分比的宽度,这些宽度共同增加到100%,可以有效地将元素居中在容器中。

Note, there are some pitfalls to using floats. 注意,使用浮点数有一些陷阱。 You need to clear the parent div to properly lay out elements following a container with floated children. 您需要清除父div才能正确放置带有浮动子项的容器之后的元素。 Also, if the child elements have any padding, this will be added to the percentage width and misalign the children unless you use box-sizing: border-box; 另外,如果子元素具有任何填充,除非您使用box-sizing: border-box;否则它将被添加到百分比宽度中并导致子元素未对齐box-sizing: border-box; on the child elements. 在子元素上。

HTML HTML

<div class="container">
    <a href="#">Previous Day</a>
    <span class="separator">|</span>
    <a href="#">Navigation</a>
    <span class="separator">|</span>
    <a href="#">Next Day</a>
</div>

CSS CSS

.container {
    color: white;
    margin: 0 auto;
    width: 80%;
    min-width: 320px;
    background-color: black;
    overflow: hidden; /* to clear the div */
}

.container a {
    text-align: center;
    float: left;
    width: 32%;
    overflow: hidden;
    background-color: orange;
}

.container .separator {
    float: left;
    text-align: center;
    padding: .5%;
}

The colors are to show that it is centered in its parent element. 颜色将显示它在其父元素中居中。

Here's a live demo jsFiddle 这是一个现场演示jsFiddle

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

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