我想知道是否有可能追踪1px方形轮廓,插入div内的特定距离.目前,css和HTML看起来像这样
.object {
width: 100px;
height: 100px;
background-color: red;
color: white;
padding: 5px;
}Run Code Online (Sandbox Code Playgroud)
<div class="object">
Hello World!
</div>Run Code Online (Sandbox Code Playgroud)
结果如下:
http://i.imgur.com/qdB0yb6.png
而预期的结果将是这样的:
方法#01:
您可以绘制outline和使用outline-offset属性.
注意:
outline-offsetIE不支持.
.object {
outline: solid #fff 1px;
outline-offset: -5px;
width: 100px;
height: 100px;
background-color: red;
color: white;
margin: 5px;
padding: 10px;
}Run Code Online (Sandbox Code Playgroud)
<div class="object">
Hello World!
</div>Run Code Online (Sandbox Code Playgroud)
方法#02:
您可以使用:before或:after伪元素绘制轮廓或background从四面延伸.
.object {
position: relative;
width: 100px;
height: 100px;
border: 1px solid #fff;
color: white;
padding: 5px;
margin: 10px;
}
.object:before {
background-color: red;
position: absolute;
content: '';
left: -5px;
right: -5px;
top: -5px;
bottom: -5px;
z-index: -1;
}Run Code Online (Sandbox Code Playgroud)
<div class="object">
Hello World!
</div>Run Code Online (Sandbox Code Playgroud)
方法#03:
您可以使用css3绘制多个背景图像linear-gradient().以下是必要的css:
div {
background-image: linear-gradient(to right, white, white),
linear-gradient(to bottom, white, white),
linear-gradient(to right, white, white),
linear-gradient(to bottom, white, white),
linear-gradient(to right, red, red);
background-repeat: no-repeat;
background-size: calc(100% - 10px) 1px, 1px calc(100% - 10px), calc(100% - 10px) 1px, 1px calc(100% - 10px), 100% 100%;
background-position: 5px 5px, top 5px right 5px, bottom 5px right 5px, 5px 5px, 0 0;
}
Run Code Online (Sandbox Code Playgroud)
.object {
width: 100px;
height: 100px;
background-image: linear-gradient(to right, white, white),
linear-gradient(to bottom, white, white),
linear-gradient(to right, white, white),
linear-gradient(to bottom, white, white),
linear-gradient(to right, red, red);
background-repeat: no-repeat;
background-size: calc(100% - 10px) 1px, 1px calc(100% - 10px), calc(100% - 10px) 1px, 1px calc(100% - 10px), 100% 100%;
background-position: 5px 5px, top 5px right 5px, bottom 5px right 5px, 5px 5px, 0 0;
color: white;
padding: 10px;
}Run Code Online (Sandbox Code Playgroud)
<div class="object">
Hello World!
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1047 次 |
| 最近记录: |