I would like to make a diamond in a rectangle. I've already did it with a square :
.box {
width:100px;
height:100px;
background:orange;
z-index:1;
position:relative;
}
.box:before {
position:absolute;
content:'';
width:70.71%;
height:70.71%;
transform: rotate(45deg);
background: red;
top: 15%;
left: 15%;
}
<div class="box"></div>
But I want to make it like this :
The rectangle is responsive so it's never the same size. Any idea ?
Thanks a lot
This approach uses two triangles generated using CSS border.
I don't think you can use % for borderwidth, so I have used viewport units instead.
.box {
width: 50vw;
height: 25vw;
background: orange;
z-index: 1;
position: relative;
}
.box:before,
.box:after {
content: '';
position: absolute;
width: 0;
height: 0;
}
.box:before {
border-right: solid 25vw red;
border-top: solid 12.5vw transparent;
border-bottom: solid 12.5vw transparent;
}
.box:after {
right: 0;
border-left: solid 25vw red;
border-top: solid 12.5vw transparent;
border-bottom: solid 12.5vw transparent;
}
<div class="box"></div>
You're attempting to create a diamond by modifying a rectangle. If you tried that with a paper rectangle, you'd understand it's not the simplest way to go about it.
You could use clip-path:
.diamond {
background-color: #eee;
-webkit-clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
margin: 0 auto;
text-align: center;
padding: 2rem;
}
<div class="diamond">I'm a diamond</div>
... and all that's left for you to do is to set it's width, height (or min-* / max-* for any of them) in order to control its proportion responsively.
Do note CSS clip-path is currently supported by only ~88% of actively used browsers most notably lacking support by IE and Edge.
If you need support for those, the only way to do it is by using two levels of wrappers and construct the outline from ::before and ::after pseudos of those wrappers.
I would like to add that line in border property, not create new div.
Is it possible?
http://i.stack.imgur.com/9SsYo.png
You can use CSS3 transform style to do that. You can use jQuery aswell: How to rotate a div using jQuery
If you choose to use CSS3 transform style, you need to set border of your div and rotate it. If you have something (image, text, etc) in your div, it will rotate with the div, so you need to un-rotate them using the same method.
If this doesn't help you, paste your code in jsFiddle and it will be way easier to help you. Good luck.
Not sure, but if you are looking to clip:
-webkit-clip-path: polygon(28% 0, 100% 0, 100% 81%, 74% 100%, 0 100%, 0 19%);
clip-path: polygon(28% 0, 100% 0, 100% 81%, 74% 100%, 0 100%, 0 19%);
You can use css pseudo classes to do this.
https://jsfiddle.net/L87jf1d8/2/
Use :before and :after to the box class
<div class="box">
Name
</div>
Css:
.box{
padding:30px;
text-align:center;
width:200px;
border:1px solid #000;
}
.box:after,.box:before{
content: " ";
width: 55px;
height: 1px;
transform: rotate(-45deg);
background: #000;
position: absolute;
}
.box:after{
margin-left: 64px;
margin-top: 29px;
}
.box:before{
margin-left: -120px;
margin-top: -12px;
}
Define a slant class like the following and apply it to any div regardless of its size:
Note that you should also set overflow: hidden; on main element;
.box1, .box2, .box3 {
width: 100px;
height: 100px;
background-color: white;
border: 1px solid black;
overflow: hidden;
margin-top: 10px;
}
.box2 {
width: 200px;
}
.box3 {
width: 300px;
}
.slant:before, .slant:after {
content: '';
border: 1px solid tomato;
display: inline-block;
transform: translate3d(-50%, -50%, 0) rotate(45deg);
position: relative;
top: 0;
right: 0;
width: 30px;
height: 30px;
}
.slant:after {
transform: translate3d(-150%, -50%, 0) rotate(45deg);
top: 100%;
left: 100%;
}
<div class="box1 slant"></div>
<div class="box2 slant"></div>
<div class="box3 slant"></div>
Is it possible to create a div with an irregular shape with CSS? I already searched but I can't find a good example. The style is something like this:
/
/ \
/ \
/ \
/___________________________\
There should be a line on the top that connects it. Basically it has different height on the left and right side.
THE SVG WAY
Since the shape you request is not possible with only CSS, I suggest you use SVG to draw the shape.
The following piece of code will create the below shape:
<svg height="150" width="150">
<polygon points="20,10 100,30 120,100 0,100" style="fill:red;" />
</svg>
SVG is a powerful tool to make shapes otherwise impossible without using images. Read up on it here.
HTML & CSS Answer
This can be done by using perspective and transform. You create two divs: one that will get the perspective and one that will be transformed. Just know that anything in the .test-div will also be transformed, so if you put text in there, it will also get transformed.
.wrapper {
width: 100px;
height: 100px;
margin-left: 100px;
perspective: 150px;
}
.test {
height: 100px;
width: 100px;
background: red;
transform: rotateX(45deg);
}
<div class="wrapper">
<div class="test"></div>
</div>
Result
JSFIDDLE
This can also be done using CSS clip-path
div {
width: 200px;
height: 150px;
background: red;
-webkit-clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
}
<div></div>
You can then just change the background element if you need an image.
div {
width: 200px;
height: 150px;
background: url(https://lorempixel.com/200/150/);
-webkit-clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
}
<div></div>
In their current state, clip-paths aren't as widely supported as inline or imported SVG but is a much cleaner, and in some cases, easier variant to use.
Browser Support
You can try using overflow hidden and transforms, though the best approach will be svg.
HTML
<div class="out">
<div class="in"></div>
</div>
CSS
body { background:url(http://www.placecage.com/g/640/480) }
.out {
height: 100px;
width: 150px;
transform-origin: 0% 100%;
transform: skew(-10deg);
overflow: hidden;
}
.in {
height: 110px;
width: 148px;
position: relative;
left: -43px;
top: -7px;
transform-origin: 0% 0%;
transform: skew(30deg) rotate(10deg);
background: rgba(9,40,0,0.8);
transition: 0.5s ease;
}
.in:hover {
background: rgba(50,0,70,0.7);
transition: 0.5s ease;
}
FIDDLE : https://jsfiddle.net/xb1jxd7g/
Following it for diamond and make a way around to rotate it and you'll get your shape :
#diamond-shield {
width: 0;
height: 0;
border: 50px solid transparent;
border-bottom: 20px solid red;
position: relative;
top: -50px;
}
#diamond-shield:after {
content: '';
position: absolute;
left: -50px; top: 20px;
width: 0;
height: 0;
border: 50px solid transparent;
border-top: 70px solid red;
}
Hello I want to achieve similar to this image.
Here is my css code
*{
background:#444;
margin:0;
padding:0;
}
.display{
width:100%;
min-height:100%;
background:green;
position:fixed;
}
.one{
width:100%;
height:300px;
margin-top:-200px;
background:red;
transform: rotate(45deg);
}
.two{
width:100%;
height:450px;
margin-top:200px;
background:blue;
transform: rotate(45deg);
}
I've try to achieve similar positioning here is my Code is here
My question is - What could you suggest me to achieve similar positioning?
Is it good to use transform for 4 div images and positioning them?
Assuming this is a background, let's simplify it with a single HTML element.
Top and bottom background colors are a single gradient with two colors
left and right background colors are :before and :after pseudo elements rotated with transform: rotate
The before and after pseudo elements get z-index: 1. Elements that should be above them get position: relative and z-index: 2
Example
body {
background: #212121;
}
div {
background: #F00;
height: 500px;
width: 500px;
position: relative;
overflow: hidden;
background: linear-gradient(to bottom, #EB1249 0%, #EB1249 50%, #251F39 50%, #251F39 100%);
margin: 0 auto;
}
div:before {
content: '';
display: block;
position: absolute;
top: 0;
left: -70%;
transform: rotate(45deg);
background: #fce4ec;
width: 100%;
height: 100%;
z-index: 1;
}
div:after {
content: '';
display: block;
position: absolute;
top: 0;
right: -59.3%;
transform: rotate(45deg);
background: #F5B8A2;
width: 90%;
height: 100%;
z-index: 1;
}
<div></div>
This is pretty easy if you set the transform-origin to the corners of your boxes. Basically, instead of rotating from the middle, you can rotate from the corner. So You'd have two boxes at, for instance:
right : 200px;
bottom : 200px;
transform-origin : 100% 100%;
one rotated 45deg, the other -45deg. Then the other two at 190, 210 or whatever. Note that you also need -webkit-transform-origin, -ms-transform-origin, -moz-transform-origin, -o-transform-origin
I need something like this:
How can achieve this with css? I know that one way is use background image, but can I achieve this only with css without any image?
There is a hacky way to do this, using the :before pseudo element. You give the :before a border, then rotate it with a CSS transform. Doing it this way adds no extra elements to the DOM, and adding/removing the strikethrough is a simple as adding/removing the class.
Here's a demo
Caveats
This will only work down to IE8. IE7 does not support :before, however will degrade gracefully in browsers that do support :before but don't support CSS transforms.
The angle of rotation is fixed. If the text is longer, the line will not touch the corners of the text. Be mindful of this.
CSS
.strikethrough {
position: relative;
}
.strikethrough:before {
position: absolute;
content: "";
left: 0;
top: 50%;
right: 0;
border-top: 1px solid;
border-color: inherit;
-webkit-transform:rotate(-5deg);
-moz-transform:rotate(-5deg);
-ms-transform:rotate(-5deg);
-o-transform:rotate(-5deg);
transform:rotate(-5deg);
}
HTML
<span class="strikethrough">Deleted text</span>
You can use background linear-gradient with currentColor to avoid hardcoding font color:
.strikediag {
background: linear-gradient(to left top, transparent 47.75%, currentColor 49.5%, currentColor 50.5%, transparent 52.25%);
}
.withpadding {
padding: 0 0.15em;
}
The value is <span class="strikediag">2x</span> 3x<br>
The number is <span class="strikediag">1234567890</span>.
<p>
The value is <span class="strikediag withpadding">2x</span>3x<br>
The number is <span class="strikediag withpadding">1234567890</span>.
If you don't need the element to be fully inline, you can use a pseudo element to place the line on top of the element. This way the angle can be adjusted by changing the pseudo element's size:
.strikediag {
display: inline-block;
position: relative;
}
.strikediag::before {
content: '';
position: absolute;
left: -0.1em;
right: -0.1em;
top: 0.38em;
bottom: 0.38em;
background: linear-gradient(to left top, transparent 45.5%, currentColor 47.5%, currentColor 52.5%, transparent 54.5%);
pointer-events: none;
}
The value is <span class="strikediag">2x</span> 3x<br>
The number is <span class="strikediag">1234567890</span>.
del {
position:relative;
text-decoration:none;
}
del::after {
content:"";
position:absolute;
top:50%; left:0; width:100%; height:1px;
background:black;
transform:rotate(-7deg);
}
Enhancing Bojangles answer from above:
.strike-diagonal-price {
position: relative;
background-color: black;
color: white;
}
.strike-diagonal-price:before {
position: absolute;
content: "";
left: 0;
top: 45%;
right: 0;
border-top: 2px solid;
border-color: red;
-webkit-transform: rotate(-25deg);
-moz-transform: rotate(-25deg);
-ms-transform: rotate(-25deg);
-o-transform: rotate(-25deg);
transform: rotate(-25deg);
}
Now I get the affect I needed:
I think you could probably apply a rotation effect to a horizontal rule. Something like:
<html>
<body>
<hr />
123456
</body>
</html>
With the CSS:
hr
{
width: 50px;
position: absolute;
background-color: #000000;
color: #000000;
border-color: #000000;
transform:rotate(-7deg);
-ms-transform:rotate(-7deg);
-moz-transform:rotate(-7deg);
-webkit-transform:rotate(-7deg);
-o-transform:rotate(-7deg);
}
Fiddle
Your mileage may vary depending on browser and version though, so I'm not sure if I'd resort to this. You might have to pull off some funky VML code to support older versions of IE, for example.
CSS3 gradient
background-image: linear-gradient(left bottom, rgb(234,20,136) 0%, rgb(255,46,164) 50%, rgb(255,74,197) 0%);
background-image: -o-linear-gradient(left bottom, rgb(234,20,136) 0%, rgb(255,46,164) 50%, rgb(255,74,197) 0%);
background-image: -moz-linear-gradient(left bottom, rgb(234,20,136) 0%, rgb(255,46,164) 50%, rgb(255,74,197) 0%);
background-image: -webkit-linear-gradient(left bottom, rgb(234,20,136) 0%, rgb(255,46,164) 50%, rgb(255,74,197) 0%);
background-image: -ms-linear-gradient(left bottom, rgb(234,20,136) 0%, rgb(255,46,164) 50%, rgb(255,74,197) 0%);
background-image: -webkit-gradient( linear, left bottom,right top,color-stop(0, rgb(234,20,136)), color-stop(0.5, rgb(255,46,164)), color-stop(0, rgb(255,74,197)) );
My example won't fill your needs perfectly but, for more info and funny tweaks, see http://gradients.glrzad.com/.
What you have to do is create a background-gradient of white-black-white and position your opacity at something like 48% 50% 52%.
This is an old question but as an alternative you can use CSS3 Linear Gradients for example (http://codepen.io/yusuf-azer/pen/ojJLoG).
For extensive explaining and a LESS Solution check
http://www.yusufazer.com/tutorials-how-to/how-to-make-a-diagonal-line-through-with-css3/
span.price--line-through {
background-color: transparent;
background-image: -webkit-gradient(linear, 19.1% -7.9%, 81% 107.9%, color-stop(0, #fff), color-stop(.475, #fff), color-stop(.5, #000), color-stop(.515, #fff), color-stop(1, #fff));
background-image: -webkit-repeating-linear-gradient(287deg, #fff 0%, #fff 47.5%, #000 50%, #fff 51.5%, #fff 100%);
background-image: repeating-linear-gradient(163deg, #fff 0%, #fff 47.5%, #000 50%, #fff 51.5%, #fff 100%);
background-image: -ms-repeating-linear-gradient(287deg, #fff 0%, #fff 47.5%, #000 50%, #fff 51.5%, #fff 100%);
}
I don't think there is a sustainable css solution to this.
My pure CSS solution would be to place another element of text behind your first element of text that is identical in number of characters (characters being ' '), a text-decleration of line-through, and a transform of rotate.
Something like:
<html>
<head>
<style>
.strike{
text-decoration: line-through;
-webkit-transform: rotate(344deg);
-moz-transform: rotate(344deg);
-o-transform: rotate(344deg);}
</style>
</head>
<body>
<p>Text to display</p>
<p class='strike'></p>
</body>
</html>
Text Rotation example
I am looking forward to seeing better answers from other users.
Here is my solution using clip-path, which is responsive also
p{
position : relative
}
span{
position: absolute;
width: 100%;
height: 100%;
color : black;
clip-path: polygon(100% 0 , 100% 12% ,0% 100% , 0% 88%);
}
<p><span/>$100</p>
I did it this way using an SVG in the HTM with a CSS class:
HTML:
<ul>
<li>Regular Price: <div class="zIndex-10a">$4.50</div><div class="zIndex-10b"><svg height="16" width="5"><line x1="0" y1="20" x2="40" y2="0" class="Strike-01a"></svg></div></li>
</ul>
CSS:
/* -- CONTAINS TEXT TO STRIKE ---- */ .zIndex-10a { display: inline-block; position: relative; left: 0px; z-index: 10; }
/* -- CONTAINS SVG LINE IN HTML -- */ .zIndex-10b { display: inline-block; position: relative; right: 40px; z-index: 11; }
/* -- SVG STROKE PROPERTIES ------ */ .Strike-01a { stroke: rgb(255,0,0); stroke-width:2; }
There might be simpler easier ways by now. I just cooked this up in a pinch for my product detail special offer page. Hope it helps someone.
Try
.mydiv {
background-color: #990000;
color: #FFFF00;
font-size: 2em;
padding-top: 10px;
padding-bottom: 10px;
border-radius: 15px;
max-width: 300px;
width: 100%;
}
.strikethrough-100p, .strikethrough-50p{
position: relative;
text-align: center;
}
.strikethrough-100p:before {
position: absolute;
content: "";
left: 0;
top: 50%;
right: 0;
border-top: 3px solid;
border-color: inherit;
-webkit-transform:rotate(-5deg);
-moz-transform:rotate(-5deg);
-ms-transform:rotate(-5deg);
-o-transform:rotate(-5deg);
transform:rotate(-5deg);
}
.strikethrough-50p:before {
position: absolute;
content: "";
width:50%;
left: 25%;
top: 50%;
right: 0;
border-top: 3px solid;
border-color: inherit;
-webkit-transform:rotate(-5deg);
-moz-transform:rotate(-5deg);
-ms-transform:rotate(-5deg);
-o-transform:rotate(-5deg);
transform:rotate(-5deg);
}
<div class="mydiv">
<div class="strikethrough-100p">123456</div>
</div>
<br>
<div class="mydiv">
<div class="strikethrough-50p">123456</div>
</div>
And here's a fancy version:
background:none repeat scroll 0 0 rgba(255, 0, 0, 0.5);
-moz-border-radius:20px 0;
-webkit-border-radius:20px 0;
border-radius:20px 0;
content: "";
height:5px; left:-5%;
position:absolute;
top:30%;
-moz-transform:rotate(-7deg);
-webkit-transform:rotate(-7deg);
transform:rotate(-7deg);
transform-origin:50% 50% 0;
width:110%;