Recent Posts

Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Applying CSS to an iframe

There are two different things here: the style of the iframe block and the style of the page embedded in the iframe. You can set the style of the iframe block the usual way:
<iframe name='iframe1' id="iframe1" src="empty.htm" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
The style of the page embedded in the iframe must be either set by including it in the child page:
<link type="text/css" rel="Stylesheet" href="Style/simple.css" />
Or it can be loaded from the parent page with Javascript:
var cssLink = document.createElement("link") 
cssLink.href = "style.css"; 
cssLink .rel = "stylesheet"; 
cssLink .type = "text/css"; 
frames['frame1'].document.body.appendChild(cssLink);

HTML: Removing spaces between inline-block elements

Given this HTML:

<p>
    <span> Foo </span>
    <span> Bar </span>
</p>

and this CSS:

span { 
    display:inline-block;
    width:100px;
}

as a result, there will be a 4px wide space between the SPAN elements. Demo: http://jsfiddle.net/dGHFV/

I understand why this happens, and I also know that I could get rid of that space by removing the white-space between the SPAN elements in the HTML source code, like so:

<p>
    <span> Foo </span><span> Bar </span>
</p>

However, I was hoping for a CSS solution that doesn't require the HTML source code to be tampered with.

I know how to solve this with JavaScript - by removing the Text nodes from the container element (the paragraph), like so:

// jQuery
$('p').contents().filter(function() { return this.nodeType === 3; }).remove();

Answer:

Since this answer has become rather popular, I'm rewriting it significantly.

Let's not forget the actual question that was asked:

How to remove the space between inline-block elements? I was hoping for a CSS solution that doesn't require the HTML source code to be tampered with. Can this issue be solved with CSS alone?

It is possible to solve this problem with CSS alone, but there are no completely robust CSS fixes.

The solution I had in my initial answer was to add font-size: 0 to the parent element, and then declare a sensible font-size on the children.

http://jsfiddle.net/thirtydot/dGHFV/1361/

This works in recent versions of all modern browsers. It works in IE8. It does not work in Safari 5, but it does work in Safari 6. Safari 5 is nearly a dead browser (1.49%, July 2013).

Most of the possible issues with relative font sizes are not complicated to fix.

However, while this is a reasonable solution if you specifically need a CSS only fix, it's not what I recommend if you're free to change your HTML (as most of us are).

This is what I, as a reasonably experienced web developer, actually do to solve this problem:

<p>
    <span>Foo</span><span>Bar</span>
</p>

Yes, that's right. I remove the whitespace in the HTML between the inline-block elements.

It's easy. It's simple. It works everywhere. It's the pragmatic solution.

You do sometimes have to carefully consider where whitespace will come from. Will appending another element with jQuery add whitespace? No, not if you do it properly.

Let's go on a magical journey of different ways to remove the whitespace, with some new HTML:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

You can do this, as I usually do:

<ul>
    <li>Item 1</li><li>Item 2</li><li>Item 3</li> 
</ul>

http://jsfiddle.net/thirtydot/dGHFV/1362/

Or, this:

<ul>
    <li>Item 1</li
    ><li>Item 2</li
    ><li>Item 3</li>
</ul>

Or, use comments:

<ul>
    <li>Item 1</li><!--
    --><li>Item 2</li><!--
    --><li>Item 3</li>
</ul>

Or, you can even skip certain closing tags entirely (all browsers are fine with this):

<ul>
    <li>Item 1
    <li>Item 2
    <li>Item 3
</ul>

Now that I've gone and bored you to death with "one thousand different ways to remove whitespace, by thirtydot", hopefully you've forgotten all about font-size: 0.



CSS: How to align horizontally one div inside another div

How do I horizontally center a div in a div with CSS (if it's possible at all)? The outer div has 100%:

<div id="outer" style="width:100%">  
    <div id="inner">Foo foo</div>
</div>
In this example the outter div have 100% width, to align horizontally the inner div we can apply this code in the inner div:

#inner {
    width: 50%;
    margin: 0 auto;
}
Of course, you don't have to set the width to 50%. Any width less than the containing div will work. The margin: 0 auto is what does the actual centering.

If you are targeting IE8+, it might be better to have this instead:

#inner {
    display: table;
    margin: 0 auto;
}
It will make the inner element center horizontally and it works without setting a specific width.




HTML Best Practices: HREF attribute for JavaScript link # or javascript:void(0)

<a href="#" onclick="myJsFunc();">Run JavaScript Code</a>
<a href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>
Neither is the correct way to build a JavaScript link.

If you can have an actual URL that makes sense use that as the HREF. The onclick won't fire if someone middle-clicks on your link to open a new tab or if they have JavaScript disabled.

If that is not possible, then you should at least inject the anchor tag into the document with JavaScript and the appropriate click event handlers.

I realize this isn't always possible, but in my opinion it should be striven for in developing any public website.

Check out Unobtrusive JavaScript and Progressive enhancement (both Wikipedia).

Convert a HTML and CSS document to PDF with PHP

To convert an HTML document with CSS stylesheets you have to take a look at PrinceXML.

It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming is not free either, so if it saves you 10 hours of work, you're home free.)

Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2!?

PrinceXML Samples

Rename the content inside a tag and confirm with jQuery

This examples target one simple container and change his name and then make the confirmation:

For this you need to identify the relevant h2/textarea

You also should nest your handler binding because each time your try to edit, you add a new handler

HTML:

<div class="rename">
     <h2 class="replaceble">Dell PC</h2>
 <a href="#" class="replace">Rename</a>
 <a href="#" class="confirm">Confirm</a>

</div>
<div class="rename">
     <h2 class="replaceble">Dell PC</h2>
 <a href="#" class="replace">Rename</a>
 <a href="#" class="confirm">Confirm</a>

</div>
<div class="rename">
     <h2 class="replaceble">Dell PC</h2>
 <a href="#" class="replace">Rename</a>
 <a href="#" class="confirm">Confirm</a>

</div>
JavaScript:

(function ($) {
    $.fn.changeElementType = function (newType) {
        var attrs = {};

        $.each(this[0].attributes, function (idx, attr) {
            attrs[attr.nodeName] = attr.nodeValue;
        });

        this.replaceWith(function () {
            return $("<" + newType + "/>", attrs).append($(this).contents());
        });
    }
})(jQuery);

$(document).keypress(function (e) {
    if ($('textarea:visible')) {
        if (e.which == 13) {
            alert('You pressed enter!');
            $("textarea").changeElementType("h2");
            $('.replace').css('opacity', '1');
        }
    }
});

$('.replace').on('click', function () {
    var container = $(this).closest('.rename')
    container.find('h2').changeElementType("textarea");
    $(this).hide();
    $(this).next('a').show();
});

$('.confirm').on('click', function () {
    var container = $(this).closest('.rename')
    var $textarea = container.find('textarea');

    $(this).hide();
    $(this).prev('a').show();
    $textarea.html($textarea.val()).changeElementType("h2");
});
CSS:

textarea {
    height: 30px;
    overflow: hidden;
    padding:0;
    border: 0;
    background:none;
    resize: none;
    font-size: 24px;
    text-align: center;
    font-family: $font-family;
    color: $white;
    height:30px;
}
div.rename {
    border:1px solid red;
    float:left;
    width:60%;
    margin:20px;
}
h2 {
    height:30px;
    color:red;
}
.confirm {
    display:none;
}