Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

HTML Forum


You are currently viewing our HTML Forum as a guest. Please register to participate.
Login



Post a Project »

Find a Professional HTML Freelancer!

Find a Freelancer to help you with your HTML projects

FREE Outsourcing eBook!

Reply
Best way to surround text with css box inside of p
Old 12-23-2009, 09:14 AM Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
I'm just curious of the best way to do this..

I have a custom content management system that I'm slowly building from the ground up. Now on the pages that display my content it's done all in PHP. So it looks something like..

Code:
echo "<p>$content</p>";
Which works great, but sometimes I like to add code samples to a page and have arranged a CSS class that surrounds the text with a CSS padded border and new background (very similar to the same way that phpBB and other forums do it).

Now of course, putting a <div class="code">whatevermycodeis</div> inside of the P doesn't validate right and isn't the correct way of doing it, so I've gotten around it temporarily by doing <object><div class="code">code</div></object> ..but it doesn't display properly in IE.

So what would be the best way of doing this?

Thanks
bswinnerton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-23-2009, 09:51 AM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
use a <pre> elelment

see http://www.modtalk.co.uk/article/c-a...sign/uri-case/ for an example.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-23-2009, 09:52 AM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Which - oddly enough, is part of a CMS that was built from the ground up
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 12-23-2009 at 09:53 AM..
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-23-2009, 09:56 AM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Is that the right link?
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 10:15 AM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Here are some links for future reference of readers:

http://www.w3schools.com/TAGS/tag_pre.asp

And wrapping the <pre> tags:
http://www.longren.org/2006/09/27/wr...side-pre-tags/

Thanks Chrishirst
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 10:19 AM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by bswinnerton View Post
Is that the right link?
Looks ok to me

did you scroll down the page you you could see the box with the code displayed in it??
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-23-2009, 10:19 AM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Shoot, I just realized that when using <pre><div class="code">code</div></pre>, it breaks my <p> tags. Anything after code in the browser is no longer in <p>.

How can this be fixed?
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 10:20 AM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Looks ok to me

did you scroll down the page you you could see the box with the code displayed in it??
Ohhh, sorry I thought that you meant that it was a page about <pre> tags. My fault.

But please see my post above this one. I'm confused
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 10:26 AM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Hmm, W3C really didn't like that..

Code:
document type does not allow element "pre" here; missing one of "object", "applet", "map", "iframe", "button", "ins", "del" start-tag
Using XHTML 1.0 Transitional
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 10:39 AM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
look at my source code and you'll find that my system converts the < & > delimiters into entities so they are NOT renderable code and will NOT "break" the page code.

http://www.webmaster-talk.com/asp-fo...tml#post542714
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-23-2009, 10:42 AM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
I could be wrong, but I don't think that the < and > are breaking the code. This would only be if I had </p> in the code, wouldn't it? I think that the problem is that you cannot have a <pre> tag inside of a <p> tag.
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 10:45 AM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
a <pre> is a block element as is a <p> so it is perfectly legitimate
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-23-2009, 10:49 AM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Hmm, W3C seems to disagree with you:

I just made this test file:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<p><pre>this is a test of a pre inside of a p</pre></p>

</body>
</html>
And here is what W3C had to say
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 12:09 PM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://htmlhelp.com/reference/html40/block.html

No idea why the validator should regard that as invalid they are both block elements and neither are deprecated.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-23-2009, 12:19 PM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
That's very odd.

I still don't understand why it's ending my <p> elements at the start of the <pre>. For example I could have something like <pre><div class="code">there are no slashes or carrots here</div></pre> and anything after that won't be part of p.

Is there another way I can do this? Or should I start looking into a way for PHP to automatically insert </p> before any div's and <p> at the end of the divs?
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 04:48 PM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
for the HTML code you want to show as a text block of visible code rather rendered code (HTML elements) just change the HTML delimiters to HTML entities

Look at the source code of http://www.modtalk.lcl/article/c-and...gn/dhtml-tabs/

go look at the source code of http://validator.w3.org/check?uri=ht...idator%2F1.654 as the W3c do exactly the same.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-24-2009, 08:08 AM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.webmaster-talk.com/html-f...play-code.html
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-24-2009, 12:46 PM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Okay, so I took your PHP function from here:

Code:
function toCode($strItem) {
           $html = array("<",">",chr(34));
           $entities = array("&lt;","&gt;","&quot;");
           $strItem = str_replace($html, $entities, $strItem);
return $strItem;
}
but now it breaks all of the code stuff that I want to display. For example now that I use this function, if I write the following in my textbox to submit to the website:

Code:
<pre><div class="code">this is some code <a> test </a> more code</div></pre>
It will display it as:

Code:
<pre><div class="code">this is some code <a> test </a> more code</div></pre>
instead of just displaying the <a></a> (which might I add makes perfect sense), but this isn't what I want it to do. I only want it to take the "<,>"'s when it's surrounded by the <pre> tags.

Last edited by bswinnerton; 12-24-2009 at 12:48 PM..
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-24-2009, 12:53 PM Re: Best way to surround text with css box inside of p
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You only parse the code you want to display INSIDE the<pre> through the function, not all of the page code.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-24-2009, 12:54 PM Re: Best way to surround text with css box inside of p
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
I'm still a little confused. How is this going to fix the content after the <pre><div class="code></div></pre> from not being part of the p class?

Because even if the content inside of <pre><div class="code></div></pre> doesn't actually have any code (say, it's just text), anything after it isn't part of p. So I don't think that it's the greater than and less than symbols that are causing the trouble. I think that it's the pre or div element.

Edit: and also, as to only parsing things that are inside of the <pre> element, that will take a little research on my part. I'll have to search the content for <pre> and then once it found it, run the function and then stop running it when it finds </pre>, right?

Last edited by bswinnerton; 12-24-2009 at 12:59 PM..
bswinnerton is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Best way to surround text with css box inside of p

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.59804 seconds with 13 queries