47

I am confused as to have to make it work in CSS only to have a div where the border would be only visible on half it's width.

The border style is a simple 1px solid #000;

However, I want the top of the div's border to not be visible everywhere (on 100% on the div's width), rather only on the first 50% of the div's width.

I haven't been able to get an example of this anywhere, and it needs to be in percentages, so the usual bag of tricks (image over the second half,...).

Devin Burke
  • 13,642
  • 12
  • 55
  • 82
  • Code please! Tough to answer questions without it. – Casey Robinson Jan 10 '12 at 14:05
  • I'd say this is near impossible to achieve only with css and without markup changes or using tricks specific to the css/html on your site. – kontur Jan 10 '12 at 14:05
  • 1
    @Drewdin and Casey what do you need code for? The question is simple: it's about a div and a border. – Mr Lister Jan 10 '12 at 14:06
  • 1
    My bad guys, I'm not asking for anything specific, just if it's even possible to accomplish this using only CSS. The code would look something like this: HTML:
    CSS: #holder { border: 1px solid #000; height: 200px; width: 200px; }
    –  Jan 10 '12 at 14:07
  • you need border gradient for this check out this links to achieve this http://stackoverflow.com/questions/2717127/css3-gradient-borders – Bipin Chandra Tripathi Jan 10 '12 at 14:10
  • @MrLister providing code shows effort and creates a framework so that everyone is on the same page. – Casey Robinson Jan 10 '12 at 16:45
  • I cannot see what code he could provide besides `1px solid #000;`. – RSinohara Jul 31 '16 at 15:57

5 Answers5

35

If you do not want to mess with the HTML at all, you can do it with an empty pseudoelement, using CSS only. You still need to know the background color, of course (assuming white here):

<span class="half-a-border-on-top">Hello world!</span>

<style>
.half-a-border-on-top {
  border-top:1px solid black;
  position: relative;
}
.half-a-border-on-top:after {
  padding:0;margin:0;display:block;/* probably not really needed? */
  content: "";
  width:50%;
  height:1.1px;/* slight higher to work around rounding errors(?) on some zoom levels in some browsers. */
  background-color:white;
  position: absolute;
  right:0;
  top:-1px;
}
</style>

Result:

Half of a top border visible above the text "Hello world"

Snippet

    .half-a-border-on-top {
      border-top:1px solid black;
      position: relative;
    }
    .half-a-border-on-top:after {
      padding:0;margin:0;display:block;/* probably not really needed? */
      content: "";
      width:50%;
      height:1.1px;
      background-color:white;
      position: absolute;
      right:0;
      top:-1px;
    }
    <span class="half-a-border-on-top">Hello world!</span>

Fiddle:

http://jsfiddle.net/vL1qojj8/

Edit 2023: Now that even Safari seems to fully and properly support linear-gradient, the answer by 红了樱桃绿了吧唧 is probably more elegant, and will work without knowing the background color.

leo
  • 8,106
  • 7
  • 48
  • 80
35

You can use CSS gradient border

half of border

.half-a-border-on-top {
  border-top: 1px solid;
  border-image: linear-gradient(to right, #000 50%, transparent 50%) 100% 1;
}
<span class="half-a-border-on-top">Hello world!</span>

    
31

Would this work:

#holder {
        border: 1px solid #000;
        height: 200px;
        width: 200px;
        position:relative;
        margin:10px;
} 
#mask {
        position: absolute;
        top:-1px;
        left:1px;
        width:50%;
        height: 1px;
        background-color:#fff;
}
<div id="holder">
        <div id="mask"></div>
</div>

    
niyasc
  • 4,440
  • 1
  • 23
  • 50
jeanreis
  • 898
  • 8
  • 20
1

let show you how i edit the code of leo, to put a half border at left in center.

try this:

html code

<div class="half-a-border-on-left">Hello world!</div>

css code

   <style>
.half-a-border-on-left {
  border-left: 1px solid black;
  position: relative;
  height: 50px;
  background: red;
}
.half-a-border-on-left:after {
  padding:0;
  margin:0;
  content: "";
  width: 1px;
  height: 10px;
  background-color:white;
  position: absolute;
  left:-1px;
  top: -10px;
}
.half-a-border-on-left:before {
  padding:0;
  margin:0;
  content: "";
  width: 1px;
  height: 10px;
  background-color:white;
  position: absolute;
  left: -1px;
  bottom: -5px;
}
</style>

Those are code i use to put a half border thank you leo,

kadibra
  • 89
  • 1
  • 1
  • 9
-4

I love Hyderabad

***

.div_1 {
    width: 50px;
    border-bottom: 2px solid black;
}
.div_2 {
   width: max-content;
   margin-bottom: 10px;
}
<div class="div_1" ><div class="div_2">I love Hyderabad</div></div>