Scroll relative to device size - html

I am making a site where you can click on a button and it will scroll you down to the information below. My issue is that the size of the opening screen is relative to the device screen. That means that a fixed scroll can be too far or not far enough on different devices. In summary my question is: how do i jump the exact device sreen height?
Here is my code:`
<div id='textPage1'>
<div id='leesMeer'>
<a href="#page2" class='leesMeer'type="button" value='Lees Meer'>LEES MEER</a>
</div>
</div>
<div id='page2'>
<div id='textPage2'>
<p>hi</p>
</div>
</div>
And my css:
#textPage1 {
float: right;
margin-right: 100px;
z-index: -1;
}
#beschrijving {
font-family: agency;
font-size: 20px;
color: #f2f2f2;
letter-spacing: 2px;
transform: scale(1, 1.1);
line-height: 30px;
z-index: 1;
margin-left: 50px;
}
.leesMeer {
font-family: agency;
font-size: 30px;
color: #f2f2f2;
border: 5px solid #f2f2f2;
border-radius: 15px;
padding: 12px 40px 12px 40px;
text-decoration: none;
-webkit-transition-duration: 0.5s;
position: absolute;
}

You can use Javascript to specify the scroll down like this example:
HTML:
<div class="first"><button type="button" onclick="smoothScroll(document.getElementById('second'))">Click Me!</button></div>
<div class="second" id="second">Hi</div>
CSS:
.first {
width: 100%;
height: 1000px;
background: #ccc;
}
.second {
width: 100%;
height: 1000px;
background: #999;
}
Javascript:
window.smoothScroll = function(target) {
var scrollContainer = target;
do { //find scroll container
scrollContainer = scrollContainer.parentNode;
if (!scrollContainer) return;
scrollContainer.scrollTop += 1;
} while (scrollContainer.scrollTop == 0);
var targetY = 0;
do { //find the top of target relatively to the container
if (target == scrollContainer) break;
targetY += target.offsetTop;
} while (target = target.offsetParent);
scroll = function(c, a, b, i) {
i++; if (i > 30) return;
c.scrollTop = a + (b - a) / 30 * i;
setTimeout(function(){ scroll(c, a, b, i); }, 20);
}
// start scrolling
scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0);
}
here is the code in jsfiddle http://jsfiddle.net/rjSfP/

Related

jQuery Trouble triggering click event on popup

I have a fairly simple page with a couple divs designed to be pop ups...
I'm having an issue with the .close div located in a jQuery populated popup. A btn is clicked .. jquery brings up a div and then populates it with content from another div.
This all works as expected.
There's a .close div in the popups designed to be, well a close button.
$('body, .close').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
$('#info').html('').removeClass('pop').fadeOut(300);
$('#pup').children('h1').html('');
$('#pup').fadeOut(300);
$('#cov').fadeOut(300);
console.log('clk');
});
This works perfectly when the .close div is hard coded into the popup itself.. but it will not function in the when content is populated from somewhere else.
Can anyone shed some light on what I'm missing please?
// included so snippet functions....
var cards = $(".smpl");
cards.each(function(i) {
var im = $(this).attr('data-im');
var fadeTime = 180;
$(this).css('background-image', 'url(_core/img/p/' + im + ')')
$(this).delay(fadeTime * i).fadeIn(fadeTime);
});
// loaded #info with content from other divs
$('.about, .cont').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var wht = $(this).attr('data-t');
$('#info').removeClass('pop').html(''); //empty's info popup if its present
$('#pup').hide(); //hides other popup if its present
$('#cov').fadeIn(300);
$('#info').addClass('pop').html($(wht).html()).fadeIn(300);
console.log(wht);
});
// Should close all popups - FAIL for the #info .close div
// ###### this is where something is not right or borken...
$('body, .close').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
$('#info').html('').removeClass('pop').fadeOut(300);
$('#pup').children('h1').html('');
$('#pup').fadeOut(300);
$('#cov').fadeOut(300);
console.log('clk');
});
$('#info').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
});
$('#pup').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
});
//This opens #pup div
$('.sp, .ai, .ot').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var imgsrc = $(this).attr('data-im');
var longDes = $(this).children('.dspt').attr('data-ld');
$('#cov').fadeIn(300);
$('#pup').addClass('pop');
$('#pup h1').html(longDes);
$('#pup').css('background-image', 'url(_core/img/p/' + imgsrc + ')').fadeIn(300);
});
.nav {
position: absolute;
right: 30px;
top: 20px;
list-style: none;
z-index: 9997;
}
.nav a:link,
.nav a:visited {
display: block;
transition: all 0.5s;
font-size: 13px;
line-height: 22px;
width: 76px;
text-transform: uppercase;
background: #07e;
color: #fff;
padding: 2px 5px;
border-radius: 3px;
height: 25px;
position: relative;
right: 20px;
cursor: pointer;
margin: 5px 0;
text-align: center;
box-shadow: 0 -2px 0 #fff;
}
.smpl {
overflow: hidden;
position: relative;
float: left;
border: 1px solid #aaa;
padding: 0;
display: none;
width: 100%;
max-width: calc((100% - 60px) / 10);
height: auto;
margin: 5px 5px 0 0;
background-size: cover;
background: #aaa;
}
h1.dspt {
position: absolute;
bottom: -50px;
width: 100%;
background: #2d2d2d;
color: #fff;
text-transform: none;
padding: 5px;
opacity: 0;
transition: all .5s;
}
.smpl:hover h1.dspt {
bottom: 0;
transition: all .5s;
background: #07e;
opacity: 0.8;
}
#over,
#cov {
width: 100%;
height: 100%;
background: #aaa;
display: none;
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
z-index: 9991;
opacity: 0.8;
}
#cov {
background: #fffs;
opacity: 0.9;
}
#pup,
#info {
width: 150px;
height: 150px;
background-color: #fff;
display: none;
position: absolute;
top: 50%;
left: 0;
right: 0;
transform: translate(0, -50%);
z-index: 9998;
opacity: 1;
margin: 0 auto;
border: 1px solid #aaa;
box-shadow: 0 5px 50px #666;
overflow: hidden;
display: none;
}
#pup h1.dspt {
position: absolute;
bottom: -50px;
width: 100%;
background: #2d2d2d;
color: #fff;
text-transform: none;
font-size: 16px;
padding: 15px 40px;
opacity: 0;
transition: all .5s;
text-align: left;
text-shadow: 2px 0 1px #000;
}
#pup:hover h1.dspt {
bottom: 0;
transition: all .5s;
background: #2d2d2d;
opacity: 0.8;
}
#info {
padding: 10px;
overflow-Y: auto;
border: 3px solid #333;
border-radius: 6px;
}
.pop .close {
position: absolute;
top: -40px;
right: -40px;
width: 30px;
height: 30px;
border-radius: 50%;
background: #333;
color: #fff;
opacity: .5;
font-weight: bold;
text-align: center;
font-size: 16px;
line-height: 30px;
transition: all .5s;
cursor: pointer;
}
.pop:hover .close {
top: 10px;
right: 10px;
transition: all .5s;
background: #2d2d2d;
opacity: 0.8;
z-index: 9999;
}
#about,
#cont {
display: none;
background: #fff;
padding: 10px;
position: relative;
text-align: left;
margin: 10px 0 0 0;
border: 1px solid #ddd;
border-radius: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="cov"></div>
<div id="info"></div>
<div id="pup">
<h1 class="dspt"></h1>
<div class="close">X</div>
</div>
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="large-12 medium-12 cell masthead">
<div class="base">
<ul class="nav">
<li>about</li>
<li>contact</li>
</ul>
</div>
</div>
<div class="large-12 medium-12 cell" id="content">
<div id="about">
<div class="close">X</div>
<h4>about - Info Div</h4>
<p>Hover to see close btn, but it fails to function.
<p>
</div>
<div id="cont">
<div class="close">X</div>
<p>Contact - Info div</p>
<p>Hover to see close btn, but it fails to function.
<p>
</div>
<div class="grid-x grid-padding-x">
<div class="large-12 medium-12 cell inner">
<div class="smpl sp" data-im="h1pc_15px.png">
<div class="zm"></div>
<h1 class="dspt" data-ld="#pup div">Pup Div</h1>
<img alt="H" src="_core/img/p/_blank.png"></div>
</div>
</div>
</div>
</div>
<!-- // grid padding -->
</div>
<!-- //grid container -->
Snippet Above ..
If you click the [broken] image
the #pup popup shows
clicking outside #pup(grey area) removes the popup (- GOOD)
clicking the .close div removes the popup (- GOOD).
Click "about" or "contact" in the header
the #info popup shows
clicking outside that popup (grey area) closes it (- GOOD)
clicking the .close div simply won't trigger the close event (- FAIL).
Suggested that it was due to the click event on body and stopPropagation... So I tested...
$('.about, .cont').on('click', function (e) {
e.preventDefault();
//e.stopPropagation();
var wht = $(this).attr('data-t');
$('#info').removeClass('pop').html(''); //empty's info popup if its present
$('#pup').hide(); //hides other popup if its present
$('#cov').fadeIn(300);
$('#info').addClass('pop').html($(wht).html()).fadeIn(300);
console.log(wht);
});
$('#cov, .close').on('click', function(e) {
e.preventDefault();
//e.stopPropagation();
$('#info').html('').removeClass('pop').fadeOut(300);
$('#pup').children('h1').html('');
$('#pup').fadeOut(300);
$('#cov').fadeOut(300);
console.log('clk');
});
Issue remains.
It works if you change the click function to: $(document).on('click', 'body', '.close', function()
You also need to remove this bit:
$('#info').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
});
$('#pup').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
});

Remove unused space from letter

I am trying to use a single letter as a logo, the problem is that the containing box of the letter, also called 'em-box' is too large and/or too small, like here:
I would like it to have the exact same size as the letter so that I can centre it perfectly into the circle, like the 'w' logo. The 'b' one, using the same css, is totally off.
#import url('https://fonts.googleapis.com/css2?family=Fredoka:wght#200&family=Press+Start+2P&display=swap');
.container {
display: flex;
gap: 30px;
}
.circle {
display: flex;
height: 175px;
width: 175px;
justify-content: center;
align-items: center;
border-radius: 100px;
background: #1231b3;
}
.text {
display: flex;
font-family: 'Press Start 2P';
font-size: 120px;
color: white;
background-color: light-blue;
}
.light {
background: #7c8dd7;
}
<div class="container">
<div class="circle">
<div class="text">b</div>
</div>
<div class="circle">
<div class="text light">b</div>
</div>
</div>
CSS doesn't know where a character begins and ends (in terms of its visible parts as opposed to its overall width/height).
To find out the top/bottom/left/right visible extremities of a character this snippet draws it on a canvas and then scans the canvas rows and columns to find the first points that have non-zero alpha settings.
A logo is taken to be the full rounded square and its contents. The inner circle is drawn as a before pseudo element.
The character is drawn not in the DOM but as content to the after pseudo element.That way its position can be adjusted depending on its visible dimensions.
Characters that have no ascenders (e.g. w and c in the example given in the question) are moved up slightly (depending on their overall visible height) so they are centered.
The typeface in question differs a bit from the standard typefaces in that the descenders hardly have any height so the position of the baseline in relation to an overall character is different.
This snippet cheats slightly by building in the height of a character with an ascender (a lowercase b in this case) as a guide for which characters need adjusting. It's therefore not a completely general solution for any typeface which might be thrown at it. A bit more work would need to be done to first ascertain the range of heights in any given font.
<!doctype html>
<html>
<head>
<title>Chars as logos</title>
<!-- https://stackoverflow.com/questions/72772445/remove-unused-space-from-letter -->
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
<style>
body {
font-family: 'Press Start 2P';
letter-spacing: -4px;
font-size: 30px;
}
/* container added just for demo */
.container {
display: flex;
gap: 3vw;
background: #eeeeee;
rtext-align: center;
}
.logo {
width: 70px;
height: 70px;
display: flex;
justify-content: center;
align-items: center;
background-color: var(--col1);
border-radius: 10px;
position: relative;
padding: 0;
margin: 0;
line-height: 1em;
}
.logo::before {
content: '';
width: 50px;
height: 50px;
background-color: var(--col2);
border-radius: 50%;
position: absolute;
top: 10px;
left: 10px;
padding: 0;
margin: 0;
}
.logo::after {
content: var(--char1);
font-family: 'Press Start 2P';
font-size: 30px;
height: calc(var(--h) * 1px);
width: calc(var(--w) * 1px);
padding: 0;
margin: 0;
display: inline-block;
position: absolute;
color: white;
z-index: 1;
text-align: center;
margin-top: calc(var(--top) * 1px);
}
canvas {
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div class="container">
<div class="logo" style="--char: g; --char1: 'g'; --col1: #c920df; --col2: #e48fef;"></div>
<div class="logo" style="--char: w; --char1: 'w'; --col1: #df208d; --col2: #ef8fc6;"></div>
<div class="logo" style="--char: b; --char1: 'b'; --col1: #1231b3; --col2: lightblue;"></div>
</div>
<script>
const logos = document.querySelectorAll('.logo');
function fontLoaded() {
logos.forEach(logo => {
let canvas = document.createElement("canvas");
canvas.width = 200;
canvas.height = 200;
let ctx = canvas.getContext("2d");
ctx.font = "30px 'Press Start 2P'";
ctx.fillText(logo.style.getPropertyValue('--char'), 10, 60); //baseline of the character will be at 60
let d = ctx.getImageData(0, 0, 200, 200);
let foundTop = false;
foundBottom = false;
foundLeft = false;
foundRight = false;
let top = [];
let bottom = [];
let left = [];
let right = [];
let r, c;
//// Find the visible height ////
for (r = 0; r < 200; r++) {
for (c = 3; c < (800 - 1); c += 4) {
if (d.data[(r * 800) + c] != 0) {
foundTop = true;
top = [r, c];
break;
}
}
if (foundTop) break;
}
for (r = 200 - 1; r >= 0; r--) {
for (c = (800 - 1); c >= 0; c -= 4) {
if (d.data[(r * 800) + c] != 0) {
foundBottom = true;
bottom = [r, c];
break;
}
}
if (foundBottom) break;
}
//// now find the width ////
for (c = 3; c < (800 - 1); c += 4) {
for (r = 0; r < (200 - 1); r++) {
if (d.data[(r * 800) + c] != 0) {
foundLeft = true;
left = [r, c];
break;
}
}
if (foundLeft) break;
}
for (c = (800 - 1); c >= 0; c -= 4) {
for (r = 200 - 1; r >= 0; r--) {
if (d.data[(r * 800) + c] != 0) {
foundRight = true;
right = [r, c];
break;
}
}
if (foundRight) break;
}
logo.style.setProperty('--h', bottom[0] - top[0]);
logo.style.setProperty('--w', (right[1] - left[1] - 1) / 4);
if ((bottom[0] - top[0]) < 26) logo.style.setProperty('--top', (top[0] - bottom[0]) / 2);
});
}
WebFont.load({
google: {
families: ['Press Start 2P:300,400,700']
},
loading: function() {},
active: function() {
fontLoaded();
}
});
</script>
</body>
</html>
Note: before a typeface is drawn on a canvas we have to be sure that it has been loaded, hence the use of the google library
# A Haworth's answer is awesome. I could never have come up with that.
But if you want a quick and dirty css-only solution, get rid of the flex properties, set text-align to center, and massage the padding for the .text boxes. Use em measurements so the layout will scale without breaking.
Big Font: 120px
#import url('https://fonts.googleapis.com/css2?family=Fredoka:wght#200&family=Press+Start+2P&display=swap');
/* only used for this example */
.wrapper {
display: flex;
gap: 30px;
}
.container {
font-size: 120px;
/* font-size: 24px;*/
display: inline-block;
padding: .25em;
border-radius: .5em;
}
.container.blue {
background-color: #8fa1ef;
}
.container.purple {
background-color: #e48fef;
}
.container.red {
background-color: #ef8fc6;
}
.circle {
height: 1.5em;
width: 1.5em;
border-radius: 1em;
}
.blue .circle {
background-color: #1231b3;
}
.red .circle {
background-color: #df208d;
}
.purple .circle {
background-color: #c920df;
}
.text {
font-family: 'Press Start 2P';
color: white;
text-align: center;
}
.blue .text {
padding: .18em 0 0 .125em;
}
.red .text {
padding: .08em 0 .04em .04em;
}
.purple .text {
padding: .08em 0 0 .1em;
}
<div class="wrapper">
<div class="container blue">
<div class="circle">
<div class="text">b</div>
</div>
</div>
<div class="container red">
<div class="circle">
<div class="text">c</div>
</div>
</div>
<div class="container purple">
<div class="circle">
<div class="text">w</div>
</div>
</div>
</div>
Small Font: Exactly the same, but the font size is set to 24px.
#import url('https://fonts.googleapis.com/css2?family=Fredoka:wght#200&family=Press+Start+2P&display=swap');
/* Only used for this example */
.wrapper {
display: flex;
gap: 30px;
}
.container {
/* font-size: 120px;*/
font-size: 24px;
display: inline-block;
padding: .25em;
border-radius: .5em;
}
.container.blue {
background-color: #8fa1ef;
}
.container.purple {
background-color: #e48fef;
}
.container.red {
background-color: #ef8fc6;
}
.circle {
height: 1.5em;
width: 1.5em;
border-radius: 1em;
}
.blue .circle {
background-color: #1231b3;
}
.red .circle {
background-color: #df208d;
}
.purple .circle {
background-color: #c920df;
}
.text {
font-family: 'Press Start 2P';
color: white;
text-align: center;
}
.blue .text {
padding: .18em 0 0 .125em;
}
.red .text {
padding: .08em 0 .04em .04em;
}
.purple .text {
padding: .08em 0 0 .1em;
}
<div class="wrapper">
<div class="container blue">
<div class="circle">
<div class="text">b</div>
</div>
</div>
<div class="container red">
<div class="circle">
<div class="text">c</div>
</div>
</div>
<div class="container purple">
<div class="circle">
<div class="text">w</div>
</div>
</div>
</div>

Show/Hide the Image and Youtube Video based on Cookie

In my web application initially showing Image with overlapping the video when the user click on it the form will show up after submitting the form storing the value in cookie for One day. If cookie have value the YouTube video will show. So that user can watch this is the functionality we have.
The functionality are working but the video are not showing if cookies have value. For this I tried below code.
Here I have submitting the code If cookie have value replacing the YouTube video. It's not working for me. Please suggest us where I went wrong.
<html>
<head>
<title>Set Cookies</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<style type="text/css">
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: -60px;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #f7f7f7;
margin: auto;
padding: 20px;
border: 1px solid #888;
max-width: 340px;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal{
padding-left: 35px;
padding-right: 35px;
}
#contactform input,select {
height: 40px;
border: 1px solid #C6C6C6;
color: #000;
margin: 5px 0 0;
}
.errorMessage{
color: #d13800;
font-size: 12px;
display: none;
}
.customButton{
background-color: #0095eb !important;
color: #fff !important;
}
</style>
<style type="text/css">
.youTypeLink{
display: none;
}
.thumb {
display: flex;
flex-wrap: wrap;
list-style: none;
}
.thumb li {
width: 193px;
}
.thumb li ~ li {
margin-left: 20px;
}
.thumb .thumbCaption {
padding: 10px 0;
}
.overlay {
position: relative;
}
.overlay .thumbnail {
display: block;
}
.overlay .time {
position: absolute; z-index: 2;
right: 3px; bottom: 3px;
padding: 2px 5px;
background-color: rgba(0, 0, 0, 0.6);
color: white;
}
.overlay .playWrapper {
opacity: 0;
position: absolute; z-index: 1;
top: 0;
width: 192px; height: 109px;
background: rgba(0,0,0,0.6) url("http://wptf.com/wp-content/uploads/2014/05/play-button.png") no-repeat scroll center center / 50px 50px;
}
.playWrapper .playBtn {
position: absolute;
z-index: 2;
width: 50px;
height: 50px;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto; /* center */
}
.thumb .overlay:hover .playWrapper {
opacity: 1;
}
</style>
<script>
function setCookie(cname,cvalue,exdays) {
let d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
let expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
let user = getCookie("username");
if (user !== "") {
$('#youTubeImage').css({"display":"none !important"});
$('#youTypeLink').css({"display":"initial !important"});
/* player = new YT.Player('player', {
width : '320',
height : '180',
videoId : 'qlEUrEVqDbo',
playerVars: { 'autoplay': 1 },
events : {
'onReady' : onPlayerReady,
'onStateChange' : onPlayerStateChange
}
});*/
console.log(user);
setCookie("username", "cookiesValue", 1);
}else{
$('#youTubeImage').css({"display":"initial"});
$('#youTypeLink').css({"display":"none"});
}
</script>
</head>
<body>
<div>
<ul id="youTubeImage" class="thumb">
<li>
<div class="overlay">
<img class="thumbnail" src="https://homepages.cae.wisc.edu/~ece533/images/monarch.png" width="192" height="109" alt="">
<span class="time">3:28</span>
<a href="#" class="playWrapper" id="myBtn">
<!--<span class="playBtn"><img src="http://wptf.com/wp-content/uploads/2014/05/play-button.png" width="50" height="50" alt=""></span>-->
</a>
</div>
<div class="thumbCaption">This is the description of the video...</div>
</li>
</ul>
<div class="youTypeLink">
<ul class="thumb">
<li>
<div class="overlay">
<iframe width="370" height="303" src="https://www.youtube.com/embed/OXmIptsCsw0">
</iframe>
</div>
<div class="thumbCaption">This is the description of the video...</div>
</li>
</ul>
</div>
</div>
</body>
</html>
Please suggest us to work properly. If cookie have value show video or else showing image.

Add a text in the background of a input file

I have this simple form for uploading files:
<form method="post" action="">
<input id="holder" type="file" style="padding: 5px; width:100%; height:100px; border: 5px dashed #ccc">
</form>
http://jsfiddle.net/e98b2w7s/
But now I need a way to add this text in the background inside the dotted area:
"or Drag and Drop your file here"
Is there a way to do that?
You may do better to just set your styles to your form instead, in which case you can just give the form itself the background image for a bit of a cleaner solution:
Edit: Removed background image to show with text instead for a cleaner approach.
form {
padding: 5px;
width: 100%;
height: 100px;
border: 5px dashed #ccc;
}
form h1 {
text-align: center;
color: #ccc;
}
<form method="post" action="">
<input id="holder" type="file">
<h1>Drag & Drop Files Here</h1>
</form>
You don't need to use a background image, however, you could just create an h1 that says "Drag & Drop Here" and then use an SVG or FontAwesome to add the cloud and/or arrow icons.
Please try below solution
<form action="youractionfile.php" method="POST">
<input type="file" multiple>
<p>Drag your files here or click in this area.</p>
<button type="submit">Upload</button>
</form>
CSS
<style type="text/css">
body{
background: rgba(0,0,0,0.9);
}
form{
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -250px;
width: 500px;
height: 200px;
border: 4px dashed #fff;
}
form p{
width: 100%;
height: 100%;
text-align: center;
line-height: 170px;
color: #ffffff;
font-family: Arial;
}
form input{
position: absolute;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
outline: none;
opacity: 0;
}
form button{
margin: 0;
color: #fff;
background: #16a085;
border: none;
width: 508px;
height: 35px;
margin-top: -20px;
margin-left: -4px;
border-radius: 4px;
border-bottom: 4px solid #117A60;
transition: all .2s ease;
outline: none;
}
form button:hover{
background: #149174;
color: #0C5645;
}
form button:active{
border:0;
}
</style>
below javascript code
$(document).ready(function(){
$('form input').change(function () {
$('form p').text(this.files.length + " file(s) selected");
});
});
As you mentioned a question here I'm posting my answer
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({ top: y - 15, left: x - 100 });
} else {
inputFile.offset({ top: -400, left: -400 });
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({ top: y - 15, left: x - 160 });
} else {
inputFile.offset({ top: -400, left: -400 });
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
})
#drop-zone {
/*Sort of important*/
width: 300px;
/*Sort of important*/
height: 200px;
position:absolute;
left:50%;
top:100px;
margin-left:-150px;
border: 2px dashed rgba(0,0,0,.3);
border-radius: 20px;
font-family: Arial;
text-align: center;
position: relative;
line-height: 180px;
font-size: 20px;
color: rgba(0,0,0,.3);
}
#drop-zone input {
/*Important*/
position: absolute;
/*Important*/
cursor: pointer;
left: 0px;
top: 0px;
/*Important This is only comment out for demonstration purposes.
opacity:0; */
}
/*Important*/
#drop-zone.mouse-over {
border: 2px dashed rgba(0,0,0,.5);
color: rgba(0,0,0,.5);
}
/*If you dont want the button*/
#clickHere {
position: absolute;
cursor: pointer;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: 20px;
line-height: 26px;
color: white;
font-size: 12px;
width: 100px;
height: 26px;
border-radius: 4px;
background-color: #3b85c3;
}
#clickHere:hover {
background-color: #4499DD;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="drop-zone">
or Drag and Drop files here
<div id="clickHere">
or click here..
<input type="file" name="file" id="file" />
</div>
</div>
Html:
<input id="holder" type="file" placeholder="or Drag and Drop your file here" />
<label for="holder"> or Drag and Drop your file here </label>
Css:
[type=file] {
position: absolute;
filter: alpha(opacity=0);
opacity: 0;
}
input,
[type=file] + label {
border: 1px solid #CCC;
border-radius: 3px;
text-align: left;
padding: 10px;
width: 150px;
margin: 0;
left: 0;
position: relative;
}
Js:
$("[type=file]").on("change", function(){
// Name of file and placeholder
var file = this.files[0].name;
var dflt = $(this).attr("placeholder");
if($(this).val()!=""){
$(this).next().text(file);
} else {
$(this).next().text(dflt);
}
});
Working example here. Hope this work

DIV won't align to center

I have problem with centering div element. I tried with marings (margin: o auto) and with left/right CSS atributes (left: 50%, right: 50%) but result was wrong. Can someone tell me where's the problem?
EDIT:
I am using JavaScript to fill content of "boxcard" div. Problem is that content of "boxcard" div is not aligned to center (it's aligned to left). I am using JavaScript code (added below) to fill DIV's content.
This is what I have:
EDIT 2:
Here is jsfiddle : jsfiddle
CSS:
* {
margin: 0;
padding: 0;
}
body {
font: 18px Verdana;
color: #FFF;
background: #CCC;
}
#picbox {
margin: 0px auto;
width: auto;
}
#boxcard {
z-index: 1;
margin: 0px auto;
width: auto;
}
#boxcard div{
float: left;
width: 100;
height: 120;
margin: 5px;
padding: 5px;
border: 4px solid #EE872A;
cursor: pointer;
border-radius: 10px;
box-shadow: 0 1px 5px rgba(0,0,0,.5);
background: #B1B1B1;
z-index: 2;
}
#boxcard > div:nth-child(6n+1) {
clear: both;
}
#boxcard div img {
display: none;
border-radius: 10px;
z-index: 3;
}
#boxbuttons {
text-align: center;
margin: 20px;
display: block;
}
#boxbuttons .button {
text-transform: uppercase;
background: #EE872A;
padding: 5px 10px;
margin: 5px;
border-radius: 10px;
cursor: pointer;
}
#boxbuttons .button:hover {
background: #999;
}
HTML
<div id="picbox">
<span id="boxbuttons">
<span class="button" id="rezz">
Result
<span id="counter">0</span>
</span>
<span class="button" id="ttime"></span>
<span class="button">
<a onclick="ResetGame();">Reset</a>
</span>
</span>
<div id="boxcard" align="center"></div>
</div>
This is JS Code that creates DIV blocks (i.e. Cards)
function ShuffleImages() {
var ImgAll = $(Source).children();
var ImgThis = $(Source + " div:first-child");
var ImgArr = new Array();
for (var i = 0; i < ImgAll.length; i++) {
ImgArr[i] = $("#" + ImgThis.attr("id") + " img").attr("src");
ImgThis = ImgThis.next();
}
ImgThis = $(Source + " div:first-child");
for (var z = 0; z < ImgAll.length; z++) {
var RandomNumber = RandomFunction(0, ImgArr.length - 1);
$("#" + ImgThis.attr("id") + " img").attr("src", ImgArr[RandomNumber]);
ImgArr.splice(RandomNumber, 1);
ImgThis = ImgThis.next();
}
}
$(function() {
for (var y = 1; y < 3 ; y++) {
$.each(ImgSource, function(i, val) {
$(Source).append("<div id=card" + y + i + "><img src=" + val + " />");
});
}
$(Source + " div").click(OpenCard);
ShuffleImages();
});
You need to add display: table. This is how you center an element with a undefined width
DEMO http://jsfiddle.net/s7w3q/2/
#boxcard {
z-index: 1;
display: table;
margin: 0px auto;
width: auto;
}
#boxcard div{
width: 100px;
display:inline-block;
height: 120px;
margin: 5px;
padding: 5px;
border: 4px solid #EE872A;
cursor: pointer;
border-radius: 10px;
box-shadow: 0 1px 5px rgba(0,0,0,.5);
background: #B1B1B1;
z-index: 2;
}
You were using float:left; on #boxcard div even when you were using align"center" withHTML`
Also you needed to use px after height and width
another way to do this would be more efficient. As it'd Remove the need to separately align boxbuttons.
#boxcard {
display: inline-block;
...
}
....
#picbox {
text-align: center;
...
}