Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw diagonal lines with css [duplicate]

Tags:

html

css

I need to draw in my div diagonal line. It should look like this:

http://i2.minus.com/jLWAKktNdYTdt.PNG

My HTML:

<div style="height: 28px; width: 28px; border: 1px solid rgb(219,225,230);background-color:white;" >
</div>    

Is it possible to do it only with CSS?

like image 352
Server Khalilov Avatar asked Jul 18 '14 07:07

Server Khalilov


People also ask

How do you make a diagonal line in CSS?

From a CSS point of view, a diagonal line is nothing but a horizontal line which is rotated at +45 or -45 degrees angle. So, to draw a diagonal line in CSS, we have to simply rotate a normal horizontal line at an angle of + or – 45 degrees. The rotation in CSS is done using the transform property.

How do I make an oblique line in CSS?

In order to make oblique lines with CSS, there are two main approaches. The first approach involves the clip-path property of CSS and in the second approach, we use transform property with skew() of CSS. Approach 1: Using clip-path property: The clip-path property is generally used to clip an element to a basic shape.

How do you add a diagonal line in HTML?

width: 100px; The fun thing though, is that you can turn it into a diagonal just by changing the degree of rotation! To move the line around the page you can add the parameters translateX and translateY to the transform property. This will move the line along the X and Y axis respectively.

How do you draw a diagonal line in coding?

fill(60, abs((x - b)/2 ) + abs((y - a)/2), 100); and this: y += 25; to get a diagonal movement.


2 Answers

You can achieve the desired effect by using just one single div. Check the DEMO.

div{
  border:1px solid gray;
  width:28px;
  height:28px;
  position:relative;
}

div:after{
  content:"";
  position:absolute;
  border-top:1px solid red;
  width:40px;
  transform: rotate(45deg);
  transform-origin: 0% 0%;
}

Note: please add the vendor prefix for older browsers i.e. -moz, -webkit.

like image 133
Kheema Pandey Avatar answered Oct 04 '22 15:10

Kheema Pandey


Using CSS transform property you can achieve this. Look at the following HTML and CSS.

HTML

 <div style="border: 1px solid #000; width:100px; height:100px;">
   <div id="hr" style="border-top:1px solid #ff00ff; height:100px; margin-left:-140px;"></div>
 </div>

CSS

 #hr {
 -moz-transform: rotate(45deg);  
   -o-transform: rotate(45deg);  
 -webkit-transform: rotate(45deg);  
  -ms-transform: rotate(45deg);  
      transform: rotate(45deg); 
   }

DEMO

like image 34
Suresh Ponnukalai Avatar answered Oct 04 '22 16:10

Suresh Ponnukalai