Deedub
Deedub

Reputation: 349

Styling empty HTML markup

I have a right sidebar in my design that pulls in testimonials dynamically, if there are any.

The HTML looks like:

<h4> dynamic content</h4>

Here is my CSS:

#testimonials {
    background: #eeeeee; 
    padding: 30px; 
    width: auto; 
    height: auto;
}

#testimonials h4{
    font-size: 20px;
    line-height: 30px;
    font-family: "freight-big-pro";
    font-style: italic; 
    border-bottom: 1px solid #666; 
    padding-bottom: 20px; 
    padding-top: 20px;
}

#testimonials h4 strong{
    display: block; 
    font-family:"freight-sans-pro", sans-serif; 
    font-style: normal; 
    font-size: 12px; 
}

The issue is that when there is no content in the <h4> element, the style is still being picked up and adds a background and a border as specified in the CSS. I am assuming that it's generating the h4. Is there a way to have it be empty if there is not any content?

Update:

I am trying this and it seems to work in jsfiddle, but not in the file:

<script type="text/javascript"> $(document).ready(function(){ if ($("#testimonials").text().length < 65) { $('#testimonials').hide(); } });</script>

It counts the HTML inside as text, I think.

Upvotes: 0

Views: 1116

Answers (4)

Gray
Gray

Reputation: 7130

Update

Here is another JsFiddle, but this also probably won't work for the OP as it uses jQuery.

jQuery

//onload:
checkStyle();   

//checks if the h4 is empty, and hides the whole div if so.
function checkStyle ()
{
    if ($('#testimonials h4').is(':empty')) 
        $('#testimonials').hide();
    else 
        $('#testimonials').show();    
}


This does not necessarily work for what the asker is looking for, but could be beneficial for future readers. It is for not styling the h4, not the parent div as op wants.

Assuming you are ok with CSS3, and the <h4> is literally empty, you can modify your CSS to use the :not and :empty selectors.

CSS

#testimonials h4:not(:empty) {
    font-size: 20px;
    line-height: 30px;
    font-family:"freight-big-pro";
    font-style: italic;
    border-bottom: 1px solid #666;
    padding-bottom: 20px;
    padding-top: 20px;
}
#testimonials h4:not(:empty) strong {
    display: block;
    font-family:"freight-sans-pro", sans-serif;
    font-style: normal;
    font-size: 12px;
}

Here is a JsFiddle. You can add content to the h4 to see how it works.

Alternatively, you could even do the opposite, and have a display:none; for empty <h4>s:

#testimonials h4:empty{
    display:none;
}

Upvotes: 2

KnowHowSolutions
KnowHowSolutions

Reputation: 680

This seems like alot of front side work when it isn't needed. If you are able to output content into the h4, then you are able to output and additional tag.

<section id="testimonials"></section>

Server Side pushes out:

<h4>all my content</h4>

Then your CSS will work without any work from js.

Most likely you have one for each testimonial?

Upvotes: 1

SRay
SRay

Reputation: 286

Do a display:none on your css initially when there is no content. Use javascript or jquery to show content. Styling will be applied when the content gets displayed.

Initially when there is no content: #testimonials { background: #eeeeee; padding: 30px; width: auto; height: auto; display :none; }

When content gets generated dynamically use: $("#testimonials").show();

Upvotes: 1

Aaron Miller
Aaron Miller

Reputation: 3780

Give #testimonials a display: none; property in your CSS; then, just before whatever Javascript code you use to pull in testimonials finishes running, have it check whether it actually retrieved any, and set display: block; on #testimonials if so.

Somewhat related: When asking questions on Stack Overflow, it's ideal to post as much information as possible, as for example the code you're using to retrieve testimonials dynamically -- it's mentioned in the question and its behavior affects what you're asking about, which makes it well within scope. If you'll update your question with your testimonial-retrieving code, I'll update my answer to show a specific solution.

Upvotes: 1

Related Questions