html - 如何在纯CSS中使伪元素具有相同的高度和宽度?

标签 html css css-selectors css-shapes

.speech 的高度可能会有所不同,因此寻找使 .speech::after 的宽度与 .speech 的宽度相同的解决方案高度。

理想的结果 enter image description here

body {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.speech {
  width: 250px;
  padding: 1rem;
  background-color: tomato;
  color: white;
  border: 1px solid;
  position: relative;
}

.speech::after {
  content: '';
  position: absolute;
  top: 0;
  left: 100%;
  border: 3px solid deepskyblue;
  height: 100%;
  padding-left: 50%;
  background: linear-gradient(45deg, transparent 50%, green 0%);
  transform: translateX(-50%) rotate(45deg);
}
<div class="speech">Lorem ipsum dolor sit amet consectetur adipisicing elit. Facilis minima possimus, maxime, fugit dolorum optio, tempora et doloribus doloremque ullam inventore repellendus veniam modi quasi iusto numquam amet eos! Exercitationem.</div>

最佳答案

你不一定需要伪元素,只要你使用 CSS 渐变,因为所有支持渐变的浏览器也支持多背景:

body {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.speech {
  width: 250px;
  padding: 1rem;
  background: linear-gradient(tomato,tomato),
              linear-gradient(to top right, tomato 49%,transparent 51%),
              linear-gradient(to bottom right, tomato 49%,transparent 51%);
  background-size: calc(100% - 30px) 100%, 30px 50%, 30px 50%;
  background-position: 0 0, 100% 0, 100% 100%;
  background-repeat: no-repeat;
  color: white;
  border: 1px solid;
  position: relative;
}
<div class="speech">Lorem ipsum dolor sit amet consectetur adipisicing elit. Facilis minima possimus, maxime, fugit dolorum optio, tempora et doloribus doloremque ullam inventore repellendus veniam modi quasi iusto numquam amet eos! Exercitationem.</div>

关于html - 如何在纯CSS中使伪元素具有相同的高度和宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47285379/

相关文章:

javascript - 无法在滚动条上分配背景颜色

jquery div 等于窗口高度随内容延伸

HTML 列表框未显示正确的选定选项

javascript - 在鼠标悬停时内联显示 block

css - 使用 :not() and styling with nth-of-type odd & even 忽略行

asp.net - 如何在 ASP.NET 中使用按钮标记?

jquery - IE8 不会使用 JQuery 和 html5shiv 设置Figcaption 元素的样式

javascript - 更改高度后如何修复输入类型日期的上下控件/旋转

javascript - 复杂的 jquery 选择,还涉及自定义 xml 标签

html - 使用文档片段的 anchor 元素的 CSS 选择器