【问题标题】:How to position text over border?如何在边框上放置文本?
【发布时间】:2020-09-29 05:03:36
【问题描述】:

我能够让它在白色背景下工作:

但在背景不是白色的情况下,解决方案也不起作用:

应该很明显我做了什么为什么它不起作用(负边距+背景设置为背景颜色)。是否有任何解决方案可以让它始终看起来不错?

【问题讨论】:

标签: html css


【解决方案1】:

一种方法是使用 spacer spans 和一个包装器(在本例中为 header),所有带有 display 的元素都设置为 table-cells (example)。

HTML

<header>
    <span class="spacer"></span><!-- Place this wherever you want the border -->
    <h1>Title</h1>
    <!-- Spacing is automatically added next to elements (but not on ends) -->
    <span class="spacer"></span>
    <a href="http://www.example.com/">View Blog</a>
</header>

CSS

header {
    display:table-row;
    line-height:1.5em;
    font-size:2em;
    white-space:nowrap; /* Prevent titles from wrapping when more than one word is used */
}
header h1 {
    font-size:inherit; /* Change font-size in header */
    overflow:hidden;
    display:table-cell;
    vertical-align:middle;
}
header span.spacer { /* Makes spacers stretch */
    display:table-cell;
    width:50%;
}
header span.spacer { /* Adds spacing on both sides of spacers */
    padding:0 10px;
}
header span.spacer:first-child { /* Adds spacing only on right side of first spacer */
    padding:0 10px 0 0;
}
header span.spacer:last-child { /* Adds spacing only on left side of last spacer */
    padding:0 0 0 10px;
}
header span.spacer:after { /* Adds border to spacer */
    display:inline-block;
    width:100%;
    content:".";
    font-size:0;
    color:transparent;
    height:2px;
    background:#000;
    vertical-align:middle;
    position:relative;
    top:-1px;
}
header > a { /* Styles links according to example */
    font-size:.4em;
    vertical-align:middle;
    background:#25a2a4;
    color:#fff;
    text-transform:uppercase;
    font-family:monospace;
    border-radius:.5em;
    padding:.3em .5em;
    text-decoration:none;
}

【讨论】:

  • 谢谢 :) 我会在一秒钟内对其进行测试,并让您知道它是否有效。
  • 工作就像一个魅力。它甚至比 cmets 中的链接更好:) 另一个需要图像。非常感谢!
  • @Flow,不客气。请务必在您需要支持的任何浏览器中对其进行测试。它适用于最新版本的 Firefox 和 Chrome,但我目前无法访问任何其他内容。它应该适用于所有现代浏览器,不包括 IE7。
【解决方案2】:

您是否将文本和按钮放在背景设置为白色的 div 中?不显示 CSS 就很难判断你在做什么。

如果您使用的是带背景的 div,为什么不直接删除它呢?或者如果有一些限制,为什么不将背景设置为 rgba(#,#,#,0.0)?

background:rgba(255,255,255,0.0);

alpha 属性有助于设置不透明度。

【讨论】:

  • 如果background 被删除,边框将显示出来。
  • 我正在寻找任何容器结构。只需要一个想法。我正在寻找一种方法来创建类似的内容: ----- TEXT ----- 所以我不可能将不透明度设置为 0,因为该行将通过文本。
【解决方案3】:

您可以使用&lt;fieldset&gt; 标签来完成此操作。

示例:https://jsbin.com/tovuwimezu/edit?html,css,output

HTML

<form>
    <fieldset>
        <legend class="blogLegend">New Blog Post</legend>

        blog details here
    </fieldset>
</form>

CSS

.blogLegend {
    font-weight: 600;
    color: rgb(63, 169, 196);
    padding: 10px;
    text-align: center;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 2020-11-16
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多