Проблемы с подчеркиваниме при наведении на ссылку css

Рейтинг: 0Ответов: 1Опубликовано: 29.03.2023

Никак не могу выровнять линии подчеркивания,выходят за пределы слова. Не могу понять как указать длину border-bottom при наведении. Кто знает как это исправить?

body{
    background: linear-gradient(230.06deg, #C9C2C2 46.57%, rgba(209, 195, 195, 0) 72.79%);
}

header {
  max-width: 1444px;
  height: 130px;
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  font-family: "Inter";
  background: linear-gradient(
    133.19deg,
    #000000 46.98%,
    rgba(0, 0, 0, 0) 138.79%
  );
}

.logo {
  padding-right: 223px;
  padding-left: 37px;
}

.menu {
  flex-grow: 1;
}

.menu ul {
  display: flex;
  flex-flow: row nowrap;
  margin: 2px 0 0;
  padding: 0;
  font-size: 17px;
  line-height: 18px;
  list-style: none;
  letter-spacing: -0.02em;
}

.menu li {
  margin-right: 20px;
}

.menu li:last-child {
  margin-right: 0;
}

.menu a {
  display: inline-block;
  padding: 0px 34px;
  letter-spacing: -0.02em;
}

.menu a {
  padding-top: 53px;
  color: #000;
  text-decoration: none;
  color: #fff;
}

.RightN {
  padding-top: 53px;
  font-size: 17px;
  line-height: 24px;
  padding-right: 124px;
}

.RightN a {
  color: #fff;
  text-decoration: none;
}

.right {
  padding-right: 98px;
}

.menu a:hover {
  border-bottom: 2px solid #ccb81d;
}

.RightN a:hover {
  border-bottom: 2px solid #ccb81d;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/style.css">
</head>
<body>
    <header>
        <div class="logo">
          <a href="/">
            <img src="https://bureau.ru/soviet/130568/files/logo.svg" width="123" height="120px" width="123" height="125px" />
          </a>
        </div>
        <nav class="menu">
          <ul>
            <li><a href="/products">Продукция</a></li>
          <li><a href="/services">Услуги</a></li>
          <li><a href="/services">Блог</a></li>
          <li><a href="/contacts">Контакты</a></li>
          </ul>
        </nav>
        <div class="RightN">
            
          <a href="#"><span class="right">Регистрация</span></a>
          <a href="#">Войти</a>
        </div>
      </header>
</body>
</html>

Ответы

▲ 1

Проблема из-за свойства padding для ссылок в меню, что приводит к тому, что при наведении и добавлении border-bottom линия расширяется на величину padding и может выходить за пределы слова.

можно использовать свойство display с значением inline-block для ссылок и задавать отступы с помощью свойств margin. Также можно использовать box-sizing: border-box, чтобы размеры блоков включали границы и отступы.

Для указания длины border-bottom при наведении на ссылку можно через ::after:

.menu a {
  display: inline-block;
  margin-right: 20px;
  padding: 53px 34px 0;
  text-decoration: none;
  color: #fff;
  box-sizing: border-box;
}

.menu a:hover::after {
  content: '';
  display: block;
  border-bottom: 2px solid #ccb81d;
  margin-top: 5px;
  width: 100%;
}

Итог:

body{
    background: linear-gradient(230.06deg, #C9C2C2 46.57%, rgba(209, 195, 195, 0) 72.79%);
}

header {
  max-width: 1444px;
  height: 130px;
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  font-family: "Inter";
  background: linear-gradient(
    133.19deg,
    #000000 46.98%,
    rgba(0, 0, 0, 0) 138.79%
  );
}

.logo {
  padding-right: 223px;
  padding-left: 37px;
}

.menu {
  flex-grow: 1;
}

.menu ul {
  display: flex;
  flex-flow: row nowrap;
  margin: 2px 0 0;
  padding: 0;
  font-size: 17px;
  line-height: 18px;
  list-style: none;
  letter-spacing: -0.02em;
}

.menu li {
  margin-right: 20px;
}

.menu li:last-child {
  margin-right: 0;
}

.menu a {
  display: inline-block;
  margin-right: 20px;
  padding: 53px 34px 0;
  text-decoration: none;
  color: #fff;
  box-sizing: border-box;
}

.menu a:hover::after {
  content: '';
  display: block;
  border-bottom: 2px solid #ccb81d;
  margin-top: 5px;
  width: 100%;
}

.RightN {
  padding-top: 53px;
  font-size: 17px;
  line-height: 24px;
  padding-right: 124px;
}

.RightN a {
  color: #fff;
  text-decoration: none;
}

.right {
  padding-right: 98px;
}

.RightN a:hover {
  border-bottom: 2px solid #ccb81d;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/style.css">
</head>
<body>
    <header>
        <div class="logo">
          <a href="/">
            <img src="https://bureau.ru/soviet/130568/files/logo.svg" width="123" height="120px" width="123" height="125px" />
          </a>
        </div>
        <nav class="menu">
          <ul>
            <li><a href="/products">Продукция</a></li>
          <li><a href="/services">Услуги</a></li>
          <li><a href="/services">Блог</a></li>
          <li><a href="/contacts">Контакты</a></li>
          </ul>
        </nav>
        <div class="RightN">
            
          <a href="#"><span class="right">Регистрация</span></a>
          <a href="#">Войти</a>
        </div>
      </header>
</body>
</html>