Overflowing border with gradient background - css

I have a block (container) with gradient as background color. The container have a border, and inside it, i have a text that I want to overflow part of a border and have the same background color. Now I have something like this:
But I want to achieve something like this:
To overflow some border area but still having the gradient background color, because if I add background color to the block of text, it still different from the gradient effect.
EDIT: I'd like to achieve something like this:

According to your JSFiddle code, I think the following code will help you.
.gradient-box {
background-image: linear-gradient(white, grey);
display: inline-block;
padding: 10px;
}
.container {
margin-top: 10px;
border: 3px solid black;
text-align: center;
display: inline-block;
padding: 10px;
}
.text {
margin-top:-20px;
}
.highlight {
background-color: #a9b0a9;
color: #429778;
}
<div class="gradient-box">
<div class="container">
<div class="text"> <span class="highlight"> TEXT </span></div>
</div>
</div>

You're wanting something like this:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
word-wrap: break-word;
}
body {
background-color: #FBBFBF;
}
.container {
width: 300px;
height: 300px;
margin: 50px;
background-color: #FBBFBF;
position: relative;
border: 3px solid black;
border-radius: 15px;
}
.float-text {
position: absolute;
top: -15px;
right: 20px;
padding: 5px 10px;
background-color: #FBBFBF;
/* background-image: linear-gradient(to right, red 0%, orange 100%); maybe you mentioning gradient you meant like this? */
text-transform: uppercase;
}
<div class="container">
<div class="float-text">
Text
</div>
</div>
EDIT: With transparent BG. Think this is only possible with square borders...
* {
margin: 0;
padding: 0;
box-sizing: border-box;
word-wrap: break-word;
}
body {
background-image: linear-gradient(to right, red 0%, orange 100%);
}
.container {
width: 300px;
height: 300px;
margin: 50px;
position: relative;
border-top: 0;
border-left: 2px solid black;
border-right: 2px solid black;
border-bottom: 2px solid black;
}
.top-border {
width: calc(100% - 100px);
height: 2px;
background-color: black;
position: absolute;
top: -1px;
left: -1px;
}
.top-border-end {
width: 20px;
height: 2px;
background-color: black;
position: absolute;
top: -1px;
right: -1px;
}
.float-text {
width: 80px;
position: absolute;
top: -15px;
right: 20px;
padding: 5px 10px;
background-color: transparent;
/* background-image: linear-gradient(to right, red 0%, orange 100%); maybe you mentioning gradient you meant like this? */
text-transform: uppercase;
text-align: center;
}
<div class="container">
<div class="top-border"></div>
<div class="float-text">
Text
</div>
<div class="top-border-end"></div>
</div>

I would do it like this:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
word-wrap: break-word;
}
body {
background-color: rgba(255,0,0,0.25);
}
.container {
width: 300px;
height: 300px;
margin: 50px;
background-color: purple;
position: relative;
border: 3px solid black;
border-radius: 15px;
}
.float-text {
position: absolute;
top: -15px;
right: 20px;
padding: 5px 10px;
background-color: #fff2ac;
background-image: linear-gradient(to right, red 0%, orange 100%); /* maybe you mentioning gradient you meant like this? */
text-transform: uppercase;
}
<div class="container">
<div class="float-text">
Text
</div>
</div>
Edit
You mention a gradient, maybe you mean like this? (I added the gradient behind the the text box). If not - remove the background-image gradient if you just want solid colour.

Related

How to give border to a shape?

I am building a testimonial component in react and I have to make a shape direction towards pic, I have done the shape exactly how I want but the testimonial div has border color when I apply the div gets a border but the shape is left outside I have tried several ways but couldn't find a solution, I have attached the picture of what I want and how it is right now.
How I want it
What I have achieved till now
Below is my CSS
#page {
background-color: lightgray;
padding: 40px;
}
.container {
position: relative;
background-color: #FFFFFF;
max-width: 600px;
height: auto;
border: 1px solid #E7E7E7;
padding: 30px;
box-sizing: border-box;
}
.container:after {
position: absolute;
width: 0;
height: 0;
border-top: 50px solid white;
border-right: 40px solid transparent;
top:101%;
left: 40%;
content: '';
transform: rotate(14deg);
margin-top: -10px;
}
<div id="page">
<div class="container">This is a test</div>
</div>
You may use a filter , choice: drop-shadow.
support ? , don't be afraid : https://caniuse.com/?search=drop-shadow All but IE 6-11 and Opera mini
here is an exemple to run:
#page {
background-color: lightgray;
padding: 40px;
}
.container {
position: relative;
background-color: #FFFFFF;
max-width: 600px;
height: auto;
filter:
/* draw borders without blur*/
drop-shadow(0 1px )
drop-shadow(1px 0px )
drop-shadow(0 -1px )
drop-shadow(-1px 0px )
/* add eventually a shadow */
drop-shadow(0 0 3px )
/*and another for demo purpose */
drop-shadow(30px 30px 3px gray );
padding: 30px;
box-sizing: border-box;
}
.container:after {
position: absolute;
width: 0;
height: 0;
border-top: 50px solid white;
border-right: 40px solid transparent;
top:101%;
left: 40%;
content: '';
transform: rotate(14deg);
margin-top: -10px;
}
<div id="page">
<div class="container">This is a test</div>
</div>
You can use a :before that's 1px bigger than your :after which uses the border colour instead and then it will be mostly covered by the :after, giving you your "fake" border. Just makes sure your z-indexing is correct so it doesn't show inside your bubble.
EDIT: Adding in example css.
I modified some colours and spacing for illustrative purposes:
#page {
background: #ffc;
padding: 40px 40px 60px;
position: relative;
z-index: 0;
}
.container {
position: relative;
background: #fff;
max-width: 600px;
height: auto;
border: 1px solid #000;
padding: 30px;
box-sizing: border-box;
}
.container:after,
.container:before {
position: absolute;
width: 0;
height: 0;
top: 101%;
left: 40%;
content: "";
transform: rotate(14deg);
margin-top: -10px;
}
.container:after {
border-top: 50px solid #fff;
border-right: 40px solid transparent;
}
.container:before {
border-top: 52px solid #000;
border-right: 42px solid transparent;
margin-left: -1px;
z-index: -1;
}
<div id="page">
<div class="container">This is a test</div>
</div>
Adding both a :before and :after is a good idea to get the effect you want. Using a CSS box-shadow or outline won't work because it actually renders a complete square around your arrow/triangle shape. A z-index is added to the before to push it to the background. In that way it's not overlapping the other objects.
Here's an example of what you might want. You can adjust the border sizes to finetune it.
.container {
position: relative;
background-color: #FFFFFF;
max-width: 600px;
height: auto;
border: 1px solid #E7E7E7;
padding: 30px;
box-sizing: border-box;
}
.container:before {
position: absolute;
width: 0;
height: 0;
border-top: 53px solid #e7e7e7;
border-right: 43px solid transparent;
top: 100%;
left: 40%;
content: '';
transform: rotate(14deg);
margin-top: -10px;
z-index: -1;
}
.container:after {
position: absolute;
width: 0;
height: 0;
border-top: 50px solid white;
border-right: 40px solid transparent;
top:101%;
left: 40%;
content: '';
transform: rotate(14deg);
margin-top: -10px;
}
<div class="container"></div>

Create this shape in CSS (half pipe shape)

I've been trying to make this shape in CSS, but unfortunately could not find a way how to. It's half pipe like shaped:
The pixelated corner should be smooth (it's a zoomed in image).
Anyone knows how to create this? Or can get me on the right tracks?
You will need to use pseudo element :after for the inner block and then apply border-radius for curved corner.
div {
height: 60px;
width: 60px;
border: solid red;
position: relative;
background: red;
border-width: 0 10px 10px 0;
box-sizing: border-box;
}
div:after {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-bottom-right-radius: 10px;
background: #fff;
}
<div></div>
simple
.shape {
width: 140px;
height: 200px;
background-color: gray;
}
.shape:after {
content: '';
position: absolute;
width: 100px;
height: 160px;
background-color: #fff;
border-bottom-right-radius: 50px;
}
<div class="shape"></div>
I assume that the problem here is to create the "outer" curve, on the arbitrary background. It's achievable by the trick with clipping the needed part from the thick rounded border of the pseudo-element:
div {
width: 50%;
min-height: 4em;
margin: auto;
border: #888 solid;
border-width: 0 1em 1em 0;
position: relative;
}
div::after {
content: '';
border: #888 solid;
border-width: 0 1em 1em 0;
border-radius: 0 0 100% 0;
position: absolute;
right: -1em;
bottom: -1em;
width: 1em;
height: 1em;
clip: rect(0 1em 1em 0);
}
div:hover::after {
border-color: red; /* highlight the curved part */
}
body {
/* just as a background example */
background: repeating-linear-gradient(-45deg, #ddd, #ddd 1px, transparent 1px, transparent 10px);
min-height: 90vh;
}
<div></div>
You can do this with one element and with transparency like this:
.box {
width:80px;
height:100px;
border-right:20px solid grey;
border-bottom:20px solid grey;
box-sizing:border-box;
background:
radial-gradient(farthest-side at top left,transparent 98%,grey ) bottom right/20px 20px no-repeat;
}
body {
background:pink;
}
<div class="box">
</div>

How do I create a box-shadow effect that looks like a curve?

I am familiar with creating basic box-shadows but I can't understand how to create something that looks like a curve. To better explain what I am looking for, I've attached the image. I need something similar to that. How can I create it?
I've tried to achieve that somehow with using absolute positioned elements, box shadow and a radial gradient. But overall I would use a background image.
But still have a look at my fiddle which unfortunately doesn't really look like your goal. But it might give you a starting point.
HTML
<div id="wrapper">
<ul id="nav">
</ul>
<div id="first"></div>
<div id="second"></div>
</div>
CSS
body {
background: #FEFEFE;
}
#wrapper {
position: relative;
display: block;
width: 960px;
height: 80px;
overflow: hidden;
}
ul {
height: 40px;
width: 100%;
background: #FFF;
box-shadow: 0px 3px 5px 0px #ccc;
position: absolute;
z-index: 300;
display: block;
padding: 0;
margin: 0;
border: solid 2px #EEE;
border-bottom: solid 1px #FEFEFE;
}
#first {
position: absolute;
background: none;
width: 800px;
height: 50px;
box-shadow: 0px 10px 50px 5px #000;
margin-left: -400px;
top:-20px;
left: 50%;
z-index: 100;
}
#second {
left: 50%;
position: absolute;
width: 900px;
z-index: 200;
margin-left: -450px;
height:80px;
top: 15px;
background: radial-gradient(ellipse at center, rgba(255,255,255,1) 0%,rgba(255,255,255,1) 15%,rgba(255,255,255,1) 25%,rgba(255,255,255,0.28) 79%,rgba(255,255,255,0) 100%); /* W3C */
}

Full size of <div> inside another <div> next to the <div> [duplicate]

This question already has answers here:
div set height equal
(4 answers)
Closed 8 years ago.
this is my html of my little game.
<div id="game">
<div id="choice" onmouseover="npcRoll()">
<p>Chosse your weapon!</p>
<button id="rock" onClick="choose(1)">Rock</button>
<button id="paper" onClick="choose(2)">Paper</button>
<button id="scissors" onClick="choose(3)">Scissors</button>
<p>You chose <span id="userChoice">none</span>!</p>
</div>
<div id="confirm">
<p>When you are ready, click on <strong>Fight</strong>.</p>
<button id="resulot" onClick="resulte()">Fight!</button>
</div>
<div id="clear"></div>
</div>
And this is my CSS
body {
background-color: #DFEFF0;
text-align: center;
}
button {
font-size: 22px;
border: 2px solid #87231C;
border-radius: 100px;
width: 100px;
height: 100px;
color: #FF5A51;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
padding-top: 36px;
}
button:active {
font-size: 22px;
border: 2px solid #328505;
color: #32A505;
border-radius: 100px;
width: 100px;
height: 100px;
padding-top: 36px;
}
#rock {
width: 100px;
height: 100px;
background-color: white;
background-image: url(img/rock.png);
background-repeat: no-repeat;
background-size: 80px 80px;
background-position: center center;
}
#paper {
width: 100px;
height: 100px;
background-color: white;
background-image: url(img/paper.png);
background-repeat: no-repeat;
background-size: 80px 80px;
background-position: center center;
}
#scissors {
width: 100px;
height: 100px;
background-color: white;
background-image: url(img/scissors.png);
background-repeat: no-repeat;
background-size: 80px 80px;
background-position: center center;
}
#result {
background-color: #ECECEC;
border:2px solid gray;
border-radius:25px;
width: 200px;
height: 100px;
}
#choice {
border: 2px solid #87231C;
border-radius: 12px;
border-top-right-radius: 0px;
border-bottom-right-radius: 0px;
background-color: #FF5A51;
width: 350px;
float: left;
}
#game {
border: 2px solid #fff;
border-radius: 15px;
background-color: white;
width: 500px;
margin: 0 auto;
display: block;
overflow: auto;
}
#confirm {
border: 2px solid #00008B;
border-radius: 12px;
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
background-color: #1E90FF;
width: 142px;
height: 100%;
float: right;
}
#clear {
clear: both;
}
You can check it out here on http://jsfiddle.net/RWfhQ/ . I want to make the blue div to be same size as the red one. I want to make them same size. It's possible that blue div may get bigger than red one, so I need to have them same size.
Thank you very much.
The obvious solution is to use position: relative on #game container and position: absolute on #confirm:
#confirm {
...
position: absolute; // <-- stretch the div
top: 0;
bottom: 0;
right: 0;
}
In this case you don't need height: 100% and float: right anymore.
http://jsfiddle.net/RWfhQ/1/
Since this is a fixed width, you could use the faux-columns trick. Wrap both in a <div> to handle the background, and use a background image which is half blue and half red.

Any way to declare a size/partial border to a box?

Any way to declare a size/partial border to a box in CSS? For example a box with 350px that only shows a border-bottom in its firsts 60px. I think that might be very useful.
Examples:
Not really. But it's very easy to achieve the effect in a way that degrades gracefully and requires no superfluous markup:
div {
width: 350px;
height: 100px;
background: lightgray;
position: relative;
margin: 20px;
}
div:after {
content: '';
width: 60px;
height: 4px;
background: gray;
position: absolute;
bottom: -4px;
}
<div></div>
I know, this is already solved and pixels were requested. However, I just wanted to share something...
Partly underlined text elements can easily achieved by using display:table or display:inline-block
(I just don't use display:inline-block because, yeah you know, the awkward 4px-gap).
Textual Elements
h1 {
border-bottom: 1px solid #f00;
display: table;
}
<h1>Foo is not equal to bar</h1>
Centering, display:table makes it impossible to center the element with text-align:center.
Let's work around with margin:auto...
h1 {
border-bottom: 1px solid #f00;
display: table;
margin-left: auto;
margin-right: auto;
}
<h1>Foo is not equal to bar</h1>
Well, that's nice, but it's not partially.
As bookcasey already introduced, pseudo-elements are worth gold.
h1 {
display: table;
margin-left: auto;
margin-right: auto;
}
h1:after {
border-bottom: 1px solid #f00;
content: '';
display: block;
width: 50%;
}
<h1>Foo is not equal to bar</h1>
Offset, the underline is left aligned right now. To center it, just push the pseudo-element the half of its width (50% / 2 = 25%) to the right.
h1 {
display: table;
margin-left: auto;
margin-right: auto;
}
h1:after {
border-bottom: 1px solid #f00;
content: '';
display: block;
margin-left: 25%;
width: 50%;
}
<h1>Foo is not equal to bar</h1>
...as davidmatas commented, using margin:auto is sometimes more practical, than calculating the margin-offset by hand.
So, we can align the underline to the left, right or center (without knowing the current width) by using one of these combinations:
Left: margin-right: auto (or just leave it off)
Middle: margin: auto
Right: margin-left: auto
Full example
.underline {
display: table;
margin-left: auto;
margin-right: auto;
}
.underline:after {
border-bottom: 1px solid #f00;
content: '';
display: block;
width: 50%;
}
.underline--left:after {
margin-right: auto; /* ...or just leave it off */
}
.underline--center:after {
margin-left: auto;
margin-right: auto;
}
.underline--right:after {
margin-left: auto
}
<h1 class="underline underline--left">Foo is not equal to bar</h1>
<h1 class="underline underline--center">Foo is not equal to bar</h1>
<h1 class="underline underline--right">Foo is not equal to bar</h1>
Block-Level Elements
This can easily be adopted, so that we can use block-level elements. The trick is to set the pseudo-elements height to the same height as its real element (simply height:100%):
div {
background-color: #eee;
display: table;
height: 100px;
width: 350px;
}
div:after {
border-bottom: 3px solid #666;
content: '';
display: block;
height: 100%;
width: 60px;
}
<div></div>
Here is another solution that rely on linear-gradient where you can easily create any kind of line you want. You can also have multiple lines (on each side for example) by using multiple background:
.box1 {
width: 200px;
padding: 20px;
margin: 10px auto;
text-align: center;
background:
linear-gradient(to right, transparent 20%, #000 20%, #000 40%, transparent 40%) 0 100% / 100% 3px no-repeat,
#ccc
}
.box2 {
width: 200px;
padding: 20px;
margin: 10px auto;
text-align: center;
background:
linear-gradient(to right, transparent 20%, red 20%, red 80%, transparent 80%) 0 100% / 100% 2px no-repeat,
#ccc
}
.box3{
width: 200px;
padding: 20px;
margin: 10px auto;
text-align: center;
background:
linear-gradient(to right, transparent 20%, red 20%, red 80%, transparent 80%) 0 100% / 100% 2px no-repeat,
linear-gradient(to right, transparent 30%, blue 30%, blue 70%, transparent 70%) 0 0 / 100% 2px no-repeat,
linear-gradient(to bottom, transparent 30%, brown 30%, brown 70%, transparent 70%) 0 0 / 3px 100% no-repeat,
linear-gradient(to bottom, transparent 20%, orange 20%, orange 70%, transparent 70%) 100% 0 / 3px 100% no-repeat,
#ccc
}
<div class="box1">
Box1
</div>
<div class="box2">
Box2
</div>
<div class="box3">
Box3
</div>
Here is another syntax to achieve the same as above:
.box1 {
width: 200px;
padding: 20px;
margin: 10px auto;
text-align: center;
background:
linear-gradient(#000 0 0) top /40% 3px no-repeat,
#ccc
}
.box2 {
width: 200px;
padding: 20px;
margin: 10px auto;
text-align: center;
background:
linear-gradient(red 0 0) bottom/ 60% 2px no-repeat,
#ccc;
}
.box3{
width: 200px;
padding: 20px;
margin: 10px auto;
text-align: center;
background:
linear-gradient(red 0 0)bottom left/ 60% 2px,
linear-gradient(blue 0 0) 60% 0 / 40% 2px,
linear-gradient(brown 0 0) left/ 3px 30%,
linear-gradient(orange 0 0) right / 3px 40%,
#ccc;
background-repeat:no-repeat;
}
<div class="box1">
Box1
</div>
<div class="box2">
Box2
</div>
<div class="box3">
Box3
</div>
I used a grid to build draw some of the borders.
See here.
Code:
/* ungrid without mobile */
.row {
width: 100%;
display: table;
table-layout: fixed;
}
.col {
display: table-cell;
}
/* things to change */
.row {
width: 70%;
margin: auto;
}
.mid.row>.col {
height: 150px;
}
/* draw box and align text */
.col {
text-align: center;
}
.top.left.col {
border-top: 1px solid black;
border-left: 1px solid black;
}
.top.right.col {
border-top: 1px solid black;
border-right: 1px solid black;
}
.bottom.left.col {
border-bottom: 1px solid black;
border-left: 1px solid black;
}
.bottom.right.col {
border-bottom: 1px solid black;
border-right: 1px solid black;
}
.mid.row>.col {
border-left: 1px solid black;
border-right: 1px solid black;
vertical-align: middle;
}
.top.center.col {
position: relative;
top: -0.5em;
}
.bottom.center.col {
position: relative;
bottom: -0.5em;
}
<div class="row">
<div class="top left col"></div>
<div class="top center col">Top</div>
<div class="top right col"></div>
</div>
<div class="mid row">
<div class="col">Mid</div>
</div>
<div class="row">
<div class="bottom left col"></div>
<div class="bottom center col">Bottom</div>
<div class="bottom right col"></div>
</div>
CSS does not support partial borders. You'd need to use an adjacent element to simulate this.
Been playing a bit around with your solutions and came up with that.
I'd appreciate your comments and thoughts.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test file</title>
<style>
#box {
background-color: gray;
position: relative;
left: 10px;
top: 10px;
height: 180px;
width: 380px;
}
#grad1 {
position: absolute;
left: -10px;
top: -10px;
height: 40px;
width: 2px;
background-image: linear-gradient(red, red);
}
#grad2 {
position: absolute;
left: -10px;
top: -10px;
height: 2px;
width: 40px;
background-image: linear-gradient(red, red);
}
</style>
</head>
<body>
<div id="box">
<div id="grad1"></div>
<div id="grad2"></div>
</div>
</body>
</html>

Resources