Maintaining the tilt angle and length of diagonal lines in CSS drawing

Creating lines with angles other than vertical or horizontal can be tricky. When trying to draw lines at a certain angle, properly sizing and positioning them can be a challenge. Many methods involve manipulating small boxes with tiny dimensions and rotating them using CSS transforms.

For example, let's say we have a canvas that is a square:

.box{
    position: relative;
    height: 640px;
    width: 640px;
    background: white;
  }

If we try to draw a diagonal line through the center:

.shape1{
     position: absolute;
     top: 50%;
     background-color:black;
     width: 100%;
     height: 1%;
}

(assuming shape1 is inside the box)

Rotating this line by an angle like 45 degrees may cause it to become misaligned. Finding a way to maintain the correct angle while ensuring the line starts and ends where intended (e.g., from point (100,0) to (0,100) in a square with maxX=100 and maxY=100) can be challenging.

While translateX and translateY functions can help adjust position, finding a solution to maintain the length of the line remains a puzzle for many developers.

If you have any insights on how to achieve this, your input would be greatly appreciated.

Thank you!

Answer №1

One option to consider is utilizing the linear-gradient property:

section {
  display: inline-block;
  margin: 1em;
  outline: solid 1px red;
  background: linear-gradient(to top right, #0000 calc(50% - 1px), #000, #0000 calc(50% + 1px))
}

.square {width:100px; height:100px;}

.rect {width:187px; height:103px;}
<section class="square"></section>
<section class="rect"></section>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

monitor margins causing a variety of visual impacts

Check out my website at the following link: Visit Homepage Upon visiting the page, you will notice two boxes - one labeled "Get Started" and the other "Try It Free". Below is the HTML code I used to create them: <div style="margin-top:-100px"> ...

Creating a dynamic layout using multiple columns in CSS with varying inputs

Can someone help me create two responsive columns with different inputs on the same row? The first column should display: The second column should show: How it currently appears: Here's how I want it to look: I believe there might be a mistake in ...

Is there a way to remove a pseudo element in CSS using Internet Explorer

I've been struggling to make some pseudo elements work in Internet Explorer, but it seems like I can't get it to function as intended. It's as if IE is ignoring the CSS rules that I have set up for these elements, and it's quite frustr ...

Web server experiencing issues with loading scripts and CSS files

After successfully building my project using CodeIgniter on localhost, I encountered an issue when trying to run it on a webhost. The site was functional but the design elements such as scripts and stylesheets were not loading properly. Before uploading t ...

Differences between Firefox and Webkit browsers when it comes to scrolling - How side navigation, text truncation, and

Scrollbar appearance is causing an issue across different browsers. The truncated text in my side-navigation component is not displaying correctly on Firefox and IE browsers. View this fiddle on both Chrome and Firefox: https://jsfiddle.net/d84b9m49/10/ ...

Choosing particular contenteditable divisions using jQuery

Consider the following HTML structure for a specific type of blog post editor: <div class="entry"> <div class="title" contenteditable="true"> <h2>Title goes here</h2> </div> <div class="content" contenteditable ...

Styling with CSS will keep your content centered on the page, while the background

I'm looking to create a layout with the following specifications: The content wrapper will have a fixed size and be centered: .content { width:960px; margin:0px auto; } My issue is how to implement the left-aligned background bar for the su ...

Creating a stylish flyout search box using CSS

I am looking to create a search box that pops up when a button is clicked and disappears when clicked outside. My approach involves using CSS to achieve this functionality. Here is the HTML structure I am working with: <div tabindex="0" class="search" ...

The importance of z-index in web design and its compatibility with Chrome

Chrome version 26 seems to be having trouble with z-index, especially when I have two divs. <style> #div1{ position:relative; z-index:1000; } #div2{ position:absolute; z-index:10001; ...

The design is not applied correctly to this table

After neglecting this aspect for a while in favor of focusing on the working part, I have now become more concerned about it and unfortunately, the issue persists. <table cellspacing="0" cellpadding="0" style="border:none"> <tr> ...

Having trouble with the alignment of the product grid on Woocommerce with the Wootique theme?

Hey there, I've been facing an issue with the woocommerce product display grid layout. The items keep getting misaligned, with one on a line and the others right next to each other. I attempted to fix it by adding some custom CSS code: .woocommerce ...

Choosing an option will display a specific div while hiding another using JQuery

My question involves a Select Option <select class="input_select" name="customer_type" id="central_company"> <option value="standard">Standard</option> <option value="central">Central Station</option> </select> ...

Is it possible to adjust the height of a window on mobile using CSS or jQuery?

I'm facing an issue with window height on mobile browsers. I am using css 100vw, but it's not always accurate, especially when the browser displays additional elements like a navigation bar at the top or functional buttons at the bottom. In simp ...

Having difficulty resolving issues with the chat box (div) scroll bar staying fixed at the bottom

I am currently working on a chat application and I am facing an issue with fixing the scroll bar at the bottom of a div when loading my PHP file. I have already written a script to achieve this, but despite accessing it using CSS Id, I am having trouble ge ...

Minimize the gap between col-lg elements in responsive mode within Bootstrap

My website is built using Bootstrap 4 and the following is the HTML code: <div class="container"> <div class="row text-center "> <div class="col-lg-6">A</div> <div class="col-lg-6">B</div> </div> < ...

React Traffic Light Component: Colors Stuck After Timeout

I've been working on solving a React issue and followed a tutorial on YouTube. I'm using CodeSandbox for my project, but I'm facing a problem where the colors of the signal are not showing up and do not change after some time. I even tried u ...

Creating a mask overlay that covers the entire screen while excluding a specific sibling div

My goal is to create a div that acts as a mask covering the entire window. It should have an opacity of 0.5 and a background color of lightgray. However, there is one particular div on the screen that I do not want to be affected by the mask. I attempted t ...

I'm wondering why my positive numbers aren't displayed in green and negative numbers in red

I am currently working on a commodities quotes widget. I have already set up the 'Current' and '24-hour' divs, but I'm facing an issue where positive values are not displaying in green and negatives in red as intended. I have check ...

Fresh class retains the characteristics of the former class

As I navigate my way through learning jQuery, I've encountered a puzzling issue that I can't seem to solve. No existing articles seem to address my specific problem, so I am turning to this platform in hopes of finding a solution. I'm puzz ...

Participant joins in the game table and reacts

I am looking to set up a table where players can join and have their usernames displayed at the bottom of the screen just like in other games. How can I achieve this? export const GameStart = (props) => { const params = useParams(); const gameID = p ...