This lesson is exclusive to members

Responsive Web Design Essentials - HTML5 CSS3 Bootstrap

How to add a css style to the first line of a p tag on a website

Daniel Walter Scott

Download Exercise Files Download Completed Files

Contents

Certificates

We’re awarding certificates for this course!

Check out the How to earn your certificate video for instructions on how to earn yours and click the available certificate levels below for more information.

Introduction

I recommend hosting your new website with Bluehost, you can get a big discount by signing up with this link: https://www.bluehost.com/track/byol/byol_webess_88

You need to be a member to view comments.

Join today. Cancel any time.

Sign Up
Hey there, this video we're going to look at adding a CSS class, to make things bold for the first line only. Doesn't matter what's in the line, everything will become bold, you can do it for the first later, or the first line. It's a funny little bit of CSS. Let's work out how to do it now in VS code. 

So looking at my mock-up, that's what I want to do. I want to make the first line bold, and then the second, or all the rest of the lines not bold. So we are going to look at our weird little CSS class. So Visual Studio code, first thing we need to do is add a bit of text. So we're going to add it to all of our little boxes. At the moment we've got nothing, so to clean everything up let's get rid of the background color. I'm going to use it for in here, not in the background. 

So I'm going to go to VS code, my container has a color, I want to steal, and add back to the columns. Before, I wanted to get rid of it, now I want to add it. It's kind of peeking out the bottom there, but it's all right, I'm going to add text and make it really big. You could-- what we're going to do, actually let's just do it, Dan. So the column here is going to have some text inside of it. At the moment it has an image, after the image-- because my image kind of finishes there, I'm going to put it in a P-tag, and I'll put in some Lorem Ipsum, I want about, keywords, and I want at the beginning here, I'll put in the word 'Branding'. Cool. 

So that's what I want to style, I want to target this. Let's have a look at it in here. You can see how-- what I've done here, I've made the column, it doesn't have a height so it expands for the stuff to fit it in. There's an image in it, and because they can't fit together, they squish out and the column gets a little bit bigger. I could make a second Div underneath it, Col, and then, I don't know, col name, and style that green and background, but this way is a bit simpler. I want to go and style it, all looking really good, but let's just put in the bold first, because that's what you came for. 

The way to attack it is we're going to put it in our Global styles. It's not crazy different syntax except we use two colons to join them up. I want to style the first line, you can do the first letter. The line, I really want the first word, that's not an option. So I'm going to do first line, and I'm going to make the font weight. Font weight. I'm going to make it bold, only because I don't have a-- I should put in like 700, is a very normal bold size, but I haven't picked a font yet, which is using Times New Roman. You can put any style you like as long as it's text styling, and it's going to kind of work, let's have a look, looks perfect. 

The reason I said kind of is that I'm kind of attacking all P-tags first lines, and in our mock-up here we're going to have more than one bit of text. So there's going to be text here, here, here. I'm bound to have another page with, like, I only actually have one P-tag on this website, but if I go to the Contact Us page, and I build that out, I'm going to use lots of P-tags. So I don't want them all to be bold so we're going to be very specific. We're going to say, only the first line of P-tags that are inside columns. So a space between those two, two colons to join those two up, and it's how to do that one. 

Let's go through and install a font, we will get it looking like this, Are we going to cover anything new? No. We're just going to put some padding in, make it wide to make it the right font. You can skip along if that all sounds easy to you, but if it doesn't, carry on, we'll do it together. First up, a font, so I'm going to go to Google Fonts, google.com/fonts. The font that I've decided - type it in properly - is something called ASP. Just a really good free all-rounder. Comes in condensed as well, which is really cool. I'm just going to use the regular, I'm going to hit '+'. We've done this before, but it's good to kind of recover these things, little minus, '-'. You can download them now to start using them on your computer, but to use them on your website you use the embed. 

I want to customize it because I decided to use regular and bold, and none of the other ones. So I'm going to embed it, I'm going to grab you, copy that, goes into our Head tag. So as long as it's before the closing of the head, there you go. You'll notice in this website, I'm only-- guess it's kind of a simple website, but I've decided to, instead of using like a Display font or a Serif font, I'm just using the bold and the light version, to get away with kind of like, trying to organize the hierarchy of information. So I got the font, let's apply it. So what I'm going to do is I'm going to apply it to everything. Do you remember how to do that? Everything in this document, I want to be ASAP. That's right, you do it to the body. So the Body tag here is going to be font family, and I'm going to grab this, font family. Save it, let's have a little look in our preview, there you go. 

It's the right font, at least. Let's pick a size and a color, and some padding. I'm going to check my mock-up. You are a font size of 14. Now I'm going to create my own one, because I don't want to do all P-tags. This, I want the P-tags that are inside the columns to do this thing. Actually I could probably do it to the column, everything inside of them, but we'll do it this way. Let's go 'color', it's going to be white, and let's do our font size. Remember, calculator. It is going to be 14x0.0625, random. And, where is he? That's going to be it, and it's going to be rems. 

We're going to do some padding, and we'll do our fancy all the way around. So top is going to be, no idea, and side to side is going to be, no idea. Just good at guesses, let's have a look. Yeah, it's all right, it's not looking as kind of defined as that. So let's add a bit more. I guess I show you this because I could go through before the class, and I guess write this out, a little note for myself to make it seem smoother, but this is the kind of stuff that I end up doing. I'm like, "No, back here, no, back here." Testing, trialing, so maybe 25 for the height. That will do for me. 

You'll notice that our ball went away. You might have noticed already, I just noticed there. Is, why? Because we're trying to apply a, a font weight that doesn't exist. This one uses 700, not the word bold. Some fonts use bold, I assume that was it, just make sure I refresh. Right click. Why are you not loading now? So p first line col, that is all still true, that is all still true. 

All right, confession time, I was gone for a big five minutes there, trying to work out what happened, you might have already noticed it. It's a syntax problem, the problem is this guy, was missing his little, I am a Class, full stop in front of him, took me a little while though, I guess I want to leave this in because it happens to everybody. And I'll kind of go, I'll kind of give you the skinny version, of what I tried to do, and it effects it, and how I eventually did it. Basically it worked backwards and you go, "Ah, what was the last thing I did?", and I was like, "That was the last thing I did." So let's just turn that off. 

So remember, 'Command /' on a Mac, 'Ctrl /' on a PC, it adds it to commenting. It means it turns it off so it's not read by the browser anymore. So it's as good as deleting it, except it's still there. You guys are ruing, I'm like, "Great." Actually, we'll add that fix problem. So that was the problem, right? We'll ignore that. So I turned it off and it was still not doing it. I was like, "Okay,"so with it on it doesn't work, with it off, doesn't work, so it's not you. It sounds like, maybe I need to go, and I keep turning things off, and then I'm like, up here maybe I need to set the font weight, up here to be 400 first, okay, 400, then later on turn it to 700. That didn't work either. 

So I was just kind of messing about, and eventually I get to the point where I'm like, "No idea," nothing visually, so I just started typing it out. So turn that on and off, didn't work, then I'm like, "Okay, let's retype it" So I just went through and I started typing, and instantly you can start to see, it's one character out already, and I was like, "Geez," that was it. Don't lose your full stops, sorry periods. All right, are we back, we are back, and I turn this back on. You can see, it's nice, just turn this stuff on and off, you're back, you're back, we're looking good. 

Another thing we'll do, is it's jammed together because we're using a CSS reset. So this col p is going to have a line height as well. As long as it's above 1, line height, it's a good place to get started; 1.5 rem, too big, 1.25, awesome. Actually, in this case as long as it's above that, not 1, as long as it's above whatever the font size is being set. So as long as it's above 0.8 it will get bigger. Anything less than that, and it will get smaller, negative line height. Hmm, not something that I've done. 

Now I'm going to go through and just keep adding to these boxes. So you can skip along, I'm just going to copy and paste, but you can hang around if you like. You never know what we'll dig up in terms of shortcuts, or getting lost, in fixing it. So I'm going to go to 'View'. I actually might close this down now, go to 'Full View', make it a little easier. I'm going to use this P-tag repetitively. So I'm going to copy it, I'm going to use my multi cursor to go, you, you, holding down 'Option' key on a Mac, 'Alt' key on a PC. Grabbing all these guys, that go there, and pasting them in. Hopefully it should all still work if I paste it in the right place; nice. 

The benefit of the first line class rather than wrapping this first P-tag, and maybe having two P-tags in here, and just putting a style on the first one, is that if the text gets longer, or changes, so one's branding-- Let's actually make this. If you put on more than one Lorem Ipsum, we're doing a test together, if I put in eight words again you get different Lorem Ipsum. Nope, exactly the same every time. All right, my high school typing class, you impressed? It's hard, can't see my fingers, but we've got this third one, and there we go. Because it is doing the first line, doesn't matter what you type in there, it's going to be bold, which is handy. Be sweet if you could do, kind of a Grep style style where you can kind of get the first word. Hey-ho, that's all we wanted to do. A little production video though, we kind of started off learning some cool CSS, and then we went on a little bit of a journey, but we're here, we made it, get on to the next video.