Grow your CSS skills. Land your dream job.

If a div has no content – add class for display:none ?

  • # December 8, 2011 at 12:48 pm

    I’m trying to come up with a solution, where if a div is empty, I would like to add a class so that I can display:none;. I tried searching without any luck.

    I don’t know how to write this expression though? Any help would be appreciated :)




    < ?php
    echo $errors;
    echo $errors;
    echo $errors;
    echo $errors;
    ?>


    # December 8, 2011 at 12:57 pm

    I don’t really know what you’re doing… but what if it was more like this:

    < ?php if ( empty($errors) ) {
    // do nothing
    } else { ?>
    # December 8, 2011 at 1:00 pm

    I have a div that contains all my errors on a form. I have it styled and positioned where needed, but when there are no errors, I would like the div to not be visible.

    I’m going to see if I can work with the idea above, thanks.

    # December 8, 2011 at 2:02 pm

    Well that’s not going over too well lol. I tried writing that function ( I always get mixed up about the names of php [statements/expressions/functions]) outside the div, inside the class=”” itself and lots of different ways, I could never get it to work. I think I was doing it wrong, but not sure?

    # December 8, 2011 at 2:05 pm

    I’m not the strongest with PHP, so my solution might not be EXACTLY what you need to write.

    # December 8, 2011 at 2:08 pm

    Thanks for trying @TheDoc.

    # December 8, 2011 at 2:18 pm

    Even with my limited knowledge, there has to be something we can do! What code did you try?

    # December 8, 2011 at 2:25 pm

    Sweet, I asked a buddy and he helped out. I was thinking of it the same way and trying like you said, if empty do not display vice if not empty display.



    < ?php
    if(!empty($errors)){
    echo '
    ';
    echo $errors;
    echo '
    ';
    }
    ?>

    # December 8, 2011 at 2:53 pm

    wait, if you’re using php why have an empty div in the first place? use php to output the div only when there is an error

    # December 8, 2011 at 3:22 pm

    I think that’s what he finished with there.

    # December 8, 2011 at 9:55 pm

    yup, that’ll teach me not to refresh the page after having a cup of coffee…

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".