CSS3 to achieve cool cutting image rotation effect

Time:2019-10-23

Today we’ll learn how to create a cool image rotation component using CSS. The idea is simply to switch background images and animations by clicking on the label element. The core is the use of radio buttons associated with labels and the use of universal sibling selectors to locate each image.

The final result we want to achieve is as follows:

CSS3 to achieve cool cutting image rotation effect

The composition of rotation

HTML consists of three main parts: radio buttons and tags, a container containing four panels and a “slice” of each image, and a title. A container set to class=”cr-bgimg” will divide each image into four panels, in which the background image will be positioned in the appropriate position through the backstage-position attribute. So, the first panel will have four slices, each with an image as the background and located at the far left of the container. The second panel also has four slices, which are located on the right side of the first panel to display the second slice section of the image:

<section class="cr-container">            

    <! -- radio buttons and labels -->
    <input id="select-img-1" name="radio-set-1" type="radio" class="cr-selector-img-1" checked/>
    <label for="select-img-1" class="cr-label-img-1">1</label>

    <input id="select-img-2" name="radio-set-1" type="radio" class="cr-selector-img-2" />
    <label for="select-img-2" class="cr-label-img-2">2</label>

    <input id="select-img-3" name="radio-set-1" type="radio" class="cr-selector-img-3" />
    <label for="select-img-3" class="cr-label-img-3">3</label>

    <input id="select-img-4" name="radio-set-1" type="radio" class="cr-selector-img-4" />
    <label for="select-img-4" class="cr-label-img-4">4</label>

    <div class="clr"></div>    

    <! - panel - >
    <div class="cr-bgimg">
        <div>
            <span>Slice 1 - Image 1</span>
            <span>Slice 1 - Image 2</span>
            <span>Slice 1 - Image 3</span>
            <span>Slice 1 - Image 4</span>
        </div>
        <div>
            <span>Slice 2 - Image 1</span>
            <span>Slice 2 - Image 2</span>
            <span>Slice 2 - Image 3</span>
            <span>Slice 2 - Image 4</span>
        </div>
        <div>
            <span>Slice 3 - Image 1</span>
            <span>Slice 3 - Image 2</span>
            <span>Slice 3 - Image 3</span>
            <span>Slice 3 - Image 4</span>
        </div>
        <div>
            <span>Slice 4 - Image 1</span>
            <span>Slice 4 - Image 2</span>
            <span>Slice 4 - Image 3</span>
            <span>Slice 4 - Image 4</span>
        </div>
    </div>

    <! - the title -- -- >
    <div class="cr-titles">
        <h3>
            <span>Serendipity</span>
            <span>What you've been dreaming of</span>
        </h3>
        <h3>
            <span>Adventure</span>
            <span>Where the fun begins</span>
        </h3>
        <h3>
            <span>Nature</span>
            <span>Unforgettable eperiences</span>
        </h3>
        <h3>
            <span>Serenity</span>
            <span>When silence touches nature</span>
        </h3>
    </div>

</section>

The h3 element contains two span elements, one for the main heading and one for the subheading.

CSS

For the sake of simplicity, the CSS in the article omitted the browser prefix, but the code you get is inclusive.

Our first goal is to trigger the corresponding radio button when the label element is clicked by simply matching the for attribute value of the label element with the ID value of the radio button.

The second step is to position all the background images in the correct position. The third step, when you click the label label, displays the corresponding picture slice and title.

Step by step, we first set the style of the outermost section element, giving it a light shadow effect.

.cr-container{
    width: 600px;
    height: 400px;
    position: relative;
    margin: 0 auto;
    border: 20px solid #fff;
    box-shadow: 1px 1px 3px rgba(0,0,0,0.1);
}

Because later we’ll use the universal sibling selector “~” to select the corresponding image slice and title, we’ll put all the label elements at the top of the code. Ensure that the label elements are displayed above the image and text by setting the z-index attribute, and make them 350px away from the overall upper border through the razor-top attribute.

.cr-container label{
    font-style: italic;
    width: 150px;
    height: 30px;
    cursor: pointer;
    color: #fff;
    line-height: 32px;
    font-size: 24px;
    float:left;
    position: relative;
    margin-top: 350px;
    z-index: 1000;
}

Next, to beautify the label element by adding a small circle, we create a pseudo-element and place it in the center of the text.

.cr-container label:before{
    content:'';
    width: 34px;
    height: 34px;
    background: rgba(130,195,217,0.9);
    position: absolute;
    left: 50%;
    margin-left: -17px;
    border-radius: 50%;
    box-shadow: 0px 0px 0px 4px rgba(255,255,255,0.3);
    z-index:-1;
}

To create a dividing line between panels, we use another pseudo-element of the label element and extend it from the top edge of the image to the bottom edge with a gradient background.

.cr-container label:after{
    width: 1px;
    height: 400px;
    content: '';
    background: linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
    position: absolute;
    bottom: -20px;
    right: 0px;
}

There should be no divider line on the right side of the last panel, so we set its width to 0.

.cr-container label.cr-label-img-4:after{
    width: 0px;
}

Now that the style of the label is complete, we can hide all radio buttons.

.cr-container input{
    display: none;
}

When we click on a label element, its corresponding radio button is selected. In turn, you can use the universal sibling selector to select the label element corresponding to the selected button and change its text color.

.cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1,
.cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2,
.cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3,
.cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4{
    color: #68abc2;
}

We can also change the color of the small circle and add a shadow effect.

.cr-container input.cr-selector-img-1:checked ~ label.cr-label-img-1:before,
.cr-container input.cr-selector-img-2:checked ~ label.cr-label-img-2:before,
.cr-container input.cr-selector-img-3:checked ~ label.cr-label-img-3:before,
.cr-container input.cr-selector-img-4:checked ~ label.cr-label-img-4:before{
    background: #fff;
    box-shadow: 0px 0px 0px 4px rgba(104,171,194,0.6);
}

The image container will occupy all the widths and be absolutely positioned. Use this container later to set the background image to the currently selected image. We also need a default visible image, so add some background attributes:

.cr-bgimg{
    width: 600px;
    height: 400px;
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: 1;
    background-repeat: no-repeat;
    background-position: 0 0;
}

Because we have four panels, each 150 pixels wide (600 divided by 4). Panels are set to float left and display side by side, while they are set to overflow and hide, because we don’t want to see slices come out when we slide:

.cr-bgimg div{
    width: 150px;
    height: 100%;
    position: relative;
    float: left;
    overflow: hidden;
    background-repeat: no-repeat;
}

Each slice is also set to absolute positioning and hidden from the panel with left:-150px:

.cr-bgimg div span{
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: -150px;
    z-index: 2;
    text-indent: -9000px;
}

Again, let’s work with the container and the background image for each slice:

.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(1){
    background-image: url(../images/1.jpg);
}
.cr-container input.cr-selector-img-2:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(2){
    background-image: url(../images/2.jpg);
}
.cr-container input.cr-selector-img-3:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(3){
    background-image: url(../images/3.jpg);
}
.cr-container input.cr-selector-img-4:checked ~ .cr-bgimg,
.cr-bgimg div span:nth-child(4){
    background-image: url(../images/4.jpg);
}

We also need to provide different positioning for the background image of the slice according to different panels:

.cr-bgimg div:nth-child(1) span{
    background-position: 0px 0px;
}
.cr-bgimg div:nth-child(2) span{
    background-position: -150px 0px;
}
.cr-bgimg div:nth-child(3) span{
    background-position: -300px 0px;
}
.cr-bgimg div:nth-child(4) span{
    background-position: -450px 0px;
}

When we click a label, we slide all slices to the right:

.cr-container input:checked ~ .cr-bgimg div span{
    animation: slideOut 0.6s ease-in-out;
}
@keyframes slideOut{
    0%{
        left: 0px;
    }
    100%{
        left: 150px;
    }
}

But in addition to the label we selected, the corresponding slice of the image slides from -150px to 0px:

.cr-container input.cr-selector-img-1:checked ~ .cr-bgimg div span:nth-child(1),
.cr-container input.cr-selector-img-2:checked ~ .cr-bgimg div span:nth-child(2),
.cr-container input.cr-selector-img-3:checked ~ .cr-bgimg div span:nth-child(3),
.cr-container input.cr-selector-img-4:checked ~ .cr-bgimg div span:nth-child(4)
{
    transition: left 0.5s ease-in-out;
    animation: none;
    left: 0px;
    z-index: 10;
}

Finally, set the style of the main subtitle in the h3 label. When we click a certain label, the transparency of its corresponding title will change from 0 to 1:

.cr-titles h3{
    position: absolute;
    width: 100%;
    text-align: center;
    top: 50%;
    z-index: 10000;
    opacity: 0;
    color: #fff;
    text-shadow: 1px 1px 1px rgba(0,0,0,0.1);
    transition: opacity 0.8s ease-in-out;
}
.cr-titles h3 span:nth-child(1){
    font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
    font-size: 70px;
    display: block;
    letter-spacing: 7px;
}
.cr-titles h3 span:nth-child(2){
    letter-spacing: 0px;
    display: block;
    background: rgba(104,171,194,0.9);
    font-size: 14px;
    padding: 10px;
    font-style: italic;
    font-family: Cambria, Palatino, "Palatino Linotype", "Palatino LT STD", Georgia, serif;
}
.cr-container input.cr-selector-img-1:checked ~ .cr-titles h3:nth-child(1),
.cr-container input.cr-selector-img-2:checked ~ .cr-titles h3:nth-child(2),
.cr-container input.cr-selector-img-3:checked ~ .cr-titles h3:nth-child(3),
.cr-container input.cr-selector-img-4:checked ~ .cr-titles h3:nth-child(4){
    opacity: 1;
}

That’s it! In this way, the effect of rotary seeding is realized. Of course, through this effect, we can extend more slice effects, such as:

Effects of two
CSS3 to achieve cool cutting image rotation effect

The effect of three
CSS3 to achieve cool cutting image rotation effect

The effect of four
CSS3 to achieve cool cutting image rotation effect

To obtain the above rotation effect code can:

CSS3 to achieve cool cutting image rotation effect