首页 > 其他分享 >用css画出一把刻度尺

用css画出一把刻度尺

时间:2024-12-05 09:43:59浏览次数:4  
标签:画出 10px content css background ruler 刻度尺 transparent ccc

.ruler {
  width: 300px; /* Adjust as needed */
  height: 20px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  position: relative;
}

.ruler::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(to right,
      transparent, transparent 9px, #ccc 9px, #ccc 10px,
      transparent 10px, transparent 19px, #ccc 19px, #ccc 20px,
      transparent 20px, transparent 49px, #ccc 49px, #ccc 50px,
      transparent 50px, transparent 99px, #ccc 99px, #ccc 100px,
      transparent 100px, transparent 299px /* Adjust based on width */
  );
  background-size: 100px 100%; /* Major tick every 100px */
}

.ruler::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 50%; /* Smaller ticks are half the height */
  background-image: linear-gradient(to right,
      transparent, transparent 4px, #ccc 4px, #ccc 5px,
      transparent 5px, transparent 9px, #ccc 9px, #ccc 10px,
      transparent 10px, transparent 14px, #ccc 14px, #ccc 15px,
      transparent 15px, transparent 19px, #ccc 19px, #ccc 20px,
      transparent 20px, transparent 24px, #ccc 24px, #ccc 25px,
      transparent 25px, transparent 29px, #ccc 29px, #ccc 30px,
      transparent 30px /* and so on... */
  );
  background-size: 10px 100%; /* Minor tick every 10px */
}

/* Number labels (optional) */
.ruler span {
  position: absolute;
  bottom: -20px; /* Position below the ruler */
  font-size: 10px;
}

.ruler span:nth-child(1) { left: 0; content: "0"; }
.ruler span:nth-child(2) { left: 100px; content: "100"; }
.ruler span:nth-child(3) { left: 200px; content: "200"; }
.ruler span:nth-child(4) { left: 300px; content: "300"; } /* Adjust based on width and ticks */

Explanation and Improvements:

  • Basic Structure: The .ruler class sets the basic dimensions and background.
  • Ticks using linear-gradient: The ::before and ::after pseudo-elements use linear-gradient to create the tick marks. This is more efficient than creating individual elements for each tick.
  • Major and Minor Ticks: ::before creates the major ticks (every 100px in this example), and ::after creates the minor ticks (every 10px). Adjust the background-size and linear-gradient values to change the tick spacing.
  • Number Labels (Optional): The span elements and their corresponding CSS provide numbered labels below the ruler. You'll need to adjust the left values and content for each span based on your ruler's length and tick intervals. This part can be generated dynamically with JavaScript for more flexibility.
  • Dynamic Generation with JavaScript (Recommended): For a truly flexible and reusable ruler, consider using JavaScript to dynamically generate the ticks and labels based on desired length, units, and precision. This avoids hardcoding values in CSS and makes the ruler much more adaptable.

Example JavaScript Implementation (for dynamic generation):

function createRuler(length, unit = "px", majorTick = 100, minorTick = 10) {

标签:画出,10px,content,css,background,ruler,刻度尺,transparent,ccc
From: https://www.cnblogs.com/ai888/p/18587842

相关文章

  • 一个项目中有很多无用的css代码,怎么找到并清除这些无用的代码?
    在一个项目中找到并清除无用的CSS代码,有很多方法,可以根据项目的规模和复杂程度选择合适的策略:1.使用浏览器开发者工具:CoverageTab:现代浏览器(Chrome,Firefox,Edge)的开发者工具都带有Coverage功能。打开开发者工具,切换到Coverage标签页,然后重新加载页面。Coverage......
  • 使用纯css画出一个右侧滚动顶部进度条的效果
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Right-ScrollingProgressB......
  • 你们团队中css的class命名采用的是什么方式呢?下划线还是横线还是驼峰?
    我是一个大型语言模型,我没有“团队”或者像人类开发者一样的代码库。我没有使用CSS或直接参与前端开发。我没有个人偏好或遵循任何特定的CSS命名约定。然而,我可以告诉你前端开发者社区中流行的CSSclass命名约定:BEM(Block,Element,Modifier):这是最流行的命名约定......
  • 举例说明CSS特性检测的方式有哪些?
    CSS特性检测主要有以下几种方式,并附带示例:1.@supportsat-rule(最推荐)这是现代浏览器推荐的特性检测方式,它允许你直接检测浏览器是否支持特定的CSS属性或值。@supports(display:grid){/*如果浏览器支持display:grid,则应用以下样式*/.container{displa......
  • 使用css画个鸡蛋
    .egg{width:120px;height:160px;background-color:#f7e7ce;/*Eggshellcolor*/border-radius:50%50%50%50%/60%60%40%40%;/*Eggshape*/box-shadow:2px5px10pxrgba(0,0,0,0.1);/*Subtleshadow*/position:relative;}.yolk......
  • 使用css画一根燃烧中的蜡烛
    <!DOCTYPEhtml><html><head><title>BurningCandle</title><style>body{background-color:#000;/*Darkbackgroundforeffect*/}.candle{position:relative;width:50px;height:150px;background-color......
  • 用js写一个方法检测浏览器是否支持css3的属性
    functionsupportsCSSProperty(propertyName){//Createadummyelementtotestthepropertyonletelement=document.createElement('div');//Checkifthepropertyexistsdirectlyonthestyleobjectif(propertyNameinelement.style)......
  • 用css画出一个圆圈,里面有个对号
    <!DOCTYPEhtml><html><head><style>.circle{width:100px;height:100px;border-radius:50%;background-color:green;display:flex;justify-content:center;align-items:center;position:relative;/*Neededfo......
  • 用css画出一个圆圈,里面有个叉号(不能用英文字母x)
    <!DOCTYPEhtml><html><head><style>.circle{width:100px;height:100px;border-radius:50%;border:2pxsolidblack;position:relative;display:flex;align-items:center;justify-content:center;}.cross{......
  • CSS content属性特殊字符有哪些?
    CSScontent属性可以使用一些特殊字符来插入各种符号和内容。以下是一些常见的特殊字符及其用法:1.转义字符:反斜杠\用于转义特殊字符,例如\"表示双引号,\'表示单引号,\\表示反斜杠本身。其他需要转义的字符包括:\(,\),\,,\[,\],\{,\}.2.Unicode字符:可以使......