I'm wanting to have a diagonal line drawn from the upper-right corner of a <div> or <span> to the lower-left corner. The problem is that the content will sometimes be longer or shorter. So, something like a static image won't work. Basically I want what's described here (How to create a diagonal line within a table cell?) but for a <div> or a <span>.
This has some interesting ideas: http://jsfiddle.net/zw3Ve/11/
So does this: http://css-tricks.com/forums/discussion/13384/diagonal-line/p1
This is kind of a retry at this post: How to strike through obliquely with css
I can't seem to figure any of this out though. It seems like a pure CSS solution should work here, but I just don't have the skills to make that happen. jQuery is an option for me as well.
You can do this with an inline SVG background, using only CSS and no javascript.
.background {
// Draw an SVG top left to bottom right
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' preserveAspectRatio='none' viewBox='0 0 10 10'> <path d='M0 0 L0 10 L10 10' fill='red' /></svg>");
background-repeat:no-repeat;
background-position:center center;
//scale it
background-size: 100% 100%, auto;
}
See my fiddle:
http://jsfiddle.net/3GWw2/
Is first fiddle as example with image in background instead not good enough?
http://jsfiddle.net/zw3Ve/410/
.line {
display:inline-block;
border: 1px solid #ccc;
margin: 10px;
padding: 10px;
background:url(http://i.piccy.info/i7/c7a432fe0beb98a3a66f5b423b430423/1-5-1789/1066503/lol.png);
background-size:100% 100%;
}
You can do this with linear-gradient. For example if I want a green and white square that cuts diagonally from bottom left to top right, I give it this background attribute:
background: linear-gradient(to bottom right, white, white 50%, green 50%, green);
This means that it starts as white at the top left corner and continues as white all the way to the diagonal line. The transition is immediately from white to green with no actual gradient as both are set at 50%. If you want a gray line between, you could try this:
background: linear-gradient(to bottom right, white, white 48%, gray 48%, gray 52%, green 52%, green);
You might use an SVG image like this one:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
<svg version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="200px" height="50px" viewBox="0 0 200 50">
<line fill="none" stroke="#000" x1="0" y1="0" x2="200" y2="50"/>
</svg>
Set it as background of your span or div
.class-of-div-or-span { background: url("line.svg") no-repeat scroll 0 0 / 100% 100% transparent; }
Note: you have to give your span display:block or display:inline-block in order to work.
You could also inline the svg, use it in an object tag or embed it in your css.
Actually this is more of a question about geometry than coding. With squares this is easy, but with rectangles you'll have to do the math yourself. Remember those kids complaining that they'll never have to calculate a diagonal's length in "real life"? :P
Here's what I did:
div.container /*makes a square container (300x300)*/
{
width: 300px;
height: 150px;
background-color: #aaa;
padding-top: 150px;
}
div.line
{
position: relative;
z-index: 1;
left: -61px; /*this is something I don't understand but apparently is required*/
width: 423px; /*since container is a square this equals to its diagonal: 300*1.41*/
height: 1px;
background-color: #000;
transform: rotate(45deg); /*again, this is easy since its a square. In rectangle you'll have to find a tangent*/
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
HTML:
<div class="container">
<div class="line"></div>
</div>
and a jsfiddle: http://jsfiddle.net/LWAKn/
Related
I'm trying to just have a word or phrase be repeated at a 45 degree angle downwards at a very low transparency, and have it cut off by the edges of the element. Just wondering if this is possible without just making an image in another program.
Something like this:
It'd be appreciated if anyone could help me out with this.
You can use SVG to create the rotated text then apply it as background. You can easily control the size by adjusting the background-size value
html {
min-height:100%;
background:
url('data:image/svg+xml;utf8,<svg style="transform:rotate(45deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 60"><text x="0" y="25" fill="%23000">Lorem </text></svg>')
0 0/50px 50px; /* update the 50px 50px to control the size */
}
Consider pseudo element to have it above all the content:
body:before {
content:"";
position:absolute;
inset:0;
opacity:0.8;
background:
url('data:image/svg+xml;utf8,<svg style="transform:rotate(45deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 60"><text x="0" y="25" fill="%23000">Epuuc </text></svg>')
0 0/60px 60px;
}
body {
position:relative;
background:purple;
min-height:200vh;
}
I'm trying to figure out how to create a custom background effect for text.
In other words, how can I make something like this:
Use the <mark> element and tweek the line-height
mark {
display: inline-block;
line-height: 0em;
padding-bottom: 0.5em;
}
<h1><mark>Lorem ipsum</mark></h1>
A very good article explains a nice way to do that with gradients: https://thirtyeightvisuals.com/blog/low-highlight-heading-links-squarespace
.highlight {
background: linear-gradient(180deg,rgba(255,255,255,0) 50%, #FFD0AE 50%);
}
For these I usually use an SVG pixel (a 1x1 stretchable HTML-encoded SVG with a fill color) that we can manoeuvre anyway we want:
h1 {
background: url("data:image/svg+xml;charset=utf8,%3Csvg version='1.1' xmlns='http://www.w3.org/2000/svg' x='0px' y='0px' width='1px' height='1px' viewBox='0 0 1 1' preserveAspectRatio='none'%3E%3Crect x='0' y='0' width='1' height='1' fill='red' /%3E%3C/svg%3E") no-repeat 100% 100%;
background-size: 100% 50%;
}
<h1>My Text</h1>
This also allows for animations to be easily added. This only works on single-line items, however. You can change the color inside the svg fill property. If encoded it works on IE9+, so it's pretty compatible! Just remember that the hash sign in front of hex colors needs to be encoded as well - its %23 (personally I use sass to encode it for me).
You can easily achieve this using a linear-gradient set to the background property.
CSS
.low-highlight {
background:linear-gradient(180deg, transparent 60%, yellow 60%);
}
HTML
<p>You can easily <span class="low-highlight">do underline</span> me.</p>
p {
font-size:68px;
font-weight:600;
font-family:'Merriweather';
}
.low-highlight {
background:linear-gradient(180deg, transparent 60%, yellow 60%);
}
<p>You can easily <span class="low-highlight">do underline</span> me.</p>
Try resizing the line size. Line-height. And then highlight the text.
I want to create a perspective crop like effect with CSS3 like this:
I did try it doing my self but couldn't, any help would be appreciated.
here's my attempt: http://jsfiddle.net/krish7878/y9eusob9/
HTML Code:
<div class="container">
<img src="http://img.netcarshow.com/Lotus-Evora_GX_Racecar_2013_1600x1200_wallpaper_01.jpg" alt="main image" />
</div><!-- /.container -->
CSS Code:
.container{
overflow: hidden;
width: 300px;
height: 200px;
margin-top: 30px;
margin-left: 30px;
border-top-right-radius: 20px;
}
One option could be to add a container <div>, e.g..perspective, that gets the yellow background color and to use a polygon clip-path on the image.
A good tool to create these polygons is Bennett Feely's clippy. Browser support for clip-path isn't quite there yet though. See Clipping and Masking in CSS for more background information and options.
.perspective {
display: inline-block;
background-color: #ff0;
}
.perspective__image {
display: block;
-webkit-clip-path: polygon(10% 20%, 90% 10%, 90% 90%, 10% 80%);
clip-path: polygon(10% 20%, 90% 10%, 90% 90%, 10% 80%);
}
<div class="perspective">
<img src="http://www.nicenicejpg.com/400/300" class="perspective__image"/>
</div>
You might be able to accomplish this effect using a layering technique like what is found here:
http://cssglobe.com/angled-content-mask-with-css/
Basically, the author used a few nested elements that were rotated back and forth with overflow: hidden on the container elements.
For maximum browser support you could use svg's clipPath.
<svg width="400" height="400">
<defs>
<clipPath id="shape">
<path d="M50,50 L400,0 L400,400 L50,350" />
</clipPath>
</defs>
<foreignObject clip-path="url(#shape)" width="400" height="400">
<img src="http://www.lorempixel.com/400/400" />
</foreignObject>
</svg>
I found a great tutorial on css-tricks that talks about doing exactly this:
http://css-tricks.com/almanac/properties/p/perspective/
You'll be making use of the transform property to make the rotations you need to simulate the perspective change.
Hope this helps!
I am trying to create gradient text with plain HTML and CSS. Something like the text below
Check the FIDDLE. It is self explanatory.
I know how to achieve this in webkit-browsers only. But i need a Cross-browser solution which has backward compatibity till IE8 atleast.
I know how to generate the gradient. That is not an issue. In the fiddle i have only created gradient for webkit browsers but i know how to do it for IE too. My main issue is how can i make the text transparent so it shows me the gradient of the underlying div.
No JS/jQuery solutions please.
CODE
HTML
<div id="div1" style="width:200px;height:200px"></div>
<div id="div2" style="width:200px;height:200px">CAN YOU SEE THIS? THIS TEXT IS SUPPOSED TO HAVE COLORED GRADIENTS LIKE THE HELLO WORLD TEXT</div>
CSS
#div1 {
/* Chrome,Safari4+ */
background: -webkit-linear-gradient(45deg, rgba(252, 234, 187, 1) 0%, rgba(252, 205, 77, 1) 50%, rgba(248, 181, 0, 1) 51%, rgba(251, 223, 147, 1) 100%);
}
#div1 {
z-index:-100;
position:absolute;
left:0px;
top:0px;
}
#div2 {
z-index:100;
left:10px;
top:10px;
background: black;
text-align:center;
font-size:20px;
color: rgba(255, 255, 255, 0.5);
position:absolute;
}
EDIT: I believe my question is not clear . I Know about gradients. I want my text to be transparent so that the gradient of the div below can be shown on the transparent text.
Something like this example
You could use SVG, its a little outside the box, but its cross browser compatible and gives you some more options.
Working Example
<svg height="50">
<linearGradient id="g1" x="0%" y="100%">
<stop stop-color="blue" offset="0%" />
<stop stop-color="green" offset="25%" />
<stop stop-color="yellow" offset="50%" />
<stop stop-color="orange" offset="75%" />
<stop stop-color="red" offset="100%" />
</linearGradient>
<text font-size="40" fill="url(#g1)">
<tspan x="10" y="40">Hello World!</tspan>
</text>
</svg>
Tested and working in Chrome, Firefox, and IE9
If you've really got your heart set on a cutout effect, it can also be done with SVG.
Working Example 2
<div class="wrap">
<div class="black">
<svg width="300" height="100">
<mask id="cutouttext">
<rect width="280" height="50" x="0" y="15" fill="white" />
<text x="50%" y="55%" text-anchor="middle" font-size="48">Hello World</text>
</mask>
<rect width="280" height="50" x="25" y="15" fill="black" mask="url(#cutouttext)" />
</svg>
</div>
</div>
Fall back for IE8 and below:
<!--[if lt IE 9]>
<style>
.wrap {
color: #ff0000;
font-size:48px;
text-align: center;
padding-top: 10px;
height: 90px;
}
.black {
background: black;
margin: 0 auto;
width:250px;
}
</style>
<![endif]-->
The way it looks in modern browsers:
and how it looks in IE8 and below:
Documentation:
MDN SVG Gradients
MDN SVG Text
MDN Mask
I think you are looking for background-clip.
The catch is that you need to use an image, you can't use a css gradient afaik
update:
it's only supported on webkit though..
Following trick will only work if your text/content is on solid background.
I'm summarizing points from this blog post
Put an empty span with in the text container, for eg.
<h1> <span> </span> This this heading
</h1> etc.
Embed background-image [png] through css to this
span
And position it absolutely, upon the text.
It works in IE6 onwards, but IE PNG hack is required!
Hope it helps! :)
There are 2 thing to be covered in IE
For IE10+:
background: -ms-linear-gradient(top, #1e5799 0%,#207cca 27%,#2989d8 50%,#207cca 79%,#7db9e8 100%); /* IE10+ */
background: linear-gradient(to bottom, #1e5799 0%,#207cca 27%,#2989d8 50%,#207cca 79%,#7db9e8 100%); /* W3C */
For IE6-IE9:
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-9 */
I think it will be easy for you to replace the color codes. I would also suggest you to take a look at ColorZilla for generating the CSS Gradient.
i dont think what you are saying is possible...because you have a parent div whose background is gradient and then you have an inner div whose background is not transparent but black (#000000) nou you have written text on inner div but want font's background as gradient which is in parent div...this is not posiible using css/css3 ...
now i can show you 2 ways (>=IE 9) by following which you can achieve same style with little alteration in your mark-up and css
OPTION 1::(need some photoshop work)
have a <div> set that <div>'s background black.then create gradient photoshop work(ex .png).now set that image as a font/text background using background-clip: text;
webkit-background-clip: text;
-moz-background-clip: text;
background-clip: text;
OPTION 2:: (above process is little time consuming ..there is another smarter way)
first have div with black background then write the text in inner div...set inner div's background as gradient then use -webkit-background-clip: text;-webkit-text-fill-color: transparent;
for example ::
MARK-UP
<div class="black">
<div class="a">
CAN YOU SEE THIS? THIS TEXT IS SUPPOSED TO TRANPARENT TO SHOW COLOR OF UNDERLYING DIV SO THAT IT LOOKS LIKE THE "HELLO WORLD" TEXT
</div>
</div>
CSS
.black{
background:black;
overflow:hidden;
}
.a{
background: #000000 -webkit-linear-gradient(red, green);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
LIVE EXAMPLE
ALSO CHECK BROWSER COMPATIBILITY OF background-clip AT caniuse.com
OPTION 3::(to make this style for IE8) (this process is simple,straight but not smart because it is IE specific)
create a photoshop work..make a text with gradient effect..now declare a div with black background..and include that image with <img src="" /> tag . :)
Try this :
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#000000'); /* For < IE 7 */
-ms-filter : progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#000000'); /* For >= IE 8 */
When you go to the page http://m.google.com using Mobile Safari, you will see the beautiful bar on the top of the page.
I wanna draw some trapeziums (US: trapezoids) like that, but I don't know how. Should I use css3 3d transform? If you have a good method to achieve it please tell me.
As this is quite old now, I feel it could use with some new updated answers with some new technologies.
CSS Transform Perspective
.trapezoid {
width: 200px;
height: 200px;
background: red;
transform: perspective(10px) rotateX(1deg);
margin: 50px;
}
<div class="trapezoid"></div>
SVG
<svg viewBox="0 0 20 20" width="20%">
<path d="M3,0 L17,0 L20,20 L0,20z" fill="red" />
</svg>
Canvas
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(30, 0);
ctx.lineTo(170, 0);
ctx.lineTo(200, 200);
ctx.lineTo(0, 200);
ctx.fillStyle = "#FF0000";
ctx.fill();
<canvas id="myCanvas" width="200" height="200"></canvas>
You can use some CSS like this:
#trapezoid {
border-bottom: 100px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
height: 0;
width: 100px;
}
<div id="trapezoid"></div>
It is really cool to make all this shapes, Take a look to more nice shapes at:
http://css-tricks.com/examples/ShapesOfCSS/
EDIT:
This css is applied to a DIV element
Simple way
To draw any shape, you can use the CSS clip-path property like below.
You can use free online editors to generate this code (ex: https://bennettfeely.com/clippy/)
.trapezoid {
clip-path: polygon(0 0, 100% 0, 84% 41%, 16% 41%);
}
With reusable code
If you want it more adaptative, you can define a Sass mixin like :
#mixin trapezoid ($top-width, $bottom-width, $height) {
$width: max($top-width, $bottom-width);
$half-width-diff: abs($top-width - $bottom-width) / 2;
$top-left-x: 0;
$top-right-x: 0;
$bottom-left-x: 0;
$bottom-right-x: 0;
#if ($top-width > $bottom-width) {
$top-left-x: 0;
$top-right-x: $top-width;
$bottom-left-x: $half-width-diff;
$bottom-right-x: $top-width - $half-width-diff;
} #else {
$top-left-x: $half-width-diff;
$top-right-x: $bottom-width - $half-width-diff;
$bottom-left-x: 0;
$bottom-right-x: $bottom-width;
}
clip-path: polygon($top-left-x 0, $top-right-x 0, $bottom-right-x $height, $bottom-left-x $height);
width: $width;
height: $height;
}
And then use it for the desired element like this (here parameters are $top-width, $bottom-width, $height) :
.my-div {
#include trapezoid(8rem, 6rem, 2rem);
}
This is an old question... but I want to add a method that has not been mentioned. It is possible to draw triangles with gradients of half color half transparent, and then it is possible to build a trapezoid from 3 gradient shapes. Here is an example code, the 3 blocks drawed in different colors for better understanding:
#example {
width: 250px;
height: 100px;
background-image:
linear-gradient(to top left, red 0 50%, transparent 50% 100%),
linear-gradient(to top right, green 0 50%, transparent 50% 100%),
linear-gradient(blue 0 100%);
background-size:
20% 100%, 20% 100%, 60% 100%;
background-position:
left top, right top, center top;
background-repeat: no-repeat;
}
<div id="example"></div>
You have a few options available to you. You can just plain use an image, draw something with svg or distort a regular div with css transforms. An image would be easiest, and would work across all browsers. Drawing in svg is a bit more complex and is not guaranteed to work across the board.
Using css transforms on the other hand would mean you'd have to have your shape divs in the background, then layer the actual text over them in another element to that the text isn't skewed as well. Again, browser support isn't guaranteed.