So I'm drawing elements in CSS, using this tutorial as a guide. I need some help with borders, though. For instance, here's my code for a curved trapezoid:
.foobar {
height: 0px;
width: 140px;
position: relative;
border-bottom: 200px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom-left-radius: 150px 70px;
border-bottom-right-radius: 100px 25px;
}
The problem: I want to draw a 1px line border around the foobar element, but I'm already using the border properties to draw the element in the first place.
Is there an easy way to do this? My sense is that I'll have to create a shadow element that is the same shape as -- but slightly larger than -- the foobar element.
Thanks in advance!
You can position a :pseudo element behind with slightly adjusted dimensions.
.foobar, .foobar:before {
height: 0px;
width: 140px;
position: relative;
border-bottom: 200px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom-left-radius: 150px 70px;
border-bottom-right-radius: 100px 25px;
}
.foobar:before {
content: "";
display:block;
position: absolute;
left: -31px;
top: -1px;
width: 142px;
z-index: -1;
border-bottom: 202px solid black;
/* add these lines if you're a pixel perfectionist */
border-bottom-left-radius: 150px 71px;
border-bottom-right-radius: 100px 26px;
}
http://jsfiddle.net/4vNGL/2
You can use a pseudo element drawn behind with same rules with a small increase of scale.
.foobar, .foobar:before {
height: 0px;
width: 140px;
position: relative;
border-bottom: 200px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom-left-radius: 150px 70px;
border-bottom-right-radius: 100px 25px;
position:relative;
}
.foobar:before {
content:'';
position:absolute;
display:block;
z-index:-1;
top:0;
left:-30px;
width: 140px;
-webkit-transform-origin:center;
-webkit-transform:scale(1.03);/* adapt here the width of your fake border */
transform-origin:center;
transform:scale(1.03);
border-bottom: 200px solid black; /* color of fake border */
}
http://codepen.io/gc-nomade/pen/eDIGJ
You can even play with both pseudo-element and still add some shadows: http://codepen.io/gc-nomade/pen/axmsc
Related
I have created a div that looks like an arrow with css border.
.blue-arrow-right {
width: 0;
height: 0;
position: relative;
float: left;
margin-left: 0px;
margin-top: 5px;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
border-left: 30px solid #009de1;
}
Now i want to create an extra border on the right side of that div, lets say: 1px solid black
How can i do that?
hers is the fiddle: https://jsfiddle.net/wqehc9vv/4/
So it should look like this:
image preview
You can use a pseudo-element like :before for that. And make it slightly bigger than the div. Also position it accordingly. See below
.blue-arrow-right {
width: 0;
height: 0;
position: relative;
float: left;
margin-left: 0px;
margin-top: 5px;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
border-left: 30px solid #009de1;
}
.blue-arrow-right:before {
content:"";
position:absolute;
left:-30px;
top:-32px;
border-top: 32px solid transparent;
border-bottom: 32px solid transparent;
border-left: 32px solid black;
z-index:-1;
}
<div class="blue-arrow-right">
</div>
How would I achieve the following as seen in the image below, in the best way as possible? I want a thick top border, but as it goes down I want the sides to become thinner and just "mend" (if that's right expression) into the black block.
This is my CSS code for the black block:
.containerMain {
background: #000;
padding: 15px;
border-radius: 5px;
width: 250px;
}
You can use the after pseudo-element to position an upside-down trapezoid behind your element.
Look here for a trapezoid shape example.
body { padding: 30px; }
.containerMain {
background: black;
padding: 15px;
border-radius: 5px;
width: 250px;
height: 250px;
position: relative;
}
.containerMain:after {
content: '';
border-radius: inherit;
margin: -20px;
margin-top: -25px;
width: 100%;
height: 50%;
position: absolute;
z-index: -1;
/* upside-down red trapezoid props */
border-top-width: 150px;
border-top-style: solid;
border-top-color: red;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
}
<div class="containerMain"></div>
I want to be able to skew an element in the way the image displays below.
I have been playing around with it, but dont seem to be able to get close to replicating that shape.
My css code is
transform:skew(30deg,30deg);
Is transform even the right way to do this? Please let me know the best, most browser compatible, solution.
You can apply some rotate transform around the X axis and apply an appropriate pespective before:
div {
width:300px;
height:200px;
background:url(http://placekitten.com/300/200);
border:2px solid red;
border-top-width:4px;
border-bottom-width:1px;
-webkit-transform: perspective(200px) rotateX(40deg);
margin:100px;
}
Demo
Try this:
Html
<div class="trapezium"></div>
StyleSheet
.trapezium {
border-bottom: 80px solid #fff;
border-left: 45px solid transparent;
border-right: 45px solid transparent;
padding: 0 8px 0 0;
height: 0;
width: 120px;
position: relative;
margin: 2em auto;
}
.trapezium:before {
border-bottom: 90px solid #000;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
padding: 0 8px 0 0;
height: 0;
width: 130px;
position: absolute;
bottom: -85px;
left: -55px;
content: "";
z-index: -1;
}
Here is the Demo
I have a testimonials box that I would like to add a triangle to.
.arrow {
float: left;
margin-left: 25px;
margin-top: 20px;
width: 0;
height: 0;
border-top: 20px solid #eee;
border-left: 0px solid transparent;
border-right: 25px solid transparent;
}
The problem is the triangle ends up being solid, as opposed to white with a gray border. Below is a screenshot of how the CSS currently displays. Thanks in advance for the time and help.
You can create two triangles, one that overlaps the other, to create this bordered effect. You can do this with the ::before and ::after pseudo-elements so that you don't even have any superfluous HTML.
http://jsfiddle.net/7K2c4/
.mybox {
position: relative;
border: 1px solid #ccc;
}
.mybox:before,
.mybox:after { position: absolute;
left: 20px;
bottom: -19px;
display: block;
width: 0;
height: 0;
border-width: 0 25px 20px;
border-style: solid;
border-color: transparent;
border-left-color: #fff;
content: ' ';
}
.mybox:before { left: 19px;
bottom: -21px;
border-left-color: #ccc; }
You can place another triangle over it, smaller with the same color of the box background. You don't even need to create another HTML element, just use a pseudo-element selector.
I tried to create this canvas in css:
this is my jsfiddle:
http://jsfiddle.net/alonshmiel/kyfha/35/
I have a problem only with the circle border, it's not exactly like the canvas.
this is my css:
.PersonaCanvas {
width: 100px;
height: 100px;
border-top: 12px solid rgb(238,238,238);
border-left: 12px solid rgb(238,238,238);
border-right: 12px solid rgb(238,238,238);
-webkit-border-radius: 100px;
-khtml-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
overflow: hidden;
}
.PersonaCanvas img {
display: block;
margin-top:16px;
width:100%;
height:80%;
}
and my html:
<div class="PersonaCanvas">
<img src="http://s8.postimg.org/ij71l6xol/New_Pers_achiever_1.png"/>
</div>
any help appreciated!
Basically you set a width and height of the container to 100px, but when you add a border, it automatically grows in size; if you inspect, you have the final width which is 124px, which comes from the 100px you set, along with the 12px border-left and another 12px border-right. You would either have to manually change it to accommodate the size to add up to 100px, or you can use a css3 method of box-sizing: border-box to do the calculation for you. Also, we had to change the width of the img to 80%, since you want it to stay in proportion to its height within the container. Lastly, the size of the img is fixed, but we have to align its margin: 16px auto 0. Try this updated one:
.PersonaCanvas {
width: 100px;
height: 100px;
border-top: 12px solid rgb(238,238,238);
border-left: 12px solid rgb(238,238,238);
border-right: 12px solid rgb(238,238,238)
-webkit-border-radius: 100px;
-khtml-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
overflow: hidden;
box-sizing: border-box
}
.PersonaCanvas img {
display: block;
margin: 16px auto 0;
width: 80%;
height: 80%;
}
The issue with the width of the border decreasing is cause by the lack of a bottom border (which is assumed to be 0 pixels)
So by adding a transparent bottom border you can make the width consistent.. (but you will need extra elements to make the ends be curved)
border-bottom: 12px solid transparent;
http://jsfiddle.net/kyfha/39/
I added two other div's. One for a white circle to cover part of the solid bottom border. And I also added the css elements and made te absolute so they will still fit wherever you need them. The border ends are not perfect, but if you made a white image with the correct edhe you have and inseted it into the thrid div tag, you will be perfect.
I hope this helps :)
http://jsfiddle.net/kyfha/44/
<div class="PersonaCanvas">
<img src="http://s8.postimg.org/ij71l6xol/New_Pers_achiever_1.png"/>
</div>
<div class="PersonaCanvas2">
</div>
<div class="PersonaCanvas3">
</div>
Here is the CSS
I changed the size of the image slightly and moved it around a little to give you your image.
.PersonaCanvas {
width: 100px;
height: 100px;
border-top: 12px solid transparent;
border-left: 12px solid transparent;
border-right: 12px solid transparent;
border-bottom: 12px solid transparent;
overflow: hidden;
position: absolute;
z-index: 100;
}
.PersonaCanvas2 {
width: 100px;
height: 100px;
border-top: 12px solid rgb(238,238,238);
border-left: 12px solid rgb(238,238,238);
border-right: 12px solid rgb(238,238,238);
border-bottom: 12px solid rgb(238,238,238);
-webkit-border-radius: 100px;
-khtml-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
overflow: hidden;
position: absolute;
z-index: 1;
}
.PersonaCanvas3 {
position: absolute;
width: 50px;
height: 50px;
-webkit-border-radius: 50px;
-khtml-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
background: #FFF;
top: 110px;
left: 46px;
z-index: 10;
}
.PersonaCanvas img {
display: block;
margin-top:16px;
width:80px;
height:80px;
position: absolute;
left: 10px;
top: -0px;
}
Basicallly I have two instances of the div tag and the image is in one without the border or the radius.