4

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

在此处输入图像描述

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

在此处输入图像描述

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

4

3 回答 3

1

一种方法是使用 spacer spans 和一个包装器(在这种情况下header),所有元素都带有displayset ,因此它们显示为table-cells (示例)。

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;
}
于 2012-06-21T17:07:28.147 回答
0

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

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

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

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

于 2012-06-21T17:09:28.787 回答
0

您可以使用<fieldset>标签来完成此操作。

示例: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;
}
于 2020-09-28T21:03:36.983 回答