128

Any way to declare a size/partial border to a box in CSS? For example a box with 350px that only shows a border-bottom in its firsts 60px. I think that might be very useful.

Examples:

enter image description here enter image description here

0

6 Answers 6

187

Not really. But it's very easy to achieve the effect in a way that degrades gracefully and requires no superfluous markup:

div {
  width: 350px;
  height: 100px;
  background: lightgray;
  position: relative;
  margin: 20px;
}

div:after {
  content: '';
  width: 60px;
  height: 4px;
  background: gray;
  position: absolute;
  bottom: -4px;
}
<div></div>

5
  • 3
    Nothing like being marked the right answer without +1'd. But I +1'd you! This was a perfect solution for my problem. Thanks! edit I guess you could have been +1'd be OP and -1'd by someone else. Oh well. I appreciate your answer, and that's all that matters, right ;-)
    – CWSpear
    Jun 19, 2012 at 18:28
  • 8
    How do you get this to be in the center, like in his second example? Aug 19, 2014 at 2:55
  • I should add, when the parent element is a fluid-width inline-block element. Obviously when it's fixed width it's easy. Aug 19, 2014 at 3:05
  • 5
    Nevermind, I got the effect I wanted - dabblet.com/gist/e5a78f2d4bf50b6be4d3. It's a shame the ::outside and ::inside pseudo-elements aren't available yet, I hate putting in markup just for styling, but I don't think there is any other way. Aug 19, 2014 at 3:15
  • This works! To use it with React, you cannot use content in :after as inline style or JSS, but it works fine using styled components. Nov 11, 2019 at 10:39
34

I know, this is already solved and pixels were requested. However, I just wanted to share something...

Partly underlined text elements can easily achieved by using display:table or display:inline-block

(I just don't use display:inline-block because, yeah you know, the awkward 4px-gap).

Textual Elements

h1 {
  border-bottom: 1px solid #f00;
  display: table;
}
<h1>Foo is not equal to bar</h1>

Centering, display:table makes it impossible to center the element with text-align:center.
Let's work around with margin:auto...

h1 {
  border-bottom: 1px solid #f00;
  display: table;
  margin-left: auto;
  margin-right: auto;
}
<h1>Foo is not equal to bar</h1>

Well, that's nice, but it's not partially.
As bookcasey already introduced, pseudo-elements are worth gold.

h1 {
  display: table;
  margin-left: auto;
  margin-right: auto;
}

h1:after {
  border-bottom: 1px solid #f00;
  content: '';
  display: block;
  width: 50%;
}
<h1>Foo is not equal to bar</h1>

Offset, the underline is left aligned right now. To center it, just push the pseudo-element the half of its width (50% / 2 = 25%) to the right.

h1 {
  display: table;
  margin-left: auto;
  margin-right: auto;
}

h1:after {
  border-bottom: 1px solid #f00;
  content: '';
  display: block;
  margin-left: 25%;
  width: 50%;
}
<h1>Foo is not equal to bar</h1>

...as davidmatas commented, using margin:auto is sometimes more practical, than calculating the margin-offset by hand.

So, we can align the underline to the left, right or center (without knowing the current width) by using one of these combinations:

  • Left: margin-right: auto (or just leave it off)
  • Middle: margin: auto
  • Right: margin-left: auto

Full example

.underline {
  display: table;
  margin-left: auto;
  margin-right: auto;
}

.underline:after {
  border-bottom: 1px solid #f00;
  content: '';
  display: block;
  width: 50%;
}

.underline--left:after {
  margin-right: auto; /* ...or just leave it off */
}

.underline--center:after {
  margin-left: auto;
  margin-right: auto;
}

.underline--right:after {
  margin-left: auto
}
<h1 class="underline underline--left">Foo is not equal to bar</h1>
<h1 class="underline underline--center">Foo is not equal to bar</h1>
<h1 class="underline underline--right">Foo is not equal to bar</h1>

Block-Level Elements

This can easily be adopted, so that we can use block-level elements. The trick is to set the pseudo-elements height to the same height as its real element (simply height:100%):

div {
  background-color: #eee;
  display: table;
  height: 100px;
  width: 350px;
}
div:after {
  border-bottom: 3px solid #666;
  content: '';
  display: block;
  height: 100%;
  width: 60px;
}
<div></div>

2
  • 2
    Great answer. For the :after I like more a margin: 0 auto approach instead the margin-left: 25% because it will work with any width you declare without need to do maths.
    – davidmatas
    May 20, 2016 at 8:49
  • 1
    @davidmatas Good point! I've just used the mathy-way to clarify how to position it manually by hand. With this example anyone could understand how to align it left/center/right. Anyway, I will add the auto-simplificator :)
    – yckart
    May 20, 2016 at 9:05
31

Here is another solution that rely on linear-gradient where you can easily create any kind of line you want. You can also have multiple lines (on each side for example) by using multiple background:

.box1 {
  width: 200px;
  padding: 20px;
  margin: 10px auto;
  text-align: center;
  background: 
    linear-gradient(to right, transparent 20%, #000 20%, #000 40%, transparent 40%) 0 100% / 100% 3px no-repeat, 
    #ccc
}

.box2 {
  width: 200px;
  padding: 20px;
  margin: 10px auto;
  text-align: center;
  background: 
    linear-gradient(to right, transparent 20%, red 20%, red 80%, transparent 80%) 0 100% / 100% 2px no-repeat, 
    #ccc
}

.box3{
  width: 200px;
  padding: 20px;
  margin: 10px auto;
  text-align: center;
  background: 
    linear-gradient(to right, transparent 20%, red 20%, red 80%, transparent 80%) 0 100% / 100% 2px no-repeat,
    linear-gradient(to right, transparent 30%, blue 30%, blue 70%, transparent 70%) 0 0 / 100% 2px no-repeat,
    linear-gradient(to bottom, transparent 30%, brown 30%, brown 70%, transparent 70%) 0 0 / 3px 100% no-repeat,
    linear-gradient(to bottom, transparent 20%, orange 20%, orange 70%, transparent 70%) 100% 0 / 3px 100% no-repeat,
  #ccc
}
<div class="box1">
  Box1
</div>
<div class="box2">
  Box2
</div>
<div class="box3">
  Box3
</div>

Here is another syntax to achieve the same as above:

.box1 {
  width: 200px;
  padding: 20px;
  margin: 10px auto;
  text-align: center;
  background: 
   linear-gradient(#000 0 0) top /40% 3px no-repeat, 
   #ccc
}

.box2 {
  width: 200px;
  padding: 20px;
  margin: 10px auto;
  text-align: center;
  background: 
    linear-gradient(red 0 0) bottom/ 60% 2px no-repeat, 
    #ccc;
}

.box3{
  width: 200px;
  padding: 20px;
  margin: 10px auto;
  text-align: center;
  background: 
   linear-gradient(red 0 0)bottom left/ 60% 2px,
   linear-gradient(blue 0 0) 60% 0 / 40% 2px,
   linear-gradient(brown 0 0) left/ 3px 30%,
   linear-gradient(orange 0 0) right / 3px 40%,
   #ccc;
  background-repeat:no-repeat;
}
<div class="box1">
  Box1
</div>
<div class="box2">
  Box2
</div>
<div class="box3">
  Box3
</div>

4
  • 3
    Wow... I've been fiddling for hours to find a way to have only the upper left and upper right corner of an "implied" box showing. :before and :after was too limited, it messed up the object's positioning. This is a brilliant solution!
    – Wouter
    Jan 16, 2020 at 10:11
  • 1
    Some background information on why and how this works: webfx.com/blog/web-design/background-css-shorthand
    – Wouter
    Jan 16, 2020 at 10:18
  • 2
    It clips the corner borders if used with border-radius
    – Vandesh
    Sep 14, 2020 at 14:36
  • Great solution. I wonder how to stick a text or an image in the middle of each line.
    – Ferit
    Jun 19 at 15:28
2

I used a grid to build draw some of the borders.

See here.

Code:

/* ungrid without mobile */

.row {
  width: 100%;
  display: table;
  table-layout: fixed;
}

.col {
  display: table-cell;
}


/* things to change */

.row {
  width: 70%;
  margin: auto;
}

.mid.row>.col {
  height: 150px;
}


/* draw box and align text */

.col {
  text-align: center;
}

.top.left.col {
  border-top: 1px solid black;
  border-left: 1px solid black;
}

.top.right.col {
  border-top: 1px solid black;
  border-right: 1px solid black;
}

.bottom.left.col {
  border-bottom: 1px solid black;
  border-left: 1px solid black;
}

.bottom.right.col {
  border-bottom: 1px solid black;
  border-right: 1px solid black;
}

.mid.row>.col {
  border-left: 1px solid black;
  border-right: 1px solid black;
  vertical-align: middle;
}

.top.center.col {
  position: relative;
  top: -0.5em;
}

.bottom.center.col {
  position: relative;
  bottom: -0.5em;
}
<div class="row">
  <div class="top left col"></div>
  <div class="top center col">Top</div>
  <div class="top right col"></div>
</div>
<div class="mid row">
  <div class="col">Mid</div>
</div>
<div class="row">
  <div class="bottom left col"></div>
  <div class="bottom center col">Bottom</div>
  <div class="bottom right col"></div>
</div>

1
  • @SverriM.Olsen since the person edited the code, the comment is obsolete
    – Sagar V
    Jul 17, 2017 at 9:39
-1

CSS does not support partial borders. You'd need to use an adjacent element to simulate this.

1
  • Or a pseudo-element, which you can do entirely with CSS and not add any markup, as illustrated by the answers above
    – OG Sean
    Jan 5, 2020 at 20:49
-1

Been playing a bit around with your solutions and came up with that.

I'd appreciate your comments and thoughts.

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>test file</title>
  <style>
    #box {
      background-color: gray;
      position: relative;
      left: 10px;
      top: 10px;
      height: 180px;
      width: 380px;
    }
    
    #grad1 {
      position: absolute;
      left: -10px;
      top: -10px;
      height: 40px;
      width: 2px;
      background-image: linear-gradient(red, red);
    }
    
    #grad2 {
      position: absolute;
      left: -10px;
      top: -10px;
      height: 2px;
      width: 40px;
      background-image: linear-gradient(red, red);
    }
  </style>
</head>

<body>
  <div id="box">
    <div id="grad1"></div>
    <div id="grad2"></div>
  </div>
</body>

</html>

Not the answer you're looking for? Browse other questions tagged or ask your own question.