Solved

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

Posted on 2011-02-17
5
676 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now