当前位置:首页 > 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;
}

添加数字标签

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

.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制作三角形

使用 border 属性创建三角形 通过设置元素的 border 属性可以实现三角形效果。原理是利用边框的交界处形成的斜边。 .triangle { width: 0; height: 0;…

网页制作css

网页制作css

CSS 基础语法 CSS(层叠样式表)用于控制网页的样式和布局。基本语法由选择器和声明块组成: 选择器 { 属性: 值; 属性: 值; } 例如: p { colo…

css网页制作教程

css网页制作教程

CSS网页制作教程 CSS(层叠样式表)是用于控制网页外观和布局的核心技术。以下是CSS网页制作的关键步骤和技巧: 基础语法与引入方式 CSS规则由选择器和声明块组成: 选择器 { 属性: 值…

css如何制作圆

css如何制作圆

使用 border-radius 属性制作圆 通过设置 border-radius 属性为元素添加圆角效果。当 border-radius 的值等于元素宽度和高度的一半时,元素会呈现圆形。 .cir…

制作css

制作css

CSS基础语法 CSS规则由选择器和声明块组成。选择器指向需要设置样式的HTML元素,声明块包含一个或多个用分号分隔的声明。每个声明由属性和值组成,用冒号分隔。 选择器 { 属性: 值; 属…

网页制作 css

网页制作 css

CSS 基础语法 CSS(层叠样式表)用于控制网页的样式和布局。基本语法由选择器和声明块组成: 选择器 { 属性: 值; } 例如设置段落文字颜色为红色: p { color: red;…