Extra pixel in before and after pseudo elements - html

I'm trying to create a background effect using before and after pseudo elements by making it one pixel taller and wider than the actual element, but it always seems to have one extra pixel to the right or left. This only happens when the browsers is maximized (Firefox, Chrome and Edge), but does not happen when the browser has a smaller width.
*, *::before, *::after{ box-sizing: border-box; }
body {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: center;
background-color: #111;
}
img {
max-width: 300px;
display: block;
padding: 4px;
}
.main-box {
position: relative;
}
.img-box {
padding: 0;
margin: 0;
background-color: #000;
}
.img-box::before{
content: '';
position: absolute;
top: -1px;
left: -1px;
right: -1px;
bottom: -1px;
filter: blur(10px);
z-index: -2;
}
.img-box::after{
content: '';
position: absolute;
top: -1px;
left: -1px;
right: -1px;
bottom: -1px;
z-index: -1;
}
.img-box::before, .img-box::after{
background-image: linear-gradient(45deg, #ff0000, #111, #0000ff);
opacity: 0.7;
transition: opacity ease-out 150ms;
}
.main-box:hover .img-box::after {
opacity: 1;
}
<div class="main-box">
<div class="img-box"><img src="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" alt="keyboard"></div>
</div>
It's not quite clear in the picture, but in the browser all sides of the background-image is 1px more except for the left side where it's 2px more.
OUTPUT: Thicker line on the left

This looks like antialiasing. I guess you have either your browser's either your OS's zoom level set to something else than 100%.
Some browsers will try to round the positionning, but at some zoom level, this can't be done properly and you'll end up having one side floored and the other ceiled.
To circumvent this, you can use the translate property which should allow proper antialiasing to kick in (it will be blurry, but of the same size).
*, *::before, *::after{ box-sizing: border-box; }
body {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: center;
background-color: #111;
}
img {
max-width: 300px;
display: block;
padding: 4px;
}
.main-box {
position: relative;
}
.img-box {
padding: 0;
margin: 0;
background-color: #000;
}
.img-box::before{
content: '';
position: absolute;
top: 0px;
left: 0px;
width: calc( 100% + 2px );
height: calc( 100% + 2px );
transform: translate(-1px,-1px);
filter: blur(10px);
z-index: -2;
}
.img-box::after{
content: '';
position: absolute;
top: 0px;
left: 0px;
width: calc( 100% + 2px );
height: calc( 100% + 2px );
transform: translate(-1px,-1px);
z-index: -1;
}
.img-box::before, .img-box::after{
background-image: linear-gradient(45deg, #ff0000, #111, #0000ff);
opacity: 0.7;
transition: opacity ease-out 150ms;
}
.main-box:hover .img-box::after {
opacity: 1;
}
<div class="main-box">
<div class="img-box"><img src="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" alt="keyboard"></div>
</div>

Related

How to create expanding line animation from this base?

So I want to create this animation where basically this happens:
When you hover on an icon, it gets bigger, increases in opacity, and some text appears.
In addition to this, 2 lines of color extend in width from the center out to the sides, then they increase in height.
The bottom color should expand downwards, while the top color should expand upwards.
I created this test-base and was wondering how I would go about making this from here. I tried tweaking the height, width, and opacity but those also edit the icon, so I'm wondering if I'm taking the wrong approach or just doing it wrong. Any help and/or pointers are appreciated.
Current code:
* {
margin: 0;
padding: 0;
font-family: "Consolas";
}
body {
background: #212121;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.hoverCard {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
width: 320px;
height: 480px;
border-radius: 30px;
background: #191919;
overflow: hidden;
}
.hoverCard::before {
background: blue;
content: "";
position: absolute;
width: 100%;
height: 50%;
transition: 0.5s;
}
.hoverCard .mainImg {
opacity: 0.25;
height: 160px;
width: 160px;
transition: 0.5s;
margin: 10;
margin-top: 50%;
}
.hoverCard .mainText {
opacity: 0;
color: blue;
margin-top: 0%;
transition: 0.5s;
}
.hoverCard .subText {
opacity: 0;
color: blue;
margin-top: 0%;
transition: 0.5s;
}
.mainImg:hover {
transform: scale(1.5);
opacity: 1;
margin-top: 30%;
}
.mainImg:hover~.mainText {
margin-top: 20%;
opacity: 1;
}
.mainImg:hover~.subText {
margin-top: 25%;
opacity: 1;
}
<html>
<head>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="hoverCard">
<img class="mainImg" src="../Media/Link-Logos/Discord.png">
<p class="mainText">Discord</p>
<p class="subText">Ex0tic_Python#7571</p>
</div>
</body>
</html>
As the requirement is to have lines drawn and expanding when the image is hovered, and the image itself cannot have pseudo elements, this snippet adds a further element after the img, .lines.
This is positioned absolutely and sized relative to the overall card (parent) element.
It has before and after pseudo elements which draw lines and then expand vertically using a CSS animation.
As I was unclear what you wanted to happen about the line going downwards (if it keeps the same background color as the card then of course it can't be seen) so this snippet makes it pink.
Of course you will want to alter timings and perhaps dimensions to suit your specific requirements, this is just a simple example to demonstrate one possibility.
<html>
<head>
<link rel="stylesheet" href="main.css">
<style>
* {
margin: 0;
padding: 0;
font-family: "Consolas";
}
body {
background: #212121;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.hoverCard {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
width: 320px;
height: 480px;
border-radius: 30px;
overflow: hidden;
}
.hoverCard .mainImg {
opacity: 0.25;
height: 160px;
width: 160px;
transition: 0.5s;
margin: 10;
margin-top: 50%;
}
.hoverCard .mainText {
opacity: 0;
color: blue;
margin-top: 0%;
transition: 0.5s;
}
.hoverCard .subText {
opacity: 0;
color: blue;
margin-top: 0%;
transition: 0.5s;
}
.mainImg:hover {
transform: scale(1.5);
opacity: 1;
margin-top: 30%;
}
.mainImg:hover~.mainText {
margin-top: 20%;
opacity: 1;
}
.mainImg:hover~.subText {
margin-top: 25%;
opacity: 1;
}
.lines {
content: '';
position: absolute;
width: 100%;
height: 100%;
z-index: -1;
display: flex;
justify-content: center;
background-color: #191919;
}
.lines::before,
.lines::after {
content: '';
margin: 0 auto;
position: absolute;
width: 160px;
height: 10px;
opacity: 0;
}
.lines::before {
background-color: blue;
bottom: 50%;
}
.lines::after {
background-color: pink;
top: 50%;
}
#keyframes expand {
0% {
opacity: 0;
width: 160px;
height: 10px;
}
9.99% {
opacity: 0;
}
10% {
width: 240px;
opacity: 1;
}
50% {
width: 100%;
height: 10px;
}
100% {
opacity: 1;
width: 100%;
height: 100%;
}
}
.mainImg:hover~.lines::before,
.mainImg:hover~.lines::after {
display: block;
animation: expand 3s linear forwards 0.3s;
}
</style>
</head>
<body>
<div class="hoverCard">
<img class="mainImg" src="https://picsum.photos/id/1015/200/200">
<div class="lines"></div>
<p class="mainText">Discord</p>
<p class="subText">Ex0tic_Python#7571</p>
</div>
</body>
</html>

CSS Element with sloped borders [duplicate]

I'm trying to replicate a graphical design using css, but I have failed for responsive, I can achieve an static form but with tiny defects (due to putting together two elements).
This is the graphical design:
I prefer it a bit more tilted, like: skew(-40deg). But the idea is to have an inner rounded border that wraps that key-button just like in the image.
The html is simple:
<header>
<nav></nav>
</header>
The css:
body > header > nav {
display: flex;
align-items: flex-end;
justify-content: center;
width: 100vw;
height: 90px;
padding: 10px 0;
text-align: center;
z-index: 1
}
body > header > nav::before {
content: '';
position: absolute;
top: 0; left: 0;
width: 80vw; height: 100%;
background-color: rgb(147, 147, 147);
border-bottom-right-radius: 15px;
transform: skew(-40deg);
transform-origin: 100% 0%;
}
body > header > nav::after {
content: '';
position: absolute;
top: 0; right: 0;
width: 28.7%;
border-top: 7px solid rgb(147, 147, 147);
border-left: 50px solid rgb(147, 147, 147);
height: 75px;
border-top-left-radius: 75px;
transform: skew(-33deg);
}
I've prepared a https://jsfiddle.net/uj4qsf37/
Is there a cleaner way to do this? Like not having to use two elements? With one element it would be easy to make it responsive.
I would do it like this:
.header {
border-top: 20px solid blue;
height:100px;
position: relative;
overflow: hidden;
}
.header:before,
.header:after {
content: "";
vertical-align:top;
display: inline-block;
transform-origin: top right;
transform: skew(-40deg);
}
.header:before {
height: 100%;
width: 50%;
border-radius: 0 0 20px 0;
background: blue;
}
.header:after {
height: 20px;
width: 20px;
margin-left:-1px;
background: radial-gradient(circle at bottom right, transparent 68%, blue 73%);
}
/*to illustrate different values of skew*/
.header:before,
.header:after {
animation:change 2s linear infinite alternate;
}
#keyframes change{
from{transform: skew(0deg);}
top{transform: skew(-40deg);}
}
<div class="header"></div>

CSS - How to change background of intersection of two objects?

I'm trying to replicate the styling of this animation but I don't know how to "fill in" the background color of the intersection of these two shapes. In the animation, the intersection is conveniently stepwise and stops where the edge of the square intersections with the origin of the circle; I can imagine using a clipping-mask to fill in that quadrant of the circle. However, is it possible to do the same more dynamically? Can you fill in the background of two intersecting shapes (while still having a transparent background otherwhere)?
.shape-interconnected {
width: 400px;
height: 300px;
position: relative;
background-color: black;
color: white;
margin: 1rem;
border-radius: 4px;
}
.shape-interconnected > .square, .shape-interconnected > .circle {
width: 50px;
height: 50px;
position: absolute;
border: 5px solid white;
transform: translate(-50%, -50%);
}
.shape-interconnected > .square {
border-radius: 4px;
top: 45%;
left: 55%;
}
.shape-interconnected > .circle {
border-radius: 50%;
top: 55%;
left: 45%;
}
<div class="shape-interconnected">
<div class="square"></div>
<div class="circle"></div>
</div>
You can recreate the dribble using html with a little css pseudo and animation magic.
This example below works at any set css variable set size border defined in the root css vars.
:root {
--size: 250px;
--border: 5px;
}
The trick in my example is by using positioning as percentages, meaning the parent .shape-interconnected controlled by the css var size, dictates all the child and child pseudo element position.
There is a lot of css to explain here, I've added comments in css, see if this inspires you to get you where you need to go...
Here is a fiddle... https://jsfiddle.net/joshmoto/378Lcgp0/
/* our root css vars */
:root {
--size: 250px;
--border: 5px;
}
BODY {
background: black;
min-height: 100%;
}
/* reset our box sizing on psuedo elems */
*, ::after, ::before {
box-sizing: border-box;
}
/* our shape intersect container positioned center of window */
/* this can be positioned where ever you want */
.shape-interconnected {
background: black;
width: var(--size);
height: var(--size);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
animation: shape-interconnected 2s infinite;
}
/* animate height and width equally */
#keyframes shape-interconnected {
0% {
width: var(--size);
height: var(--size);
}
50% {
width: calc(var(--size) * 0.6);
height: calc(var(--size) * 0.6);
}
100% {
width: var(--size);
height: var(--size);
}
}
/* our square calculated at 40% of parent */
/* position and overflow hidden are key, hiding pseudo child elems */
.shape-interconnected > .square {
width: calc(var(--size) * 0.4);
height: calc(var(--size) * 0.4);
background: transparent;
position: absolute;
overflow: hidden;
right: 0;
top: 0;
}
/* our square before pseudo elem emulating inner white filled circle */
/* position absolute with animation keyframes */
.shape-interconnected > .square::before {
content: "";
display: block;
width: 100%;
height: 100%;
background: #fff;
border-radius: 50%;
position: absolute;
animation: circle-interconnected 2s infinite;
}
/* start top/right 150% away, overflowing out of view */
/* 50% keyframe top/right 50% away, in view */
#keyframes circle-interconnected {
0% {
top: 150%;
right: 150%;
}
50% {
top: 50%;
right: 50%;
}
100% {
top: 150%;
right: 150%;
}
}
/* our square after pseudo elem emulating white border */
.shape-interconnected > .square::after {
content: "";
display: block;
width: 100%;
height: 100%;
background: transparent;
border: var(--border) solid white;
position: relative;
}
/* our circle calculated at 40% of parent */
.shape-interconnected > .circle {
width: calc(var(--size) * 0.4);
height: calc(var(--size) * 0.4);
position: absolute;
bottom: 0;
left: 0;
}
/* our circle after pseudo elem emulating white border */
.shape-interconnected > .circle::after {
content: "";
display: block;
width: 100%;
height: 100%;
background: transparent;
border: var(--border) solid white;
border-radius: 50%;
position: relative;
}
<div class="shape-interconnected">
<div class="square"></div>
<div class="circle"></div>
</div>
Here is another example using the same code above but with these css root var settings...
:root {
--size: 500px;
--border: 2px;
}
Live example below...
/* our root css vars */
:root {
--size: 500px;
--border: 2px;
}
BODY {
background: black;
min-height: 100%;
}
/* reset our box sizing on psuedo elems */
*, ::after, ::before {
box-sizing: border-box;
}
/* our shape intersect container positioned center of window */
/* this can be positioned where ever you want */
.shape-interconnected {
background: black;
width: var(--size);
height: var(--size);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
animation: shape-interconnected 2s infinite;
}
/* animate height and width equally */
#keyframes shape-interconnected {
0% {
width: var(--size);
height: var(--size);
}
50% {
width: calc(var(--size) * 0.6);
height: calc(var(--size) * 0.6);
}
100% {
width: var(--size);
height: var(--size);
}
}
/* our square calculated at 40% of parent */
/* position and overflow hidden are key, hiding pseudo child elems */
.shape-interconnected > .square {
width: calc(var(--size) * 0.4);
height: calc(var(--size) * 0.4);
background: transparent;
position: absolute;
overflow: hidden;
right: 0;
top: 0;
}
/* our square before pseudo elem emulating inner white filled circle */
/* position absolute with animation keyframes */
.shape-interconnected > .square::before {
content: "";
display: block;
width: 100%;
height: 100%;
background: #fff;
border-radius: 50%;
position: absolute;
animation: circle-interconnected 2s infinite;
}
/* start top/right 150% away, overflowing out of view */
/* 50% keyframe top/right 50% away, in view */
#keyframes circle-interconnected {
0% {
top: 150%;
right: 150%;
}
50% {
top: 50%;
right: 50%;
}
100% {
top: 150%;
right: 150%;
}
}
/* our square after pseudo elem emulating white border */
.shape-interconnected > .square::after {
content: "";
display: block;
width: 100%;
height: 100%;
background: transparent;
border: var(--border) solid white;
position: relative;
}
/* our circle calculated at 40% of parent */
.shape-interconnected > .circle {
width: calc(var(--size) * 0.4);
height: calc(var(--size) * 0.4);
position: absolute;
bottom: 0;
left: 0;
}
/* our circle after pseudo elem emulating white border */
.shape-interconnected > .circle::after {
content: "";
display: block;
width: 100%;
height: 100%;
background: transparent;
border: var(--border) solid white;
border-radius: 50%;
position: relative;
}
<div class="shape-interconnected">
<div class="square"></div>
<div class="circle"></div>
</div>
You might add a white circle inside the square and position it to the same coords the transparent one has.
Set overflow: hidden to the square to hide the outside part of the white circle:
.shape-interconnected {
width: 400px;
height: 300px;
position: relative;
background-color: black;
color: white;
margin: 1rem;
border-radius: 4px;
--animation-props: 1s alternate linear infinite;
}
.shape-interconnected>.square,
.shape-interconnected>.square:before,
.shape-interconnected>.circle {
width: 50px;
height: 50px;
position: absolute;
border: 5px solid white;
transform: translate(-50%, -50%)
}
.shape-interconnected>.square {
top: 35%;
left: 65%;
border-radius: 4px;
overflow: hidden;
animation: for_square var(--animation-props);
}
.shape-interconnected>.circle {
top: 65%;
left: 35%;
border-radius: 50%;
animation: for_transparent_circle var(--animation-props);
}
.shape-interconnected>.square:before {
content: '';
border-radius: 50%;
background: #fff;
top: 230%;
left: -190%;
animation: for_white_circle var(--animation-props);
}
#keyframes for_square {
to {
top: 50%;
left: 55%;
}
}
#keyframes for_transparent_circle {
to {
top: 55%;
left: 50%;
}
}
#keyframes for_white_circle {
to {
top: 80%;
left: 10%;
}
}
<div class="shape-interconnected">
<div class="square"></div>
<div class="circle"></div>
</div>

I'm trying to achieve this curve effect using only CSS [duplicate]

I'm trying to replicate a graphical design using css, but I have failed for responsive, I can achieve an static form but with tiny defects (due to putting together two elements).
This is the graphical design:
I prefer it a bit more tilted, like: skew(-40deg). But the idea is to have an inner rounded border that wraps that key-button just like in the image.
The html is simple:
<header>
<nav></nav>
</header>
The css:
body > header > nav {
display: flex;
align-items: flex-end;
justify-content: center;
width: 100vw;
height: 90px;
padding: 10px 0;
text-align: center;
z-index: 1
}
body > header > nav::before {
content: '';
position: absolute;
top: 0; left: 0;
width: 80vw; height: 100%;
background-color: rgb(147, 147, 147);
border-bottom-right-radius: 15px;
transform: skew(-40deg);
transform-origin: 100% 0%;
}
body > header > nav::after {
content: '';
position: absolute;
top: 0; right: 0;
width: 28.7%;
border-top: 7px solid rgb(147, 147, 147);
border-left: 50px solid rgb(147, 147, 147);
height: 75px;
border-top-left-radius: 75px;
transform: skew(-33deg);
}
I've prepared a https://jsfiddle.net/uj4qsf37/
Is there a cleaner way to do this? Like not having to use two elements? With one element it would be easy to make it responsive.
I would do it like this:
.header {
border-top: 20px solid blue;
height:100px;
position: relative;
overflow: hidden;
}
.header:before,
.header:after {
content: "";
vertical-align:top;
display: inline-block;
transform-origin: top right;
transform: skew(-40deg);
}
.header:before {
height: 100%;
width: 50%;
border-radius: 0 0 20px 0;
background: blue;
}
.header:after {
height: 20px;
width: 20px;
margin-left:-1px;
background: radial-gradient(circle at bottom right, transparent 68%, blue 73%);
}
/*to illustrate different values of skew*/
.header:before,
.header:after {
animation:change 2s linear infinite alternate;
}
#keyframes change{
from{transform: skew(0deg);}
top{transform: skew(-40deg);}
}
<div class="header"></div>

CSS - Placing <divs> over transparent <div>'s

Is there any way I can make the content/images none-transparent when inside a <div> that has a transparency?
Here's the HTML:
<div id="main-button-wrapper" class="left">
<div id="button-bg-layer" class="box-bg-layer corners"></div>
<div class="buttons-bg-overlay box-bg-overlay corners">
<img alt="Test" src="http://www.schroff.co.uk/railway/src/symbol_test.gif" />
</div>
</div>
CSS:
#main-button-wrapper {
height: 319px;
margin-left: 22px;
position: relative;
width: 321px;
}
#button-bg-layer {
position: absolute;
right: 0;
height: 319px;
width: 321px;
}
.buttons-bg-overlay {
position: relative;
right: 0;
margin: 11px;
height: 66px;
width: 299px;
text-align: center;
padding-top: 26px;
}
#buttons-wrapper {
position: relative;
width: 299px;
height: 297px;
z-index: 3;
margin: 22px;
}
/* Background Layers */
.box-bg-layer {
background-color: #010101;
z-index: 1;
zoom: 1;
filter: alpha(opacity=40);
opacity: 0.4;
}
.box-bg-overlay {
background-color: #010101;
z-index: 2;
zoom: 1;
filter: alpha(opacity=40);
opacity: 0.4;
}
I've tried putting a z-index: 4; on the image. The only other way I can think of is setting the div backgrounds as absolute positioning, then positioning the content outside of the div but there must be an easier way?
Any help would be much appreciated!
See JSFiddle:
http://jsfiddle.net/Sa8jw/
Instead of using opacity use rgba where a stands for alpha. This will make the child elements non transparent...
background-color: rgba(0,0,0,.5); /* RGBA for #010101 will be rgba(1,1,1,.4) */
Where .4 for a is equivalent to opacity: 0.4
Demo
Here's a js fiddle for your help :-)
FIDDLE
and the changed code . where the opacity is being added to pseudo class after
#main-button-wrapper {
height: 319px;
margin-left: 22px;
position: relative;
width: 321px;
}
#button-bg-layer {
position: absolute;
right: 0;
height: 319px;
width: 321px;
}
.buttons-bg-overlay {
position: relative;
right: 0;
margin: 11px;
height: 66px;
width: 299px;
text-align: center;
padding-top: 26px;
}
#buttons-wrapper {
position: relative;
width: 299px;
height: 297px;
z-index: 3;
margin: 22px;
}
/* Background Layers */
.box-bg-layer{
background-color: #010101;
z-index: 1;
zoom: 1;}
.box-bg-layer : after{
filter: alpha(opacity=40);
opacity: 0.4;
}
.box-bg-overlay { background-color: red;
z-index: 2;
zoom: 1;
}
.box-bg-overlay :after{
filter: alpha(opacity=40);
opacity: 0.4;
}