Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

CSS Tips and Tricks


CSS Tips and Tricks

CSS Selectors

.class1.class2<div class="name1 name2">...</div>Selects all elements with both name1 and name2 set within its class attribute
.class1 .class2<div class="name1"><div class="name2">Selects all elements with name2 that is a descendant of an element with name1
element,elementdiv, pSelects all <div> elements and all <p> elements
element elementdiv pSelects all <p> elements inside <div> elements

Background Images

.post-body {
  background-color: #336655;
  background-image: url("https://cdn12.picryl.com/photo/2016/12/31/cat-black-cat-black-animals-5ef19b-1024.jpg");
  background-repeat: no-repeat;
}

Use a CSS Class to Style an Element

<style>
  .blue-text {
    color: blue;
  }
</style>
<h2 class="blue-text">CatPhotoApp</h2>

Blockquotes CSS

blockquote {
 margin: 0;
 border-left: 3px solid #ccc;
}
blockquote p {
 padding: 15px;
 background: #eee;
 border-radius: 5px;
}
blockquote p::before {content: '\201C';}
blockquote p::after {content: '\201D';}

Table CSS

.post-body table {
  border-collapse: collapse;
}
.post-body table, th, td {
  border: 1px solid black;
}

Center

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

Remove SyntaxHighlighter Vertical Scrollbar


In previous post, I put java code in Syntax Highlighter pre section, noticed that there is an unnecessary vertical scollbar in Chrome(Works fine in Firefox) which only scrolls for one or two pixels. 

To fix this issue, edit Blogspot template, add the following code after stylesheet shCore.css:
.syntaxhighlighter {
 overflow-y: hidden !important; 
}

If you add previous code before loading shCore.css, the overflow definition .syntaxhighlighter {overflow: auto !important;} in shCore.css will overwrite previous definition.

If you want to hide horizontal scrollbar, add following code snippet after stylesheet shCore.css:
.syntaxhighlighter {
 overflow-y: hidden !important; 
}

Conditional Display of AdSense/Alternative Ads to Maximize Revenue


[Updated]Please read Display AdSense Ads Efficiently and Conditionally Show Alternative Ads, which introduce a simpler way to implement same function.

AdSense allows 3 standard ad units and 3 link units in one page. 
This is enough for us in most cases. But sometimes we want more flexibility: to be able to conditional display of AdSense Ads.

For example, in lifelongprogrammer.blogspot.com, we display 3 standard ad units in post: one at header, one at left-sidebar, and one inside the post body. 

But when view the homepage or archive page, label page, it's kind of messed up: it does display 3 standard ads, but one is at header as expected, the other two is inside the first two posts. The ads slot at left side bar is left blank. This makes the site looks ugly.

This is because AdSense fills up to 3 ads in the order of ads in html page. In blogspot template, the html order is: the header, then center post, left sidebar, then right sidebar.

I want to make sure the ad in left side bar always display and always display 3 AdSense Ads. So I make the following changes: 
At home, archive and label page, display alternative ads(I choose chitika) in post body, display 2 AdSense ads in left sidebar.
At post/item page, display AdSense ads in post, display one AdSense ads, one chitika ads in left sidebar..

Next, how to do it?
Conditional Display Ads in Post Body
Put the following code to the location where we want to display ads when write a post.
This checks whether the pathname of url matches /[0-9]{4}/[0-9]{2}/.* If so, means this is a post/item page, display AdSense ads, otherwise display chitika ads.

<script>
//<![CDATA[ 
var isItemPage = (document.location.pathname.match(new RegExp("/[0-9]{4}/[0-9]{2}/.*")) !=null);
if(isItemPage){
  document.write("<ins class='adsbygoogle' ... data-ad-client='' data-ad-slot=''></ins>
  <script>(adsbygoogle = window.adsbygoogle || []).push({});<\/script>")
} else {
    chitika-ads-code
}
//]]>  
</script>
Conditional Display Ads for the Second Ads in Left Sidebar
Add a HTML/JavaScript gadget at Left Sidebar in Layout page, put the following code in content text box.
It is not a post/item page, display AdSense ads, otherwise display chitika ad.
<script>
var isItemPage = (document.location.pathname.match(new RegExp("/[0-9]{4}/[0-9]{2}/.*")) !=null);
if(!isItemPage){
  document.write("Google-AdSense-Ads-Code");
}  else {
  chitika-ads-code
}
</script>
Now it looks much better, no blank ad, and always display 3 AdSense ads. Display alternative(chitika) ads for ads slot in post when at home, archive and label page.

For better performance, I will put the code which detect page type  before the start body tag, so it will be just executed one time.
<script>var isItemPage = (document.location.pathname.match(new RegExp("/[0-9]{4}/[0-9]{2}/.*")) !=null)</script>
Utilize Horizontal Link Units
dSense also allow us to put 3 links units, so I choose one above the header ad - this is easy, just add one HTML/JavaScript gadget, one below the title, one below post body, and I want these two only display in item/post page.

How to do it? Basically, it is to edit Blogspot template, put the following code below
and .
<script>
if(isItemPage){
  document.write("<ins class='adsbygoogle' ... data-ad-client='' data-ad-slot=''></ins>
  <script>(adsbygoogle = window.adsbygoogle || []).push({});<\/script>")
}</script>
Read more from
How To Insert Adsense Ads In Blogger Post

Change G+ Comments Widget Height


Height of G+ Comments Widget is set to 603px. It will occupy 603px even there is no or only 1, 2 comments: See the picture at right. This is a waste of page space.
As I place one ads below the post body, I would rather reader to view the ads rather view the empty space G+ Comments occupies.

Because G+ Comments Widget set the height property directly in the gpluscommentsdiv and iframe tag: the html code looks like below:
<div id="gpluscomments" class="cmt_iframe_holder" style="display: inline-block; width: 957px; height: 603px;"><iframe frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" style="position: static; top: 0px; width: 956px; margin: 0px; border-style: none; left: 0px; visibility: visible; height: 603px;"></iframe></div>

So I have to use javascript to change the height: to make it smaller.
The JavaScript code looks like below - css will not work here.


<script>
//<![CDATA[ 
function myOnLoad() {
 var gpluscomments = document.getElementById("gpluscomments");
 gpluscomments.style.height="400px";

 var gpluscommentsIFrame=document.querySelector('#gpluscomments iframe'); 
 gpluscommentsIFrame.style.height="400px"; 
}
window.onload = myOnLoad;
//]]>
</script> 
 


Go to dashboard -> "Template", Click "Edit Html", add the javascript code above before </html>
The code will be executed after page(DOM) is loaded, and change the height of gpluscomments and theinner iframe height to 400px.

Now the the widget is shorter, and when user clicks the comment text box, it will expand and show all comment.

Add Outbrain Related Posts Widget to Blogger and Remove Empty DIV


Adding Related Posts to your article/blog is an effective solution to increase site traffic. There are several Related Posts Widgets that wrks in Blogger such as Outbrain, LinkWithin. I chose Outbrain for my blogger. 

About how to add Outbrain Related Posts Widgets to Blogge, please read Get Outbrain Widget

After added it, it shows related posts.

But I noticed there is a white div section like below at the end of my post. 
So next I have to remove this empty div.
go to dashboard -> "Template", Click "Edit Html", add the following CSS code before </head>

Save the template, now the empty div is gone.

Use CSS to Remove Borders from Adsense Ads


In this article, I would like to introduce how to use CSS to remove the white borders from Adsense/Chitika Ads.

In previous article Use Javascript to Remove Borders from Adsense Ads, I introduced how to do this via javascript code.

But it has some disadvantage: if the network is slow, user may notice the white border appears and disappear. Not very user friendly.
Luckily we can do this via CSS - it;ss a much better solution.

CSS Code to Remove White Borders from Adsense Ads

<style type='text/css'>

#AdSense1, #AdSense2, #HTML1, #HTML2, #AdSense1 .widget-content, 
#AdSense2 .widget-content, #HTML1 .widget-content, #HTML2 .widget-content{

border:0px none transparent;
margin:0;
padding:0;

}

#AdSense1, #AdSense2, #HTML1, #HTML2{
background-color: inherit;
}

</style>


AdSense1, AdSense2 are ids of Adsense Ads, HTML1, HTML2 are ids of Chitika Ads.

Now we can see the white border is gone.
For more information. please view Use Javascript to Remove Borders from Adsense Ads

Use Javascript to Remove Borders from Adsense Ads


In this article, I would like to introduce how to use javascript to remove the white borders from Adsense/Chitika Ads.
In a later post, I will provide a better solution: use CSS to remove the white borders. 

Recently I joined in Google Adsense and Chitika to try to make some money from my humble technical blog.
After add ads to the blog, I notice there is big white border around the Ads. I have to remove the border as it makes the site ugly.



  

Google Adsense or chitika doesn't provide us an option to remove ads directly. So I have to write some javascript to remove the borders around ads.
The JavaScript code looks look below:
<script>
//<![CDATA[
var adStrs = ["AdSense1","AdSense2", "HTML1" , "HTML2"];
for (var i=0,len=adStrs.length; i<len; i++)
{
 var adStr = adStrs[i];
 var ad=document.getElementById(adStr);
 ad.style.backgroundColor="inherit";
 ad.style.border="0px none transparent";
 ad.style.margin=0;
 ad.style.padding=0; 
 var widget=document.querySelector('#' + adStr + ' .widget-content'); 
 widget.style.border="0px none transparent";
 widget.style.margin=0;
 widget.style.padding=0;
}
//]]>
</script>
AdSense1, AdSense2 are ids of Adsense Ads, HTML1, HTML2 are ids of Chitika Ads.
Next login to blogger, go to dashboard -> "Template", Click "Edit Html", at the end of page(before </html> tag) and paste the code above.
-- Here I also use CDATA tag to avoid have to escape special xml characters.

Now the ads looks like below. Now it looks much better.


You may also view: Use CSS to Remove Borders from Adsense Ads

Labels

ANT (6) Algorithm (69) Algorithm Series (35) Android (7) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) JSON (7) Java (186) JavaScript (27) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) adsense (5) bat (8) regex (5) xml (5)