Solved

CSS question: How to change P tag style within one Div only

Posted on 2011-02-17
5
678 Views
Last Modified: 2012-06-22
This is an HTML or CSS question dealing with the Paragraph and Division tags.
If say  the P tag has a default width of  600px. Then assume you want the
P style to have a width of 400px but only with in one Div.
How can this be done without having to add a style to each P in the Div?
Is there some sort of style look up function?

0
Comment
Question by:jimr111998
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
HagayMandel earned 500 total points
ID: 34916769
You give your containing div an id:
<div id="sample">
<p>
Sample text
</p>

Open in new window


then you can apply the style the following way
#sample p {
	width: 400px;
}

Open in new window


The text in the P tag that is in the "sample" div (and only there),  will have a width of 400px

0
 
LVL 2

Expert Comment

by:moehler
ID: 34917042
Are you saying you have something like this:

<html>
<head>
      
</head>

</body>
      <div>
      <p>para1</p>
      <p>para2</p>
      <p>para3</p>
      <p>para4</p>
      </div>
      <div>
      <p>para1</p>
      <p>para2</p>
      <p>para3</p>
      <p>para4</p>
      </div>
      <div>
      <p>para1</p>
      <p>para2</p>
      <p>para3</p>
      <p>para4</p>
      </div>
</body>

and you want to only apply a given style to para1?

if you want to limit it to just the first para of the first div, you can do this

<style>
      div:first-child p:first-child{
            background:yellow;
      }
</style>

if you want it to be all first para1 then do this

<style>
      div p:first-child{
            background:yellow;
      }
</style>

If so
0
 
LVL 2

Expert Comment

by:moehler
ID: 34917062
the comment was submitted before I was done editing it.

Sorry, that was for background color. If you want to do width, just make appropriate replacement.
0
 

Author Comment

by:jimr111998
ID: 34918037
HagayMandel,

Yes tried it and it works but I wanted something more general.
Something that can be used whenever needed without having
to create another style def.

So I took your information and tried this

.P400px P {
  width: 400px;
}

Then used it like this
<div class="BaseDef  P400px">

</div>
and it works. So now whenever I need to limit paragraph widths in a div to 400px
i just add P400px to the class def.

Thanks for pointing me in the right direction. You get the points.









0
 

Author Closing Comment

by:jimr111998
ID: 34918053
see my response to HagayMandel
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question