CSS using div as a mask - html

So, I've been trying to use an hamburger menu I made from 3 stacked div tags as a mask for a background with a color gradient (the red/yellow one, the blue/violet one is the main background).
The result I would like to achieve is this: result
I know I could simply use a svg file of the hamburger, but I would like to do this with the div tags I made so I can later animate them to transition in a x icon, as this whole thing is to create a sidebar.
html, body {
height: 100%;
background: linear-gradient(-45deg, #c850c0, #4158d0);
font-family: 'Poppins', sans-serif;
font-size: 20px;
}
.hamburger{
background: linear-gradient(-45deg, #faf617, #ff0000);
width: 50px;
height: 40px;
position: absolute;
top: 20px;
padding: 1px;
left: 20px;
}
.line{
width: 30px;
height: 4px;
margin: 7px;
border-radius: 2px;
display: block;
background-color: #ffffff;
}
<div class="hamburger">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>

Give them the same background and play with position to create the effect:
html {
height: 100%;
background: linear-gradient(-45deg, #c850c0, #4158d0);
}
.hamburger {
position: absolute;
top: 20px;
left: 20px;
/* add this to see that it's the same
background: linear-gradient(-45deg, #faf617, #ff0000); */
}
.line {
width: 30px;
height: 4px;
margin: 7px;
border-radius: 2px;
background: linear-gradient(-45deg, #faf617, #ff0000);
background-size:calc(100% + 2*7px) calc(100%*3 + 4*7px);
}
.line:nth-child(1) {
background-position:-7px -7px; /*7 = 7*1 + 0*4*/
}
.line:nth-child(2) {
background-position:-7px -18px; /*18 = 7*2 + 1*4*/
}
.line:nth-child(3) {
background-position:-7px -29px; /*29 = 7*3 + 2*4*/
}
<div class="hamburger">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
Another idea where you can use mask with a pseudo element. The trick is to not set position to .line to make the pseudo element positionned relatively to the hamburger then you hide the overflow using mask:
html {
height: 100%;
background: linear-gradient(-45deg, #c850c0, #4158d0);
}
.hamburger {
position: absolute;
top: 20px;
left: 20px;
/* add this to see that it's the same
background: linear-gradient(-45deg, #faf617, #ff0000); */
}
.line {
width: 30px;
height: 4px;
margin: 7px;
border-radius: 2px;
-webkit-mask:linear-gradient(#fff,#fff);
mask:linear-gradient(#fff,#fff);
}
.line:before {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background: linear-gradient(-45deg, #faf617, #ff0000);
}
<div class="hamburger">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>

Related

How can I make a background continuous?

As you can see, in the title block, only the upper half has background, I want the whole title block to have the same background. Of course, I can set background for the title block itself, but this way the background won't look continuous, as you can see in the fiddle.
Is there a way to achieve this with pure css?
.header {
width: 100%;
padding-top: 30%;
background: url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%;
background-size: cover;
position: relative;
}
.title {
position: absolute;
transform: translateY(-50%);
padding: 8px 24px;
font-size: 24px;
background: none;
border-radius: 50px;
border: 4px solid white;
left: 10%
}
body {
background-color: #eee
}
.title.b {
background: url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%;
background-size: contain
}
<div class="header">
<div class="title"> Title Title </div>
</div>
<div class="header" style="margin-top:60px">
<div class="title b">
Title Title
</div>
</div>
Fiddle: https://jsfiddle.net/s7pkr2w8/1/
Here is an idea using clipping and masking
.header {
padding-top: 30%;
position: relative; /* relative here !! **/
display:flex;
z-index:0;
}
.title {
font-size: 24px;
color:#fff;
border-radius: 50px;
margin:auto auto 0 10%; /* position the element using flexbox instead of absolute */
-webkit-mask:linear-gradient(#fff 0 0); /* clip the pseudo element to only the title shape*/
}
/* extra div needed for the white border*/
.title > div {
padding: 8px 24px;
border:4px solid #fff;
position:relative;
border-radius: inherit;
}
/**/
/* two pseudo element relative to the container having the same background
to have the continuous effect
*/
.title::before,
.header::before{
content:"";
position:absolute;
z-index:-1;
top:0;
bottom:0;
left:0;
right:0;
background: url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%/cover;
}
.header::before {
clip-path:inset(0 0 20px 0); /* cut 20px from the bottom to be around the middle of the title */
}
body{
background-color:#eee
}
<div class="header">
<div class="title">
<div>Title Title</div>
</div>
</div>
you can try to set the background on a parent element or just event to the whole body:
body{
background:url('https://cchc-herald.org/images/discuss_cavatar/titleSampleBG.jpg') no-repeat 50% 50%;
background-size:cover;
}

How to draw a circle inside a right angled triangle in html page?

Is it possible to draw a circle inside a right angled triangle in an HTML page using CSS.
Is it also possible to put some text inside it?
The output should be something like
Here's a minimal example that can hopefully get you started: https://jsfiddle.net/8mfx9qhj
I always end up back at https://css-tricks.com/the-shapes-of-css/ when trying to do things like this with CSS. If you're looking to draw a more complex diagram like the image above, I'd recommend doing it with SVG and some drawing library (e.g. http://snapsvg.io/) instead, which will make the job a lot easier.
Code from linked jsfiddle:
<div id="triangle">
<div id="circle">
hello world
</div>
</div>
#triangle {
position: relative;
width: 0;
height: 0;
border-bottom: 100px solid blue;
border-right: 150px solid transparent;
}
#circle {
position: absolute;
top: 30px;
width: 38px;
height: 38px;
padding: 16px;
border-radius: 50%;
background: red;
color: white;
}
Output:
As you can see lots of css and nested html is needed to create the shape. As #evnp said
SVG would make your life easier.
#right-triangle-red {
width: 0;
height: 0;
border-bottom: 300px solid red;
border-right: 500px solid transparent;
position: relative;
}
#right-trianlg-white {
width: 0;
height: 0;
border-bottom: 276px solid white;
border-right: 461px solid transparent;
position: relative;
z-index: 10;
top: 16px;
left: 10px;
}
.triangles {
position: relative;
}
#circle-purple {
width: 200px;
height: 200px;
background: purple;
border-radius: 50%;
position: relative;
}
#circle-white {
width: 190px;
height: 190px;
background: white;
border-radius: 50%;
position: relative;
z-index: 20;
top: 5px;
left: 5px;
}
#circle-green {
width: 15px;
height: 15px;
background: green;
border-radius: 50%;
position: absolute;
z-index: 40;
top: 86px;
left: 86px;
}
.circles {
position: relative;
z-index: 20;
top: -207px;
left: 10px;
}
.label {
font-size: 30px;
color: red;
z-index: 30;
position: relative;
}
.label-b {
top: -219px;
left: 510px;
}
.label-c {
top: -205px;
left: -31px;
}
.label-d {
color: green;
top: -341px;
left: 64px;
}
.shape-container {
position: relative;
}
<span class="label label-a">
A
</span>
<div class="shape-container">
<div class="triangles">
<div id="right-triangle-red">
<div id="right-trianlg-white">
</div>
</div>
</div>
<div class="circles">
<div id="circle-purple">
<div id="circle-white">
<div id="circle-green">
</div>
</div>
</div>
</div>
</div>
<span class="label label-b">
B
</span>
<span class="label label-c">
C
</span>
<span class="label label-d">
D
</span>
I will consider this previous answer to draw the triangle and will add another gradient for the circle
.triangle {
--t:10; /* Thickness */
--c:black; /* Color */
--r:25px; /* Radius*/
width:100px;
height:100px;
display:inline-block;
border:calc(var(--t)*1px) solid transparent;
border-image:
linear-gradient(to bottom left,
transparent 49.5%,var(--c) 50%) var(--t);
background:
/* Left side */
linear-gradient(to bottom left,
transparent 49.5%,var(--c) 50% calc(50% + var(--t)*1px),
transparent calc(50% + var(--t)*1px + 1px)),
/* circle */
radial-gradient(circle var(--r) at var(--r) calc(100% - var(--r)),
transparent calc(100% - 5px), yellow calc(100% - 4px) 99%,transparent 100%);
background-origin:border-box,padding-box;
background-repeat:no-repeat;
}
body {
background:pink;
}
<div class="triangle"></div>
<div class="triangle" style="--t:5;--c:blue;width:150px;--r:33px;"></div>
<div class="triangle" style="--t:8;--c:red;height:150px;--r:31px;"></div>
<div class="triangle" style="--t:15;--c:green;width:80px;--r:20px"></div>
You simply need some math to calculate the radius. Related: https://math.stackexchange.com/q/2247599/695610
As you can see, it's a simple div at the end so you can write your content inside it and align it like you want

How do you create a div with a triangle shape using css only?

I want a div that has an "angle like shape on the left". How can I create this with CSS3 only? I am assuming this requires 2 divs? I know I can make a rectangle div and fill it back, and have yellow text. Though I don't know what I can do to make the triange shape on the left. Can it be done with done div only? Or does it need 2? Looking for the best way to do this.
You can achieve this using linear-gradient. Demo:
.text {
width: 400px;
background-image: linear-gradient(45deg, transparent 50px, black 50px);
padding-left: 100px;
color: yellow;
}
<div class="text">
<h1>Some Name Here</h1>
</div>
Why not try something like this:
.triangle {
width: 0;
height: 0;
border: 50px solid black;
border-bottom-color: transparent;
border-left-color: transparent;
float: left;
}
.text {
width: 400px;
height: 100px;
background-color: black;
float: left;
color: yellow;
}
<div class="triangle"></div>
<div class="text"><h1>Some Name Here</h1></div>
See How do CSS triangles work? for more info on this.
You can use of Pseudo Elements ::before or ::after
.triangle {
padding: 10px;
position: relative;
background-color: #000;
color: yellow;
display: inline-block;
margin-left: 40px;
}
.triangle::after {
content: "";
position: absolute;
border: 19px solid #000;
height: 0;
width: 0;
left: -38px;
top: 0;
bottom: 0;
margin: auto;
border-left-color: transparent;
border-bottom-color: transparent;
}
<div class="triangle">
text-here
</div>
Link for reference
Style Accordingly.
You can use clip-path but it has not so good browser support. I'm using 100vmax 100vmax here to achieve 45 degrees clipping. Demo:
.text {
width: 400px;
background-color: black;
-webkit-clip-path: polygon(100vmax 100vmax, 0% 0%, 100% 0%, 100% 100%);
clip-path: polygon(100vmax 100vmax, 0% 0%, 100% 0%, 100% 100%);
padding-left: 100px;
color: yellow;
}
<div class="text">
<h1>Some Name Here</h1>
</div>

How to center a button right in the middle of two divs, but a z-index order higher?

I'm having issues implementing the following design:
My specific problem is the blue button in the center, and subsequently the arrow in the middle of it.
I currently have two elements:
<div id="hero">
<!-- this is where the black background is -->
</div>
<div id="content">
<!-- This is the white background -->
</div>
Now somehow, I have to put that blue button right in the middle of the hero and the rest of the content, and on top of that, there has to be an arrow in the middle of that button and the button.
I don't know where to start doing this. I'm assuming the button needs to have a z-index higher than the hero and the content, and the arrow has to have a z-index higher than the blue button.
But in what part of the HTML document do I write the tags in? For example:
<div id="hero">
<!-- This is the black background -->
</div>
<button type="button" class="cta">Button Tex bla bla</button>
<button type="button" class="arrow"><img src="..."></button>
<div id="content">
<!-- This is the white background -->
</div>
I'm using Bootstrap for the grid.
With the combination of position and margin you can position the elements the way you want:
#container{
position: relative;
border: 1px solid black;
}
#hero{
height: 150px;
background-color: black;
color: white;
text-align: center;
padding-top: 30px;
}
#hero span{
max-width: 300px;
display: block;
margin: 0 auto;
font-size: 30px;
}
#content{
height: 200px;
background-color: white;
color: black;
text-align: center;
font-size: 15px;
padding-top: 80px;
}
#content span{
max-width: 300px;
display: block;
margin: 0 auto;
font-size: 25px;
}
.cta{
position: absolute;
width: 200px;
height: 50px;
left: 50%;
margin-left: -100px;
margin-top: -25px;
background-color: #2196F3;
border: none;
color: white;
}
.arrow{
position: absolute;
border: none;
left: 50%;
width: 30px;
height: 30px;
margin-left: -15px;
margin-top: 10px;
background-color: black;
color: white;
border-radius: 50%;
}
<div id="container">
<div id="hero">
<span>hero headline so cool buy now</span>
</div>
<button type="button" class="cta">Button Tex bla bla</button>
<button type="button" class="arrow">V</button>
<div id="content">
<span>give me more money</span>
</div>
</div>
I would use style tag relative position and offset the top by half of the buttons length. And yes use the z-index to place it on top.
<div id="hero">
<!-- This is the black background -->
</div>
<button type="button" class="cta" style="position:relative; top: -20px;">Button Tex bla bla</button>
<button type="button" class="arrow" style="position:relative; top: -20px;"><img src="..."></button>
<div id="content">
<!-- This is the white background -->
</div>
here you can do
.cta{
position:relative;
background: #3498db;
background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
background-image: -moz-linear-gradient(top, #3498db, #2980b9);
background-image: -ms-linear-gradient(top, #3498db, #2980b9);
background-image: -o-linear-gradient(top, #3498db, #2980b9);
background-image: linear-gradient(to bottom, #3498db, #2980b9);
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0px;
font-family: Arial;
color: #ffffff;
font-size: 24px;
padding: 10px 40px 10px 40px;
text-decoration: none;
}
.arrow{
position: absolute;
left: 110px;
top: 20px;
}
<div id="hero">
<!-- This is the black background -->
</div>
<button type="button" class="cta">
Button Tex bla bla
<a href="#" class="arrow">
<img src="http://files.softicons.com/download/toolbar-icons/super-mono-sticker-icons-by-double-j-design/png/64/arrow-down.png">
</a>
</button>
<div id="content">
<!-- This is the white background -->
</div>
use position:relative; on parent element and position:absolute; on child element
Here is my try :) - JSFiddle
I am basically wrapping the buttons in an absolute div button-wrapper ( absolute to the Hero ) then I just center its content using text-align:center
<div id="hero">
<h1>My Title</h1>
<div class="button-wrapper">
<button type="button" class="blue cta">Button Tex bla bla</button>
<button type="button" class="arrow"><span>↓</span>
</button>
</div>
</div>
<div id="content">
<!-- This is the white background -->
</div>
#hero {
background:#000;
height: 300px;
text-align:center;
}
h1 {
color:white;
padding: 50px;
}
button {
display: block;
position: relative;
margin: 0 auto;
}
.button-wrapper {
position: absolute;
bottom: 0;
right: 0;
left: 0;
margin: auto;
text-align: center;
}
#content {
background-color:yellow;
height:500px;
z-index:10;
}
.blue {
background: #0185CB;
color:#fff;
}
.cta {
border: 0;
font-size: 25px;
padding: 15px;
-webkit-transform: rotate(-358deg) scale(1) skew(0deg) translate(0px);
-moz-transform: rotate(-358deg) scale(1) skew(0deg) translate(0px);
-o-transform: rotate(-358deg) scale(1) skew(0deg) translate(0px);
-ms-transform: rotate(-358deg) scale(1) skew(0deg) translate(0px);
}
.arrow {
background: #000;
border-radius: 50%;
width: 50px;
height: 50px;
border-color: transparent;
color: #fff;
font-weight: bold;
font-size: 25px;
line-height: 25px;
text-align: center;
margin-top: -20px;
}
.arrow > span {
position: absolute;
left: 0;
right: 0;
top: 50%;
transform: translateY(-50%);
}
Are the two buttons doing the same thing? If yes, you should use only one button.
You can try this since you are using bootstrap:
HTML
<div id="content">
//container fluid will always have full width of its parent
<div class="container-fluid" id="my-button-wrapper">
//you create a row in order to manage the div inside
<div class="row">
//create a column as wide as you want and push it to center it
<div class="col-sm-8 col-sm-push-2">
//then you add the button
<button type="button" class="my-button-class">Button text</button>
</div>
</div>
</div>
</div>
CSS
#my-button-wrapper {
margin-top: -20px;
}
//give the position relative in order to position the arrow at its center
.my-button-class {
background: #000;
position: relative;
}
//this will add an element after the button content
.my-button-class:after {
position: absolute;
width: 30px;
height: 30px;
bottom: -15px;
left: 50%;
margin-left: -15px;
background-image: url('your-image-url');
content: "";
}
Of course width and height of my element are just for example, you should use your own sizes or bootstrap's ones.

How to bring an image appear on an image on-hover using sprites

I am trying to make some line appear(say about 10px) after hovering mouse on an image at the bottom of the image
I saw this on MTV's website in their "You would also like these" section below every post.They use css-background sprites to do that.
I am going mad after repeated failed attempts to recreate.Everythings works,except the main onhover line coming up.
This is my code so far
CSS
.yel_strip{background-position:-280px -495px; width:165px; margin:-8px 0 0 0; height:5px; position:absolute; display:none; z-index:1;}
.yel_strip:hover{ background:url(http://mtv.in.com/images/sprite_v1.png) no-repeat;}
HTML
<div class="movieL hover_thumb">
<div><img width="165" height="93" alt="" src=""/>
<div class="yel_strip"></div>
</div> </div>
Any help would be appreciated.Thanks
I've made working fiddle for you with no extra not needed markup in your html: http://jsfiddle.net/PJMPw/3/
Your HTML:
<a href="#" class="hoverable">
<img src="http://placekitten.com/g/300/300" />
</a>
And CSS:
.hoverable {
display: block;
position: relative;
width: 300px;
height: 300px;
overflow: hidden;
}
.hoverable:hover:after {
bottom: 0;
}
.hoverable:after {
content: "";
position: absolute;
width: 100%;
height: 10px;
bottom: -10px;
left: 0;
background: -moz-linear-gradient(left, rgba(46,170,232,1) 0%, rgba(255,235,137,1) 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(46,170,232,1)), color-stop(100%,rgba(255,235,137,1)));
background: -webkit-linear-gradient(left, rgba(46,170,232,1) 0%,rgba(255,235,137,1) 100%);
background: -o-linear-gradient(left, rgba(46,170,232,1) 0%,rgba(255,235,137,1) 100%);
background: -ms-linear-gradient(left, rgba(46,170,232,1) 0%,rgba(255,235,137,1) 100%);
background: linear-gradient(to right, rgba(46,170,232,1) 0%,rgba(255,235,137,1) 100%);
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
This is the HTML:
Replace http://yoururl with your url.
<div class="container">
<span></span>
</div>
This is the CSS:
Replace http//yourimage with your image address.
.container {
width: 165px;
height: 93px;
background: url('http//yourimage');
position: relative;
}
#internal_image {
display: blocK;
width: 165px;
height: 93px;
}
#internal_image:hover span {
display: block;
width: 165px;
height: 5px;
position: absolute;
background: url(http://mtv.in.com/images/sprite_v1.png) no-repeat;
background-position: -280px -495px;
bottom: 0;
}
EDIT: Added EXAMPLE: http://jsfiddle.net/BmwCe/3/
The simples thing you could do is set a border on the image on hover.
i.e
markup
<div class="image-container">
<img src="../styles/images/Desert.jpg" />
</div>
css
.image-container {
display: inline-block;
position: relative;
}
.image-container img {
width: 100px;
height: 100px;
}
.image-container img:hover {
border-bottom: 5px solid green;
}
If you insist that you want to have a background image instead of border you could do this
<div class="image-container">
<img src="../styles/images/Desert.jpg" />
<div class="shiny-border"></div>
</div>
.image-container {
display: inline-block;
position: relative;
}
.image-container img {
width: 100px;
height: 100px;
}
.image-container .shiny-border {
position: absolute;
top: 90px; //subtract the height of the shiny-border from 100px which is the height // to have the inset effect of the image
height: 10px;
width: 100%;
display: none;
}
.image-container img:hover + .shiny-border {
display: block;
background-image: url(../styles/images/Hydrangeas.jpg);
}