SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Style <br /> ??

  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,923
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Style <br /> ??

    Is it possible to style a break <br /> similar to how you would style the paragraph <p> tags?


    Debbie

    P.S. Is this <p> called a "Tag" or an "Element" or are the terms synonymous?!

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,299
    Mentioned
    460 Post(s)
    Tagged
    8 Thread(s)
    You can certainly apply some styles to <br>s. But I use them extremely rarely, so I'd first question whether you really need them.

    A paragraph is an "element", but <p> and </p> are "tags". (That's how I understand it, anyhow. )

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,923
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    You can certainly apply some styles to <br>s. But I use them extremely rarely, so I'd first question whether you really need them.

    A paragraph is an "element", but <p> and </p> are "tags". (That's how I understand it, anyhow. )
    So what do I do with this new dilemma...

    If a User writes a book - 5 paragraphs - in my TextArea in the Form, and submits it, the browser just inserts hidden \n wherever there are carriage returns. But I have no "hooks" (i.e. <p></p>) to style?!

    And while I can use something like nl2br in PHP, I still have no proper way to style my pseudo-paragraphs?!

    (That's a pretty STUPID design on the part of the browser engineers or whoever?!)

    Because my Q&A Form is designed to get people to type mini-books, I don't want this big slop of horrendously long lines of TEXT followed by an occasional <br />?! YUK!!!


    (Example of just 2 Sentences to make my point...)
    Elizabeth, New Jersey, when my mother was being raised there in a flat over her father’s grocery store, was an industrial port a quarter the size of Newark, dominated by the Irish working class and their politicians and the tightly knit parish life that revolved around the town’s many churches, and though I never heard her complain of having been pointedly ill-treated in Elizabeth as a girl, it was not until she married and moved to Newark’s new Jewish neighborhood that she discovered the confidence that led her to become first a PTA “grade mother,” then a PTA vice president in charge of establishing a Kindergarten Mothers’ Club, and finally the PTA president, who, after attending a conference in Trenton on infantile paralysis, proposed an annual March of Dimes dance on January 30 – President Roosevelt’s birthday – that was accepted by most schools.<br />

    Their honour precarious, their liberty provisional, lasting only until the discovery of their crime; their position unstable, like that of the poet who one day was feasted at every table, applauded in every theatre in London, and on the next was driven from every lodging, unable to find a pillow upon which to lay his head, turning the mill like Samson and saying like him: “The two sexes shall die, each in a place apart!”; excluded even, save on the days of general disaster when the majority rally round the victim as the Jews rallied round Dreyfus, from the sympathy–at times from the society–of their fellows, in whom they inspire only disgust at seeing themselves as they are, portrayed in a mirror which, ceasing to flatter them, accentuates every blemish that they have refused to observe in themselves, and makes them understand that what they have been calling their love (a thing to which, playing upon the word, they have by association annexed all that poetry, painting, music, chivalry, asceticism have contrived to add to love...<br />
    Follow me?!


    Debbie

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,299
    Mentioned
    460 Post(s)
    Tagged
    8 Thread(s)
    Hm, as a quick experiment, you could try something like

    Code:
    br {display: block; margin-top: 20px;}
    But I'd like to know if there's a better way—via PHP—to add in <p> tags around that text. But I can't advise on that. Surely it must be possible with basic PHP, though. CMSes add in <p> tags.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To start with the entire content entered should be wrapped inside <p> and </p>

    Then whenever two \n occur with nothing but whitespace in between you replace them with </p><p> which can be easily done in PHP using a regular expression. Any single \n should generally be ignored so that the text can adapt to fit the width available.

    Doing that you wouldn't end up with any <br/> at all.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,923
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    To start with the entire content entered should be wrapped inside <p> and </p>

    Then whenever two \n occur with nothing but whitespace in between you replace them with </p><p> which can be easily done in PHP using a regular expression.
    I thought I knew Regex fairly well, but I'm unsure of how to do what you are describing.

    Care to share how you'd do that?


    Any single \n should generally be ignored so that the text can adapt to fit the width available. Doing that you wouldn't end up with any <br/> at all.
    Wouldn't you want to use a Regex to replace an \n with a <br /> ??

    Thanks,


    Debbie

  7. #7
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    if your text are only includes Ps, you could do it with a lowly sequenced str_replace. for this it is faster, and simpler to read.
    PHP Code:
    $content'<p>'.$content.'</p>';
    $contentstr_replace("\n\n","</p><p>")
    $contentstr_replace("\n","<br \>"); 

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To allow for someone having entered spaces between the linefeeds you could use:

    PHP Code:
    $contentstr_replace("\n\s*\n","</p><p>"); 
    I'd probably follow that with:

    PHP Code:
    $contentstr_replace("\n",""); 
    since except for poetry and a few other rare situations the content of the paragraphs should just fill the available width without linefeeds in specific places that could cause words to appear on lines by themselves.

    If you are going to replace them with <br/> tags then don't include the unnecessary space in the tag (that was only necessary to get the code to work with Nescape 4).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $contentstr_replace("\n\s*\n","</p><p>",$yourstring); 
    It's a good point, but I dont think str_replace() works with regex. so you may have to go with preg_replace() for that.

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dresden_phoenix View Post
    It's a good point, but I dont think str_replace() works with regex. so you may have to go with preg_replace() for that.
    You're right - I didn't look closely enough at which function was being used and had mistaken the one for the other since I had been thinking that a regex solution was the best option from the start.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •