Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Foundations Text, Fonts, and Lists Text Properties

Elena Paraschiv
Elena Paraschiv
9,938 Points

::first-line and text-indent

I am trying to make only every first line of the paragraph after each h3 indent, but does not seem to work Imgur

this is my code :

<div class="main supporting" id="zen-supporting" role="main">
        <div class="explanation" id="zen-explanation" role="article">
            <h3>So What is This About?</h3>
            <p>There is a continuing need to show the power of <abbr title="Cascading Style Sheets">CSS</abbr>. The Zen Garden aims to excite, inspire, and encourage participation. To begin, view some of the existing designs in the list. Clicking on any one will load the style sheet into this very page. The <abbr title="HyperText Markup Language">HTML</abbr> remains the same, the only thing that has changed is the external <abbr title="Cascading Style Sheets">CSS</abbr> file. Yes, really.</p>
            <p><abbr title="Cascading Style Sheets">CSS</abbr> allows complete and total control over the style of a hypertext document. The only way this can be illustrated in a way that gets people excited is by demonstrating what it can truly be, once the reins are placed in the hands of those able to create beauty from structure. Designers and coders alike have contributed to the beauty of the web; we can always push it further.</p>
        </div>

        <div class="participation" id="zen-participation" role="article">
            <h3>Participation</h3>
            <p>Strong visual design has always been our focus. You are modifying this page, so strong <abbr title="Cascading Style Sheets">CSS</abbr> skills are necessary too, but the example files are commented well enough that even <abbr title="Cascading Style Sheets">CSS</abbr> novices can use them as starting points. Please see the <a href="http://www.mezzoblue.com/zengarden/resources/" title="A listing of CSS-related resources"><abbr title="Cascading Style Sheets">CSS</abbr> Resource Guide</a> for advanced tutorials and tips on working with <abbr title="Cascading Style Sheets">CSS</abbr>.</p>
            <p>You may modify the style sheet in any way you wish, but not the <abbr title="HyperText Markup Language">HTML</abbr>. This may seem daunting at first if you&#8217;ve never worked this way before, but follow the listed links to learn more, and use the sample files as a guide.</p>
            <p>Download the sample <a href="/examples/index" title="This page's source HTML code, not to be modified.">HTML</a> and <a href="/examples/style.css" title="This page's sample CSS, the file you may modify.">CSS</a> to work on a copy locally. Once you have completed your masterpiece (and please, don&#8217;t submit half-finished work) upload your <abbr title="Cascading Style Sheets">CSS</abbr> file to a web server under your control. <a href="http://www.mezzoblue.com/zengarden/submit/" title="Use the contact form to send us your CSS file">Send us a link</a> to an archive of that file and all associated assets, and if we choose to use it we will download it and place it on our server.</p>
        </div>

        <div class="benefits" id="zen-benefits" role="article">
            <h3>Benefits</h3>
            <p>Why participate? For recognition, inspiration, and a resource we can all refer to showing people how amazing <abbr title="Cascading Style Sheets">CSS</abbr> really can be. This site serves as equal parts inspiration for those working on the web today, learning tool for those who will be tomorrow, and gallery of future techniques we can all look forward to.</p>
        </div>
    h3+p::first-line{
        background: yellow;
        text-indent:2em;
    }
Johnathan Mercier
Johnathan Mercier
5,527 Points

Hi, unfortunately text-indent and margin-left can not be applied to the first-line pseudo-class. Your best option is to apply a class to each of the first p tags like this. Hope this helps

.firstLine {
    text-indent: 2em;
}
<div>
   <h3>Example Headline</h3>
   <p class="firstLine">Put your first paragraph here and you should be all set to go :D</p>
   <p>This second line without the class will just render itself normally :D</p>
</div>

4 Answers

I would suggest instead of using the pseudo class of :first-line, I would make the div a block-level element and use the selector text-indent. The problem is that first-line only works with fonts, background effects and nothing else. you can target the first line of the p tags, if you make the div a block-level element and use that selector. Here are the docs on text-indent and :first-line:

text-indent

:first-line

I hope this helps.

Elena Paraschiv
Elena Paraschiv
9,938 Points

It helps if I indent all the p in the div, but I want to indent just the first paragraph inside every divs. You can see there are more p .

The first thing I thought of is to create a class for your first p tags, then use that class to style that p tag, or you could see if nth-of-type or nth-child will work. I believe nth-of-type should do it.

Milutin Jovkovic
Milutin Jovkovic
2,614 Points

I think you need just this

p { background: yellow; text-indent:2em; }

Milutin Jovkovic
Milutin Jovkovic
2,614 Points

and put class at first paragraphs..

Elena Paraschiv
Elena Paraschiv
9,938 Points

worked like a charm. Thanks Jacob!

Awesome! nth-of-type work? or did you go the way of the class?