html - 如何在html5中仅使用css在容器内绘制具有四个div的矩形?
问题描述
如何从下面的代码中仅使用 css 构建矩形?
<div id='rectangle'>
<div class='line'></div>
<div class='line'></div>
<div class='line'></div>
<div class='line'></div>
</div>
解决方案
您可以使用以下内容。我不建议使用id
for 样式。如果由于某种原因您想要多个rectangle
比并且id
不是要走的路。这是因为并且id
可以被视为identification
一个人。意味着 anid
在页面上应该是唯一的。
.rectangle {
width: 200px;
height: 200px;
position: relative;
}
.line {
position: absolute;
background: red;
}
.top, .bottom {
height: 1px;
width: 100%;
left: 0;
}
.top {
top: 0;
}
.bottom {
bottom: 0;
}
.left, .right {
height: 100%;
width: 1px;
top: 0;
}
.left {
left: 0;
}
.right {
right: 0;
}
<div class="rectangle">
<div class="line top"></div>
<div class="line right"></div>
<div class="line bottom"></div>
<div class="line left"></div>
</div>
或者:
.rectangle {
border: 1px solid red;
width: 200px;
height: 200px;
}
<div class="rectangle"></div>
推荐阅读
- python - AttributeError: 'dict' 对象没有属性 'append' 试图写入 .JSON 文件
- matlab - 如何使用 MATLAB 在嵌套的 for 循环中使每个数组相互相乘?
- c# - 需要将两个 observable 压缩在一起
- javascript - 如何在单击提交按钮之前从数据库中获取值并显示计算值
- javascript - JS:在命名函数中调用函数
- javascript - 如何通过标签名称找到伟大的父节点onclick javascript中的嵌套子节点?
- r - 使用kmeans()后:如何判断哪个点属于哪个组?
- regex - 在 Haskell 中实现 Regex 时遇到问题
- ruby - Ruby 多进程同步
- python - 尝试在 Python 中使用 f.write 写入文本文件