Border line next to text

I have a tag p. I want a border next to him.

<p style="margin-left: -30px;font-size: 12px;margin-bottom: 2px;"><strong> Categories</strong></p>

enter image description here

I want to add a line next to p tag as the next image.

How can I implement it?

Please help, thanks

+2
source share
5 answers

Use pseudo-element

Jsfiddle demo

CSS

p {
    font-size: 12px;
    margin-bottom: 2px;
    overflow: hidden;
    position: relative;

   }

p:after {
    content:"";
    position: absolute;
    border-bottom:1px solid grey; /* border-size & color are now controllable */
    width:100%;   
    height:1em;
    display: inline;
    margin-left: 1em;
}
+5
source

There are many other ways to achieve this: one of them will apply border-bottomto the pseudo-element (which sets a new block formatting format to prevent overlapping) and the floating element <strong>from left to right:

p.hasBorder {
  overflow: hidden; /* Establish a new block formatting context */
}

p.hasBorder > strong {
  float: left;
}

p.hasBorder:after {
  content: "";
  display: block;
  border-bottom: 3px solid silver;
  overflow: hidden;  /* Establish a new block formatting context */
  height: 1em;       /* Up to you */
}
<p class="hasBorder">
  <strong>Categories</strong>
</p>
Run codeHide result
+5
source
p{

}

P:after{
    content:'________';
}

DEMO

+1

JS Fiddle

p {
    font-size: 12px;
    margin-bottom: 2px;
    position:relative
}
p::after {
    content:"";
    border-bottom:1px solid grey;
    width:100px;
    position:absolute;
    bottom:2px;
}
+1

p {
  margin-left: 0px;
  font-size: 12px;
  margin-bottom: 2px;
  position: absolute;
  margin-top: -7px;
  background-color: #fff;
  color: #333;
  padding-right: 5px;
}
.line-back {
  border-bottom: 1px solid #ccc;
  margin: 25px;
}
<div class="line-back">
  <p>
    <strong> Categories</strong>
  </p>
  </div
Hide result
+1

Source: https://habr.com/ru/post/1626677/


All Articles