user1805900
user1805900

Reputation: 437

Display only one paragraphs...no matter if there are more, with blank, with white-space, with class...only and only with css

I am looking css properties to hide paragraphs with or without css class, if it contains white-space ( ) or blank, but wants to keep at least- only one paragraph with or without   if there are more.

Hide paragraphs if it is blank or contains white-space( ) preferably with only css...if no other options at all then only with JavaScript/jquery

// Ideally I don't want to use javascript/jquery

$("p").html(function(i, html) {
                  return html.replace(/ /g, '');
                });
        p:nth-child(n+2):empty,
        p:nth-child(n+2):blank,
        .MsoNormal p:nth-child(n+2):empty,
        .MsoNormal p:nth-child(n+2):blank {
        margin: 0 0 0px;
        display: none;
        }

        p::before {
        content: ' ';
        }

        p:empty::before {
        content: '';
        display: none;
        }

        p:first-child:empty+p:not(:empty)::before {
        content: '';
        }

        p:first-child:empty+p:empty+p:not(:empty)::before {
        content: '';
        }

        p::after {
        content: '';
        display: none;
        p:empty::after {
        display: none;
        }
        p:first-child:empty+p:not(:empty)::after {
        content: '';
        }
        p:first-child:empty+p:empty+p:not(:empty)::after {
        content: '';
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> some text - 1 </div>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <div> some text - 2 </div>
    <p>&nbsp;</p>

    <div> some text - 3 </div>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp; &nbsp; &nbsp; </p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>
    <p>&nbsp;</p>
    <div> some text - 4 </div>
    <p>&nbsp; &nbsp; &nbsp;</p>
    <p>&nbsp;</p>
    <div> some text - 5 </div>
    <p></p>
    <p></p>
    <div> some text - 6 </div>
    <p class="MsoNormal"></p>
    <p></p>
    

    <b>So above html, I would like to display:</b>

    <div> some text - 1 </div>
    <p>&nbsp;</p>
    <div> some text - 2 </div>
    <p>&nbsp;</p>
    <div> some text - 3 </div>
    <p>&nbsp;</p>
     <div> some text - 4 </div>
    <p>&nbsp; &nbsp; &nbsp;</p>
    <div> some text - 5 </div>

So, I am trying to get it through pseudo classes & pseudo elements, but no luck. (Note- I have jQuery which is working here, but don't want to use it preferably.)

Upvotes: 0

Views: 111

Answers (1)

jimixjay
jimixjay

Reputation: 74

You can't do this only with CSS, as far as I know.

With jQuery is the most easy and clean way to do this. I don't understand why you have jQuery but you don't want to use it but do this with pure js is more "ugly" for me. Although I give you 2 pieces of code.

JS code:

// get the elements and transform from HTMLCollection object to array
var array_p = document.getElementsByTagName("P");
array_p = Array.prototype.slice.call(array_p);

array_p.forEach(function(value, index) {
    var text = value.innerHTML;
    text = text.replace(new RegExp('&nbsp;', 'g'), '');
    text = text.replace(new RegExp(' ', 'g'), '');
    value.style.display = "none";
});

I add a jQuery code for example if you want to use it:

$.each($("p"), function(index, value) {
    var text = $(this).html();  
    text = text.replace(new RegExp('&nbsp;', 'g'), '');
    text = text.replace(new RegExp(' ', 'g'), '');
    if (text.length == 0) {
        $(this).css("display", "none");
    }

})

Upvotes: 1

Related Questions