Displaying responsive Google Ads - CSS problems - html

I'm trying to display responsive Google Ads on my website. The box that includes Google Ads' code is responsive as well and it transforms itself to different sizes based on Google's Ad unit sizes.
So for different screen sizes, I show fitting ad sizes from that page, like so (Complete LESS for the ad space):
.ads {
width: 733px;
height: 116px;
margin: 70px 0 0 30px;
border: 2px solid #dry-red;
background-color: white;
padding: 0;
border-radius: 4px;
margin-right: 15px;
float: right;
.headline {
background-color: #dry-red;
width: 100%;
font-size: .8em;
padding: 2.5px 0 2.5px 4px;
color: white;
margin: 0;
border-bottom: 1px solid #dry-red-xl;
}
#media (max-width: 1200px){
width: 468px;
height: 86px;
}
#media (max-width: 992px){
width: 323px;
height: 126px;
}
#media (max-width: 768px){
width: 468px;
height: 61px;
margin: 50px 20px;
float: none;
}
#media (max-width: 766px){
margin: 5px auto 5px auto;
}
#media (max-width: 470px){
/* display: none; */
width: 323px;
height: 126px;
}
}
So far so good. The box transforms when it reaches its peak. Note that the original ad sizes from Google were increased for about 26px+ in order to fit along with .headline.
So now, I've placed Google Ads unit's code in the HTML like this:
<section class="ads col-lg-8 col-md-7 col-sm-7">
<div class="headline">Advertisement</div>
<!-- IMGzer_Header -->
<style type="text/css">
.adslot_1 { width: 728px; height: 90px; }
#media (max-width:470px) { .adslot_1 { width: 320px; height: 100px; } }
#media (max-width:768px) { .adslot_1 { width: 468px; height: 60px; } }
#media (max-width:992px) { .adslot_1 { width: 320px; height: 100px; } }
#media (max-width:1200px) { .adslot_1 { width: 468px; height: 60px; } }
</style>
<ins class="adsbygoogle adslot_1" style="display:inline-block" data-ad-client="SOME_NUMBER" data-ad-slot="SOME_NUMBER"></ins>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</section>
As you might notice, I've tried using two variants:
Using CSS3 media queries (which are commented)
And using the data-ad-format="horizontal" data attribute.
I've read about these two in this Google Ads article.
None of these actually worked and my ad size remained 728x90px. My ads from Google simply wont resize to their respective size when the browser screen is smaller.
Ideas and help would be appreciated.
My actual website having this problem
Update
So I've played around and still achieved nothing. I tried to get closer to Google's examples and use min-width in the media queries instead of max-width. No results. I'm still getting the 728x90 sizes even though it's not included anywhere else except the min-width: 1200px media query and it still refuses to resize. This is really irritating.
<section class="ads col-lg-8 col-md-7 col-sm-7">
<div class="headline">Advertisement</div>
<style>
.adslot_1 { width: 320px; height: 100px; }
#media (min-width: 768px) { .adslot_1 { width: 468px; height: 60px; } }
#media (min-width: 992px) { .adslot_1 { width: 320px; height: 100px; } }
#media (min-width: 1200px) { .adslot_1 { width: 728px; height: 90px; } }
</style>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- IMGzer_Header -->
<ins class="adsbygoogle adslot_1" style="display:inline-block" data-ad-client="ca-pub-1234" data-ad-slot="XXXXXX"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</section>
<div class="clearfix"></div>
The CSS/LESS for .ads is still the same as above. The thing that I've noticed is that when I check my code via Firebug, I see this line:
<ins data-ad-slot="XXXXX" data-ad-client="ca-pub-1234" style="display: inline-block; width: 728px; height: 90px;" class="adsbygoogle adslot_1" data-adsbygoogle-status="done"><ins style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent"><ins style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent" id="aswift_0_anchor"><iframe width="728" scrolling="no" height="90" frameborder="0" style="left:0;position:absolute;top:0;" name="aswift_0" id="aswift_0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0"></iframe></ins></ins></ins>
Where did this code get the width and height? The width is 728 and the height is 90. It's like they're fixed there somehow and they are ignoring my media queries totally. I'm absolutely certain that I choose Responsive unit in the AdSense settings.
What is it that I'm not seeing?

the class (.) is less than the ID(#) (terms of styles).
google ads have their own styles written in JS.
<div class="least" id="medium"></div>
<style type="text/css">
.least{
width:100px;
height:100px;
background-color:red;
}
#medium{
width:200px;
height:200px;
background-color:green;
}
</style>
the best way to handle styles google ads
<div id="google-ads-1"></div>
//Calculate the width of available ad space:
<script type="text/javascript">
ad = document.getElementById('google-ads-1');
if (ad.getBoundingClientRect().width) {
adWidth = ad.getBoundingClientRect().width; // for modern browsers
} else {
adWidth = ad.offsetWidth; // for old IE
}
/* Replace ca-pub-XXX with your AdSense Publisher ID */
google_ad_client = "ca-pub-XXX";
/* Replace 1234567890 with the AdSense Ad Slot ID */
google_ad_slot = "1234567890";
/* Do not change anything after this line */
if ( adWidth >= 728 )
google_ad_size = ["728", "90"]; /* Leaderboard 728x90 */
else if ( adWidth >= 468 )
google_ad_size = ["468", "60"]; /* Banner (468 x 60) */
else if ( adWidth >= 336 )
google_ad_size = ["336", "280"]; /* Large Rectangle (336 x 280) */
else if ( adWidth >= 300 )
google_ad_size = ["300", "250"]; /* Medium Rectangle (300 x 250) */
else if ( adWidth >= 250 )
google_ad_size = ["250", "250"]; /* Square (250 x 250) */
else if ( adWidth >= 200 )
google_ad_size = ["200", "200"]; /* Small Square (200 x 200) */
else if ( adWidth >= 180 )
google_ad_size = ["180", "150"]; /* Small Rectangle (180 x 150) */
else
google_ad_size = ["125", "125"]; /* Button (125 x 125) */
document.write (
'<ins class="adsbygoogle" style="display:inline-block;width:'
+ google_ad_size[0] + 'px;height:'
+ google_ad_size[1] + 'px" data-ad-client="'
+ google_ad_client + '" data-ad-slot="'
+ google_ad_slot + '"></ins>'
);
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
see documentation easy.

Related

shows different screen on multiple device width responsiveness error

<html>
<head>
<style>
html,body{
margin: 0;
padding: 0;
overflow: hidden;
}
.main-container-pg{
color: #fff;
}
.main-container-pg:before{
content: '';
margin: 0;
padding: 0;
left: 0;
height: 100vh;
width: 100vw;
background-image: url('https://i.ibb.co/c1cKTkc/bg-mid.png');
background-size: 104% 100% ;
background-repeat: no-repeat;
background-position: center;
display: block;
position: absolute;
z-index: -1;
}
.logo-box-pg{
width: 100%;
display: flex;
justify-content: center;
}
.logo-head-pg{
height: calc(100% - 82%);
}
.main-text-pg{
display: flex;
font-size: 1.4em;
margin: auto;
justify-content: center;
flex-direction: column;
align-items: center;
padding-top: 1em;
}
.input-pg-box{
padding-top: 3em;
display: flex;
justify-content: center;
}
.input-pg-box *{
font-size: 1em;
}
.input-pg-box .pg-button-icon{
background-color: #6BB9E6;
border: none;
width: 10.625em;
border-radius: 30px;
margin-left: 1em;
height: 3.5em;
outline: none;
display: flex;
align-items: center;
justify-content: space-around;
padding-left: 1em;
padding-right: 1em;
}
.drop-shadow{
filter: drop-shadow(10px 20px 15px rgba(0, 0, 0, 0.161));
}
.input-email-pg{
width: 25%;
height: 3.5em;
outline: none;
border: none;
border-radius: 30px;
color: #6BB9E6;
padding-left: 10px;
}
.text-width{
width: calc(10.625em + 22%);
}
/* Extra small devices (phones, 600px and down) */
#media only screen and (max-width: 600px) {
.input-email-pg{
background-color: red;
}
}
/* Small devices (portrait tablets and large phones, 600px and up) */
#media only screen and (min-width: 600px) {
.input-email-pg{
background-color: green;
}
}
/* Medium devices (landscape tablets, 768px and up) */
#media only screen and (min-width: 768px) {
.input-email-pg{
background-color: blue;
}
/*
1. Logo Position
2. Text Position
3. input Field Postition
*/
}
/* Large devices (laptops/desktops, 992px and up) */
#media only screen and (min-width: 992px) {
.input-email-pg{
background-color: orange;
}
.main-container-pg:before{
background-size: 134% 100%;
}
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
#media only screen and (min-width: 1200px) {
.input-email-pg{
background-color: black;
}
.main-container-pg:before{
background-size: 104% 100%;
}
}
</style>
</head>
<body>
<div class="main-container-pg">
<div class="logo-box-pg">
<img src="logo.png" class="logo-head-pg">
</div>
<div class="main-text-pg">
<div class="text-width">
<h2 class="">
Partyguru sta arrivando per <br /> rivoluzionare il mondo degli<br />eventi privati
</h2>
</div>
<div class="text-width">
<h2>
Vuoi saperne di più? <br />Iscriviti alla nostra newsletter
</h2>
</div>
</div>
<div class="input-pg-box">
<input type="email" placeholder="Indirizzo E-mail" class="input-email-pg drop-shadow"/>
<button class="pg-button-icon drop-shadow">
<svg width="34" height="34" viewBox="0 0 34 34" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M19.3942 22.005C19.1356 22.8099 18.9371 23.6328 18.8002 24.467C18.3922 26.533 17.9462 28.858 16.0792 30.726C15.2055 31.6005 14.1681 32.2942 13.0262 32.7675C11.8843 33.2409 10.6603 33.4845 9.42416 33.4845C8.18804 33.4845 6.96405 33.2409 5.82214 32.7675C4.68023 32.2942 3.6428 31.6005 2.76916 30.726C1.61617 29.5948 0.785058 28.1771 0.36117 26.6184C-0.062717 25.0598 -0.064092 23.4164 0.357173 21.857C0.777483 20.253 1.61619 18.7893 2.78756 17.6157C3.95894 16.442 5.42095 15.6004 7.02416 15.177L8.12417 19.209C7.22959 19.4445 6.41356 19.9132 5.75946 20.5673C5.10535 21.2214 4.63663 22.0374 4.40118 22.932C4.16711 23.7853 4.16795 24.686 4.40365 25.5388C4.63935 26.3917 5.10113 27.165 5.74017 27.777C6.22562 28.2637 6.80231 28.6498 7.43722 28.9133C8.07213 29.1767 8.75278 29.3123 9.44018 29.3123C10.1276 29.3123 10.8082 29.1767 11.4431 28.9133C12.078 28.6498 12.6547 28.2637 13.1402 27.777C14.0922 26.825 14.3772 25.365 14.7112 23.67C14.8352 23.027 14.9712 22.359 15.1442 21.691L19.3942 22.005Z"
fill="white" />
<path
d="M8.11506 19.1999C8.64706 19.0519 9.21506 18.9399 9.82206 18.8169C11.8881 18.3959 14.2221 17.9169 16.0691 16.0709C17.3851 14.7545 18.2813 13.0775 18.6443 11.2518C19.0074 9.42609 18.821 7.53375 18.1087 5.814C17.3964 4.09424 16.1902 2.62428 14.6426 1.58994C13.095 0.555595 11.2755 0.00332359 9.41407 0.00292969C9.27808 0.00292969 9.15408 0.0149236 9.01408 0.0279236C6.57833 0.12419 4.27553 1.16411 2.59272 2.92772C0.909922 4.69133 -0.0209178 7.04033 -0.00291755 9.47792C0.0103367 11.4978 0.676868 13.4592 1.89708 15.0689C2.99775 14.1597 4.25544 13.4595 5.60807 13.0029C4.92803 12.2867 4.46526 11.3921 4.2736 10.4232C4.08194 9.45431 4.16927 8.45092 4.5254 7.52969C4.88153 6.60847 5.49184 5.80725 6.28538 5.21922C7.07892 4.63119 8.02307 4.28051 9.00807 4.20793C9.14407 4.19593 9.26806 4.18292 9.40806 4.18292C10.0956 4.18093 10.7768 4.31551 11.4119 4.57884C12.0471 4.84217 12.6236 5.22903 13.1081 5.71693C14.0884 6.7006 14.6388 8.03271 14.6388 9.42143C14.6388 10.8102 14.0884 12.1423 13.1081 13.1259C12.1551 14.0779 10.6841 14.3749 8.97706 14.7339C8.33406 14.8699 7.67708 14.9939 7.02308 15.1789L8.11506 19.1999Z"
fill="white" />
<path
d="M30.7261 17.416C28.9608 15.6516 26.567 14.6604 24.0711 14.6604C21.5752 14.6604 19.1815 15.6516 17.4161 17.416C16.2809 18.6034 15.4936 20.0797 15.1401 21.684L19.4081 22.006C19.6067 21.3951 19.9407 20.8369 20.3851 20.373C21.3688 19.3927 22.7009 18.8423 24.0896 18.8423C25.4784 18.8423 26.8105 19.3927 27.7941 20.373C28.2807 20.8556 28.6668 21.4298 28.9301 22.0625C29.1934 22.6951 29.3286 23.3737 29.3281 24.059V24.071C29.3333 25.0876 29.0409 26.0835 28.4867 26.9359C27.9326 27.7882 27.1411 28.4597 26.2098 28.8675C25.2785 29.2753 24.2483 29.4015 23.2461 29.2307C22.2439 29.0598 21.3137 28.5993 20.5701 27.906C20.1569 29.2759 19.4896 30.5558 18.6031 31.679C20.1957 32.8473 22.12 33.4759 24.0951 33.473H24.1201C26.6119 33.4651 28.9989 32.47 30.7585 30.7057C32.5181 28.9414 33.5068 26.5518 33.5081 24.06V24.048C33.4939 21.5569 32.4932 19.1729 30.7251 17.418"
fill="white" />
<path
d="M23.045 0.0249023H18.876C18.8813 3.89756 20.422 7.6101 23.1604 10.3485C25.8988 13.0869 29.6113 14.6276 33.484 14.6329V10.4649C30.7153 10.4644 28.0601 9.36429 26.1024 7.40652C24.1446 5.44876 23.0445 2.7936 23.044 0.0249023"
fill="white" />
</svg>
<span style="color: #fff;"> iscriviti </span>
</button>
</div>
</div>
</body>
</html>
I already use media query in this design but not working with my code
When I am testing this on multiple devices then its shows different results please help me to make it responsive
when width of screen is changed then the buttons color will change help me to resolve this
I am new to web designing please help me to resolve this

width doesn't change in css

hello I have made a pop up effect in css it is a div that contains two floating divs the first one width is 34.8% of the main pop up and the second one is 65%
now I want to change the design of the whole pop up once the screen get smaller so I used a media query to change the width and rearrange the elements ;however it won't change I think its because of the float thing here is the code please help
<div class="overlay"></div>
<div class="popup">
<div id="itemphoto">
<img src="../images/stuff%20for%20sale/1.jpg" alt="none">
<p id="itemtitle">IPhone 6s</p>
</div>
<div id="itemdesc">
<p><span>Seller :</span> 116825349</p>
<p><span>Seller Rate :</span> Trusted</p>
<p><span>Lower Bid : </span>250</p>
<p><span>Bidders :</span> 30</p>
<p><span>Higher Bid : </span>366</p>
<p><span>Brief Description :</span> SAN FRANCISCO — September 9, 2015 — Apple® today announced iPhone® 6s and iPhone 6s Plus, the most advanced iPhones ever, adding a powerful new dimension to iPhone’s revolutionary Multi-Touch™ interface. The new iPhones introduce 3D Touch, which senses force to enable intuitive new ways to access features and interact with content. iPhone 6s and iPhone 6s Plus feature Retina® HD displays made from the strongest glass on any smartphone and 7000 series aluminum, the same alloy used in the aerospace industry, in gorgeous metallic finishes that now include rose gold.</p>
</div>
<button id="bid">BID</button>
<button id="notify">NOTIFY ME</button>
<button id="close">CLOSE</button>
</div>
.popup
{
width: 70%;
height: 550px;
background: #292929;
z-index: 1001;
position: fixed;
top :10%;
left: 15%;
border-radius: 15px;
/* display: none; */
}
.popup #itemphoto
{
width: 34.8%;
margin-right: 0.2%;
height: 450px;
float: left;
border-top-left-radius: 15px;
overflow: hidden;
background: #EFF1F2;
}
.popup #itemdesc
{
width: 65%;
height: 450px;
float: left;
border-top-right-radius: 15px;
padding: 20px;
overflow-y: scroll;
background: #EFF1F2;
}
/*media queries*/
#media screen and (max-width:1000px)
{
.popup
{
width:80%;
left: 10%;
}
#itemphoto
{
float: none;
width: 100%;
}
}
change your media query to:
.popup #itemphoto {
float: none;
width: 100%;
}
As it's more specific than #itemphoto, it's keeping the properties of the main css, changing it to this will make it apply correctly.
https://jsfiddle.net/h5wbjcsq/

Bootstrap grid for printing

I would like to design a report page with a different layout for printing to mobile. I am using bootstrap v3. It seems the grid can't differentiate between the two as the breakpoint for printing is the same as the breakpoint for mobile (xs)
For example: In the below test html my printed page (or print preview) shows the xs6 columns side by side but the sm6 columns stacked. There isn't a breakpoint between xs and sm.
Surely my printed page is wider than my mobile viewport so shouldn't it use the sm layout?
Am I doing something wrong or is this the way it is? Is there a defined viewport width for printing?
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-6">
xs6
</div>
<div class="col-xs-6">
xs6
</div>
</div>
<div class="row">
<div class="col-sm-6">
sm6
</div>
<div class="col-sm-6">
sm6
</div>
</div>
</div>
</body>
</html>
What I did was to manually recreate those columns classes in my print css.
.col-print-1 {width:8%; float:left;}
.col-print-2 {width:16%; float:left;}
.col-print-3 {width:25%; float:left;}
.col-print-4 {width:33%; float:left;}
.col-print-5 {width:42%; float:left;}
.col-print-6 {width:50%; float:left;}
.col-print-7 {width:58%; float:left;}
.col-print-8 {width:66%; float:left;}
.col-print-9 {width:75%; float:left;}
.col-print-10{width:83%; float:left;}
.col-print-11{width:92%; float:left;}
.col-print-12{width:100%; float:left;}
Then I just use those classes like I use bootstrap classes to make my columns for print only. I also created .visible-print and .hidden-print to hide/show elements only in the print version.
It still needs some work, but that quick patch helped me a lot.
If you want the Bootstrap's grid do not print with col-xs (mobile settings) , and want to use col-sm-?? instead , Based on Fredy31 answer and you don't even need to define col-print-??. simply rewrite all col-md-?? css class definitions inside a: #media print { /* copy and paste from bootstrap.css*/ } like this:
#media print {
.col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
float: left;
}
.col-sm-12 {
width: 100%;
}
.col-sm-11 {
width: 91.66666667%;
}
.col-sm-10 {
width: 83.33333333%;
}
.col-sm-9 {
width: 75%;
}
.col-sm-8 {
width: 66.66666667%;
}
.col-sm-7 {
width: 58.33333333%;
}
.col-sm-6 {
width: 50%;
}
.col-sm-5 {
width: 41.66666667%;
}
.col-sm-4 {
width: 33.33333333%;
}
.col-sm-3 {
width: 25%;
}
.col-sm-2 {
width: 16.66666667%;
}
.col-sm-1 {
width: 8.33333333%;
}
}
The Sass version of Fredy31 solution:
#for $i from 1 through 12 {
.col-print-#{$i} {
width: #{percentage(round($i*8.33)/100)};
float: left;
}
}
For Bootstrap 4 (using SASS)
#each $breakpoint in map-keys($grid-breakpoints) {
#include media-breakpoint-up($breakpoint) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
#for $i from 1 through $grid-columns {
#media print {
.col-print#{$infix}-#{$i} {
#include make-col($i, $grid-columns);
}
}
}
}
}
will create
#media print {
.col-print-1 {
flex: 0 0 8.33333%;
max-width: 8.33333%; } }
#media print {
.col-print-2 {
flex: 0 0 16.66667%;
max-width: 16.66667%; } }
#media print {
.col-print-3 {
flex: 0 0 25%;
max-width: 25%; } }
#media print {
.col-print-4 {
flex: 0 0 33.33333%;
max-width: 33.33333%; } }
#media print {
.col-print-5 {
flex: 0 0 41.66667%;
max-width: 41.66667%; } }
#media print {
.col-print-6 {
flex: 0 0 50%;
max-width: 50%; } }
#media print {
.col-print-7 {
flex: 0 0 58.33333%;
max-width: 58.33333%; } }
#media print {
.col-print-8 {
flex: 0 0 66.66667%;
max-width: 66.66667%; } }
#media print {
.col-print-9 {
flex: 0 0 75%;
max-width: 75%; } }
#media print {
.col-print-10 {
flex: 0 0 83.33333%;
max-width: 83.33333%; } }
#media print {
.col-print-11 {
flex: 0 0 91.66667%;
max-width: 91.66667%; } }
#media print {
.col-print-12 {
flex: 0 0 100%;
max-width: 100%; } }
Your switch styles like this
<div class="row">
<div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
<div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
<div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
</div>
See
#grid-example-mixed or #grid-example-mixed-complete
and may you need to clearfix
<!-- Add the extra clearfix for only the required viewport -->
<div class="clearfix visible-xs"></div>
#grid-responsive-resets
Edit: 04/2019
Since Bootstrap 4.x there are new classes that can be used to set the display behavior when printing. SEE 4.3 Docs
I had a similar problem, for me the easiest solution was to manually modify the width for elements I wanted to appear differently when printed (and I added a specific class -in my case: title-container, details-container for those, along the col-xs-6 etc.).
For example:
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-xs-12 col-ms-3 col-sm-6 col-md-6 title-container">
Some stuff
</div>
<div class="col-xs-12 col-ms-9 col-sm-6 col-md-6 details-container">
Some more stuff
</div>
</div>
</div>
</div>
#media print {
.title-container {
width: 360px;
float: left;
}
.details-container {
width: 300px;
float: right;
}
}
In my case I needed a column to be floated on the right, one to the left, thus the floats...
You could set in your custom css the width also for .col-xs-6 etc. just a quick and dirty solution, but did the job for a page where I needed this...
Instead of recreating with new column names like .col-print-1 , .col-print-2 , write a media query which will be enable while printing the document.
#media print {
.col-md-1,.col-md-2,.col-md-3,.col-md-4,
.col-md-5,.col-md-6,.col-md-7,.col-md-8,
.col-md-9,.col-md-10,.col-md-11,.col-md-12 {
float: left;
}
.col-md-1 {
width: 8%;
}
.col-md-2 {
width: 16%;
}
.col-md-3 {
width: 25%;
}
.col-md-4 {
width: 33%;
}
.col-md-5 {
width: 42%;
}
.col-md-6 {
width: 50%;
}
.col-md-7 {
width: 58%;
}
.col-md-8 {
width: 66%;
}
.col-md-9 {
width: 75%;
}
.col-md-10 {
width: 83%;
}
.col-md-11 {
width: 92%;
}
.col-md-12 {
width: 100%;
}
}
So by this way we can be able to apply print css styles directly without changing the column names.
The following works great to create grid elements specific for print media. Using Bootstrap 3.
#media print {
.make-grid(print);
}
Then you can use all the grid col elements with the print keyword. Ex: col-print-6 col-print-offset-2, etc.
Maybe you could use Bootstrap 2. If you are familiar with Bootstrap 2, then you can use it as an alternative, as this offers non responsive CSS. Bootstrap 2 was not mobile first, you had to add an extra style sheet to make your web pages responsive.
Or you can add clearfixes for the mobile part. See http://getbootstrap.com/css/#grid-responsive-resets
And the SASS version of Ehsan Abidi's answer using MiCc83's answer:
#for $i from 1 through 12 {
.col-sm-#{$i} {
width: #{percentage(round($i*8.33)/100)};
float: left;
}
}
I prefer this because I always spec the "sm" size and that most closely approximates a print page in my applications. Then I only need to add something specifically for print when I've got an outlier condition.
If you only have 2 columns, you can try it. I fixed it with the code below.
<div class="row">
<div class="w-50 p-3 float-left">
</div>
<div class="w-50 p-3 float-right">
</div>
</div>
If it's just one line of text in two columns you can use the accepted answer here.
For Bootstrap 4 sass here are some snippets that I applied across several projects. These are making adjustments on:
Grid (follows the LG breakpoint)
Spacers (rewrite all below LG margins/paddings)
Buttons (since background-colors does not work in print preview, switch filled buttons to outline)
Display (rewrite all below LG displays)
Text alignments (rewrite all below LG breakpoint)
#media print {
$grid-breakpoints-print: (lg: 992px); // keep breakpoint that you would like to apply for print
/* Rewrite margins, padding, display & alignment to keep the LG and not the mobile ones */
#each $breakpoint in map-keys($grid-breakpoints-print) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints-print);
// rewrite all displays for your print breakpoint
#each $value in $displays {
.d#{$infix}-#{$value} {
display: $value !important;
#each $v in $displays {
&.d-#{$v} {
display: $value !important;
&.d-print-none,
&.table__sort {
display: none !important;
}
}
}
&.d-print-none {
display: none !important;
}
}
}
// rewrite all spacings for your print breakpoint
#each $prop, $abbrev in (margin: m, padding: p) {
#each $size, $length in $spacers {
.#{$abbrev}#{$infix}-#{$size} {
#{$prop}: $length !important;
#each $s, $l in $spacers {
&.#{$abbrev}-#{$s},
&.#{$abbrev}-auto {
#{$prop}: $length !important;
}
}
}
.#{$abbrev}t#{$infix}-#{$size},
.#{$abbrev}y#{$infix}-#{$size} {
#{$prop}-top: $length !important;
#each $s, $l in $spacers {
&.#{$abbrev}t-#{$s},
&.#{$abbrev}y-#{$s},
&.#{$abbrev}t-auto,
&.#{$abbrev}y-auto {
#{$prop}-top: $length !important;
}
}
}
.#{$abbrev}r#{$infix}-#{$size},
.#{$abbrev}x#{$infix}-#{$size} {
#{$prop}-right: $length !important;
#each $s, $l in $spacers {
&.#{$abbrev}r-#{$s},
&.#{$abbrev}x-#{$s},
&.#{$abbrev}r-auto,
&.#{$abbrev}x-auto {
#{$prop}-right: $length !important;
}
}
}
.#{$abbrev}b#{$infix}-#{$size},
.#{$abbrev}y#{$infix}-#{$size} {
#{$prop}-bottom: $length !important;
#each $s, $l in $spacers {
&.#{$abbrev}b-#{$s},
&.#{$abbrev}y-#{$s},
&.#{$abbrev}b-auto,
&.#{$abbrev}y-auto {
#{$prop}-bottom: $length !important;
}
}
}
.#{$abbrev}l#{$infix}-#{$size},
.#{$abbrev}x#{$infix}-#{$size} {
#{$prop}-left: $length !important;
#each $s, $l in $spacers {
&.#{$abbrev}l-#{$s},
&.#{$abbrev}x-#{$s},
&.#{$abbrev}l-auto,
&.#{$abbrev}x-auto {
#{$prop}-left: $length !important;
}
}
}
}
}
// rewrite all text alignments for your print breakpoint
.text#{$infix}-left {
text-align: left !important;
&.text-left,
&.text-right,
&.text-center {
text-align: left !important;
}
}
.text#{$infix}-right {
text-align: right !important;
&.text-left,
&.text-right,
&.text-center {
text-align: right !important;
}
}
.text#{$infix}-center {
text-align: center !important;
&.text-left,
&.text-right,
&.text-center {
text-align: center !important;
}
}
}
/* Rewrite grid to keep the LG and discard the mobile */
#for $i from 1 through 12 {
.col-lg-#{$i} {
flex: 0 0 #{percentage(round($i*8.33)/100)} !important;
max-width: #{percentage(round($i*8.33)/100)} !important;
#for $k from 1 through 12 {
&.col-xs-#{$k},
&.col-sm-#{$k},
&.col-md-#{$k},
&.col-#{$k} {
flex: 0 0 #{percentage(round($i*8.33)/100)} !important;
max-width: #{percentage(round($i*8.33)/100)} !important;
}
}
}
}
/* Since the print will not fill background-colors you need to transform filled buttons into outline */
#each $color, $value in $theme-colors {
.btn-#{$color} {
color: $value !important;
background-color: $white !important;
}
}
}
Here is a working Fiddle. Keep in mind that only #media print { ... } matters in the fiddle example. Variables from the beginning where copied to have a working fiddle.
Based on Fredy31's answer using Bootstrap's exact widths and removing repetition of float left.
[class^="col-print"] {float:left;}
.col-print-1 {width:8.33333333%;}
.col-print-2 {width:16.66666667%;}
.col-print-3 {width:25%;}
.col-print-4 {width:33.33333333%;}
.col-print-5 {width:41.66666667%;}
.col-print-6 {width:50%;}
.col-print-7 {width:58.33333333%;}
.col-print-8 {width:66.66666667%;}
.col-print-9 {width:75%;}
.col-print-10{width:83.33333333%;}
.col-print-11{width:91.66666667;}
.col-print-12{width:100%;}

How to fix Wordpress admin bar destroying 100% height

I'm creating a web site that fits to the screen with Wordpress.
When the site owner logs in, the admin bar will appear but it adds the following style:
html{ margin-top: 28px !important; }
This causes a vertical scroll bar to appear. Is there any way to fix this using just CSS?
Someone had a similar issue but he got no answer.
My relevant html structure:
<html>
<body>
<div id="page">
<div class="site-main" id="main">
<div class="content-area" id="primary">
<div role="main" class="site-content" id="content">
</div><!-- #content .site-content -->
</div><!-- #primary .content-area -->
</div><!-- #main .site-main -->
</div><!-- #page -->
<div id="wpadminbar">
</div>
</body>
</html>
And relevant CSS:
html, body, #page {
width: 100%;
height: 100%;
min-width: 350px;
margin: 0;
padding: 0;
}
#main {
height: 100%;
}
#primary {
float: right;
width: 100%;
margin-left: -200px;
height: 100%;
}
#content {
margin-left: 250px;
height: 100%;
}
For the admin bar:
#wpadminbar {
height: 28px;
left: 0;
min-width: 600px;
position: fixed;
top: 0;
width: 100%;
z-index: 99999;
}
I've tried using (negative) margins and paddings, also setting the admin bar's position to absolute instead of fixed but no luck.
2022 here.
I recently noticed that WordPress has been setting a relevant CSS variable on the front-end when logged in as an admin:
html {
--wp-admin--admin-bar--height: 32px;
scroll-padding-top: var(--wp-admin--admin-bar--height);
}
#media screen and (max-width: 782px)
html {
--wp-admin--admin-bar--height: 46px;
}
}
This is very convenient, as it allows you to do things like this (making a hero element as high as the viewport, but subtracting the height of the admin bar):
.hero {
min-height: calc(100vh - var(--wp-admin--admin-bar--height));
}
There is a slight issue with that, though: If you’re not signed in as an admin, the CSS variable is not set, and this can break things like calculations.
This can be fixed by checking for a body class that WordPress also sets:
.hero {
min-height: 100vh;
}
body.admin-bar .hero {
min-height: calc(100vh - var(--wp-admin--admin-bar--height));
}
…but this can get messy rather quickly, so I came up with a little one-size-fits-all solution that I’ll probably be using a lot:
body:not(.admin-bar) {
--wp-admin--admin-bar--height: 0px;
}
This allows me to do things like this, and not having to worry about whether the user is logged in or not:
.site-header {
top: var(--wp-admin--admin-bar--height);
}
Look into wordpress/wp-includes/class-wp-admin-bar.php at the beginning, and you will find this. Watch closely the comments for the actual answer:
if ( current_theme_supports( 'admin-bar' ) ) {
/**
* To remove the default padding styles
* from WordPress for the Toolbar,
* use the following code:
* add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );
*/
$admin_bar_args = get_theme_support( 'admin-bar' );
$header_callback = $admin_bar_args[0]['callback'];
}
if ( empty($header_callback) )
$header_callback = '_admin_bar_bump_cb';
add_action('wp_head', $header_callback);
wordpress/wp-includes/admin-bar.php contains the default implementation of _admin_bar_bump_cb:
/**
* Default admin bar callback.
*
* #since 3.1.0
*/
function _admin_bar_bump_cb() { ?>
<style type="text/css" media="screen">
html { margin-top: 28px !important; }
* html body { margin-top: 28px !important; }
</style>
<?php
}
In your php code (on the page where you DON'T want the admin bar to appear), just add the following:
add_filter('show_admin_bar', '__return_false');
See here: http://davidwalsh.name/hide-admin-bar-wordpress
Try following:
add_action('get_header', 'fix_adminbar');
function fix_adminbar()
{
if (is_admin_bar_showing()) {
remove_action('wp_head', '_admin_bar_bump_cb');
add_action(
'wp_head', function () {
ob_start();
_admin_bar_bump_cb();
$code = ob_get_clean();
$code = str_replace('margin', 'padding', $code);
$code = preg_replace('/{/', '{ box-sizing: border-box;', $code, 1);
echo $code;
}
);
}
}```

div height 100%

I know this has been asked a million times, but I haven't had much luck making it work.I'm working on a Posterous layout, and I'm trying to get my may content to flow to the bottom.
Here is a link to the layout so far
You can see that the content is held within center_col, so I basically need this column to stretch to the bottom of the page/window regardless of how much content is in there.
Here is the current HTML:
<!DOCTYPE html>
<html>
<head>
<title>{Title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
* {margin:0;padding:0}/* mac hide \*/
html { height: 100%;}
* html #wrap {height: 100%;}/* end hide */
body {
background: #FFFFFF;
color: #fff;
height:100%;
min-width:800px;}
#inner {
position:relative;
width:100%
}
#wrap {
min-height: 100%;
margin-left:50%;
position:relative;
background:#F9F9F9;
color:#000;
z-index:1
}
#center_col {
float: left;
width: 800px;
height: auto;
margin-left:-380px;/* drag it central on the page*/
position:relative;
display:inline;/* ie double margin bug*/
background:#FFFFFF;
}
#sidebar {
width:204px;
height: 100%;
padding-right:26px;
float:left;
min-height:234px;
position:relative;
background: #FFFFFF;
}
#content {
width:570px;
height: 100%;
min-height: 100%;
position:relative;
float:left;
background: #F9F9F9 url('http://www.rockettree.com/images/bg-content.png') left top repeat-y;
padding-top:21px;
padding-bottom:48px;
}
.postunit {
width: 500px;
margin-left: 30px;
padding: 10px 5px 20px 5px;
background: #FFFFFF;
border: 1px solid #F9F9F9;
}
.sidebar {
border: 1px solid #000000;
background-color: #FFFFFF;
margin-top: 50px;
padding-left: 10px;
float: left;
height: auto;
width:200px;
-moz-border-radius: 10px;
border-radius: 10px;
}
p{
padding:5px;
margin-bottom:1em;
}
</style>
<!--[if IE]><style type="text/css">body {width:expression( documentElement.clientWidth < 802 ? (documentElement.clientWidth == 0 ? (body.clientWidth < 802 ? "802" : "auto") : "802px") : "auto" );}</style><![endif]-->
</head>
<body>
<div id="wrap">
<div id="inner">
<div id="center_col">
<div id="sidebar">
<div class="header">
<h1>{Title}</h1>
<p>{Description}</p>
</div>
{block:ListSidebar}
<div class="profile">
<a href="{ProfileLink}">
<img src="{PortraitURL-45}" width='75' height='75'>
</a>
<p>{Profile}</p>
</div>
{/block:ListSidebar}
</div>
<div id="content">
<div class="posts">
{block:Posts}
<div id="postunit_{PostID}" class="postunit">
{block:EditBox/}
<div class="post">
<h3><a class="posttitle" href="{Permalink}">{Title}</a></h3>
<a class="button" href="{Permalink}">Posted {TimeAgo}</a>
{Body}
</div>
{block:Responses}
{block:ResponsesList}
{/block:ResponsesList}
{block:Sharing}
{block:Tweet /} {block:FbLike /}
{/block:Sharing}
<div class="postresponses">
<a class="button" href="#">{ResponseCount} Response{ResponseCountPluralized}</a>
</div>
{block:ResponsesShow}
{Responses}
{ResponseForm}
{/block:ResponsesShow}
{/block:Responses}
</div>
{/block:Posts}
</div>
{block:Pagination/}
</div>
</div>
</div>
</div>
</body>
</html>
Set the height of the containing elements of the #center_col div to 100%. I tested this and it worked!
If you are using jQuery:
<script type="text/javascript">
$(function(){
var height = $("#content").height();
$("#sidebar").height(height);
});
</script>
where < div id="content" > is the div with the size you want to replicate in < div id="sidebar" >
If your layout is going to stay this simple, I say just fake it with a nice little background image =)
If that won't do, I think JS is your only way to go.
e.g.
window.addEventListener('load',fit,false);
window.addEventListener('resize',fit,false);
function fit(){
var myHeight;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myHeight = document.body.clientHeight;
}
document.getElementById('content').style.height=myHeight + 'px';
}