2

Есть восьмиугольник. Как вырезать внутри него круг, чтобы получилась гайка, используя только CSS ?

.octagon {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  display: block;
}

.octagon:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  border-bottom: 29px solid red;
  border-left: 29px solid #eee;
  border-right: 29px solid #eee;
  width: 42px;
  height: 0;
}

.octagon:after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 29px solid red;
  border-left: 29px solid #eee;
  border-right: 29px solid #eee;
  width: 42px;
  height: 0;
}
<i class="octagon"></i>

5
  • а SVG точно не вариант? 4 ноя 2017 в 21:36
  • точно)) нужно сделать всё только на css 4 ноя 2017 в 21:39
  • посмотрите тут или тут, а вырез я думаю с помощью маски делать можно 4 ноя 2017 в 21:46
  • Кроссбраузерность нужна?
    – user271410
    4 ноя 2017 в 22:26
  • нет. не нужна. это учебное задание, тренируюсь рисовать разные фигуры на css и вот застряла((( 4 ноя 2017 в 22:32

3 ответа 3

1

.octagon {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  display: block;
}

.octagon:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  border-bottom: 29px solid red;
  border-left: 29px solid white;
  border-right: 29px solid white;
  width: 42px;
  height: 0;
}

.octagon:after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 29px solid red;
  border-left: 29px solid white;
  border-right: 29px solid white;
  width: 42px;
  height: 0;
}

.circle {
  position: absolute;
  width: 50px;
  height: 50px;
  left: 25px;
  top: 25px;
  background: white;
  border-radius: 50%;
  z-index: 1;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>test</title>
  <link rel="stylesheet" href="D:\test\style\main.css">
  <script src="D:\test\js\main.js" defer></script>
</head>

<body>
  <div id="testwrap">
    <div id="test">
      <div class="headerwrap">
        <div class="header">

        </div>
      </div>
      <div class="contentwrap">
        <div class="content">
          <div class="menuwrap">
            <div class="menu">

            </div>
          </div>
          <div class="wrapper">
            <div class="content">
              <div class="inner--content">
                <div class="text">
                  <i class="octagon">
										<span class="circle"></span>
									</i>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="footerwrap">
        <div class="footer">

        </div>
      </div>
    </div>
  </div>

  <script src="D:\jquery\jquery-3.2.1.min (1).js"></script>
</body>

</html>

1

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  background: #ccc;
}

.nut {
  position: relative;
  margin: 45px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  box-shadow: 0 0 0 10px #f00;
  transition: transform 1s;
}

[class*=nut-] {
  position: absolute;
  background: #f00;
}

.nut-t,
.nut-b {
  width: 100px;
  height: 30px;
  left: 50%;
  margin-left: -50px;
}

.nut-t {
  top: -30px;
  -webkit-clip-path: polygon(29% 0, 71% 0, 100% 100%, 0% 100%);
  clip-path: polygon(29% 0, 71% 0, 100% 100%, 0% 100%);
}

.nut-b {
  bottom: -30px;
  -webkit-clip-path: polygon(0 0, 100% 0, 71% 100%, 29% 100%);
  clip-path: polygon(0 0, 100% 0, 71% 100%, 29% 100%);
}

.nut-l,
.nut-r {
  width: 30px;
  height: 80px;
  top: 50%;
  margin-top: -40px;
}

.nut-l {
  left: -30px;
  -webkit-clip-path: polygon(0 25%, 100% 0, 100% 100%, 0 75%);
  clip-path: polygon(0 25%, 100% 0, 100% 100%, 0 75%);
}

.nut-r {
  right: -30px;
  -webkit-clip-path: polygon(0 0, 100% 25%, 100% 75%, 0 100%);
  clip-path: polygon(0 0, 100% 25%, 100% 75%, 0 100%);
}

.nut:hover {
  box-shadow: 0 0 0 10px #00f;
  transform: rotate(90deg);
}

.nut:hover [class*=nut-] {
  background: #00f;
}
<div class="nut">
  <span class="nut-t"></span>
  <span class="nut-r"></span>
  <span class="nut-b"></span>
  <span class="nut-l"></span>
</div>

0

Если можно новый элемент добавить, то добавьте элемент круга внутри:

.octagon {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  display: block;
}

.octagon:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  border-bottom: 29px solid red;
  border-left: 29px solid #eee;
  border-right: 29px solid #eee;
  width: 42px;
  height: 0;
}

.octagon:after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 29px solid red;
  border-left: 29px solid #eee;
  border-right: 29px solid #eee;
  width: 42px;
  height: 0;
}
#circle {
  z-index: 1;
  content: '';
  width: 50px;
  height: 50px;
  display: block;
  position: absolute;
  top: 25px;
  left: 25px;
  border-radius: 50px;
  background-color: #eee;
}
<i class="octagon"><div id="circle"></div></i>

1
  • Нет. мне нужно именно гайку нарисовать. Каким способом не важно - можно несколько элементов отрисовать и соединить. Главное, чтобы на css. Я нашла только как нарисовать шестиугольник. Но мне не подходит так как нет отверстия посредине фигуры. 4 ноя 2017 в 22:31

Ваш ответ

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

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