In CSS, you can use animation attributes and @keyframes rules to achieve image carousel effects. Just use @keyframes to create the animation first; then use the animation attribute to set the time, speed and number of animations required.

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, DELL G3 computer
First prepare multiple pictures of the same size, and the pictures will be displayed horizontally Placed in an image container. Add a display container outside the image container. The size of the display container is the size of the image. Add a custom animation to the image container and set incremental offset values at different stages of the animation.
The animation effect is divided into two parts: switching and staying. The custom animation stage is related to the number of pictures, and the offset value of each stage of the animation is related to the picture size.
The example in this article is to switch from the first picture to the last picture.
HTML
<body>
<div id="container">
<div id="photo">
<img src="/static/imghwm/default1.png" data-src="images/1.jpg" class="lazy" / alt="How to implement image carousel using pure CSS" >
<img src="/static/imghwm/default1.png" data-src="images/2.jpg" class="lazy" / alt="How to implement image carousel using pure CSS" >
<img src="/static/imghwm/default1.png" data-src="images/3.jpg" class="lazy" / alt="How to implement image carousel using pure CSS" >
</div>
</div>
</body>Analysis:
Three img elements are created here. Outside the img element is a picture container, and outside the picture container is a display container.
CSS
<style type="text/css">
#container {
width: 400px;
overflow: hidden;
}
#photo {
width: 1200px;
animation: switch 5s ease-out infinite;
}
#photo > img {
float: left;
width: 400px;
}
@keyframes switch {
0%, 25% {
margin-left: 0;
}
35%, 60% {
margin-left: -400px;
}
70%, 100% {
margin-left: -800px;
}
}
</style>Rendering:

Analysis:
The size of the display container is the same as the size of the image;
Add a float effect to the image, without having to consider the troublesome margin problem;
Since the example only has three pictures, three animation stages are added. Each stage achieves the switching effect by setting an increasing margin-left value;
set animation The stage (for example: 35%~60%) is the animation stay part, and the free time of the previous stage (for example: 25%~35%) is the animation switching part. The length of each part needs to be controlled by yourself.
Recommended study: "css video tutorial"
The above is the detailed content of How to implement image carousel using pure CSS. For more information, please follow other related articles on the PHP Chinese website!
How to use attribute selectors in CSS?Jul 23, 2025 am 03:50 AMIn CSS, the attribute selector can set styles according to the attributes and values of the element, providing more flexible style control. ①Basic usage: Select elements with specific attributes, such as input[type] to match all inputs containing type attributes; ②Exact match: Use = to match specific attribute values, such as input[type="text"] to match only the text input box; ③ Partial match: Use = (include), ^= (start), and $= (end), to match part of the attribute values, such as a[href="//m.sbmmt.com/m/faq/example.com"] to match anchors containing specific links; ④Combination match: match multiple attributes at the same time, such as inputtype=&qu
What is the purpose of the CSS `will-change` property?Jul 23, 2025 am 03:47 AMwill-change is a CSS property that is used to inform browser elements in advance of possible changes to optimize performance. Its core function is to enable the browser to create layers in advance to improve rendering efficiency. Common values include transform, opacity, etc., and can also be separated by multiple attribute commas; it is suitable for non-standard attribute animations, complex component transitions and user interaction triggered animations; but it must be avoided abuse, otherwise it will lead to excessive memory usage or increased GPU load; the best practice is to apply before the change occurs and remove it after it is completed.
What is the difference between `display: none` and `visibility: hidden`?Jul 23, 2025 am 03:43 AMThe main difference between display:none and visibility:hidden is whether the element occupies space and its impact on the layout. 1.display:none will completely remove elements, do not take up space, and the layout will be readjusted; suitable for permanent or dynamic hidden elements. 2.visibility:hidden makes elements invisible but still retain their space and the layout remains unchanged; suitable for situations where structures need to be stable. 3.display:none hides all child elements at the same time, while visibility:hidden can control the display status of child elements separately. When selecting, it is determined based on whether the element is expected to affect the layout.
How to change text color in CSSJul 23, 2025 am 03:42 AMTo change the color of web page text, the most direct way is to use the color attribute of CSS. 1. Color can be set through color name, hexadecimal, RGB or HSL; 2. Different elements can be set separately, such as navigation links and paragraphs; 3. Flexible color matching is achieved through class selectors; 4. Note that the color attribute is inheritable, and the child elements will inherit the color of the parent element by default, and the style needs to be covered separately to avoid conflicts.
What is grid-auto-flow in CSS Grid?Jul 23, 2025 am 03:33 AMThegrid-auto-flowpropertyinCSSGridcontrolshowitemsareautomaticallyplacedwhennotexplicitlypositioned.Bydefault,it’ssettorow,fillingitemslefttoright,rowbyrow;changingittocolumnfillsitemstoptobottom,columnbycolumn.Itcanalsobepairedwithdensetocompactlayo
How to create a simple accordion with pure CSS?Jul 23, 2025 am 03:24 AMUsing pure CSS to achieve the accordion effect can be completed through HTML and tags. The specific steps are as follows: 1. Use and tags to achieve the expansion and collapse function, with clear semantics and support barrier-free access; 2. Set details borders, inner margins and other styles through CSS to unify the appearance, and hide the default arrows; 3. Note that multiple panels cannot be expanded at the same time through pure CSS restrictions, so JavaScript needs to be introduced to implement this function. In summary, basic functions do not require JS, but complex interactions still require script support.
Describe CSS paint orderJul 23, 2025 am 03:20 AMIn CSS, the order of drawing elements is affected by a variety of factors. By default, the browser draws in the order of positioning elements in the background and border → text content → child elements. 1. The default drawing order does not follow the DOM structure completely, but follows the painting order; 2. The hierarchy consists of whether the same stacking context belongs to the same stacking context and whether a new stacking context is created (such as transform, filter, opacity).
What is the difference between auto-fit and auto-fill in CSS Grid?Jul 23, 2025 am 03:17 AMauto-fillcreatesasmanytracksaspossible,leavingemptyslotsiftherearen’tenoughitems,whileauto-fitresizestrackstofillthespace,avoidingemptiness.1.auto-fillmaintainsafixedstructure,generatingtracksevenifempty,idealforpredictablelayouts.2.auto-fitadjuststr


Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.







