Div divided into five traingles - css

Here I'm trying to divide a <div> into 5 different triangles.
I have tried using CSS using borders but couldn't achieve the desired output. Can anyone point me in the right direction. How should I achieve this output.
.box {
display: flex;
width: 100%;
height: 100vh;
background-color: #ccc;
}
.traingle {
width: 20%;
height: 400px;
border: 3px solid #000;
}
<div class="box">
<div class="traingle"></div>
<div class="traingle"></div>
<div class="traingle"></div>
<div class="traingle"></div>
<div class="traingle"></div>
</div>

SVG and a number of polygons
div {
width: 80%;
margin: 1em auto;
}
<div>
<svg viewbox="0 0 200 100">
<polygon points="0,100 100,100 0,50 "
style="stroke:#000000; fill:#ff0000; stroke-width: 1;"/>
<polygon points="0,50 100,100 50,00 0,0 "
style="stroke:#000000; fill:#00ff00; stroke-width: 1;"/>
<polygon points="100,100 50,00 150,0"
style="stroke:#000000; fill:#0000ff; stroke-width: 1;"/>
<polygon points="100,100 200,50 200,0 150,0"
style="stroke:#000000; fill:#00ffff; stroke-width: 1;"/>
<polygon points="100,100 200,100, 200,50"
style="stroke:#000000; fill:#ff00ff; stroke-width: 1;"/>
</svg>
</div>

Use multiple background if it's only about visual result:
.box {
width:250px;
height:150px;
background:
linear-gradient(to bottom right,transparent 49.5%,green 50%) bottom right/50% 50%,
linear-gradient(to bottom right,transparent 49.5%,purple 50%) bottom right/50% 200%,
linear-gradient(to bottom left ,transparent 49.5%,yellow 50%) bottom left/50% 50%,
linear-gradient(to bottom left ,transparent 49.5%,red 50%) bottom left/50% 200%,
blue;
background-repeat:no-repeat;
}
<div class="box">
</div>
Here is an idea with clip-path if you want to consider different divs:
.box {
width:450px;
height:250px;
position:relative;
overflow:hidden;
z-index:0;
}
.box > div {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-size:cover;
background-position:center;
}
.box > div:nth-child(2),
.box > div:nth-child(4){
right:50%;
-webkit-clip-path:polygon(0 0,100% 100%, 0 100%);
clip-path:polygon(0 0,100% 100%, 0 100%);
}
.box > div:nth-child(3),
.box > div:nth-child(5){
left:50%;
-webkit-clip-path:polygon(100% 0,100% 100%, 0 100%);
clip-path:polygon(100% 0,100% 100%, 0 100%);
}
.box > div:nth-child(2),
.box > div:nth-child(3){
top:-50%;
}
.box > div:nth-child(4),
.box > div:nth-child(5){
top:50%;
}
/*Irrelevant, simply to illustrate hover effect*/
.box > div:hover {
filter:grayscale(100%);
}
<div class="box">
<div style="background-image:url(https://picsum.photos/id/1/1000/800)"></div>
<div style="background-image:url(https://picsum.photos/id/10/1000/800)"></div>
<div style="background-image:url(https://picsum.photos/id/90/1000/800)"></div>
<div style="background-image:url(https://picsum.photos/id/102/1000/800)"></div>
<div style="background-image:url(https://picsum.photos/id/118/1000/800)"></div>
</div>

Related

The text inside the svg is not visible when it overflows

<div class="base">
<div class="headline">
<svg>
<clippath id="mask">
<text dominant-baseline="hanging">Awesome Headline</text>
</clippath>
</svg>
</div>
</div>
<style>
body {
background: url('https://images.pexels.com/photos/2179483/pexels-photo-2179483.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1')no-repeat;
background-size:cover;
}
.headline {
font: 50px system-ui, sans-serif;
font-weight: 900;
backdrop-filter:invert(100%);
clip-path: url(#mask);
letter-spacing:30px;
}
.base {
position:absolute;
top:20%;
left:20%;
width:400px;
height:auto;
background-color:red;
transform: translate(-20%, -20%);
}
</style>
(https://i.stack.imgur.com/iQbqc.png)
I want to do like in this picture.
I don't know if it's because of the svg but it overflows from the width I gave the base class.
what I want to do is to make the text go down to a bottom line

coloring percentage part of an annulus [duplicate]

I've found pretty nice "percent pie chart" and want to create it with CSS only. No animation is required. Just static "picture".
I understand If I wanna create this kind of chart I need to use elements like these
The questions are
How to create element #2 ?
How to manage shape of element #2 for smaller (5%) or higher percent (80%) values ?
New answer 2021
With some modern techniques we can improve the code. You can have rounded edges and also consider animation:
#property --p{
syntax: '<number>';
inherits: true;
initial-value: 1;
}
.pie {
--p:20; /* the percentage */
--b:22px; /* the thickness */
--c:darkred; /* the color */
--w:150px; /* the size*/
width:var(--w);
aspect-ratio:1/1;
position:relative;
display:inline-grid;
margin:5px;
place-content:center;
font-size:25px;
font-weight:bold;
font-family:sans-serif;
}
.pie:before,
.pie:after {
content:"";
position:absolute;
border-radius:50%;
}
.pie:before {
inset:0;
background:
radial-gradient(farthest-side,var(--c) 98%,#0000) top/var(--b) var(--b) no-repeat,
conic-gradient(var(--c) calc(var(--p)*1%),#0000 0);
-webkit-mask:radial-gradient(farthest-side,#0000 calc(99% - var(--b)),#000 calc(100% - var(--b)));
mask:radial-gradient(farthest-side,#0000 calc(99% - var(--b)),#000 calc(100% - var(--b)));
}
.pie:after {
inset:calc(50% - var(--b)/2);
background:var(--c);
transform:rotate(calc(var(--p)*3.6deg - 90deg)) translate(calc(var(--w)/2 - 50%));
}
.animate {
animation:p 1s .5s both;
}
.no-round:before {
background-size:0 0,auto;
}
.no-round:after {
content:none;
}
#keyframes p{
from{--p:0;}
}
body {
background:#ddd;
}
<div class="pie" style="--p:20"> 20%</div>
<div class="pie" style="--p:40;--c:darkblue;--b:10px"> 40%</div>
<div class="pie no-round" style="--p:60;--c:purple;--b:15px"> 60%</div>
<div class="pie animate" style="--p:80;--c:orange;"> 80%</div>
<div class="pie animate no-round" style="--p:90;--c:lightgreen"> 90%</div>
Old answer
You can do this with multiple background.
From 0% to 50%:
.box {
width: 100px;
height: 100px;
display: inline-block;
border-radius: 50%;
padding: 5px;
background:
linear-gradient(#ccc, #ccc) content-box,
linear-gradient(var(--v), #f2f2f2 50%, transparent 0),
linear-gradient(to right, #f2f2f2 50%, blue 0);
}
<div class="box" style="--v:-90deg"></div><!-- 0% -->
<div class="box" style="--v:-45deg"></div><!-- 12.5% -->
<div class="box" style="--v: 0deg"></div><!-- 25% -->
<div class="box" style="--v: 45deg"></div><!-- 37.5% -->
<div class="box" style="--v: 90deg"></div><!-- 50% -->
<p>The formula is [p = (18/5) * x - 90]. <small>Where x is the percentage and p the degree</small></p>
<p>for x = 5% --> p = -72deg </p>
<div class="box" style="--v:-72deg"></div>
From 50% to 100%:
.box {
width:100px;
height:100px;
display:inline-block;
border-radius:50%;
padding:5px;
background:
linear-gradient(#ccc,#ccc) content-box,
linear-gradient(var(--v), blue 50%,transparent 0),
linear-gradient(to right, #f2f2f2 50%,blue 0);
}
<div class="box" style="--v:-90deg"></div><!-- 50% -->
<div class="box" style="--v:-45deg"></div><!-- 62.5% -->
<div class="box" style="--v: 0deg"></div><!-- 75% -->
<div class="box" style="--v: 45deg"></div><!-- 87.5% -->
<div class="box" style="--v: 90deg"></div><!-- 100% -->
<p>The formula is [p = (18/5) * x - 270]. <small>Where x is the percentage and p the degree</small></p>
<p>for x = 80% --> p = 18deg </p>
<div class="box" style="--v:18deg"></div>
You can combine both like this:
.box {
width:100px;
height:100px;
display:inline-block;
border-radius:50%;
padding:5px;
background:
linear-gradient(#ccc,#ccc) content-box,
linear-gradient(var(--v), #f2f2f2 50%,transparent 0) 0/calc(var(--s)*100%) ,
linear-gradient(var(--v), blue 50%,transparent 0) 0/calc((1 - var(--s))*100%),
linear-gradient(to right, #f2f2f2 50%,blue 0);
}
<div class="box" style="--v:-90deg;--s:1"></div>
<div class="box" style="--v:0deg;--s:1"></div>
<div class="box" style="--v:90deg;--s:1"></div>
<div class="box" style="--v:0deg;--s:0"></div>
<div class="box" style="--v:90deg;--s:0"></div>
Now we can optimize like below to consider percetange value:
.box {
--v:calc( ((18/5) * var(--p) - 90)*1deg);
width:100px;
height:100px;
display:inline-block;
border-radius:50%;
padding:10px;
background:
linear-gradient(#ccc,#ccc) content-box,
linear-gradient(var(--v), #f2f2f2 50%,transparent 0) 0/min(100%,(50 - var(--p))*100%),
linear-gradient(var(--v), transparent 50%,blue 0) 0/min(100%,(var(--p) - 50)*100%),
linear-gradient(to right, #f2f2f2 50%,blue 0);
}
<div class="box" style="--p:5;"></div>
<div class="box" style="--p:20;"></div>
<div class="box" style="--p:50;"></div>
<div class="box" style="--p:60;"></div>
<div class="box" style="--p:75;"></div>
<div class="box" style="--p:100;"></div>
Related question to get another version: Creating a static pie chart with CSS
We can also consider mask to add transparency:
.box {
--v:calc( ((18/5) * var(--p) - 90)*1deg);
width:100px;
height:100px;
display:inline-block;
border-radius:50%;
padding:10px;
background:
linear-gradient(var(--v), #f2f2f2 50%,transparent 0) 0/min(100%,(50 - var(--p))*100%),
linear-gradient(var(--v), transparent 50%,blue 0) 0/min(100%,(var(--p) - 50)*100%),
linear-gradient(to right, #f2f2f2 50%,blue 0);
-webkit-mask:
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
-webkit-mask-composite:destination-out;
mask-composite:exclude;
}
body {
background:linear-gradient(to right,red,yellow);
}
<div class="box" style="--p:5;"></div>
<div class="box" style="--p:20;"></div>
<div class="box" style="--p:50;"></div>
<div class="box" style="--p:60;"></div>
<div class="box" style="--p:75;"></div>
<div class="box" style="--p:100;"></div>
Also like below:
.box {
--v:calc( ((18/5) * var(--p) - 90)*1deg);
width:100px;
height:100px;
display:inline-block;
border-radius:50%;
padding:10px;
background:
linear-gradient(var(--v), transparent 50%,blue 0) 0/min(100%,(var(--p) - 50)*100%),
linear-gradient(to right, transparent 50%,blue 0);
-webkit-mask:
linear-gradient(var(--v), #f2f2f2 50%,transparent 0) 0/min(100%,(50 - var(--p))*100%),
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
-webkit-mask-composite:destination-out;
mask-composite:exclude;
}
body {
background:linear-gradient(to right,red,yellow);
}
<div class="box" style="--p:5;"></div>
<div class="box" style="--p:20;"></div>
<div class="box" style="--p:50;"></div>
<div class="box" style="--p:60;"></div>
<div class="box" style="--p:75;"></div>
<div class="box" style="--p:100;"></div>
Related: Border Gradient with Border Radius
Using the new conic gradient, this can be managed with a single div which just landed in Chrome as an experimental property.
Image of Result
:root {
--size: 100px;
--bord: 10px;
}
.chart {
width: var(--size);
height: var(--size);
margin: 1em auto;
border-radius: 50%;
background-image: conic-gradient(lightseagreen var(--value), lightgrey var(--value));
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.chart::after {
content: "";
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: calc(100% - var(--bord));
height: calc(100% - var(--bord));
background: white;
border-radius: inherit;
}
p {
position: relative;
z-index: 1;
font-size: 2em;
}
.x-60 {
--value: 60%;
}
.x-20 {
--value: 20%;
}
<div class="chart x-60">
<p>60%</p>
</div>
<div class="chart x-20">
<p>20%</p>
</div>
With thanks for Temani Afif it's possible to achieve this without the pseudo element using a border and leveraging background-clip...
background:
linear-gradient(white,white) padding-box,
conic-gradient(lightseagreen var(--value), lightgrey var(--value)) border-box;
:root {
--size: 100px;
--bord: 10px;
}
.chart {
width: var(--size);
height: var(--size);
margin: 1em auto;
border: var(--bord) solid transparent;
border-radius: 50%;
background: linear-gradient(white, white) padding-box, conic-gradient(lightseagreen var(--value), lightgrey var(--value)) border-box;
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
}
.x-60 {
--value: 60%;
}
.x-20 {
--value: 20%;
}
<div class="chart x-60">
<p>60%</p>
</div>
<div class="chart x-20">
<p>20%</p>
</div>
Hey gus you can add small CSS styles
.circle {
position: relative;
top: 5px;
left: 5px;
text-align: center;
width: 100px;
height: 100px;
border-radius: 100%;
background-color: #ffffff;
}
.circle-border {
position: relative;
text-align: center;
width: 110px;
height: 110px;
margin-left: 30%;
border-radius: 100%;
background-color: #E53B3B;
background: linear-gradient(0deg, lightgray 100%, black 0%)
}
HTML
<div class="circle-border" id="circleElement">
<div class="circle" id="circleElementValue">
aaa
</div>
</div>
JS : display the % of filling automatically
// let i = 75;
// let deg=Math.round(1.8*i);
// let completed=0;
// let remaining=100-i;
// // completed = (remaining>50)? 50 : completed;
// let backgroundStlye = 'linear-gradient('+deg+'deg, lightgray '+remaining +'%, black '+completed + '%)';
// setTimeout(function(){
// console.log(backgroundStlye);
// document.getElementById("circleElement").style.background =backgroundStlye;
// },i*100);
for(let i=1;i<=100;i++){
let deg=Math.round(1.8*i);
let completed=i;
let remaining=100-i;
completed = (remaining<50)? 0 : completed;
let backgroundStlye = 'linear-gradient('+deg+'deg, lightgray '+remaining +'%, black '+completed + '%)';
setTimeout(function(){
console.log(backgroundStlye);
document.getElementById("circleElement").style.background =backgroundStlye;
document.getElementById("circleElementValue").innerHTML = i+'%';
},i*100);
}
I created sample https://codepen.io/arun-b-shet/pen/mdVVWXo
HOPE you enjoyyyyyyyyyyyy

How to create cube window with css?

I found this image online and tryed to create these boxes with css. I failed. Has anyone an idea how to solve this problem?
This is my code. The problem is the left bar:
.box {
position: relative;
margin: 10px;
display: inline-block;
}
.content {
border-top: 2px solid red;
border-right: 2px solid red;
padding: 5px;
}
.box:before {
content: '';
position: absolute;
top: 0;
left: -10px;
height: 100%;
width: 10px;
background-color: red;
transform: skew(-45deg) rotate(-45deg);
}
.box:after {
content: '';
position: absolute;
bottom: -10px;
left: -5px;
height: 10px;
width: 100%;
background-color: red;
transform: skew(-45deg);
}
<div class="box">
<div class="content">
CONTENT<br />
Some more content
</div>
</div>
border-image with linear-gradient can do it:
.box {
margin: 10px;
display: inline-block;
border-style:solid;
border-width:2px 2px 15px 15px;
border-image-slice:2 2 15 15; /* same as border-width*/
border-image-source:linear-gradient(-45deg,transparent 9px,red 0 calc(100% - 9px),transparent 0);
}
.content {
padding: 5px;
}
<div class="box">
<div class="content">
CONTENT<br> Some more Content
</div>
</div>
<div class="box">
<div class="content">
CONTENT<br> more Content
</div>
</div>
<div class="box">
<div class="content">
AA BB
</div>
</div>
With CSS variables to have better control:
.box {
--b:2; /* border length (without unit!!)*/
--c:15; /* the cube perspective (without unit!!)*/
--g:calc((var(--c) - var(--b))*0.707px); /* 0.707 = cos(45deg) = sin(45deg) */
margin: 10px;
display: inline-block;
border-style:solid;
border-width:calc(var(--b)*1px) calc(var(--b)*1px) calc(var(--c)*1px) calc(var(--c)*1px);
border-image-slice:var(--b) var(--b) var(--c) var(--c);
border-image-source:linear-gradient(-45deg,transparent var(--g),red 0 calc(100% - var(--g)),transparent 0);
}
.content {
padding: 5px;
}
<div class="box">
<div class="content">
CONTENT<br> Some more Content
</div>
</div>
<div class="box" style="--b:3;--c:10">
<div class="content">
CONTENT<br> more Content
</div>
</div>
<div class="box" style="--b:1;--c:20">
<div class="content">
AA BB
</div>
</div>
<div class="box" style="--b:1;--c:5">
<div class="content">
AA BB
</div>
</div>
You can also have it in any direction you want:
.box {
--b:2; /* border length (without unit!!)*/
--c:15; /* the cube perspective (without unit!!)*/
--g:calc((var(--c) - var(--b))*0.707px); /* 0.707 = cos(45deg) = sin(45deg) */
margin: 10px;
display: inline-block;
border-style:solid;
}
.bottom-left {
border-width:calc(var(--b)*1px) calc(var(--b)*1px) calc(var(--c)*1px) calc(var(--c)*1px);
border-image-slice:var(--b) var(--b) var(--c) var(--c);
border-image-source:linear-gradient(-45deg,transparent var(--g),red 0 calc(100% - var(--g)),transparent 0);
}
.top-right {
border-width:calc(var(--c)*1px) calc(var(--c)*1px) calc(var(--b)*1px) calc(var(--b)*1px) ;
border-image-slice: var(--c) var(--c) var(--b) var(--b);
border-image-source:linear-gradient(-45deg,transparent var(--g),red 0 calc(100% - var(--g)),transparent 0);
}
.top-left {
border-width:calc(var(--c)*1px) calc(var(--b)*1px) calc(var(--b)*1px) calc(var(--c)*1px) ;
border-image-slice:var(--c) var(--b) var(--b) var(--c);
border-image-source:linear-gradient(45deg,transparent var(--g),red 0 calc(100% - var(--g)),transparent 0);
}
.bottom-right {
border-width:calc(var(--b)*1px) calc(var(--c)*1px) calc(var(--c)*1px) calc(var(--b)*1px) ;
border-image-slice:var(--b) var(--c) var(--c) var(--b);
border-image-source:linear-gradient(45deg,transparent var(--g),red 0 calc(100% - var(--g)),transparent 0);
}
.content {
padding: 5px;
}
<div class="box bottom-left">
<div class="content">
CONTENT<br> Some more Content
</div>
</div>
<div class="box top-right" style="--b:3;--c:10">
<div class="content">
CONTENT<br> more Content
</div>
</div>
<div class="box bottom-right" style="--b:1;--c:20">
<div class="content">
AA BB
</div>
</div>
<div class="box top-left" style="--b:1;--c:5">
<div class="content">
AA BB
</div>
</div>
We can also add more 3D effect by adjusting the coloration and using a different technique with clip-path
.box {
--b:2px; /* border length*/
--c:15px; /* the cube perspective */
--g:calc(var(--c)*0.707); /* 0.707 = cos(45deg) = sin(45deg) */
margin: 10px;
display: inline-block;
padding:var(--b) var(--b) var(--c) var(--c);
background:
linear-gradient(-45deg,transparent var(--g),#cc0404 0) left /var(--c) 100%,
linear-gradient(135deg,transparent var(--g),red 0) bottom /100% var(--c),
linear-gradient(red,red) top /100% var(--b),
linear-gradient(red,red) right /var(--b) 100%;
background-repeat:no-repeat;
clip-path:
polygon(0% calc(var(--c) - var(--b)), calc(var(--c) - var(--b)) 0%,
100% 0%,
100% calc(100% - var(--c) + var(--b)), calc(100% - var(--c) + var(--b)) 100%,
0% 100%);
}
.content {
padding: 5px;
}
<div class="box">
<div class="content">
CONTENT<br> Some more Content
</div>
</div>
<div class="box" style="--b:3px;--c:10px">
<div class="content">
CONTENT<br> more Content
</div>
</div>
<div class="box" style="--b:1px;--c:20px">
<div class="content">
AA BB
</div>
</div>
<div class="box" style="--b:1px;--c:5px">
<div class="content">
AA BB
</div>
</div>
A Codepen demo to play with the code
One way to achieve this effect is to use a ::before pseudo-element with a clip-path applied to it.
In the example below, I have repeatedly used the values 10px and 2px though these could straightforwardly be replaced with CSS Custom Properties such as:
:root {
--cube-depth: 10px;
--cube-border: 2px;
}
and then used throughout the stylesheet as var(--cube-depth) and var(--cube-border).
Working Example:
.cube {
position: relative;
float: left;
margin: 0 4px 8px 4px;
padding: 2px 2px calc(2px + 10px) calc(2px + 10px);
color: rgb(237, 62, 68);
box-sizing: border-box;
}
.cube::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgb(237, 62, 68);
clip-path: polygon(10px 0, 100% 0, 100% calc(100% - 10px), calc(100% - 10px) 100%, 0 100%, 0 10px, 10px 0, 10px calc(100% - 10px), calc(100% - 2px) calc(100% - 10px), calc(100% - 2px) 2px, 10px 2px);
}
.size-80x100 {
width: calc(100px + 10px);
height: calc(80px + 10px);
}
.size-80x200 {
width: calc(200px + 10px);
height: calc(80px + 10px);
}
.size-80x360 {
width: calc(360px + 10px);
height: calc(80px + 10px);
}
.size-80x440 {
width: calc(440px + 10px);
height: calc(80px + 10px);
}
<div class="cube size-80x100">80 x 100</div>
<div class="cube size-80x440">80 x 440</div>
<div class="cube size-80x360">80 x 360</div>
<div class="cube size-80x200">80 x 200</div>

How can aspect-ratio-preservation + centering-in-div be simultaneously satisfied for an img element?

This example from phrogz.net shows how to render an SVG element on either a page of aspect ratio close to 1:2 or 2:1. Either way the SVG appears at 1:1 ratio.
How can the same aspect-ratio-preservation + centering-in-div be simultaneously satisfied for an img element?
jsfiddle
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Sizing SVG & IMG to Fill a Container</title>
<style type="text/css" media="screen">
html, body { background:#eee; margin:0; padding:0; height:100%; }
#foo { position:absolute; left:2%; width:46%; top:2%; height:96%; background:red; }
#bar { position:absolute; left:52%; width:46%; top:2%; height:96%; background:grey; }
svg { position:absolute; top:0; left:0; width:100%; height:100%; background:green; }
img { position:absolute; top:0; left:0; width:100%; height:100%; background:green; }
.face { stroke:#000; stroke-width:20px; stroke-linecap:round }
</style>
</head><body>
<div id="foo">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" viewBox="-350 -250 700 500">
<circle r="200" class="face" fill="yellow"/>
<path fill="none" class="face"
transform="translate(-396,-230)"
d="M487.41,282.411c-15.07,36.137-50.735,61.537-92.333,61.537 c-41.421,0-76.961-25.185-92.142-61.076" />
<circle cx="-60" cy="-50" r="20" fill="#000"/>
<circle cx="60" cy="-50" r="20" fill="#000"/>
</svg>
</div>
<div id="bar">
<img src="https://openclipart.org/image/800px/svg_to_png/196201/Model-T-Ford.png&disposition=attachment" />
</div>
</body></html>
if you set you image as css style you can do it
html, body { background:#eee; margin:0; padding:0; height:100%; }
#foo { position:absolute; left:2%; width:46%; top:2%; height:96%; background:red; }
#bar {
background: url(http://www.pngall.com/wp-content/uploads/2016/07/Car-Download-PNG.png) no-repeat;
background-size: contain;
background-position: center;
position:absolute; left:52%; width:46%; top:2%; height:96%; }
svg { position:absolute; top:0; left:0; width:100%; height:100%; background:green; }
img { position:absolute; top:0; left:0; width:100%; height:100%; background:green; }
.face { stroke:#000; stroke-width:20px; stroke-linecap:round }
<div id="foo">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" viewBox="-350 -250 700 500">
<circle r="200" class="face" fill="yellow"/>
<path fill="none" class="face"
transform="translate(-396,-230)"
d="M487.41,282.411c-15.07,36.137-50.735,61.537-92.333,61.537 c-41.421,0-76.961-25.185-92.142-61.076" />
<circle cx="-60" cy="-50" r="20" fill="#000"/>
<circle cx="60" cy="-50" r="20" fill="#000"/>
</svg>
</div>
<div id="bar">
</div>
Centering
Add position:relative to #bar then add the following to <img>:
position: absolute;
top: 50%;
left: 50%;
transform: translate3D(-50%, -50%, 0);
Maintain AR
Give absolute dimensions to <img> (used min-width and min-height) and add this to <img>:
object-fit:contain;
Here's the modified FIDDLE The dimensions for the <img> and #bar are scaled back to 10% so it is on par with the SVG. Note there is no comparison between SVG and <img> in Snippet, but the code for the <img> is the same with the exception of the scaled dimensions.
SNIPPET
#bar {
min-width:208px;
min-height: 320px;
border: 3px solid red;
position: relative;
}
img {
width: 100%;
min-width: 245px;
height: 100%;
min-height: 160px;
position: absolute;
top: 50%;
left: 50%;
transform: translate3D(-50%, -50%, 0);
object-fit: contain;
outline: 1px dashed blue;
}
<div id="bar">
<img src="https://openclipart.org/image/800px/svg_to_png/196201/Model-T-Ford.png&disposition=attachment" />
</div>

Apply gradient to bottom of container if height is greater than X pixels

Note: I am not simply asking how to put a gradient to the bottom of a div. I'm asking how to only show the gradient if the div is greater than a certain height.
I currently have this:
https://jsfiddle.net/fwtj44bj/3/
I want to apply this gradient to the bottom of any .container that is great than 100px in height:
http://www.colorzilla.com/gradient-editor/#ffffff+0,ffffff+100&0+0,1+100
Meaning that the second row of the first .container should have the gradient over the boxes, but the second .container should have no gradient.
How can this be done?
My thinking is to somehow make use of max-height, but I'm not sure how.
HTML:
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
CSS:
.container {
width: 332px;
margin-bottom: 48px;
}
.box {
width: 100px;
height: 100px;
display: inline-block;
background: red;
margin-right: 8px;
}
You need javascript to get the height of div. after that you can set a class that will add gradient.
If you want gradient over boxes then we use ::after pseudo class.
have a look please
var heights = document.getElementsByClassName('container');
for (var i = 0; i < heights.length; i++) {
var height = heights[i].offsetHeight;
if(height > 104){
heights[i].className += " gradient";
}
}
.container {
width: 332px;
margin-bottom: 48px;
padding:0px;
position:relative;
}
.box {
width: 100px;
height: 100px;
display: inline-block;
background: red;
margin-right: 8px;
}
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#ffffff+0,ffffff+100&0+0,1+100 */
.gradient::after{
display:inline-block;
position:absolute;
z-index:999;
bottom:0px;
left:0px;
width:100%;
height:100%;
content:' ';
background: -moz-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=0 ); /* IE6-9 */
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
Without javascript. Use :after selector.Position your container as relative and then offset the :after element by 100px.
.container {
width: 332px;
margin-bottom: 48px;
position: relative;
}
.box {
width: 100px;
height: 100px;
display: inline-block;
background: red;
margin-right: 8px;
}
.container:after {
content: '';
position: absolute;
top: 100px;
left:0;
right: 0;
bottom: 0;
background: linear-gradient(to bottom, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
If you add jQuery to your project you can do this by adding a css class after checking each .container div's height.
https://jsfiddle.net/fwtj44bj/5/
$(".container").each(function(){
if ($(this).height() > 100) {
$(this).addClass("gradient");
}
});
Edit
I edited my answer so the pseudo element have height:calc(100% - 100px); instead of just height:100%, this way you don't need to have overflow:hidden on .container
How about this: check fiddle
.container{
width: 332px;
margin-bottom: 48px;
position:relative;
}
.container:after{
content:' ';
position:absolute;
bottom:0px;
width:100%;
height:calc(100% - 100px);
background: linear-gradient(to bottom, rgba(255,255,255,0) 0%,rgba(255,0,255,1) 100%);
}
Obvious limitations: .container should have position:relative and overflow:hidden.

Resources