outline on only one border
Asked Answered
G

8

136

How to apply an inset border into an HTML element, but just only on one side of it. Until now, I've been using an image to do that (GIF/PNG) that I would then use as a background and stretch it (repeat-x) and position a little off from the top of my block. Recently, I discovered the outline CSS property, which is great! But seems to circle the whole block... Is it possibly to use this outline property to do it on just only one border? Also, if not, do you have any CSS trick that could replace the background image? (so that I could personalize the colors of the outline later using CSS, etc).

Here's an image of what am trying to achieve: http://exiledesigns.com/stack.jpg

Gilman answered 1/10, 2012 at 10:53 Comment(1)
Your link is dead. You can just paste the image directly into the question using the picture tool.Monocle
M
55

Outline indeed does apply to the whole element.

Now that I see your image, here's how to achieve it.

.element {
  padding: 5px 0;
  background: #CCC;
}
.element:before {
  content: "\a0";
  display: block;
  padding: 2px 0;
  line-height: 1px;
  border-top: 1px dashed #000; 
}
.element p {
  padding: 0 10px;
}
<div class="element">
  <p>Some content comes here...</p>
</div>

(Or see external demo.)

All sizes and colors are just placeholders, you can change it to match the exact desired result.

Important note: .element must have display:block; (default for a div) for this to work. If it's not the case, please provide your full code, so that i can elaborate a specific answer.

Maggee answered 1/10, 2012 at 10:56 Comment(8)
Hello Giona, I just added an image to my question to make it more clear: I need space in between the limits of my block and my border.Gilman
Thanks @GionaF, but it seems the border still is 'above' the block and not 'inside'. (what is \a0 ? just random content to create the block?) exiledesigns.com/stack2.jpg (I tried with 5px of padding but nothing changed)Gilman
@CorinneStoppelli your div is floated? In that case, my demo won't work. Can you add the code of the element in your question? Because it can be done in many different ways. And yes, \a0 is a white spaceMaggee
Hey thanks Giona, I just saw your code note and I started doing it on a blank file, and it worked out - no, it's not floated, but there's probably a conflict with something somewhere cause I have loads of things in there. From here I can find the issue, thank you :)Gilman
Why did you do a0 as opposed to " "?Gunzburg
@DominicTobias i actually can't remember ;-)Maggee
For reference, \a0 is a non-breaking space.Westley
For my use case, took this and modified a bit. Needed to add absolute position to top and a width of 100% on the element with the :before pseudo class, and relative position to the element, in case anyone else has the same issue.Lanford
J
202

You can use box-shadow to create an outline on one side. Like outline, box-shadow does not change the size of the box model.

This puts a line on top:

box-shadow: 0 -1px 0 #000;

I made a jsFiddle where you can check it out in action.

The syntax is box-shadow: offset-x | offset-y | blur-radius | color


INSET

For an inset border, use the inset keyword. This puts an inset line on top:

box-shadow: 0 1px 0 #000 inset;

Multiple lines can be added using comma-separated statements. This puts an inset line on the top and the left:

box-shadow: 0 1px 0 #000 inset,
            1px 0 0 #000 inset;

For more details on how box-shadow works, check out the MDN page.

Jaquelin answered 17/9, 2013 at 4:34 Comment(4)
I like this creative approach, thank you for sharing!Prefix
You didn't create an inset border and you don't explain the syntax for box-shadow. Thanks.Monocle
This one has limitations, if custom style border needed!Lalitta
Outline respoects border-radius jsfiddle.net/skd8y5r3Tasso
M
55

Outline indeed does apply to the whole element.

Now that I see your image, here's how to achieve it.

.element {
  padding: 5px 0;
  background: #CCC;
}
.element:before {
  content: "\a0";
  display: block;
  padding: 2px 0;
  line-height: 1px;
  border-top: 1px dashed #000; 
}
.element p {
  padding: 0 10px;
}
<div class="element">
  <p>Some content comes here...</p>
</div>

(Or see external demo.)

All sizes and colors are just placeholders, you can change it to match the exact desired result.

Important note: .element must have display:block; (default for a div) for this to work. If it's not the case, please provide your full code, so that i can elaborate a specific answer.

Maggee answered 1/10, 2012 at 10:56 Comment(8)
Hello Giona, I just added an image to my question to make it more clear: I need space in between the limits of my block and my border.Gilman
Thanks @GionaF, but it seems the border still is 'above' the block and not 'inside'. (what is \a0 ? just random content to create the block?) exiledesigns.com/stack2.jpg (I tried with 5px of padding but nothing changed)Gilman
@CorinneStoppelli your div is floated? In that case, my demo won't work. Can you add the code of the element in your question? Because it can be done in many different ways. And yes, \a0 is a white spaceMaggee
Hey thanks Giona, I just saw your code note and I started doing it on a blank file, and it worked out - no, it's not floated, but there's probably a conflict with something somewhere cause I have loads of things in there. From here I can find the issue, thank you :)Gilman
Why did you do a0 as opposed to " "?Gunzburg
@DominicTobias i actually can't remember ;-)Maggee
For reference, \a0 is a non-breaking space.Westley
For my use case, took this and modified a bit. Needed to add absolute position to top and a width of 100% on the element with the :before pseudo class, and relative position to the element, in case anyone else has the same issue.Lanford
D
10

Try with Shadow( Like border ) + Border

border-bottom: 5px solid #fff;
box-shadow: 0 5px 0 #ffbf0e;
Duer answered 7/11, 2013 at 5:29 Comment(0)
L
1

I know this is old. But yeah. I prefer much shorter solution, than Giona answer

[contenteditable] {
    border-bottom: 1px solid transparent;
    &:focus {outline: none; border-bottom: 1px dashed #000;}
}
Lalitta answered 28/9, 2018 at 11:38 Comment(0)
A
1

I like to give my input field a border, remove the outline on focus, and "outline" the border instead:

input {
  border: 1px solid grey;

  &:focus {
    outline: none;
    border-left: 1px solid violet;
  }
 }

You can also do it with a transparent border:

input {
  border: 1px solid transparent;

  &:focus {
    outline: none;
    border-left: 1px solid violet;
  }
 }
Aesculapius answered 24/12, 2018 at 5:42 Comment(0)
K
1

I have another great solution, maybe it will be useful for someone

.button {
    border-bottom: 2px solid transparent;
}
.button:hover {
    border-color: #000000;
}
<div class="button">
  Content
</div>
Kolnos answered 9/6, 2022 at 14:10 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Exaggerated
R
-1

The great thing about HTML/CSS is that there's usually more than one way to achieve the same effect. Here's another solution that does what you want:

<nav id="menu1">
    <ul>
        <li><a href="#">Menu Link 1</a></li>
        <li><a href="#">Menu Link 2</a></li>
    </ul>
</nav>

nav {
    background:#666;
    margin:1em;
    padding:.5em 0;
}
nav ul {
    border-top:1px dashed #fff;
    list-style:none;
    padding:.5em;
}
nav ul li {
    display:inline-block;
}
nav ul li a {
    color:#fff;
}

http://jsfiddle.net/10basetom/uCX3G/1/

Rachelrachele answered 16/11, 2013 at 7:42 Comment(0)
M
-3

only one side outline wont work you can use the border-left/right/top/bottom

if i an getting properly your comment

enter image description here

Matland answered 1/10, 2012 at 10:55 Comment(3)
How to make the border-left/right/etc inset? Like for example, 3 pixels away from the block border but inside?Gilman
@CorinneStoppelli could you explain ?Matland
@Database_Query, I think that you missed the part of using the outline propert (or similar), which means that he doesn't want to add more weight/height to the div. The border property actually does add, but outline doesn't. The box-shadow alternative, seems to be a better option, even if you need to play a little bit with its' options.Legionary

© 2022 - 2024 — McMap. All rights reserved.