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.
Related
I'm new to the css world and I'm trying to make a responsive design like this using bootstrap, css and html5
for more information here is an illustration:
curve
how to create the same curve and a circle above? i really need help i try everything but it doesn't work
a little help will be really good
You have two options, the better is to use SVG (I would go for this one if its me), also you can implement something like you provided in HTML but not every shape is possible to have using HTML, if curve and circles all you need then, something like can be implemented:
.main {
border-radius: 50%;
width: 300px;
height: 300px;
background: red;
padding: 50px;
position:absolute;
top:-240px;
}
.inner {
border-radius: 50%;
width: 300px;
height: 300px;
background: white;
}
.innerWhite {
border: 1px solid 50px;
border-radius: 50%;
width: 50px;
height: 50px;
background: white;
position: absolute;
bottom: 45px;
}
<div class="main">
<div class="inner">
<div class="innerWhite"></div>
</div>
</div>
You can use radial-gradient combined with mask:
.box {
height:300px;
max-width:1024px;
margin:auto;
border:1px solid red;
position:relative;
}
.box::before {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background:radial-gradient(circle 1800px at 50% -840px,transparent 53.8%,red 54% 60.8%,transparent 61%);
-webkit-mask:radial-gradient(circle at 20% 70%, transparent 80px,#fff 81px);
mask:radial-gradient(circle at 20% 70%, transparent 80px,#fff 81px);
}
body {
background:url(https://i.picsum.photos/id/1000/800/800.jpg) center/cover;
}
<div class="box"><div>
I've seen a million tutorials on how to make a slanted edge like in this article for example.
but I cannot seem to find any information on how I would be able to create a div that looks like this...
a double slanted edge that comes to a point.. now I experimented a bit with using an svg image at the bottom of the div but it just wouldnt work properly.. any idea how I can recreate this??
I've seen something similar which required the user of a clip-path, but I need to support IE etc..
any help would be appreciated!
Thanks
I did a feeble attempt to recreate the same div with a double-slanted edge using before and after pseudo elements. You just have to tweak the rotation, top and left values to match your design.
div {
width: 400px;
height: 400px;
border: 1px solid black;
position: relative;
overflow: hidden;
}
div:after{
content: '';
position: absolute;
width: 100%;
background: green;
height:100%;
left: 50%;
top: 50%;
transform: rotate(75deg);
}
div:before{
content: '';
position: absolute;
width: 100%;
background: green;
height:100%;
right: 50%;
top: 50%;
transform: rotate(-75deg);
}
<div></div>
I'm not sure if this is the design that you are wanting.
This is accomplishable with just the border property of a second div, which would help with compatibility of older browsers (if you need to support older versions of IE which don't support pseudo-elements, or even current versions, which don't support use of em units on pseudo-elements)
The CSS
div {
width: 100%;
min-height: 100px;
}
#chevron {
padding-bottom: 100px;
}
#second {
width: 0px;
margin-top: -100px;
border-top: 50px solid transparent;
border-right: 50vw solid blue;
border-left: 50vw solid blue;
border-bottom: 50px solid blue;
}
The HTML
<div id='chevron'></div>
<div id='second'></div>
See this CodePen for a result.
You might use linear-gradients:
body {
background:
linear-gradient(10deg, transparent 45%, #fff 45%) 0 0 / 50vw 50vh,
linear-gradient(-10deg, transparent 45%, #fff 45%) 100% 0 / 50vw 50vh,
linear-gradient(180deg, #cde, #eee 70%) 0 0 / 100vw 100vh;
background-repeat:no-repeat;
}
I want a div that has an "angle like shape on the left". How can I create this with CSS3 only? I am assuming this requires 2 divs? I know I can make a rectangle div and fill it back, and have yellow text. Though I don't know what I can do to make the triange shape on the left. Can it be done with done div only? Or does it need 2? Looking for the best way to do this.
You can achieve this using linear-gradient. Demo:
.text {
width: 400px;
background-image: linear-gradient(45deg, transparent 50px, black 50px);
padding-left: 100px;
color: yellow;
}
<div class="text">
<h1>Some Name Here</h1>
</div>
Why not try something like this:
.triangle {
width: 0;
height: 0;
border: 50px solid black;
border-bottom-color: transparent;
border-left-color: transparent;
float: left;
}
.text {
width: 400px;
height: 100px;
background-color: black;
float: left;
color: yellow;
}
<div class="triangle"></div>
<div class="text"><h1>Some Name Here</h1></div>
See How do CSS triangles work? for more info on this.
You can use of Pseudo Elements ::before or ::after
.triangle {
padding: 10px;
position: relative;
background-color: #000;
color: yellow;
display: inline-block;
margin-left: 40px;
}
.triangle::after {
content: "";
position: absolute;
border: 19px solid #000;
height: 0;
width: 0;
left: -38px;
top: 0;
bottom: 0;
margin: auto;
border-left-color: transparent;
border-bottom-color: transparent;
}
<div class="triangle">
text-here
</div>
Link for reference
Style Accordingly.
You can use clip-path but it has not so good browser support. I'm using 100vmax 100vmax here to achieve 45 degrees clipping. Demo:
.text {
width: 400px;
background-color: black;
-webkit-clip-path: polygon(100vmax 100vmax, 0% 0%, 100% 0%, 100% 100%);
clip-path: polygon(100vmax 100vmax, 0% 0%, 100% 0%, 100% 100%);
padding-left: 100px;
color: yellow;
}
<div class="text">
<h1>Some Name Here</h1>
</div>
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>
This question already has answers here:
turning a div into transparent to see through two containers
(4 answers)
Closed 6 years ago.
Is there any way to have a div with a background-color that takes up 100% width and a transparent box inside it that shows the original background?
Solution 1: Clip-path
Clip path can be quite useful, as it keeps the code clean and simple. However, it does not have great support (yet) in browsers, and should hence only be used in test environments.
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
height: 300px;
width: 100%;
background: tomato;
position: relative;
-webkit-clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0, 50% 0, 50% 20%, 80% 20%, 80% 80%, 20% 80%, 20% 20%, 50% 20%, 50% 0);
}
<div>
</div>
Solution 2: Box shadow Trick
The box shadow trick uses a pseudo element and overflow:hidden; to create the box shadow/colouring of the element.
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
height: 300px;
width: 100%;
overflow:hidden;
position: relative;
}
div:before{
content:"";
position:absolute;
top:20%;width:60%;height:60%;left:20%;
box-shadow:0 0 0 999px tomato;
}
<div></div>
Solution 3: Gradients
You could use multiple gradient background, however this may or may not be suitable as gradients don't always turn out rendered very nicely:
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
position: relative;
height: 300px;
width: 100%;
background: linear-gradient(tomato, tomato), linear-gradient(tomato, tomato), linear-gradient(tomato, tomato), linear-gradient(tomato, tomato);
background-size: 100% 20%, 20% 100%, 100% 20%, 20% 100%;
background-position: left bottom, right bottom, left top, left top;
background-repeat: no-repeat;
}
<div></div>
Solution 4: Borders
Whilst this may or may not be suitable for you, there is still a chance that it may help, so will post here anyway:
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
position: relative;
height: 300px;
width: 100%;
box-sizing: border-box;
border-left: 20vw solid tomato;
border-right: 20vw solid tomato;
border-top: 50px solid tomato;
border-bottom: 50px solid tomato;
}
<div></div>
Solution 5: Background attachment
I have recently come across the background-attachment property, so am still coming to grips with it. However, if you wished the background to appear behind you may be able to alter the below snippet to your needs:
body {
background: url('http://butlers-web.co.uk/Content/Images/BWLOGO.png');
background-attachment: fixed;
}
.wrapper {
width: 100%;
height: 300px;
background: tomato;
position: relative;
}
.inner {
width: 80%;
height: 80%;
background: url('http://butlers-web.co.uk/Content/Images/BWLOGO.png');
background-attachment: fixed;
position: absolute;
top: 10%;
left: 10%;
box-sizing:border-box;
border:2px solid black;
}
<div class="wrapper">
<div class="inner"></div>
</div>
You're going to need two div for that. A parent, with the red background, then the inner div.
give the inner div margin: 10px auto; as a start.