CSS обратный радиус границы за пределами ограничивающей рамки элемента для создания дизайна выемки мобильного телефона

Я пытаюсь создать что-то похожее на мобильный телефон с помощью HTML и CSS, и я хотел бы, чтобы у камеры было что-то вроде «перевернутого радиуса границы», которое плавно соединяет ее с рамкой.

Я не могу просто увеличить его и замаскировать ненужную область псевдоэлементом с белым фоном, потому что содержимое экрана не всегда может быть белым.

Кроме того, я не могу использовать mask-image для того же элемента, потому что этот «перевернутый радиус границы» на самом деле выходит за пределы его ограничивающей рамки, поэтому я фактически добавляю больше области, а не вычитаю (плюс поддержка очень низкая).

Я хочу по возможности избегать использования SVG.

body {
  position: relative;
  overflow: hidden;
  height: 100vh;
  margin: 0;
}

.phone {
  width: 420px;
  height: 800px;
  padding: 12px 12px 24px;
  position: absolute;
  top: 32px;
  left: 50%;
  transform: translate(-50%, 0);
  background: #000;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, .125);
  border-radius: 16px;
}

.screen {
  height: 100%;
  overflow: hidden;
  position: relative;
  background: #FFF;
  border-radius: 8px;
}

.viewport {
  height: 100%;
  position: relative;
  overflow-x: hidden;
  overflow-y: scroll;
}

.notch {
  top: 12px;
  left: 50%;
  width: 24px;
  height: 12px;
  z-index: 10;
  position: absolute;
  transform: translate(-50%, 0);
  background: #000;
  border-bottom-left-radius: 1024px;
  border-bottom-right-radius: 1024px;
}

.camera {
  top: 0;
  left: 50%;
  width: 12px;
  border: 4px solid #33244A;
  height: 12px;
  position: absolute;
  transform: translate(-50%, -50%);
  background:  #304A58;
  border-radius: 1024px;
  box-sizing: border-box;
}
<div class="phone">
  <div class="notch">
    <div class="camera"></div>
  </div>

  <div class="screen">
    <div class="viewport"></div>
  </div>
</div>


person Danziger    schedule 11.12.2019    source источник
comment
связанный (вероятно, дубликат) stackoverflow.com/q/50402503/8620333   -  person Temani Afif    schedule 11.12.2019
comment
@TemaniAfif Не совсем дубликат, но похож. В этом другом вопросе форма, которую хочет создать OP, состоит из эллипсов, что делает некоторые из более простых решений, которые я добавил здесь, недействительными. Для более простых форм, подобных этой, можно использовать больше альтернатив.   -  person Danziger    schedule 11.12.2019
comment
вы соглашаетесь с дубликатом, добавляя свой ответ в другой вопрос с подробным описанием точно таких же методов и показывая, что, в конце концов, это тот же вопрос, и единственная разница - небольшая разница в форме. Окружность является частным случаем многоточия.   -  person Temani Afif    schedule 26.12.2019


Ответы (1)


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

  • Добавление 2 псевдоэлементов с radial-gradient.

    Самое простое и хорошо поддерживаемое решение. Наверное, тот, который я бы использовал.

  • Добавляем 2 псевдоэлемента с mask-image (то же самое, что и выше, но с худшей поддержкой).

    По коду очень похоже на превью, но с действительно плохой поддержкой (требуется префиксы браузера для тех, которые его поддерживают).

  • Добавление 2 псевдоэлементов с border-radius, box-shadow и background: transparent.

    Нужно чуть больше кода, но выглядит чуть плавнее, по крайней мере на Chrome Version 78.0.3904.108, так что может оно того стоит для вас, хотя разница минимальна. В любом случае формы, которые вы можете сделать, не могут быть такими сложными, как в предыдущих вариантах, особенно если вы хотите работать с эллипсами, а не с кругами, как в этом другом вопросе: https://stackoverflow.com/a/59278227/3723993.

  • Использование SVG.

    Я думаю, что решение SVG здесь того не стоит, но оно было бы хорошей альтернативой для более сложных фигур или анимированных/переходных фигур.

Здесь вы можете проверить первые 3 решения:

const notch = document.getElementById('notch');
const button = document.getElementById('button');
const xrayCheckbox = document.getElementById('xrayCheckbox');
const xrayLabel = document.getElementById('xrayLabel');
const label = document.getElementById('label');

const solutions = [{
  name: 'pseudoelements + radial-gradient',
  classes: 'notch notch-gradient'
}, {
  name: 'pseudoelements + box-shadow',
  classes: 'notch notch-shadow'
}, {
  name: 'pseudoelements + mask-image',
  classes: 'notch notch-mask'
}];

let currentSolutionIndex = 0;
let currentSolution = solutions[currentSolutionIndex];
let xRayEnabled = false;

button.onclick = () => {
  currentSolutionIndex = (currentSolutionIndex + 1) % solutions.length;
  currentSolution = solutions[currentSolutionIndex];
  
  updateLabels();
};

xrayCheckbox.onchange = () => {  
  xRayEnabled = xrayCheckbox.checked;
  
  updateLabels();
};

function updateLabels() {
  if (xRayEnabled) {
    notch.className = `${ currentSolution.classes }-xray`;
    label.innerText = `${ currentSolution.name } (X-Ray)`;
    xrayLabel.innerText = 'Disable X-Ray';
  } else {
    notch.className = currentSolution.classes;
    label.innerText = currentSolution.name;
    xrayLabel.innerText = 'Enable X-Ray';
  }
}
body {
  position: relative;
  overflow: hidden;
  height: 100vh;
  margin: 0;
}

.phone {
  width: 420px;
  height: 800px;
  padding: 12px 12px 24px;
  position: absolute;
  top: 32px;
  left: 50%;
  transform: translate(-50%, 0);
  background: #000;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, .5);
  border-radius: 16px;
}

.screen {
  height: 100%;
  overflow: hidden;
  position: relative;
  background: #FFF;
  border-radius: 8px;
}

.viewport {
  height: 100%;
  position: relative;
  overflow-x: hidden;
  overflow-y: scroll;
}

.notch {
  top: 12px;
  left: 50%;
  width: 24px;
  height: 12px;
  z-index: 10;
  position: absolute;
  transform: translate(-50%, 0);
  background: #000;
  border-bottom-left-radius: 1024px;
  border-bottom-right-radius: 1024px;
}

.notch::before,
.notch::after {
  top: 0;
  width: 8px;
  height: 8px;
  content: "";
  position: absolute;
}

.notch-gradient-xray,
.notch-shadow-xray,
.notch-mask-xray {
  background: red;
}

/* RADIAL GRADIENT SOLUTION */

.notch-gradient::before {
  left: -6px;
  background: radial-gradient(circle at bottom left, transparent 0, transparent 70%, black 70%, black 100%);
}

.notch-gradient::after {
  right: -6px;
  background: radial-gradient(circle at bottom right, transparent 0, transparent 70%, black 70%, black 100%);
}

.notch-gradient-xray::before {
  left: -6px;
  background: green radial-gradient(circle at bottom left, transparent 0, transparent 70%, cyan 70%, cyan 100%);
}

.notch-gradient-xray::after {
  right: -6px;
  background: green radial-gradient(circle at bottom right, transparent 0, transparent 70%, cyan 70%, cyan 100%);
}

/* BOX-SHADOW SOLUTION */

.notch-shadow::before {
  left: -6px;
  background: transparent;
  border-radius: 0 8px 0 0;
  box-shadow: 0 -4px 0 0 #000;
}

.notch-shadow::after {
  right: -6px;
  background: transparent;
  border-radius: 8px 0 0 0;
  box-shadow: 0 -4px 0 0 #000;
}

.notch-shadow-xray::before {
  left: -6px;
  background: green;
  border-radius: 0 8px 0 0;
  box-shadow: 0 -4px 0 0 cyan;
}

.notch-shadow-xray::after {
  right: -6px;
  background: green;
  border-radius: 8px 0 0 0;
  box-shadow: 0 -4px 0 0 cyan;
}

/* MASK SOLUTION */

.notch-mask::before {
  left: -6px;
  background: #000;
  -webkit-mask-image: radial-gradient(circle at bottom left, transparent 0, transparent 70%, black 70%, black 100%);
}

.notch-mask::after {
  right: -6px;
  background: #000;
  -webkit-mask-image: radial-gradient(circle at bottom right, transparent 0, transparent 70%, black 70%, black 100%);
}

.notch-mask-xray::before {
  left: -6px;
  background: cyan;
  -webkit-mask-image: radial-gradient(circle at bottom left, transparent 0, transparent 70%, black 70%, black 100%);
}

.notch-mask-xray::after {
  right: -6px;
  background: cyan;
  -webkit-mask-image: radial-gradient(circle at bottom right, transparent 0, transparent 70%, black 70%, black 100%);
}

.camera {
  top: 0;
  left: 50%;
  width: 12px;
  border: 4px solid #33244A;
  height: 12px;
  position: absolute;
  transform: translate(-50%, -50%);
  background:  #304A58;
  border-radius: 1024px;
  box-sizing: border-box;
}

#button {
  font-family: monospace;
  font-size: 16px;
  padding: 8px 16px;
  margin: 32px auto 16px;
  background: transparent;
  border: 2px solid black;
  display: block;
  border-radius: 2px;
}

#xray {
  font-family: monospace;
  font-size: 16px;
  padding: 0 16px;
  text-align: center;
  display: block;
  margin: 0 0 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#xrayCheckbox {
  margin: 0 8px 0 0;
}

#label {
  font-family: monospace;
  font-size: 16px;
  padding: 0 16px;
  text-align: center;
}
<div class="phone">
  <div id="notch" class="notch notch-gradient">
    <div class="camera"></div>
  </div>

  <div class="screen">
    <div class="viewport">
      <button id="button">Change Solution</button>
      
      <label id="xray">
        <input id="xrayCheckbox" type="checkbox" />
        <span id="xrayLabel">Enable X-Ray</span>
      </label>
      
      <div id="label">pseudoelements + radial-gradient</div>
    </div>
  </div>
</div>

person Danziger    schedule 11.12.2019