Useful Broken Images - CSS Tutorial (Day 5 of CSS3 in 30 Days)

**Using CSS Pseudo-Elements to Style Broken Images**

In this lesson, we'll explore how to style broken images using CSS pseudo-elements. We'll start by creating a class for our image element and apply some basic styles to it.

First, let's create a class for our image element. We can do this by adding the following code:

```

.image {

position: relative;

display: inline-block;

font-size: 0.7rem;

text-align: center;

text-transform: uppercase;

}

```

This code applies some basic styles to our image element, including a font size of 0.7rem and an `text-align` property set to center.

Next, we'll add the `::after` pseudo-element to our class. The `::after` pseudo-element allows us to add additional content to an element without affecting its structure or layout. We can use this pseudo-element to add a background image to our broken image.

Let's update our CSS class to include the `::after` pseudo-element:

```

.image {

position: relative;

display: inline-block;

font-size: 0.7rem;

text-align: center;

text-transform: uppercase;

}

.image::after {

content: '';

display: block;

width: 100%;

min-width: 200px;

padding: 10 20 10 45;

border: dashed 1px #ccc;

background-color: #fff;

background-image: url('no-image.png');

background-size: 10px;

background-position: 15px 10px;

z-index: 1;

}

```

In this updated code, we've added a `::after` pseudo-element to our class. This pseudo-element applies the following styles:

* We're setting its content property to an empty string (`''`). This tells the browser that we want to add additional content to our element.

* We're setting its display property to `block`. This tells the browser that we want to render this pseudo-element as a block-level element, rather than inline.

* We're setting its width and min-width properties. These values will be used to create a fixed-size container for our background image.

* We're adding some padding to our pseudo-element. This helps us create space around the icon that we'll add later.

* We're applying a dashed border to our pseudo-element. This adds a visual separator between our element and its content.

* We're setting its background color property to `#fff` (white). This sets the background color of our pseudo-element to white.

* We're adding a background image to our pseudo-element using the `background-image` property. In this case, we've used an image called `no-image.png`, which is located in our "broken images" folder.

* We're setting the `background-size` property to `10px`. This tells the browser that we want to resize our background image so that it's 10 pixels wide.

* We're applying a `background-position` property. This sets the position of our background image within its container.

* We're finally setting its z-index property to `1`. This helps us ensure that our pseudo-element is rendered on top of our element.

Now, let's add some content to our pseudo-element. We can do this by adding a string with an `alt` attribute inside the `content` property:

```

.image::after {

content: "Flying Eagle | Not Found";

}

```

In this updated code, we've added the text "Flying Eagle | Not Found" inside the `content` property of our pseudo-element. The `|` symbol separates the two words, and the `Not Found` part is displayed in the same font size as our element.

Finally, let's update our HTML to include an image with a broken link:

```

```

This code creates an `` element with a `class` attribute set to `.image`. This tells the browser which CSS styles to apply to this element. We've also added a `src` attribute, but since our image is broken, it will be displayed as a pseudo-element.

If we inspect this element in our browser, we'll see that it's been styled with the background image and text. The image itself is large, while the pseudo-element takes up less space.

To fix the layout issue, we can add some margin to the `img` element:

```

.img {

width: 100%;

min-width: 200px;

margin-bottom: 40px;

}

```

This code sets the `margin-bottom` property of our `.img` class. This adds a bottom margin that pushes the pseudo-element out of the way, allowing it to be displayed on top of the image.

By adding this CSS rule, we've fixed the layout issue and ensured that our broken image is styled correctly using pseudo-elements.

"WEBVTTKind: captionsLanguage: enhey everyone what's up welcome back to day five of css3 in 30 days today we're going to be building something super simple and super easy but it's really really meaningful and can really kind of take your websites or your applications to the next level when it comes to user experience now let me show you what i mean jump over here into my browser we're going to do something called useful broken images now whether it's a typo in the image tag source or the image tag itself was misplaced or removed on the server images break all the time on the web the problem is the browser's default way to display a broken image is typically pretty darn ugly you know where you have that little like screen shot it shows like a little image thumbnail with like a crack through the middle or a rip and it just it's a broken image icon and it doesn't look very good sometimes it's just the browser's default way of displaying that kind of looks bad you can fix that however and actually use it as an opportunity to enhance the user's experience on your web pages with css now that's your challenge this lesson so you kind of take you know when websites throw you lemons you create lemonade right so we're going to be creating useful broken images let me show you this is a standard broken image it cannot find this image on purpose i coded it improperly so it's finding an image that is not located on the server anywhere this is what it looks like looks terrible it makes you look bad but if you scroll down here check this out flying eagle not found with a nice little icon a camera with an x and then a nice dotted line around it it's just it looks nicer now the image isn't there so that sucks but it's pulling in information and notifying the user of what happened this is a much more fluid and positive user experience let's find out how to code that right now shall we over here in our code editor i have my number five useful broken images course files go ahead and download that it's in this module you can click to download it it'll give you a dropbox link something really simple so you can download the useful broken images and index is the markup it's very simple all it is is an image tag with no with uh no image there so there's no image on the server called broken image i did that on purpose and then an alt attribute that's important because we're going to be using that in css that's all it is sandbox is where we're going to be coding up our uh styles and final is the final result so let's go ahead and do that code our styles right here first thing we're going to be doing is selecting a class just called image now i could have just selected the image tag but i just wanted to do it this way and we're going to say position relative the display will be inline block the font size let's do something like 0.7 rim text align center text transform uppercase and save now if we head over to our browser you're going to see here uh that it's changed the the default style of the broken image now i want to make that font size a little bit bigger so i'm actually just going to say one rim and that should make it the default size i like that okay and now what we're going to do this is easy we're almost done image the after sudo element we're going to say display inherit so we're going to inherit the inline block style we're going to position absolute top 0 left 0 we're going to do width 100 percent min width it's going to be a minimum of 200 pixels padding will be 10 20 10 and 45 to account for the icon that we're going to put in the left side we're going to go border oh we need a semicolon up here that's why border dashed dash to one pixel triple eight and then we're gonna say background color is gonna be f zero f zero f zero and then the background image we're gonna pull that url called no image dot png right here in our useful broken images folder directory background size for that image is going to be 10 background repeat no repeat background position we're going to put it at 15 pixels in the x 10 pixels on the y z index 1 and the content this is going to be cool so i'm going to save that actually save so far and see what we have now you can see there's nothing there we did all these styles but nothing showed up that's because we didn't do the content property yet so you always have to do content on an after or before pseudo element back here in the code editor simple we could do something like content to empty string now if we head back you'll see that something is appearing but it's this obviously doesn't look very good so we're going to fix that by adding text within that element and we're going to we're going to adjust that so so here in content we're going to do the double string and then right after that we're going to say attribute so it's the attribute value for the content property and we're going to say alt so what's that doing it's looking for the html attribute alt the value of it so here the alt flying eagle so it's looking for that attribute alt so it's going to grab that value and put it as the content and then after that another string to close one double string to close that previous one and then not found and then close the string one more time so how does this work so we have an open uh double string which closes over here but we also have another set of strings in here that wrap around the attribute alt so it's just something that we kind of have to do it's like concatenating the text so attribute alt so the alt text not found save that go over here to our browser check it out flying eagle not found we got that icon from our useful broken images directory nice and now if we had a different image let's go to our index and i'm just going to add another image here and say broken image 2 and change the alt text to like let's just say paddle board and then i'm going to put uh a break tag save that check it out in the browser so paddle board not found now they're stacking on top of each other the reason why that's happening is because these are the after pseudo elements the actual image itself so if i inspect this the image itself is is this large and then the after pseudo element is that size so the image they're actually stacking on top of each other perfectly but it's the after pseudo-element that's conflicting so we just give the image some space around it so here i'm just going to say margin images will have like a margin of 0 0 40 pixels and then that should solve the problem there we go so over here we got some space nice looks great so that is day five useful broken images that's all we had to do is really simple but very powerful because hey it's nice to have a nicer user experience and we can do that with our broken images you didn't maybe you didn't know that you could do that and it's a pretty cool trick that's useful in your web development in your your portfolio your website apps applications client sites so hopefully you enjoyed the useful broken images lesson hang tight for tomorrow we're going to do day six and we've got some more fun stuff to code up in css i'll catch you there bye nowhey everyone what's up welcome back to day five of css3 in 30 days today we're going to be building something super simple and super easy but it's really really meaningful and can really kind of take your websites or your applications to the next level when it comes to user experience now let me show you what i mean jump over here into my browser we're going to do something called useful broken images now whether it's a typo in the image tag source or the image tag itself was misplaced or removed on the server images break all the time on the web the problem is the browser's default way to display a broken image is typically pretty darn ugly you know where you have that little like screen shot it shows like a little image thumbnail with like a crack through the middle or a rip and it just it's a broken image icon and it doesn't look very good sometimes it's just the browser's default way of displaying that kind of looks bad you can fix that however and actually use it as an opportunity to enhance the user's experience on your web pages with css now that's your challenge this lesson so you kind of take you know when websites throw you lemons you create lemonade right so we're going to be creating useful broken images let me show you this is a standard broken image it cannot find this image on purpose i coded it improperly so it's finding an image that is not located on the server anywhere this is what it looks like looks terrible it makes you look bad but if you scroll down here check this out flying eagle not found with a nice little icon a camera with an x and then a nice dotted line around it it's just it looks nicer now the image isn't there so that sucks but it's pulling in information and notifying the user of what happened this is a much more fluid and positive user experience let's find out how to code that right now shall we over here in our code editor i have my number five useful broken images course files go ahead and download that it's in this module you can click to download it it'll give you a dropbox link something really simple so you can download the useful broken images and index is the markup it's very simple all it is is an image tag with no with uh no image there so there's no image on the server called broken image i did that on purpose and then an alt attribute that's important because we're going to be using that in css that's all it is sandbox is where we're going to be coding up our uh styles and final is the final result so let's go ahead and do that code our styles right here first thing we're going to be doing is selecting a class just called image now i could have just selected the image tag but i just wanted to do it this way and we're going to say position relative the display will be inline block the font size let's do something like 0.7 rim text align center text transform uppercase and save now if we head over to our browser you're going to see here uh that it's changed the the default style of the broken image now i want to make that font size a little bit bigger so i'm actually just going to say one rim and that should make it the default size i like that okay and now what we're going to do this is easy we're almost done image the after sudo element we're going to say display inherit so we're going to inherit the inline block style we're going to position absolute top 0 left 0 we're going to do width 100 percent min width it's going to be a minimum of 200 pixels padding will be 10 20 10 and 45 to account for the icon that we're going to put in the left side we're going to go border oh we need a semicolon up here that's why border dashed dash to one pixel triple eight and then we're gonna say background color is gonna be f zero f zero f zero and then the background image we're gonna pull that url called no image dot png right here in our useful broken images folder directory background size for that image is going to be 10 background repeat no repeat background position we're going to put it at 15 pixels in the x 10 pixels on the y z index 1 and the content this is going to be cool so i'm going to save that actually save so far and see what we have now you can see there's nothing there we did all these styles but nothing showed up that's because we didn't do the content property yet so you always have to do content on an after or before pseudo element back here in the code editor simple we could do something like content to empty string now if we head back you'll see that something is appearing but it's this obviously doesn't look very good so we're going to fix that by adding text within that element and we're going to we're going to adjust that so so here in content we're going to do the double string and then right after that we're going to say attribute so it's the attribute value for the content property and we're going to say alt so what's that doing it's looking for the html attribute alt the value of it so here the alt flying eagle so it's looking for that attribute alt so it's going to grab that value and put it as the content and then after that another string to close one double string to close that previous one and then not found and then close the string one more time so how does this work so we have an open uh double string which closes over here but we also have another set of strings in here that wrap around the attribute alt so it's just something that we kind of have to do it's like concatenating the text so attribute alt so the alt text not found save that go over here to our browser check it out flying eagle not found we got that icon from our useful broken images directory nice and now if we had a different image let's go to our index and i'm just going to add another image here and say broken image 2 and change the alt text to like let's just say paddle board and then i'm going to put uh a break tag save that check it out in the browser so paddle board not found now they're stacking on top of each other the reason why that's happening is because these are the after pseudo elements the actual image itself so if i inspect this the image itself is is this large and then the after pseudo element is that size so the image they're actually stacking on top of each other perfectly but it's the after pseudo-element that's conflicting so we just give the image some space around it so here i'm just going to say margin images will have like a margin of 0 0 40 pixels and then that should solve the problem there we go so over here we got some space nice looks great so that is day five useful broken images that's all we had to do is really simple but very powerful because hey it's nice to have a nicer user experience and we can do that with our broken images you didn't maybe you didn't know that you could do that and it's a pretty cool trick that's useful in your web development in your your portfolio your website apps applications client sites so hopefully you enjoyed the useful broken images lesson hang tight for tomorrow we're going to do day six and we've got some more fun stuff to code up in css i'll catch you there bye now\n"