Atadj
Atadj

Reputation: 7180

How to position text over border?

I was able to get it working with a white background:

enter image description here

But in cases where the background isn't white, the solution doesn't work as well:

enter image description here

It should be quite obvious what I did any why it doesn't work (negative margin + background set to background color). Are there any solutions to make it always look good?

Upvotes: 4

Views: 7395

Answers (3)

Ryan Loggerythm
Ryan Loggerythm

Reputation: 3314

You can use a <fieldset> tag to accomplish this.

Example: 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;
}

Upvotes: 0

Arjun Abhynav
Arjun Abhynav

Reputation: 543

Are you putting the text and the button in a div whose background is set to white? It is hard to tell what you are doing without showing the CSS.

If you are using a div with background, why not just remove it? Or if there is some constraint why not set the background to rgba(#,#,#,0.0)?

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

The alpha property helps set the level of opacity.

Upvotes: 0

0b10011
0b10011

Reputation: 18795

One way would be to use spacer spans along with a wrapper (in this case header), with all elements with display set so they appear as 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;
}

Upvotes: 1

Related Questions