Can a CSS background gradient be created without specifying exact start and end positions?

Can CSS linear gradients have unanchored, independent start and end positions like in SVG gradients?

Below is an example of CSS linear gradient code:

#rectangle {
  width: 100%;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
  background: linear-gradient(225deg, rgba(255,255,255,1) 0%, rgba(250,0,0,1) 27.59%, rgba(108,22,95,1) 76.35%, rgba(39,32,32,1) 100%)
}
<div id="rectangle">

</div>

Here is the expected output in a square:

https://i.sstatic.net/6siE7.png

Expected output in a rectangle

https://i.sstatic.net/vV9wd.png

I've been looking at this page on MDN and this page on W3C.

The orientation of the gradient in SVG is defined by x1, x2, y1, and y2 attributes.

The element also takes several other attributes, which specify the size and appearance of the gradient. The orientation of the gradient is controlled by two points, designated by the attributes x1, x2, y1, and y2. These attributes define a line along which the gradient travels. The gradient defaults to a horizontal orientation, but it can be rotated by changing these. Gradient2 in the above example is designed to create a vertical gradient. - from https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Gradients

According to other documentation:

X and Y position of the start of the gradient line, as a multiple of the object's bounding box: X=0 indicates the left edge of the bounding box and X=1 indicates the right edge. The gradient line may start or end outside the object's bounding box, so values may be < 0 or > 1.

There might be issues related to pre-transform/post-transform when dealing with the gradient lines.

In my project, I retrieve the width and height of the square/rectangle, the start and end points (gradient lines), the color stop colors, and the color stop ratios. These gradient lines vary each time.

Answer №1

A great technique to try is using the calc() function, which allows you to mix pixel and percentage values in your CSS code. By leveraging this combination, you can create dynamic gradient lengths by defining the reference point with a percentage value and adjusting the length with pixels for each color stop:

.rectangle {
  width: 200px;
  height: 100px;
  display:inline-block;
  border:1px solid;
  
  background: linear-gradient(225deg, 
    rgba(255,255,255,1) calc(50% - 70px*(1 - 0)), 
    rgba(250,0,0,1)     calc(50% - 70px*(1 - 0.2759)), 
    rgba(108,22,95,1)   calc(50% - 70px*(1 - 0.7635)), 
    rgba(39,32,32,1)    calc(50% - 70px*(1 - 1)))
}
<div class="rectangle">

</div>

<div class="rectangle" style="width:100px;">

</div>

<div class="rectangle" style="width:300px;">

</div>

To further customize your gradients, you can also adjust the starting point using the same approach:

.rectangle {
  width: 200px;
  height: 100px;
  display:inline-block;
  border:1px solid;
  
  background: linear-gradient(225deg, 
    rgba(255,255,255,1) calc(50% + 70px*0), 
    rgba(250,0,0,1)     calc(50% + 70px*0.2759), 
    rgba(108,22,95,1)   calc(50% + 70px*0.7635), 
    rgba(39,32,32,1)    calc(50% + 70px*1))
}
<div class="rectangle">

</div>

<div class="rectangle" style="width:100px;">

</div>

<div class="rectangle" style="width:300px;">

</div>

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

ul/div element not displaying background color

I am attempting to showcase navigation items horizontally within a blue colored ribbon. Despite my efforts, the background-color property is not being applied to the ul element. I even tried enclosing it in a div element with a blue background, but still n ...

Tips for creating a Carousel with more than three images using Bootstrap

Recently, I attempted to enhance my Carousel in Bootstrap by adding more images. Initially, I inserted the code snippet below within the ordered list with the class "carousel-indicators." <li data-target="#carouselExampleCaptions" data-slide-to=" ...

Experiencing a problem with inline JavaScript onclick

I recently came across a fantastic pure javascript code for a smooth scrolling function, but I'm struggling with integrating it into an inline onclick function. My understanding of javascript is quite limited and I could really use some help... <d ...

Preventing Mobile Video Downloads: Tips and Tricks

I am facing a challenge with my website that contains several case studies featuring video headers set to autoplay. These videos are placed within a parent div element, which is hidden on mobile and tablet devices. Since the videos will not be visible on ...

Unable to see or play local mp4 file in Ionic iOS application

I am facing an issue with my Ionic app where I am trying to show a video playing in the background. The Jaeger25 HTMLVideo plugin works perfectly for Android, but when it comes to iOS, the video simply refuses to play. The video file is located in the www/ ...

Issues with pop-up windows on YII2 gridview

I am currently developing a web application using the Yii2 framework for my company to efficiently manage their storage. I have incorporated various plugins from kartik-v to enhance the functionality of the application. However, I am facing an issue with i ...

Optimizing background images for various mobile devices using PhoneGap

Currently in the process of creating a cross-platform app for iOS and Android using Phonegap. Facing an issue with implementing a background-image as it gets cropped or distorted on various mobile devices due to size differences. Managed to address this ...

Guide to dynamically insert rows in HTML using Vue.js based on the selected option

This is the initial row. Depending on the selection made here, different rows will be displayed <div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Case Type< ...

Loading dynamic fonts in React MaterialUI

In our web application, each user experiences a unique style with colors, fonts, border widths, and more based on the Material UI JSON theme retrieved from the database upon loading the app. The challenge lies in handling dynamic fonts. What approaches ha ...

What is the most effective way to utilize bootstrap classes and their associated functions exclusively for mobile devices by using media queries?

I am encountering an issue with a table in my application that causes the UI to break when the screen width is reduced to less than 600px. To address this, I would like to implement the following bootstrap classes to add a scroll to the application: .tabl ...

Incorporate a glyphicon into a webpage design that wasn't originally built with bootstrap while maintaining the original layout

Looking to jazz up my vintage website with some cool glyphicons on a specific page. Don't want to mess up the entire look of my site by installing bootstrap. Is there a way to add just the "glyphicons functionality" without changing everything else? ...

I encountered an issue while using mediaRecorder to save a JavaScript animation; the blob value returned as null

My current project involves using Vanta js to create an animated background and mediaRecorder to capture the canvas as a webm file. The recording process is supposed to start from the beginning and stop after 1 second, exporting the webm file. However, I ...

Discovering a particular string within a jQuery array object: Tips and tricks

One thing that is confusing me is the jQuery array object. To explain further: I have two arrays, one called brandsLink and the other called floorLink. When a user clicks on a link, I am saving the brand name in a variable called brandName, and then checki ...

Customize your Outlook emails with HTML and CSS to right-align content for a polished look

My current project involves generating a report to be sent via email, with Outlook being the mandatory application in our system. The report's content is HTML-based and utilizes a table layout. Part of the content needs to appear right-aligned within ...

Safari causing margin problems

Encountering a margin problem specifically with Safari that I'm having trouble debugging. While everything functions properly in Firefox, Chrome, IE, etc., Safari presents issues. My content div is positioned just below the header, but in Safari, it o ...

What is the best way to make JavaScript display the strings in an array as bullet points on different lines?

We were assigned a task in our computer science class to analyze and extend a piece of code provided to us. Here is the given code snippet: <!DOCTYPE html> <html> <head> <title>Example Website</title> </head> <body& ...

Obtain saved browsing history in HTML5 with the use of JavaScript

I'm currently utilizing the History.js library found at https://github.com/browserstate/History.js/ to create an AJAX-based application that leverages the HTML5 History API for dynamically changing the URL and title of the browser. Does anyone have s ...

Add content to the beginning of the input with the assistance of bootstrap. However, ensure that when utilizing "form-group row", the content is shifted

I am experiencing an issue with my Bootstrap 4 form. I am using the 'form-group row' class to display labels and inputs on the same line, but when I try to prepend a $ sign before an input, it shifts the input to the next line. Below is the code ...

Tips for maintaining a stationary background during scrolling movements

Check out the following CSS code: #bigwrapper{ background-image: url('http://www.w8themes.com/wp-content/uploads/2013/09/Water-Backgrounds.jpg'); background-repeat: no-repeat; background-position: fixed; width: 100%; } Here is t ...

Tips for adjusting the size of iframe content to fit its div area in Bootstrap 4 when the window is minimized

I recently started building a website and encountered an issue with resizing the window to fit an iframe containing a video within a div. I'm unsure where exactly in the CSS code I need to make adjustments for this. Can someone help me out? The websi ...