How can I create a create a hole within a DIV? [duplicate] - html

This question already has answers here:
Creating a hole in a <div> element
(10 answers)
Closed 7 years ago.
So say I have the following HTML structure:
<canvas></canvas>
<div class="overLay">
<button>Click me!</button>
</div>
The canvas is absolutely positioned with a negative z index, so the overlay is positioned over it (I have that much working). The issue is that I want the overlay div to have a white background, but the button to have a transparent background and show through to the canvas/body background color.
background-color:rgba(0,0,0,0);
doesn't work because it just shows the white background behind it. Any ideas on how to accomplish this effect?
The question this may have been marked as a potential duplicate of fails to account for the fact that buttons can have properties such as border-radius and offers no suitable solutions.

There's no way to create a hole inside an HTML element. A Transparent background can only be applied to an element that does not descend from a parent that has a non-transparent background.
What you could do, thought it's a little bit more complicate, is to create divs around your button, at it's same level, as siblings. Give those divs a white background, and then your transparent button should work.
I've created a sample using a table layout, where the button remains in the middle row, in the center cell. See that the button reveals the canvas background color:
canvas {
width: 200px;
height: 200px;
background: purple;
position: absolute;
z-index: -1;
}
.overLay {
width: 200px;
height: 200px;
display: table;
}
.overLay button {
background-color: Transparent;
white-space: nowrap;
}
.overLay > div {
display: table-row;
}
.overLay > .middle {
height: 1px;
}
.overLay > div > div {
display: table-cell;
text-align: center;
}
.overLay > .middle > div:nth-child(2) {
width: 1px;
}
/* Now, set the background on the divs around the button */
.top, .left, .right, .bottom {
background: white;
}
<canvas></canvas>
<div class="overLay">
<div class="top">
<div></div>
<div></div>
<div></div>
</div>
<div class="middle">
<div class="left"></div>
<div>
<button>Click me!</button>
</div>
<div class="right"></div>
</div>
<div class="bottom">
<div></div>
<div></div>
<div></div>
</div>
</div>

Since in my original answer, you pointed the intention of using a border radius. So there's another approach to make this possible.
Based on this asnwer
Make the overlay with a gradient radius background, that creates the gradient color center in the position where you want the button to be, at the button's size. Make the outer color as white, and the inner color as Transparent;.
Then, set your button's position. (I did this by centering it in a table layout):
canvas {
width: 200px;
height: 200px;
background: purple;
position: absolute;
z-index: -1;
}
.overLay {
width: 200px;
height: 200px;
display: table;
background-color: white;
background: -webkit-radial-gradient(50% 50%, circle, transparent 25px, white 0px);
background: radial-gradient(50% 50%, circle, transparent 25px, white 0px);
}
.overLay > div {
display: table-cell;
text-align: center;
vertical-align: middle;
}
button {
background-color: transparent;
border-radius: 50%;
width: 50px;
height: 50px;
position: relative;
}
<canvas></canvas>
<div class="overLay">
<div>
<button>Click me!</button>
</div>
</div>

Mmm..., have you thought about using an SVG element with a mask. That should do the trick. Take a look at the snippet.
body {
padding: 0;
margin: 0;
}
.your-canvas {
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
button {
width: 150px;
height: 100px;
background-color: transparent;
color: white;
cursor: pointer;
}
<div class="your-canvas">
<img width="512" alt="Weingarten Kuppel 8" src="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Weingarten_Kuppel_8.jpg/512px-Weingarten_Kuppel_8.jpg"/>
</div>
<svg height="324" width="512">
<defs>
<mask id="mask">
<rect width="512" height="324" fill="white"/>
<rect x="181" y="112" width="150" height="100" fill="black"/>
</mask>
</defs>
<rect width="512" height="324" style="fill:rgba(255,0,0,.6);" mask="url(#mask)"/>
<foreignObject class="node" x="181" y="112" width="150" height="100">
<body xmlns="http://www.w3.org/1999/xhtml">
<button>Click Me</button>
</body>
</foreignObject>
</svg>
As far as i know it is even possible to use masking in pure css, but haven't had the time to look it up. Here is some information on using masks with svg Clipping and masking.
Have fun.

You could...
Reduce a canvas element to button size (== a button-canvas!),
Use CSS to position the button-canvas as desired over the div,
Add a click event listener on the canvas,
Draw whatever design you need on the button-canvas.
This way you have complete flexibility using the amazing drawing tools available with the canvas element.
Here's example code and a (fanciful) Demo.
While this demo is just for fun, you can easily restyle this example & turn it into a reusable widget:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
var width=80;
var height=40;
var borderwidth=4;
var x=0;
var y=0;
x+=borderwidth;
y+=borderwidth/2;
var w=width-borderwidth-borderwidth;
var h=height-borderwidth;
var depth=5;
//
var stopCount=8;
var angle=0;
var angleDelta=360;
//
var labelColor='black';
canvas.width=width;
canvas.height=height;
requestAnimationFrame(animate);
$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseUp(e);});
function makeGradient(){
var g=ctx.createLinearGradient(0,0,cw,0);
for(var i=0;i<stopCount;i++){
var stop=i/stopCount;
var hslDegrees=(angle+angleDelta*stop)%360;
var hsl="hsl(" + hslDegrees + ",100%, 50%)"
g.addColorStop(stop,hsl);
}
return(g);
}
function animate(){
ctx.clearRect(0,0,cw,ch);
gradientBorder();
ctx.font='12px verdana';
ctx.fillStyle=labelColor;
ctx.fillText('Click Me',x+10,y+25);
angle++;
requestAnimationFrame(animate);
}
//
function gradientBorder(){
var lw=ctx.lineWidth
var ss=ctx.strokeStyle;
ctx.lineWidth=borderwidth;
ctx.strokeStyle=makeGradient();
//
ctx.beginPath();
ctx.moveTo(x+w-depth,y);
ctx.lineTo(x+depth,y);
ctx.bezierCurveTo( x-depth/2,y+h*1/6, x+depth*2,y+h*2/6, x,y+h/2);
ctx.bezierCurveTo( x+depth*2,y+h*4/6, x-depth/2,y+h*5/6, x+depth,y+h);
ctx.lineTo(x+w-depth,y+h);
ctx.bezierCurveTo( x+w+depth/2,y+h*5/6, x+w-depth*2,y+h*4/6, x+w,y+h/2);
ctx.bezierCurveTo( x+w-depth*2,y+h*2/6, x+w+depth/2,y+h*1/6, x+w-depth,y);
ctx.closePath();
ctx.stroke();
//
var b2=borderwidth/2;
ctx.beginPath();
ctx.moveTo(x+w-depth-1,y+b2);
ctx.lineTo(x+depth+2,y+b2);
ctx.bezierCurveTo( x-depth/2+b2+2,y+h*1/6+1, x+depth*2+b2,y+h*2/6, x+b2+1,y+h/2);
ctx.bezierCurveTo( x+depth*2+b2,y+h*4/6+1, x-depth/2+b2+1,y+h*5/6, x+depth+b2-2,y+h-b2);
ctx.strokeStyle='#666';
ctx.lineWidth=0.50;
ctx.moveTo(x+depth+b2-2,y+h-b2);
ctx.lineTo(x+w-depth-2,y+h-b2);
ctx.bezierCurveTo( x+w+depth/2-b2-2,y+h*5/6, x+w-depth*2-b2+1,y+h*4/6, x+w-b2-2,y+h/2);
ctx.bezierCurveTo( x+w-depth*2-b2+1,y+h*2/6, x+w+depth/2-b2-2,y+h*1/6, x+w-depth-b2+3,y+b2);
ctx.strokeStyle='#666';
ctx.lineWidth=0.50;
ctx.stroke();
//
ctx.strokeStyle=ss;
ctx.lineWidth=lw;
ctx.fillStyle='gainsboro';
ctx.fill();
}
function handleMouseDown(e){ labelColor='red'; }
function handleMouseUp(e){ labelColor='black'; }
body{ background-color:white; }
.demo{
width:200px;
height:100px;
border:1px solid red;
position:relative;
}
#canvas{
position:absolute;
left:10px;
top:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class='demo'>
<canvas id="canvas"></canvas>
</div>

Related

CSS background with some borders

I'm having troubles with adding background to div element.
<div class="bg-box">
<p>Lorem ipsum dolorem</p>
</div>
JSfiddle
It's not too much, but what I want to have is:
Any idea how can I add this?
Thanks.
EDIT:
Sorry about poor explanation. What I want and need is this to be done with css only, so no image in background. Is it possible? This is just example of the width and height, so I need it to be responsive because it will be used in site background...
Use SVG, when embedded it works just like CSS, inline and no "image" to load, it scales and is an excellent way to create patterns like triangles.
Here is a sample with 2 triangles you can use as a start.
html, body { margin: 0; height: 100%; }
.triangle {
width: 100%;
height: 100%;
background-color: #00cfaf;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256'%3E %3Cpolygon points='200,10 250,190 160,210' style='fill:red;stroke-width:0' /%3E %3Cpolygon points='100,50 170,20 160,210' style='fill:yellow;stroke-width:0' /%3E %3C/svg%3E");
background-repeat: no-repeat;
}
<div class="triangle">
</div>
HTML5 Canvas, though this need script which SVG doesn't.
function init()
{
var canvas = document.getElementById("canvas");
if(canvas.getContext)
{
var ctx = canvas.getContext("2d");
ctx.fillStyle="#0FF";
ctx.beginPath();
ctx.moveTo(10,10);
ctx.lineTo(150,10);
ctx.lineTo(200,110);
ctx.closePath();
ctx.fill();
ctx.fillStyle="#FF0";
ctx.beginPath();
ctx.moveTo(50,50);
ctx.lineTo(10,100);
ctx.lineTo(80,110);
ctx.closePath();
ctx.fill();
}
}
onload=init;
html, body { margin: 0; height: 100%; }
.triangle {
width: 100%;
height: 100%;
overflow: hidden;
}
#canvas {
width: 100%;
height: 100%;
background-color: #00cfaf;
}
<div class="triangle">
<canvas id="canvas"></canvas>
</div>
Sounds like what you're looking for is the background image. Save the image to your local directory and then do something like this
div {
background-image: url("image.jpg");
}
To use this image as a background add a css rule for either all divs or your bg-box.
.bg-box {
background-image: url("imageFile.jpg");
width:400px;
height:400px;
}
This will make your picture the background of the div.

Thin line when rendering overflow:hidden with border-radius

To implement some fancy round progress bars, I used an element with border radius and overflow:hidden. Everything looks fine, except one thing: all browsers display a thin line where the overflow ends.
here's some simple snippet that reproduces this bug in all major browsers:
function setMarginLeft(value){
var element = document.querySelector(".overflowing");
element.style.marginLeft = value+"px";
}
function setMarginTop(value){
var element = document.querySelector(".overflowing");
element.style.marginTop = value+"px";
}
.backdrop {
background-color: white;
padding: 10px;
width:100px;
height:100px;
}
.circle {
background-color: red;
width:100px;
height:100px;
border-radius: 100px;
overflow:hidden;
}
.overflowing {
width:200px;
height:200px;
margin-top: 10px;
margin-left: 10px;
background-color:#fff;
}
input {
margin-top:10px;
}
<div class="backdrop">
<div class="circle">
<div class="overflowing"></div>
</div>
</div>
<span>top and right of the circle you should see some red, bottom left not</span><br />
<em>Feel free to play around with these values:</em><br />
Top margin: <input type="number" id="cngMargin" oninput="setMarginTop(this.value)" value="10"><br />
Left margin: <input type="number" id="cngMargin" oninput="setMarginLeft(this.value)" value="10">
since the layout isn't nearly as easy as the snippet above, i can't change the layout much.
i guess the essential problem here is the browser's anti-aliasing, which i think is not alterable by css, or is it?
i googled myself stupid on that matter and can't come up with really usefull ressources. i guess if nothing else works, i'll have to do it anew in SVG or on a canvas -.-
Not fully supported right now but overflow-clip-margin could be a solution:
overflow: hidden; // for unsupported browsers
overflow: clip;
overflow-clip-margin: 2px;
Note: I don't fully understand your example but for my situation I had a title
bar that was getting a white line around it (inside the border). This is an exaggeration with overflow-clip-margin: 5px but if it's only 2px then it matches with the blue border on the white box.
This seems possible without the overlay by using a background-image with multiple linear-gradients. The JavaScript to insert the two values into the combined linear-gradient is rather clunky as I wrote it hastily to prove the solution. It can be better!
Tested in Chrome only.
var marginTop = marginLeft = 0;
function setMarginLeft(value) {
marginTop = value;
applyToCircle();
}
function setMarginTop(value) {
marginLeft = value;
applyToCircle();
}
function applyToCircle() {
var element = document.querySelector('.circle');
element.style.backgroundImage = 'linear-gradient(90deg, red '+ marginTop + '%, transparent 0%), linear-gradient(180deg, red '+ marginLeft + '%, transparent 0%)';
}
.backdrop {
background-color:white;
padding: 10px;
width:100px;
height:100px;
}
.circle {
width:100px;
height:100px;
border-radius:50%;
}
input {
margin-top:10px;
}
<div class="backdrop">
<div class="circle">
</div>
</div>
<em>Feel free to play around with these values:</em><br />
Top margin: <input type="number" id="cngMargin" oninput="setMarginTop(this.value)" value="0"><br />
Left margin: <input type="number" id="cngMargin" oninput="setMarginLeft(this.value)" value="0">

Draw vertical line on top of image

I've written a small Soundcloud player for my blog. I just added the possibility to skip in time when clicking inside the waveform. Now I'd like to draw a line at the current position in the track and a text indicating the curren position in time.
How would I draw this on top of my waveform (which is an <img>-tag) and which elements would I use?
You can do something like this. Absolutely positioned element (it doesn't really matter what element you choose - divs seem to be the best logical choice for me) to appear on top of the image, and easily scrollable. You denote the bar position using the left property. For your image, it goes between 48px and 358px. See here:
function play() {
document.getElementById('pos').className = 'end';
}
#c {
position: relative;
}
#pos {
height: 50px;
width: 1px;
position: absolute;
left: 48px;
top: 1px;
background: red;
transition: left 5s linear;
}
#pos.end {
left: 358px;
}
<div id="c">
<div id="pos"></div>
<img src="http://i.stack.imgur.com/LUNV8.png" />
</div>
<button onclick="play()">Play</button>
And combining it with your existing waveform listener is also easy:
var pos = document.getElementById('pos');
var wave = document.getElementById('wave');
wave.addEventListener('click', function(e) {
if (e.offsetX < 48)
return;
pos.style.left = e.offsetX + 'px';
});
#c {
position: relative;
}
#pos {
height: 50px;
width: 1px;
position: absolute;
left: 48px;
top: 1px;
background: red;
}
<div id="c">
<div id="pos"></div>
<img id="wave" src="http://i.stack.imgur.com/LUNV8.png" />
</div>

How to do centered <h1> with <hr/> on both sides over a background image

How do I create centered <h1> with <hr/> on both sides over a background image?
I also need it to handle various text lengths, scale well for mobile viewing and have the <hr/> go to 100% width of its container.
I want this look, but over a background image.
There are lots of answers (here, here here and here) for text with lines on either side but all of them rely on using a solid background colour behind the text, which doesn't work for me as the page I want to put this on has a background image.
Here is how I achieve the look above, which handles various lengths of text and scales well:
CSS
.title-box {
height: 2px;
background-color: rgb(215, 0, 0);
text-align: center;
}
.title-outer {
background-color:rgb(230, 230, 230);
position: relative;
top: -0.7em;
}
.title-inner {
margin:0px 20px;
font-size: 17.5px;
font-weight:bold;
color:rgb(100, 100, 100);
}
HTML
<div class="title-box">
<span class="title-outer">
<span class="title-inner">OUR STORY</span>
</span>
</div>
I have tried the method below and it kind of works but it doesn't handle various text widths or scale well due to the <h1> and the <hr/>s being in seperate <div>s:
HTML
<div class="row">
<div class="span4"><hr /></div>
<div class="span4"><h4>OUR STORY</h4></div>
<div class="span4"><hr /></div>
</div>
Note: This is example is using the Bootstrap grid system but that is not part of the problem/solution.
So any ideas how I can get the same look and behaviour but without the backgound colour for the text so it can sit over a background image?
No need JS, here is a pure CSS solution.
CSS
.title-hr hr {
display: inline-block;
width: 30%;
margin: 5px 10px;
border-top: 1px solid #e5e5e5;
}
HTML
<h1 class="title-hr"><hr />My Title<hr /></h5>
Result: http://jsfiddle.net/yptmftr4/
Ok, I've played a bit with this code and here is my solution. Yes, it's a bit dirty because I've used :before and :after, but works.
HTML
<div class="title-box">
<span id="first" class="title-inner">OUR LOOOoo oooo oOONG STORY</span>
</div>
<div class="title-box">
<span id="second" class="title-inner">OUR STORY</span>
</div>
<div class="title-box">
<span id="third" class="title-inner">STORY</span>
</div>
CSS
.title-box {
text-align: center;
}
.title-inner {
margin:0px 20px;
font-size: 17.5px;
font-weight:bold;
position: relative;
color:rgb(100, 100, 100);
}
.title-inner:after, .title-inner:before {
content:"";
float: right;
position: relative;
top: 8px;
height: 2px;
background: red;
}
.title-inner:before {
float: left;
}
jQuery
$(document).ready(function () {
function work() {
$(".title-inner").each(function () {
var full_width = $(window).width();
var id = $(this).attr("id");
var title_width = $("#" + id).width();
var new_width = (full_width - title_width) / 2 - 40;
$('head').append("<style>#" + id + ":before, #" + id + ":after{width:" + new_width + "px !important;}</style>");
});
}
work();
$(window).resize(function () {
work();
});
});
http://jsfiddle.net/ffb3X/4/
Because :before and :after are not part of DOM, I've used .append() function to append style tags in head for every title.
This code will on page load calculate everything, so it's responsive.
This code was posted originally by Arbel but his/her answer disappeared for some reason? I am reposting it (including some mods I've made) because it was the solution I ended up using. Credit where credit is due.
Working jsFiddle: http://jsfiddle.net/pA5Gu/
HTML
<div class="title-box">
<fieldset class="title-outer">
<legend id="titleInner" class="title-inner">OUR STORY</legend>
</fieldset>
</div>
CSS
.title-box {
background-image: url('http://imagezo.com/images/1302-green-bubbles-awesome-background-wallpaper.jpg');
height:100%;
}
.title-outer {
border-top:2px solid rgb(215, 0, 0);
background-color: transparent;
}
.title-inner {
width:auto;
padding:0px 20px;
border: 0;
background-color: transparent;
font-size: 17.5px;
font-weight:bold;
color:rgb(255, 255, 255);
}
jQuery
$(document).ready(function() {
var legendWidth = $('#titleInner').outerWidth();
var margin = 'calc((100% - '+legendWidth+'px) / 2)';
$('#titleInner').css('margin-left', margin);
$('#titleInner').css('margin-right', margin);
});
http://jsfiddle.net/habo/HrfuH/1/
<div class="title-box">
<div class="myContent">
<div class="title-outer"><hr /></div>
<div class="title-inner "><h4>OUR STORY</h4></div>
<div class="title-outer"><hr /></div>
</div>
</div>
.myContent{
display:block;
width:600px;
margin:0 auto;
}
.title-box {
background:#eee;
height:60px;
}
.title-outer{
}
hr {
height: 2px;
background-color:rgb(215, 0, 0);
margin: 2em 0;
width:25%;
float:left;
}
.title-inner {
margin:0px 20px;
font-size: 17.5px;
font-weight:bold;
color:rgb(100, 100, 100);
float:left;
}

Creating a "disabled" look by using a mask

I'm trying to create the following:
Using two images: one as mask (the diagonal lines) and the other the image and text themselves (the mask and image+text are the same size):
..and I just can't get it done!
I've tried all combinations with divs and z-indeces, opacity and background-image.. (should mention I'm noob to html).
Here's one shot I got at it (with only the mask and an image):
div {
position: absolute;
top: 775px;
left: 0px;
height: 188px;
width: 272px;
background-image: url('grey-out.png');
}
img {
z-index: 1000;
}
<div></div>
<img src="41_large.png" />
Which just gives the diagonal lines themselves..
Can someone please help me out?
How do I make that "disabled" look combining the (semi-transparent) mask and the div?
Thanks!
This approach works:
<div id="pspThing" class="disabled">
<img class="disabled" src="http://i.stack.imgur.com/lCTVr.png" />
</div>
#pspThing {
background: transparent url(http://i.stack.imgur.com/WpgNy.jpg) 0 0 no-repeat;
height: 93px;
width: 273px;
border: 1px solid #000;
margin: 0 auto;
overflow: hidden;
}
#pspThing img {
display: none;
opacity: 0.5;
}
#pspThing img.disabled {
display: block;
}
JS Fiddle demo
Bearing in mind that there's no transparency in your striped png (so far as the imgur hosted image is concerned, anyway, so I'm using opacity instead). Also the JS Fiddle demo's a little more complicated than necessary, so's I could show the disabled/enabled states.
Pleass consider this simple snippet. Very universal solution. Acts and feels very much like the 'disable' attribute of input elements. See the snippet
function disable(elementId, enabling) {
el = document.getElementById(elementId);
if (enabling) {
el.classList.remove("masked");
} else
{
el.classList.add("masked");
}
}
.masked {
position: relative;
pointer-events: none;
display: inline-block;
//visibility:hidden; /* Uncomment this for complete disabling */
}
.masked::before {
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
visibility: visible;
opacity: 0.5;
background-color: black;
//background: url('http://i.imgur.com/lCTVr.png'); /* Uncomment this to use the image */
content: "";
}
<button onclick="alert('Now, click \'OK\' then \'Tab\' key to focus next button.\nThen click \'Enter\' to activate it.');">Test</button>
<div id="div1" style="display:inline-block" class="masked">
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button><br/>
<br/>
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button><br/>
<img src="http://i.stack.imgur.com/WpgNy.jpg">
</div>
<button>Dummy</button>
<br/>
<button id="enableBtn" onclick="disable('div1',true);disable('enableBtn',false);disable('disableBtn',true);">Enable</button>
<button id="disableBtn" onclick="disable('div1',false);disable('enableBtn',true);disable('disableBtn',false);" class="masked">Disable</button>
I built an example here.
I doubt that the position:absolute approach is the best way to handle this since you need to know the size of the image.
For doing it by z-index your both images should be in the container with img tag.