make an element full screen in the web page - html

Any idea to make an element in the page full screen?
For example,a div or an img?
With "full screen" I mean that it should take all the space of user's screen,just like when we watch a video with the full screen model. I do not want the task bar/menu bar of the browser window display.
Any idea?
div.fullscreen{
display:block;
/*set the div in the top-left corner of the screen*/
position:absolute;
top:0;
left:0;
/*set the width and height to 100% of the screen*/
width:100%;
height:100%;
background-color:red
}
I have tried the above code,however it is not what I want,it juse take all the space of the browser's content area rather than the user's computer'screen.

HTML elements can't break out of the bounds of the browser document window. The menu and tool bar are outside of the document window (which is a child of the browser window), so you can't "reach" them.
I think the only solution is to trigger full screen mode with JavaScript.
This answer shows how you can do that: How to make the window full screen with Javascript (stretching all over the screen)

There is a relatively new fullscreen JavaScript api which can make an element full screen.
It has to be called as the result of user input to prevent possible abuse, but it's relatively straight-forward to use:
Code from MDN article:
document.addEventListener("keydown", function(e) {
if (e.keyCode == 13) {
toggleFullScreen();
}
}, false);
function toggleFullScreen() {
if (!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement && !document.webkitFullscreenElement) { // current working methods
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.cancelFullScreen) {
document.cancelFullScreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
}
}

This is not possible now, and it will probably never be.
Just imagine what would happen if every website you visit had free reign to take over your desktop.

In order to do this, you can use the screen.availWidth and screen.availHeight properties to get the screen size. Next, set the element size to their corresponding properties in js.

Related

How to Load Different Home Page according to screen size

I want to load different home page according screen size. Can anyone help about it ?
For Example,
for screen-size < 960 px I want to display default landing page as index1.html
and
for screen-size > 960 px I want to display default landing page as index2.html
Thanks in advance.
I know this was asked and answered a while ago, but I think the solution I'm adding is better than the accepted answer as it involves no reloading and works on resizing. Define two CSS classes, .HideOnMobile and .ShowOnMobile and use them in two top level DIV elements.
Then use media queries to set the display value of those two classes to non or initial, to display or hide each DIV according to the screen width.
#media (max-width: 575.98px) {
.HideOnMobile {
display: none;
}
.ShowOnMobile {
display: initial;
}
}
#media (min-width: 576px) {
.HideOnMobile {
display: initial;
}
.ShowOnMobile {
display: none;
}
}
<div class="ShowOnMobile">
MOBILE content
</div>
<div class="HideOnMobile">
DESKTOP content
</div>
You've tagged this question responsive-design, but you are asking about how to do "adaptive design." Responsive design would be having a single HTML page that adjusts to the medium it is being viewed in, for example by using media queries. I won't get into a debate about which is better, but I add this in case you're interested in responsive design so that you have some ideas to google.
A way to do what you are asking is to have a bit of JavaScript on your page that checks the width of the window and redirects if necessary:
// In index2.html
if (window.innerWidth < 960) {
window.location = "index1.html";
}
// In index1.html
if (window.innerWidth >= 960) {
window.location = "index2.html";
}
I tried the above solution. It worked but the page was reloading continuously. By this approach solved the reload problem and I've added a function that can help reload the page automatically when viewport size change.
// refreshing page automatically when viewport size change
window.onresize = function(event) {
document.location.reload(true);
}
var href = window.location.href.split("/")
var html_location = href[href.length-1]
if (window.innerWidth >= 960 && html_location !== "index.html") {
window.location = "index.html";
}
if (window.innerWidth < 960 && html_location !== "index2.html") {
window.location = "index2.html";
}

The same img in two sizes in the html

I have an img for a large screen and another for small screens.This is an easy option and if works but I am not sure if there it is a good practice to put the same img in different sizes in the html and hide one with display none? is there any other problem with that option?
CSS:
#small {
display:none;
}
#media screen and (max-width: 630px) {
#big { display:none; }
#small { display:block; }
}
HTML:
<img id="big" src="img/1-big.jpg">
<img id="small" src="img/1-small.jpg">
IMUO I think this is not a good practice, because you are loading all the images twice (and hidden then). If you are using bootstrap (or responsive page) you could use the class img-responsive or this:
img {
width: 100%;
height: auto;
}
Or if not, you could do this:
/* For width smaller than 400px: */
body {
background-image: url('1-big.jpg');
}
/* For width 400px and larger: */
#media only screen and (min-width: 400px) {
body {
background-image: url('1-small.jpg');
}
}
Doing that way, you only load the image when needed and avoid load twice the images. Another example as background image: https://www.smashingmagazine.com/2013/07/simple-responsive-images-with-css-background-images/
I don't see anything wrong with this. In fact, it is a recognised technique to reduce page load times and to keep page sizes down on mobile (providing, of course, that you only load whichever image is required for your device size).
Also note, as the only potential pitfall I can see with this, is that that simply setting the CSS property to display: none does not always prevent an image from loading (see here: Does "display:none" prevent an image from loading?)
An alternative to this would be to have images stored with the same name and a small or no suffix (for larger images) added to them (almost like you have in your example), except only have 1 html element on the screen at any one time and modify the paths using javascript. Example;
// HTML ELEMENT
<img class='thumbnail' src='img/thumb.png'>
// JAVASCRIPT
if(window.innerWidth < 640){
// This is for users with smaller screens, load the smaller image
var imgs = document.getElementsByTagName('img');
for(var i = 0; i < imgs.length; i++){
var current = imgs[i].getAttribute('src');
imgs[i].setAttribute('src', current + '-small.png');
// THIS WOULDN'T WORK AS IS, AS IT WOULD PRODUCE '.png-small.png'
// AND IS INTENDED **ONLY** TO ILLUSTRATE A CONCEPT
}
}

How do I show a graph in responsive way that works in desktop and mobile portrait and landscape?

I want to show a graph within a div.
Aspect ratio is height 50 % of width.
The graph in question looks like this:
http://simkimsia.github.io/500-charts/dygraphs/range-selector/
How do I make this div work in a responsive way in desktop and mobile?
I am astonished to find that the danvk example works the way that Kim Stacks says: if you delete the position:absolute and put in any width setting then there is no resizing (I forgot exactly what happens but it is not pleasant).
My other reason for astonishment is that I've been using Dygraphs for years and mine resize and I didn't do any of that position:absolute jazz. I instead put the graph inside a table:
<table style="margin:auto;width:90%;text-align:center;"><tr><td>
<div id="div_g"></div>
</td></tr></table>
And for the div I have the following:
<style type="text/css">
#div_g {height:320px;margin-top:50px;width=100%;}
</style>
That is, I downloaded danvk's example, NoisyData function and all, and ran it as above. It works. I get resizing. Chrome and Firefox both pass it on the narrowest screen.
If this isn't perfectly clear, setting "top:40px" and "position:absolute" is not going to fly as an acceptable HTML page layout discipline (here the 40px are to avoid drawing the graph on top of "Resize the window. The dygraph will resize with it."
Note especially that I found it necessary to set a height with this setup.
Now I will admit that, for reasons that I can't figure out, one of two pages that I have Dygraphs on that are setup this way just recently gave me a Google fail on mobile readiness. I fixed it in a painful manner by specifying the Dygraph div container widths as follows:
function adjustStyle(width) {
var width = parseInt(width);
if (width < 480) {
$(".dygraphplot").attr("style", "height:290px;max-width:290px;margin-bottom:10px;");
} else if (width < 540) {
$(".dygraphplot").attr("style", "height:320px;max-width:480px;margin-bottom:10px;");
} else if (width < 600) {
$(".dygraphplot").attr("style", "height:320px;max-width:540px;margin-bottom:10px;");
} else if (width < 640) {
$(".dygraphplot").attr("style", "height:320px;max-width:600px;margin-bottom:10px;");
} else if (width < 720) {
$(".dygraphplot").attr("style", "height:320px;max-width:640px;margin-bottom:10px;");
} else if (width < 750) {
$(".dygraphplot").attr("style", "height:320px;max-width:720px;margin-bottom:10px;");
} else if (width < 768) {
$(".dygraphplot").attr("style", "height:320px;max-width:750px;margin-bottom:10px;");
} else if (width < 800) {
$(".dygraphplot").attr("style", "height:320px;max-width:768px;margin-bottom:10px;");
} else if (width < 1080) {
$(".dygraphplot").attr("style", "height:320px;max-width:800px;margin-bottom:10px;");
} else if (width < 1200) {
$(".dygraphplot").attr("style", "height:320px;max-width:1080px;margin-bottom:10px;");
} else if (width < 1440) {
$(".dygraphplot").attr("style", "height:320px;max-width:1200px;margin-bottom:10px;");
} else if (width < 1536) {
$(".dygraphplot").attr("style", "height:320px;max-width:1440px;margin-bottom:10px;");
} else if (width < 1538) {
$(".dygraphplot").attr("style", "height:320px;max-width:1536px;margin-bottom:10px;");
} else if (width < 1600) {
$(".dygraphplot").attr("style", "height:320px;max-width:1538px;margin-bottom:10px;");
} else {
$(".dygraphplot").attr("style", "height:320px;max-width:1538px;margin-bottom:10px;");
}
}
See the JQuery panel on this page for the document-ready invocation of this function. Of course "dygraphplot" is the name of a class that I gave to the Dygraph div.
If I can find out why my other page still works without all of this I'll go back to that scheme.
Assuming this div is responsible for the graph
<p>No roll period.</p>
<div id="noroll"></div>
First set the position of the div
<style type="text/css">
#noroll {
position: absolute;
left: 10px;
right: 10px;
top: 40px;
bottom: 10px;
}
</style>
Then do not add any height or width styles to the div.
The dygraphs js will auto resize.
Reference: code https://github.com/danvk/dygraphs/blob/master/tests/resize.html
Reference: example http://dygraphs.com/tests/resize.html
You can either try using that image with a framework that handles image responsiveness, such as Bootstrap
Images in Bootstrap 3 can be made responsive-friendly via the addition of the .img-responsive class. This applies max-width: 100%; and height: auto; to the image so that it scales nicely to the parent element.
Or by far a much better approach, using a chart framework such as (but not limited to) http://www.chartjs.org/

Prevent screen from moving when clicking on <a href=></a>

I'm using <a href> element along with :target css selector to show a <div> which by default is set to display:none. Problem is, that when I click on the link to show that <div>, it is automatically scrolling down my site towards that <div>.
Is there a way to stop the screen movement?
Unfortunately I am not yet proficient in anything besides CSS and HTML.
You can use event.preventDefault() to avoid this. Something like this:
$('a.yourclass').click(function(e)
{
//your code
e.preventDefault();
});
OR:
link
in the link enter:
Link here
You'll need JS anyway:
// (in jQuery)
$el.on('click', function(e) {
// find current scroll position
var pos = document.body.scrollTop || document.documentElement.scrollTop;
// let normal action propagate etc
// in the next available frame (async, hence setTimeout), reset scroll posiion
setTimeout(function() {
window.scrollTo(0, pos);
}, 1);
})
I don't know if this will flicker the screen. It might. It's a horrible hack either way.
In my Chrome, there's no flicker: http://jsfiddle.net/rudiedirkx/LEwNd/1/show/
There are two ways to tell the browser we don't want it to act:
The main way is to use the event object. There's a method
event.preventDefault().
If the handler is assigned using on (not by
addEventListener), then we can just return false from it.
Example:
Click here
or
here
This is a bit of a hack but you could use a basic css work around:
CSS only Example
#div1 {
height: 0;
overflow:hidden;
}
#div1:target {
height: auto;
margin-top: -110px;
padding-top: 110px;
}
#div2 {
background:red;
}
Click to show
<div id="div1">
<div id="div2">Content</div>
</div>
If you need it to be a little more flexible you can add some js...
More Flexible Example with JS
$('a').click(function () {
$('#div1').css({
'margin-top': 0 - $('#div1').position().top + $(window).scrollTop(),
'padding-top': $('#div1').position().top - $(window).scrollTop()
});
});
Basically you're pulling the top of div1 up with the negative margin and then pushing div2 back down with the padding, so that the top of div1 rests at the top of the window... Like I said its a hack but it does the trick.
Those links are anchor-links and by default made for those jumps :) You could use JS to prevent the default behaviour in some way. For example using jQuery:
$('a').click(function(e){e.preventDefault();});
or by default add return false; to the links
Avoid using :target all together and just use onclick event.
function myFunction()
{
document.getElementById('hiddenDiv').style.display = 'block';
return false;
}

div to appear in full screen

I am new to html. Can I make a particular div in my web-page appear in fullscreen when I press alt and space keys? This is my code.
<!DOCTYPE html>
<html>
<body>
<p>This is some text.</p>
<div style="color:#0000FF">
<img src="./1.jpg" height="42" width="42">
</div>
</body>
</html>
Use the full-screen pseudo class (for webkit and mozila) :
:-webkit-full-screen {
/* css rules for full screen */
}
:-moz-full-screen {
/* css rules for full screen */
}
See this Mozilla article and this David Walsh article for usage
HTML and CSS provide no means to trigger full screen mode for an element. JavaScript is your only option.
HTML 5 introduces a full screen API for JavaScript. It is still experimental, so you need to use prefixed property names in some browsers and it won't work at all in others.
function makeFullScreen(element) {
if (element.requestFullScreen) {
element.requestFullScreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.msRequestFullScreen) {
element.msRequestFullScreen();
}
}
You then just need to bind an event handler to call it.
document.addEventListener('keypress', function (evt) {
if (evt.altKey && evt.keyCode === 32) {
makeFullScreen(document.querySelector('div'));
}
});
Beware of depending on modifier keys though. On my system, alt + space is captured at the OS level (to open Spotlight) so it will never reach the browser.