当前位置:首页 > CSS

css制作标尺

2026-01-08 17:33:14CSS

使用CSS制作标尺的方法

方法一:使用线性渐变和伪元素

通过CSS的线性渐变和伪元素可以创建水平或垂直标尺。以下是一个水平标尺的示例代码:

.ruler {
  width: 100%;
  height: 20px;
  background: linear-gradient(to right, #000 1px, transparent 1px) 0 0 / 10px 20px;
  position: relative;
}

.ruler::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to right, #000 10px, transparent 10px) 0 0 / 100px 20px;
}

方法二:使用边框和绝对定位

这种方法通过边框和绝对定位来创建标尺刻度:

.ruler {
  position: relative;
  width: 300px;
  height: 30px;
  border-bottom: 1px solid #000;
}

.ruler span {
  position: absolute;
  bottom: 0;
  width: 1px;
  height: 10px;
  background: #000;
}

.ruler span:nth-child(5n) {
  height: 15px;
}

.ruler span:nth-child(10n) {
  height: 20px;
}

方法三:使用Flexbox布局

Flexbox可以方便地创建均匀分布的标尺刻度:

.ruler {
  display: flex;
  width: 300px;
  height: 20px;
  border-bottom: 1px solid #000;
}

.ruler div {
  flex: 1;
  border-right: 1px solid #000;
  height: 5px;
}

.ruler div:nth-child(5n) {
  height: 10px;
}

.ruler div:nth-child(10n) {
  height: 15px;
  border-right: 2px solid #000;
}

方法四:使用CSS Grid布局

CSS Grid提供了另一种创建标尺的方式:

.ruler {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  width: 300px;
  height: 20px;
  border-bottom: 1px solid #000;
}

.ruler div {
  border-right: 1px solid #000;
  height: 5px;
}

.ruler div:nth-child(5n) {
  height: 10px;
}

.ruler div:nth-child(10n) {
  height: 15px;
  border-right: 2px solid #000;
}

垂直标尺的实现

将上述方法中的方向属性调整即可创建垂直标尺:

.vertical-ruler {
  height: 300px;
  width: 20px;
  border-right: 1px solid #000;
  background: linear-gradient(to bottom, #000 1px, transparent 1px) 0 0 / 20px 10px;
}

添加数字标签

可以通过伪元素或额外元素为标尺添加数字:

css制作标尺

.ruler::after {
  content: "0 10 20 30 40 50";
  position: absolute;
  bottom: -20px;
  left: 0;
  width: 100%;
  text-align: center;
  font-size: 12px;
}

这些方法提供了灵活的标尺创建方式,可以根据具体需求调整颜色、尺寸和刻度密度。

标签: 标尺css
分享给朋友:

相关文章

css制作图片

css制作图片

CSS 制作图片的方法 使用 background-image 属性 通过 background-image 属性可以为元素设置背景图片。这种方法适用于将图片作为背景而非内容的一部分。 .eleme…

css制作六边形

css制作六边形

使用CSS创建六边形的方法 通过CSS的clip-path属性可以轻松实现六边形效果。以下是一个基础示例: .hexagon { width: 100px; height: 55px;…

css页脚制作

css页脚制作

CSS页脚制作方法 固定定位页脚 使用position: fixed将页脚固定在页面底部,适用于需要始终显示的页脚。 footer { position: fixed; bottom: 0;…

css表头制作

css表头制作

CSS 表头制作方法 固定表头(滚动时保持可见) 使用 position: sticky 属性可以创建滚动时固定的表头。确保父容器有明确的高度和 overflow 属性。 .table-conta…

css制作tkd

css制作tkd

CSS 制作 TKD(Title, Keywords, Description) TKD(Title, Keywords, Description)是网页 SEO 优化的核心元素,通常通过 HTML…

css风格制作

css风格制作

CSS 风格制作方法 内联样式 直接在 HTML 元素的 style 属性中编写 CSS 代码,适用于单个元素的快速样式设置。 示例: <p style="color: blue; fon…