Подтвердить что ты не робот

Половинная цветная рамка сверху и снизу с помощью CSS

введите описание изображения здесь

Я хотел бы создать свой заголовок как показано выше. Здесь речь идет о границе с двумя разными цветами. Вот код, который я пробовал до сих пор. Спасибо заранее.

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1 {
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;
  border-top: 3px solid #880015;
  border-bottom: 3px solid #880015;  
}
<header>
  <h1>HEADER</h1>
</header>
4b9b3361

Ответ 1

Используйте теги ::before и ::after в теге h1 вместе с linear-gradient как background используйте высоту вместо границы, чтобы получить этот стиль,

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1{
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;
  position:relative;
}
h1:before{
  content:"";
  width:100%;
  height:3px;
  left:0;
  top:0;
  position:absolute;
  z-index:9;
  background:linear-gradient(to right, white 50%, brown 50%);
}
h1:after{
  content:"";
  width:100%;
  height:3px;
  left:0;
  bottom:0;
  position:absolute;
  z-index:9;
  background:linear-gradient(to right, brown 50%, white 50%);
}
<header>
  <h1>HEADER</h1>
</header>

Ответ 2

Здесь один путь без использования псевдоэлементов:

h1 {
  display: inline-block;
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  background:linear-gradient(to right, #ccc 50%, maroon 50%) bottom,
    linear-gradient(to right, maroon 50%, #ccc 50%) top;
  background-repeat: no-repeat;
  background-size:100% 2px;
} 

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1 {
  display: inline-block;
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  background:linear-gradient(to right, #ccc 50%, maroon 50%) bottom,
    linear-gradient(to right, maroon 50%, #ccc 50%) top;
  background-repeat: no-repeat;
  background-size:100% 3px;
}
<header>
  <h1>HEADER</h1>
</header>

Ответ 3

В отличие от многих других ответов нет, вам не нужно использовать псевдоэлементы. Использование нескольких градиентов будет работать отлично:

  • Используйте два градиента: один повернутый 90deg, а другой - -90deg. Используйте две цветовые остановки: #880015 на 50% и #fff на 50%
  • Используйте размер фона 100% ширины и 3 пикселя в высоту, т.е. background-size: 100% 3px
  • Поместите два фонов в верхнем левом и нижнем левом углу вашего элемента.

Voila - см. ниже доказательство концепции:

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1 {
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;
  
  background-image:
    linear-gradient(90deg, #880015 50%, #fff 50%),
    linear-gradient(-90deg, #880015 50%, #fff 50%);
    
  background-size: 100% 3px;
  
  background-position:
    top left,
    bottom left;
  
  background-repeat: no-repeat;
}
<header>
  <h1>HEADER</h1>
</header>

Ответ 4

Вот один из способов сделать это. Я использую линейный градиент для псевдоэлементов :before и :after, с позицией absolute для этого.

Я использовал повторяющиеся значения цвета на 50%, чтобы произвести резкое изменение цвета, причем второй цвет не имеет прозрачности, чтобы сохранить исходный цвет границы: linear-gradient(to right, #fff, #fff 50%, rgba(0,0,0,0) 50%, rgba(0,0,0,0))

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1 {
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;
  border-top: 3px solid #880015;
  border-bottom: 3px solid #880015;  
  position: relative;
  width: 199px;
}

h1::before {
    display: block;
    content: "";
    position: absolute;
    top: -3px;
    left: 0px;
    width: 5em;
    background: linear-gradient(to right, #fff, #fff 50%, rgba(0,0,0,0) 50%, rgba(0,0,0,0));
    height: 3px;
}

h1::after {
   display: block;
    content: "";
    position: absolute;
    bottom: -3px;
    left: 0px;
    width: 5em;
    background: linear-gradient(to right, rgba(0,0,0,0),  rgba(0,0,0,0) 50%, #fff 50%, #fff);
    height: 3px;
}
<header>
  <h1>HEADER</h1>
</header>

Ответ 5

Вы можете использовать Position и Psuedo Elements для достижения этого.

header {
  text-align: center;
  background-color: #7f7f7f;
  position: relative;
}

h1 {
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;
  border-top: 3px solid #880015;
  border-bottom: 3px solid #880015;
  position: relative;
}

h1:after,
h1:before {
  content: '';
  height: 3px;
  width: 50%;
  background: #fff;
  position: absolute;
}

h1:after {
  top: -3px;
  left: 0;
}

h1:before {
  bottom: -3px;
  right: 0;
}
<header>
  <h1>HEADER</h1>
</header>

Ответ 6

Добавление элемента before и after с границами и правильное позиционирование делает трюк.

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1 {
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;
  border-top: 3px solid #880015;
  border-bottom: 3px solid #880015;  
  position: relative;
}

h1:before {
  border-top: 3px solid lightgray;
  display: block;
  position: absolute;
  content: '';
  left: 0;
  right: 50%;
  bottom: -3px;
  width: 50%;
}

h1:after{
  border-top: 3px solid lightgray;
  display: block;
  position: absolute;
  content: '';
  left: 50%;
  right: 0;
  top: -3px;
  width: 50%;
}
<header>
  <h1>HEADER</h1>
</header>

Ответ 7

Я думаю, что это то, чего вы хотите.

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1 {
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;
  position:relative;
}
h1:before, h1:after {
    background: linear-gradient(to right, rgb(136, 0, 21) 0%, rgb(136, 0, 21) 50%, rgb(195, 195, 195) 50%, rgb(195, 195, 195) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f85032', endColorstr='#e73827', GradientType=1 );
    position: absolute;
    width: 100%;
    height: 2px;
    display: inline-block;
    content: "";
}
h1:before {
    top: 0;
    left: 0;
}
h1:after {
    bottom: 0;
    left: 0;
    transform: rotate(180deg);
}
<header>
  <h1>HEADER</h1>
</header>

Ответ 8

header {
  text-align: center;
  background-color: #7f7f7f;
}

h1 {
  color: #00a2e8;
  font-size: 40px;
  padding: 5px 0;
  display: inline-block;

}

h1:after {
  content: "";
  width: 50%;
  display: block;
  border-bottom: 3px solid #880015;
}
h1 span:after {
  content: "";
  width: 50%;
  display: block;
  border-bottom: 3px solid #fff;
  float:right;

}
h1:before {
  content: "";
  width: 50%;
  display: block;
  border-bottom: 3px solid #fff;
}
h1 span:before {
  content: "";
  width: 50%;
  display: block;
  border-bottom: 3px solid #880015;
  float:right;
        margin-top: -3px;
}
<header>
  <h1><span>HEADER</span></h1>
</header>