html - 如何使矩形的边框不同?

标签 html css responsive figure

HTML:

<div class="rectangle">Some text</div>

CSS:

.rectangle {
    width: 300px;
    height: 80px;
    border: 5px solid red;
}

有没有办法让div看起来像照片中的样子? enter image description here

最佳答案

您可以使用 ::after::before实现结果。

.rectangle {
  width: 300px;
  height: 80px;
  border: 5px solid red;
  border-right: none;
  position: relative;
}

/* for the triangular shape */
.rectangle::after {
  content: "";
  position: absolute;
  right:-45px;
  bottom: 0;
  top:-5px;
  width: 0;
  height: 0;
  border-left: 45px solid red;
  border-top: 45px solid transparent;
  border-bottom: 45px solid transparent;
  z-index:1000;
}


/* for hiding the portion except the border 
 of the triangle shape */
.rectangle::before {
  content: "";
  position: absolute;
  right:-40px;
  bottom: 0;
  top:0;
  width: 0;
  height: 0;
  border-left: 40px solid white;
  border-top: 40px solid transparent;
  border-bottom: 40px solid transparent;
  z-index:1001;
}
<div class="rectangle">Some text</div>


如果你不需要类似边框的结构,那么你可以避免 ::before部分并将背景颜色设置为主分区。

.rectangle {
  width: 300px;
  height: 80px;
  border: 5px solid red;
  border-right: none;
  position: relative;
  background:red;
}

.rectangle::after {
  content: "";
  position: absolute;
  right:-45px;
  bottom: 0;
  top:-5px;
  width: 0;
  height: 0;
  border-left: 45px solid red;
  border-top: 45px solid transparent;
  border-bottom: 45px solid transparent;
}
<div class="rectangle">Some text</div>

更多形状请引用:CSS Tricks

关于html - 如何使矩形的边框不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47567583/

相关文章:

html - Bootstrap 3 : side navbar on collapse into top navbar

jsf - Primefaces <p :outputPanel/> or <div/>. 我应该使用什么来响应?

html - 使用 CSS 网格定位

html - 如何更改文本大小增加的文本框大小(高度)?

html - 如何在 Bootstrap 按钮内制作响应式文本

html - 垂直响应设计 : Scaling an image proportionally in div

html - 顶栏附加到左侧栏

JavaScript 函数适用于 Chrome,但不适用于 IE

javascript - 无法更改 <label> 文本颜色

css - 在 IE8 中 anchor <a> 对齐关闭?