CSS Shorts Weekly
Free download CSS Shorts Weekly. This tutorial/course is created by Chris Converse and it has been retrieved from Lynda which you can download for absolutely free. Web and Web Development skills are covered in this course.
CSS Shorts is a weekly series covering all aspects of CSS, including properties, preprocessors, positioning, floats, and the most up-to-date styling techniques. Instructor Chris Converse returns every Tuesday, bringing you short and timely tips that will help new and experienced web developers alike create more beautiful websites and more efficient code. Tune in and learn how to take more control over the style, layout, and even the user experience of your web designs.
Note: There will be free exercise files for most episodes, which will allow you to follow along with author Chris Converse, as you explore all the possibilities CSS has to offer.
Hi, I’m Chris Converse and in this episode we’ll rotate an image and then crop it to create a diamond shape using only CSS. So if you’d like to follow along with me download the exercise files and we’ll begin by opening the HTML file in a text editor. And now once you have the HTML file opened up in the head area you’ll see a link to style dot CSS, we’ll be opening this in a moment. If you look inside of the HTML you’ll see a series of elements, heading elements and paragraph elements and we also have a figure here and inside of the figure is an image element.
So what we’re gonna be doing is rotating the figure elements and then rotating the image in a negative direction in order to keep it straight the way we see over here in the preview. And then we’re gonna create a calculation so that we can size the image inside of the figure. So to do all of this let’s go back to the exercise files and let’s open up the style dot CSS file in our text editor. And so now in the CSS file let’s scroll down, let’s find the rule that targets the figure element. The first thing I wanna do here is add a border so we can see what’s going on with our transformations.
So I’ll type border colon space, one pixel for the size, solid for the style and then we’ll just make it black so pound sign and three zeros. Save your CSS, we’ll now see in the browser we have a border on the figure element. Next still inside of the figure rule, let’s come in here and let’s rotate the figure. So we’ll type transform colon space then type rotate, beginning and ending parenthesis then a semi colon, then inside of the parenthesis we’ll set this to 45 and then DEG for degrees.
We’ll hit save, go back to the browser and we’ll now see that the figure element is being rotated. You’ll also notice that the image inside of the figure is also being rotated. So let’s come down to the rule that targets the image inside of the figure. So figure space image, after the width property let’s come in here and add a transform. We’ll set this to rotate as well, put in our parenthesis then a semi colon, and now we’re gonna set this to negative 45 degrees. Let’s hit save, go back to the browser and we’ll now see that the image is now straight again.
Now in order to make sure that the image doesn’t break outside of the figure element, let’s go back into the figure rule in our CSS and let’s add an overflow property. So we’ll type overflow colon space then hidden, then a semi colon. Save your work and now the image will be conformed or cropped based on the figure element. And so now with this in place the next thing we need to do is calculate the width of the image inside of the figure. And now if we think about our square in terms of being two right triangles, we can use the Pythagorean Theorem to figure out what the width should be.
So let’s quickly take a look at how we’re gonna calculate this. So we’re gonna start with 200, both for A and B which is the size of our figure element. Then we’re gonna square those numbers which is going to give us 40,000. Then we’re gonna add 40,000 to 40,000 which is going to give us 80,000 and 80,000 is gonna be equal to C squared. So to solve for C we’re going to take the square root of 80,000 and that’s gonna give us 282.8. And now we need to figure out how much larger 282.8 is from 200.
And we can do that by taking 282.8 and dividing it by 200 which equals 1.414. And then we can simplify that down to 141%. So the image needs to be 41% larger than the figure element that it’s inside of. And the nice thing here is if you’re always working with a square, this is always going to be the number you can use. So you won’t have to do this calculation every time. So back in our CSS file let’s come down to our image inside of the figure, let’s change the 100% value to 141%, let’s hit save.
Now we’ll see over in the preview that the width is matching exactly on the corners. And now let’s make an adjustment so that we don’t have the extra space across the top. So after the width property back in the CSS let’s come in here and set a margin dash top property. And we’re gonna set this to a negative value and we wanna use half of that 41% that we’re going above a hundred for the width. And that’s gonna be 20.5. So we’re gonna set this to negative 20.5%, let’s duplicate this line and let’s set the margin left to the same value.
And now with these two margin properties in place go back to the browser and hit reload. And you’ll now see that the image is completely filling the figure element. And now so the great thing about this technique is since we used percentage values we can come back to the CSS and change the height and width of the figure element. I’ll bring this down to a hundred, save our work and back in the browser we’ll see that everything will scale and proportion. Or I’ll come back to the CSS and change the size from 100 up to 300, hit save, go back to the browser and again we’ll see everything will scale in proportion.
So back in the CSS I’ll set this back to 200 for the height and the width. Let’s come down here in the figure element and remove the border property. And then let’s come up and adjust the margin properties. So for the margin top, the first value, we’re gonna set this to 25 pixels. For the margin right property let’s set this to 15 pixels. For the bottom we’ll set this to 40 pixels, and for the left we’re gonna set this to 65 pixels. And so with all of these in place let’s hit save, let’s go back to the browser and hit reload.
So with some transform properties and percentage based values we now have some CSS rules that you can use for any square photo that you’d like to crop into a diamond shape. And so with that I’ll conclude this episode and as always, thanks for watching.