0

How to insert text with transparent background on border ?

enter image description here

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144

2 Answers2

1

used :before and :after pseudo classes.

body {
  background-color: red;
}

.border {
  height: 120px;
  width: 400px;
  border: 2px solid black;
  position: relative;
  margin: 40px
}

.border:before {
  content: "My Header title";
  width: 180px;
  /* border: 1px solid; */
  position: absolute;
  text-align: center;
  top: -10px;
  background-color: red;
  left: 50%;
  transform: translateX(-50%);
}

.border:after {
  content: "My Footer title";
  width: 180px;
  /* border: 1px solid; */
  position: absolute;
  text-align: center;
  bottom: -10px;
  background-color: red;
  left: 50%;
  transform: translateX(-50%);
}
<div class="border">

</div>
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Xenio Gracias
  • 2,728
  • 1
  • 9
  • 16
0

lets try this below code:

<div class="border">
  <div class="border_box_left"></div>
  <div class="border_box_right"></div>
</div>

<style>
.border {
  height: 120px;
  width: 400px;
  position: relative;
  margin: 40px
}
.border:before {
  content: "My Header title";
  width: 180px;
  position: absolute;
  text-align: center;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
}
.border:after {
  content: "My Footer title";
  width: 180px;
  position: absolute;
  text-align: center;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
}
.border_box_left {
  position: absolute;
  content: "";
  width: 30%;
  height: 100%;
  left: 0px;
  top: 0px;
  border: 2px solid red;
  border-right: none;
}
.border_box_right {
  position: absolute;
  content: "";
  width: 30%;
  height: 100%;
  right: 0px;
  top: 0px;
  border: 2px solid red;
  border-left: none;
}
</style>

Want to generate dynamic borders then calculate the content width using script and give width to border_box_left and border_box_right.

Umapathi
  • 116
  • 10