如何使用CSS创建打字机效应?
使用CSS创建打字机效果的核心是通过动画控制元素宽度并隐藏溢出内容,结合overflow: hidden、border-right作为光标、white-space: nowrap保持单行,以及animation实现打字和光标闪烁效果;2. 关键自定义点包括调整animation-duration以匹配文本长度,使用steps()函数设置步数(通常等于字符数),可选单等宽字体增强效果,并可自定义光标样式;3. 响应式设计中建议设置width: fit-content和max-width: 100%以适配不同屏幕;4. 该方法仅适用于单行文本,动画需手动调参,长文本建议改用JavaScript。此方案无需JavaScript,兼容现代浏览器,性能良好且实现简单。
Creating a typewriter effect with CSS is a simple and effective way to add a dynamic, retro feel to your text — all without JavaScript. This effect simulates text being typed out character by character, usually with a blinking cursor. Here's how to do it:

1. Use animation
with width
and overflow
The core idea is to gradually reveal the text by animating the width of an element while hiding the overflow. This gives the illusion that the text is being typed in real time.
HTML:
<h1 class="typewriter">Hello, world!</h1>
CSS:
.typewriter { /* Set up the container */ overflow: hidden; /* Prevents text from overflowing */ border-right: 0.15em solid black; /* The typewriter cursor */ white-space: nowrap; /* Keeps text on a single line */ margin: 0 auto; /* Center the text */ letter-spacing: 0.15em; /* Optional: adds spacing between characters */ animation: typing 3.5s steps(30, end) forwards, /* Typing animation */ blink-caret 0.75s step-end infinite; /* Blinking cursor */ } /* Simulate typing: expand width from 0 to full */ @keyframes typing { from { width: 0 } to { width: 100% } } /* Blinking cursor animation */ @keyframes blink-caret { from, to { border-color: transparent } 50% { border-color: black } }
2. Key Points to Customize
- Animation duration: Adjust
3.5s
intyping
to match the length of your text. Longer text may need longer duration. - Steps() function: The
steps(30, end)
breaks the animation into 30 discrete steps — roughly one per character. You can tweak this number based on your text length. - Font and size: The effect works with any font, but monospace fonts (like
Courier
) enhance the typewriter feel. - Cursor style: Change
border-right
color, width, or even use::after
for a custom caret.
Optional: Monospace font
.typewriter { font-family: 'Courier New', monospace; }
3. Make it Responsive (Optional)
If you're using this on different screen sizes, you might want to set a fixed width or use max-width
to avoid issues with width: 100%
on flexible containers.

.typewriter { width: fit-content; max-width: 100%; animation: typing 3.5s steps(30, end) forwards, blink-caret 0.75s step-end infinite; }
4. Limitations
- Only works well with single-line text (due to
white-space: nowrap
). - Animation timing needs manual adjustment for different text lengths.
- Not ideal for long paragraphs — consider JavaScript for more control.
That’s it. With just a few CSS lines, you get a clean, performant typewriter effect. No JavaScript needed, and it works across modern browsers.
以上是如何使用CSS创建打字机效应?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

不同浏览器对CSS解析存在差异,导致显示效果不一致,主要包括默认样式差异、盒模型计算方式、Flexbox和Grid布局支持程度及某些CSS属性行为不一致。1.默认样式处理不一致,解决方法是使用CSSReset或Normalize.css统一初始样式;2.旧版IE的盒模型计算方式不同,建议统一使用box-sizing:border-box;3.Flexbox和Grid在边缘情况或旧版本中表现有差异,应多测试并使用Autoprefixer;4.某些CSS属性行为不一致,需查阅CanIuse并提供降级

要使用CSS创建响应式图片,主要可通过以下方法实现:1.使用max-width:100%和height:auto让图片在保持比例的同时自适应容器宽度;2.结合HTML的srcset和sizes属性智能加载适配不同屏幕的图片源;3.利用object-fit和object-position控制图片裁剪与焦点展示。这些方法共同确保图片在不同设备上清晰、美观地呈现。

opacity是CSS中用于控制元素整体透明度的属性,取值范围为0(完全透明)到1(完全不透明)。1.常用于图片hover淡出效果,通过设置opacity过渡增强交互体验;2.制作背景遮罩层提升文字可读性;3.控制按钮或图标在禁用状态下的视觉反馈。需注意它会影响所有子元素,且与rgba不同,后者仅影响指定颜色部分。搭配transition可实现平滑动画,但频繁使用可能影响性能,建议结合will-change或transform使用。合理应用opacity能增强页面层次感和交互性,但应避免干扰用户

accent-color是CSS中用于自定义复选框、单选按钮和滑块等表单元素高亮颜色的属性;1.它直接改变表单控件选中状态的默认颜色,如将复选框的蓝色勾选标记改为红色;2.支持的元素包括type="checkbox"、type="radio"和type="range"的输入框;3.使用accent-color可避免复杂的自定义样式和额外DOM结构,保持原生可访问性;4.现代浏览器普遍支持,旧浏览器需降级处理;5.设置accent-col

The:has()pseudo-classinCSSallowstargetingaparentelementbasedonitschildelements.Itworksbyusingthesyntaxparent:has(child-selector)toapplystylesconditionally.Forexample,div:has(img)appliesstylestoadivcontaininganimage.Multipleselectorscanbeusedwithcomma

为什么设置了100px宽度的盒子会显示更宽?因为默认使用的是content-box模型,实际宽度包括内容、padding和border。1.默认情况下,box-sizing是content-box,设置的width仅指内容区域,padding和border会额外增加整体宽度;2.使用border-box可让设定的width包含内容、padding和border,布局更直观;3.推荐全局设置box-sizing:border-box,避免布局错位,尤其适合响应式设计;4.特殊场景下可使用conte

要美化段落开头提升视觉吸引力,常见做法是使用CSS的伪元素或手动设置文档样式。网页开发中可用p::first-letter设置首字母样式,如放大、加粗、变色,但需注意仅适用于块级元素;若想突出整段首行,则用p::first-line来加样式;在Word等文档软件中可手动调整首字母格式或创建样式模板,而InDesign有内置“首字下沉”功能适合出版设计;应用时需注意细节,如避免复杂样式影响阅读、确保兼容性和格式一致性。

浏览器默认样式通过自动应用边距、填充、字体和表单元素样式确保基本可读性,但可能导致跨浏览器布局不一致。1.默认外边距和填充改变布局流,如标题、段落和列表自带间距;2.默认字体设置影响可读性,如16px字号和TimesNewRoman字体;3.表单元素在不同浏览器显示差异大,需重置外观;4.某些标签如strong和em有默认强调样式,需显式覆盖。解决方法包括使用Normalize.css、重置样式或全局清除边距与填充,同时自定义字体和表单样式以保证一致性。
