3

Как сделать фон под текстом, что бы он был на всю длину содержимого? Сейчас такое решение:

.title_new {
    background: #df2721;
    color: #fff;
    width: auto;
    padding: 2px 7px;
    display: inline-block;
}
a{
  text-decoration: none;
}
<div class="title_main" style="position:absolute; bottom:50px;">
                

<a href="№" class="title_new">Lorem ipsum dolor sit amet.</a><br>
<a style="cursor: pointer;" href="№">
<span style="cursor: pointer;" class="title_new">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis quis harum quos optio dicta, dignissimos, doloribus illo autem ullam porro!</span>
  </a></div>

надо что бы край фона не прижимался после переноса строки: введите сюда описание изображения

Частично решает проблему замена display: inline-block; на display: inline;

но потом это выглядит так, что не есть красиво. Нужно что бы до и после текста сохранялись падинги в 7px;
введите сюда описание изображения

Есть ли какое-то красивое решение данной задачи?

3
  • Возможный дубликат вопроса: Фон только под текстом
    – Air
    12 янв 2018 в 9:18
  • @Air ага, смотрел находил - не оно!
    – Swartex
    12 янв 2018 в 9:20
  • @Air там display: inline, который автору не подходит
    – andreymal
    12 янв 2018 в 15:21

1 ответ 1

9

Пример

* {
  box-sizing: border-box;
}

.title_new {
  background: #df2721;
  color: #fff;
  width: auto;
  padding: 2px 7px;
  display: inline;
  box-shadow: -7px solid #df2721, 7px solid #df2721;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

a {
  text-decoration: none;
}
<div class="title_main" style="position:absolute; bottom:50px;">


  <a href="№" class="title_new">Lorem ipsum dolor sit amet.</a>
  <br>
  <a style="cursor: pointer;" href="№"><span style="cursor: pointer;" class="title_new">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis quis harum quos optio dicta, dignissimos, doloribus illo autem ullam porro! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis quis harum quos optio dicta, dignissimos, doloribus illo autem ullam porro!</span>
  </a>
</div>

Еще больше вариантов решения - css-tricks.com/multi-line-padded-text

1
  • Спасибо! выручили
    – Swartex
    12 янв 2018 в 9:14

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.