I need to create a box with a diagonal edge. Below is the picture and here is a link to it.
How would I do this using only CSS and HTML?
I'm aware its possible to create triangles in CSS, so maybe I create one div with the rounded corners for the 'yellow' portion of the box. Not sure how to do the inside grey part though.
I'd like to avoid the multiple images solution because this will be on mobile so that it loads as quickly as possible.
I'm looking into a solution with 3 divs inside and one being a triangle, I found a triangle maker here, then maybe relative position on the yellow div, and absolute position the content after that?
There is a way to achieve this shape without having to use extra elements also. I can understand the reluctance in using multiple images but this approach only makes use of multiple backgrounds and should not have any impact on page load time.
In this approach, we create a linear-gradient background of a smaller size and position it at the right top of the container to achieve the triangle effect.
This approach can also be used with media queries without much issues.
.shape {
height: 400px;
width: 50vw;
background: linear-gradient(225deg, #F3D56A 50%, transparent 50%), #EFEFEF;
background-size: 75px 75px;
background-repeat: no-repeat;
background-position: right top;
border-radius: 4px;
box-shadow: 4px 4px 4px rgba(0, 0, 0, .5);
border-top: 5px #F3D56A solid;
}
#media (max-width: 300px) {
.shape {
width: 150px;
background-size: 50px 50px;
}
}
#media (min-width: 301px) and (max-width: 600px) {
.shape {
width: 300px;
background-size: 50px 50px;
}
}
<div class="shape"></div>
HTML:
<div id="content">
<span></span>
<p class="title">Gold</p>
<p class="subtitle">Starting at</p>
<p class="price">$69.99/mo</p>
More Info
</div>
CSS
#content {
font-family: Helvetica, Serif;
width: 440px;
height: 460px;
background: #EFEFEF;
text-align: center;
border-radius: 5px;
-webkit-box-shadow: 4px 6px 5px 1px rgba(0,0,0,0.41);
-moz-box-shadow: 4px 6px 5px 1px rgba(0,0,0,0.41);
box-shadow: 4px 6px 5px 1px rgba(0,0,0,0.41);
border-top:7px #F3D56A solid;
}
#content span {
border-style: solid;
border-width: 0 110px 110px 0;
border-color: transparent #f3d56a transparent transparent;
line-height: 0px;
_border-color: #000000 #f3d56a #000000 #000000;
_filter: progid:DXImageTransform.Microsoft.Chroma(color='#000000');
float: right;
}
.title {
position: relative;
left: 50px;
}
.title, .price{
color:#2C3E50;
font-size: 45px;
font-weight: 700;
text-align: center;
}
.subtitle {
color: #7A828B;
font-size: 30px;
font-weight: 300;
}
a.button {
text-decoration: none;
color:#FFF;
background: #F3D56A;
padding:20px 30px;
border-radius: 5px;
}
WORKING DEMO
Update:
Media query to 320x480:
#media only screen and (min-width: 320px) and (max-width: 480px) {
#content {
width: calc(100% - 5px);
height: 400px;
}
#content span {
border-width: 0 90px 90px 0;
}
}
Result:
Why don't you just create an image and use that as the background. You could make the image look exactly like the gray and yellow above and then just add it to your "box".
here is the link to a js fiddle I have mocked up - this works pretty well, though I didn't do the entire style https://jsfiddle.net/6pcrneat/
.container {
width:200px;
height:250px;
background:#ffffff;
border:1px solid #000;
position:relative;
}
.shape {
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-o-transform: rotate(45deg);
-ms-transform: rotate(45deg);
position: absolute;
right: -45px;
top: -10px;
border-style: solid;
width: 0px;
height: 0px;
line-height: 0px;
border-width: 0px 70px 70px 70px;
border-color: transparent transparent rgb(243, 213, 106) transparent;
moz-transition: .3s;
-ms-transition: .3s;
-o-transition: .3s;
}
.text {
margin-top: 100px;
text-align: center;
}
Edit: I'll get back to you on the rounded corner of the triangle; I didn't notice that initially
Related
How can i make the border to apply for the radial gradient so that the dotted line will apply for the curve at left and right not as a straight line
.container {
width: 160px;
height: 58px;
border: 1px dotted red;
background: radial-gradient(15px at left, #fff 98%, red) left,
radial-gradient(15px at right, #fff 98%, red) right;
background-size: 51% 100%;
background-repeat: no-repeat;
}
<div class='container'>
</div>
I played around with your code and here is what I came up with. This is a tricky one but it works. I added 2 half circle then hide them. I don't know where are you going to use this but an image will be a good idea as well for this.
.container {
width: 160px;
height: 58px;
border: 1px dotted red;
background: radial-gradient(15px at left, #fff 98%, red) left,
radial-gradient(15px at right, #fff 98%, red) right;
background-size: 51% 100%;
background-repeat: no-repeat;
}
.half-circle-left, .half-circle-right {
width: 30px;
height: 30px;
z-index:1;
background-color: white;
vertical-align:middle;
margin-top:8%;
overflow: overlay;
}
.half-circle-left {
float: left;
margin-left: -15px;
overflow: hidden;
border-radius: 0px 15px 15px 0px;
border-right: 1px dotted red;
}
.half-circle-right {
float:right;
margin-right: -15px;
border-radius: 15px 0px 0px 15px;
border-left: 1px dotted red;
}
<div class='container'>
<div class="half-circle-left">
</div>
<div class="half-circle-right">
</div>
MY TEXT HERE! Please put more text here
</div>
As I know it is not possible, instead of using a dotted border you can use box-shadow to act like a solid border and use a pseudo-elements :after :before to draw the circle in both sides.
Here is the code:
<div class='container'>
</div>
.container {
position: relative;
overflow: hidden;
width: 160px;
height: 58px;
background: red;
box-shadow: inset 0 0 0 2px #000;
}
.container: after, .container:before {
content: '';
position: absolute;
inset-inline-start: 0;
width: 30px;
aspect-ratio: 1;
background: #ffffff;
border-radius: 100%;
inset-block-start: 50%;
transform: translateY(-50%) translateX(-50%);
box-shadow: inset 0 0 0 2px #000;
}
.container: before {
inset-inline: auto 0;
transform: translateY(-50%) translateX(50%);
}
problem photo
I'm trying to make this image a circle I believe I might have too much padding. When I adjust the border-radius it doesn't round correctly. I have added the html and a link to see the problem.
button
{
background-color: #433966;
border: 5px solid #5d596a;
border-radius: 10px;
box-shadow: 8px 8px 8px rgba(65, 62, 62, 0.2);
margin: 0px auto;
display: block;
color: rgb(205, 187, 162);
font-size: 20px;
padding: 15px 20px;
transition: all 300ms ease;
text-align: justify;
font-weight: bold;
font-family: Optima;
}
img
{
border-radius: 50%;
perspective: 8px;
padding-bottom:5%;
padding-top: 20%;
border-color: silver;
width: 300px !important;
height: 300px !important;
}
<div class="fade-in-image">
<img src="images/rachel.jpg" alt="Avatar" class="center"
width= 100%>
<button>Portfolio</button>
</div>
You should wrap your image in another element and add padding and other styles to it
div
{
border-radius: 20%;
perspective: 8px;
padding-bottom:5%;
padding-top: 20%;
border-color: silver;
width: 300px !important;
height: 300px !important;
}
div img{
width: 100%;
height: 100%;
}
you can use a margin instead of padding to add space and it will work fine
img
{
border-radius: 20%;
perspective: 8px;
margin-bottom:5%;
margin-top: 20%;
border-color: silver;
width: 300px !important;
height: 300px !important;
}
image for padding
vs
image for margin
I want to make an input text field, like the one on https://www.tesla.com/en_gb/cybertruck/design#battery (it appears after clicking 'buy now') but I am unsure how to approach this.
I have tried adding border-radius but of course that only rounds the corners.
Below is my current code:
<style>
body {
background-color: black;
}
label {
color: white;
}
input {
width: 300px;
height: 40px;
border: solid white 1px;
background: transparent;
color: white;
font-family: 'Consolas';
font-size: 0.9em;
font-weight: bold;
padding: 10px 10px 10px 10px;
transition: border 0.3s ease-in-out;
box-sizing: border-box;
outline-width: 0;
border-radius: 10px 10px 10px 10px;
border-style: none;
border-width: 0 0 3px;
padding: 3px 10px;
}
input:focus {
border: solid white 3.5px;
}
</style>
<label>Test field</label>
<br>
<input type = "text">
I would like this to be responsive if possible, thanks in advance.
They are using clip-path and polygon to do this. See this page for details: https://css-tricks.com/notched-boxes/
As already mentioned, by looking at the source you can see that it's a clip path.
In particular the clip path is applied to a wrapper div as ::before pseudo element as opposed to on the input element. Here's a simple example using the exact same clip path on the website.
body,
html {
padding: 0;
margin: 0;
background-color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.wrapper {
width: 200px;
height: 40px;
position: relative;
display: block;
}
.wrapper::before {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: blue;
clip-path: polygon(0px 0px, 100% 0px, 100% calc(100% - 10px), calc(100% - 10px) 100%, 0px 100%, 0px 1.5px, 1.5px 1.5px, 1.5px calc(100% - 1.5px), calc(100% - 11.5px) calc(100% - 1.5px), calc(100% - 1.5px) calc(100% - 11.5px), calc(100% - 1.5px) 1.5px, 0px 1.5px);
}
input {
background: transparent;
border-color: transparent;
border-radius: 0;
color: white;
width : 100%;
height: 100%;
outline: none;
}
<div class="wrapper">
<input type="text" />
</div>
The clip path is kind of too complicated to explain bit-by-bit, but it essentially cuts out the middle of a solid rectangle as well as a little corner. So in this case, the background color is what controls the "border color". In order to animate on hover, it probably changes some elements of the clipping path if I were to guess.
I want just to do that:
However I have a lot of troubles to do it with css... Someone can help me?
This is my better try:
.flag.vertical {
background-color: #dd7758;
height: 0;
padding-bottom: 25px;
text-align: center;
color: white;
border-style: solid;
border-width: 0 10px 10px 10px;
border-color: transparent transparent white transparent;
}
<div class="flag-wrapper"><span class="flag vertical">-5%</span></div>
My doubts are about to make this white triangle on the bottom. Doesn't matter here the vertical align of the text and the font family.
Set the width to 0
.flag-wrapper{ background-color: #dd7758;
height: 0;
padding-bottom: 25px;
text-align: center;
color: white;
border-style: solid;
border-width: 20px 20px 20px 20px;
border-color: transparent transparent white transparent;
width: 0;
}
span{
margin-left:-10px
}
<div class="flag-wrapper"><span>-5%</span></div>
You can user after element for a triangle
Edit: You can use clip-path to make a shape as you want
This also solves your Another question that maybe do you know. If I want to add this div over an image, is it possible that the triangle will be transparent instead of white? comment
.flag.vertical {
background-color: #dd7758;
padding: 10px 10px 25px;
text-align: center;
color: white;
display: inline-block;
clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 50% 70%, 0% 100%);
}
<div class="flag-wrapper"><span class="flag vertical">-5%</span></div>
Here is an idea with gradient where it will be responsive and you will have better support than clip path:
.flag-wrapper {
background-color: #dd7758;
padding:10px 5px 30px;
margin:10px;
text-align: center;
color: white;
display:inline-block;
background:
linear-gradient(to top left,transparent 48%,#dd7758 50%) bottom left/50% 15px,
linear-gradient(to top right,transparent 48%,#dd7758 50%) bottom right/50% 15px,
linear-gradient(#dd7758,#dd7758)top/100% calc(100% - 15px);
background-repeat:no-repeat;
}
body {
background:#ccc;
}
<div class="flag-wrapper">-5%</div>
<div class="flag-wrapper">-25%</div>
<div class="flag-wrapper">-100%</div>
You can also make it working with image as background but you lose transparency:
.flag-wrapper {
background-color: #dd7758;
padding:10px 5px 30px;
margin:10px;
text-align: center;
color: white;
display:inline-block;
background:
linear-gradient(to bottom right,transparent 48%,#ccc 50%) bottom left/50.1% 15px,
linear-gradient(to bottom left,transparent 48%,#ccc 50%) bottom right/50.1% 15px,
url(https://picsum.photos/200/300?image=1069)center/cover;
background-repeat:no-repeat;
}
body {
background:#ccc;
}
<div class="flag-wrapper">-5%</div>
<div class="flag-wrapper">-25%</div>
<div class="flag-wrapper">-100%</div>
Use the following code, you'll also be able to add enough content in your div
* {
box-sizing: border-box;
}
.flag {
width: 110px;
height: 56px;
margin: 0 auto;
padding-top: 15px;
position: relative;
background: hotpink;
color: white;
font-size: 11px;
letter-spacing: 0.2em;
text-align: center;
text-transform: uppercase;
}
.flag:after {
content: ' ';
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 0;
border-bottom: 13px solid white;
border-left: 55px solid transparent;
border-right: 55px solid transparent;
}
<div class="flag">
5 Items
</div>
You may hide bottom triangle with another div
HTML:
<div class="flag-wrapper">
-5%
<div class="pointer">
</div></div>
CSS:
.flag-wrapper{
text-align: center;
padding-top: 10px;
background-color: #dd7758;
width:50px;
height:65px;
}
.pointer{
content: ' ';
position: absolute;
width: 45px;
height: 45px;
left: 10px;
top: 54.5px;
background-color: #fcfcfc;
transform: rotate(45deg) skew(5deg, 5deg);
-moz-transform: rotate(45deg) skew(5deg, 5deg);
-ms-transform: rotate(45deg) skew(5deg, 5deg);
-o-transform: rotate(45deg) skew(5deg, 5deg);
-webkit-transform: rotate(45deg) skew(5deg, 5deg);
}
I have a little project here that I am working on. I am not very far with it at the moment, but that is beside the point.
This is the CSS, since I am required to post code if I provide a link:
body{
background: #FF4D4D;
background: -webkit-radial-gradient(circle, #FF4747, #FF0000);
background: -o-radial-gradient(circle, #FF4747, #FF0000);
background: -moz-radial-gradient(circle, #FF4747, #FF0000);
background: radial-gradient(circle, #FF4747, #FF0000);
}
#background {
width: 400px;
height: 400px;
border: 15px solid #FFFFFF;
border-radius: 50%;
margin: 35px 0px 0px 700px;
background: #FF0000;
position: relative;
}
#mailicon {
border: 5px solid black;
border-radius: 25px;
width: 200px;
height: 150px;
margin: 120px 0px 0px 95px;
background: white;
clip: circle(60px 725px 460px 1125px);
}
#flap1 {
background: white;
margin: 50px 0px 0px 0px;
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
height: 115px;
width: 115px;
border: thick solid black;
margin: -65px 0px 0px 38px;
}
#flap2 {
background: red;
height: 90px;
width: 172px;
margin: -95px 0px 0px 14px;
border-bottom: thick solid black;
}
.flap {
position: absolute;
}
#opentext {
/*To be done later*/
}
My problem is that I need to hide any part of the mail icon that is outside of the outside of the background div, which is shaped as a circle and the parent div.
I have browsed around and am thinking that my problem relates to clipping or masking, but I can't find a way to properly implement those.
I would rather not just put it behind other divs to turn in invisible because the parent is circular and it would take a lot of extra divs to hide the mail icon fully.
Any suggestions?
If I'm understanding you correctly, you're looking to hide parts of the mail icon behind the circle. If that's the case, you can either use clip() like you have, or use overflow: hidden on a parent div.