CSS - Creating a Gradient Effect on div Borders for a Transparent Fade Effect across a Specified Distance

I am trying to achieve a fading effect for the background color of a div that contains a form. The solid background should fade out to transparent based on a pixel variable, allowing the background behind it to show through. This pixel value determines how much of the colored background fades out towards the edges of the div.

Here is the current appearance of the div:

https://i.stack.imgur.com/3Qo5Q.png

This is the desired look I am aiming for:

https://i.stack.imgur.com/0eJRP.png

Since the div's size is variable (height: auto;), using an image as the background is not possible in this case.

I attempted to use linear gradients in this Fiddle to achieve the effect but my lack of experience with them may have led me to cancel out some settings.

HTML:

<div class="formBackground">
  <form id="gform" method="POST" action="***">
    <input type="text" id="name" name="name" placeholder="Name" style="width: 100%; float: left;">
    <input type="text" id="email" name="email" placeholder="Email" style="width: 100%; float: left;">
    <input type="textarea" id="message" name="message" placeholder="Write your message here..." style="width: 100%; float: left;">
  </form>
</div>

CSS:

.formBackground {
    top: 0px;
    float: left;
    margin-left: 50%;
    transform: translateX(-50%);
    width: 50%;
    height: auto;
  /* Adjust gradient values to control fading */
  background-image : linear-gradient(to bottom, 
                    rgba(55,54,51, 0), 
                    rgba(55,54,51, 1) 90%);
  padding-left: 50px;
    padding-top: 50px;
    padding-right: 50px;
    padding-bottom: 50px;
    margin-top: 100px;
    overflow: hidden;
    margin-bottom: 4.5em;
}

Any help or suggestions would be greatly appreciated. Thank you!

Answer №1

In the past, I have utilized box shadows to achieve a similar effect.

  box-shadow: 0px 0px 25px 25px rgba(55,54,51, 1);

To create the desired 50px blur, you can add a box shadow with a translation of 0px in any direction, a spread distance of 25px, and a blur radius of 25px. Adjusting the margin to 50px and utilizing top, right, or other positioning properties allows for precise placement.

Here is an example of the HTML structure:

<div class="formBackground">
  <form id="gform" method="POST" action="***">
    <input type="text" id="name" name="name" placeholder="Name" style="width: 100%; float: left;">
    <input type="text" id="email" name="email" placeholder="Email" style="width: 100%; float: left;">
    <input type="textarea" id="message" name="message" placeholder="Write your message here..." style="width: 100%; float: left;">
  </form>
</div>

And the accompanying CSS styling:

.formBackground {
    height: auto;
    background-color : rgba(55,54,51, 1);
    overflow: hidden;
    margin: 50px;
    box-shadow: 0px 0px 25px 25px rgba(55,54,51, 1);
}

Answer №2

To create a visually appealing background, it's recommended to combine all the gradient elements within the same background. Utilizing radial gradients for the corners can also enhance the overall look.

Check out this example:

.box {
  width: 200px;
  height: 100px;
  background:
  /*center*/
  linear-gradient(rgba(55, 54, 51, 1),rgba(55, 54, 51, 1)) center/calc(100% - 40px) calc(100% - 40px) no-repeat,
  /*4 corners*/
  radial-gradient(circle at bottom left, rgba(55, 54, 51, 1) , rgba(55, 54, 51, 0) 70%) top right /20px 20px no-repeat,
  radial-gradient(circle at bottom right, rgba(55, 54, 51, 1) , rgba(55, 54, 51, 0) 70%) top left /20px 20px no-repeat,
  radial-gradient(circle at top right, rgba(55, 54, 51, 1) , rgba(55, 54, 51, 0) 70%) bottom left /20px 20px no-repeat,
  radial-gradient(circle at top left, rgba(55, 54, 51, 1) , rgba(55, 54, 51, 0) 70%) bottom right /20px 20px no-repeat,
  /*4 sides*/
  linear-gradient(to left, rgba(55, 54, 51, 0), rgba(55, 54, 51, 1)) right center/20px calc(100% - 40px) no-repeat,
  linear-gradient(to right, rgba(55, 54, 51, 0), rgba(55, 54, 51, 1)) left center/20px calc(100% - 40px) no-repeat,
  linear-gradient(to bottom, rgba(55, 54, 51, 0), rgba(55, 54, 51, 1)) top center/calc(100% - 40px)  20px no-repeat,
  linear-gradient(to top, rgba(55, 54, 51, 0), rgba(55, 54, 51, 1)) bottom center/calc(100% - 40px)  20px no-repeat;
}
<div class="box">
</div>

<div class="box" style="width:500px;">
</div>

<div class="box" style="height:200px;">
</div>

Here is an alternative syntax using CSS variables for easier customization of colors and distances:

.box {
  width: 200px;
  height: 100px;
  box-sizing:border-box;
  color:#fff;
  --i:55, 54, 51;
  --c:rgba(var(--i),1) , rgba(var(--i), 0);
  --d:20px;
  padding:var(--d);
  background-image:
  linear-gradient(rgba(var(--i), 1),rgba(var(--i), 1)),
  
  radial-gradient(circle at bottom left, var(--c)  70%),
  radial-gradient(circle at bottom right, var(--c) 70%),
  radial-gradient(circle at top right, var(--c) 70%),
  radial-gradient(circle at top left, var(--c) 70%),
  
  linear-gradient(to right, var(--c)),
  linear-gradient(to left, var(--c)),
  linear-gradient(to top, var(--c)),
  linear-gradient(to bottom, var(--c));
  
  background-size:
   calc(100% - 2*var(--d)) calc(100% - 2*var(--d)),
   
   var(--d) var(--d),var(--d) var(--d),var(--d) var(--d),var(--d) var(--d),
   
   var(--d) calc(100% - 2*var(--d)),var(--d) calc(100% - 2*var(--d)),calc(100% - 2*var(--d)) var(--d),calc(100% - 2*var(--d)) var(--d);
  background-position:center,
  
    top right,top left,bottom left,bottom right,
    
    right center, left center,top center, bottom center;
  background-repeat:no-repeat;
}
<div class="box">
Some content
</div>

<div class="box" style="--d:40px;--i:0,20,70">
Some content
</div>


<div class="box" style="--d:10px;--i:255,0,0">
Some content
</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

What is the best way to make an exit pop up disappear when clicking on any area?

I'm new to programming in JavaScript and jQuery and I wanted to implement an exit pop-up feature. While I came across a helpful tutorial online, I encountered an issue where users could only exit by clicking on the "X" button. I want them to be able t ...

What is the best way to set up an anchor element to execute a JavaScript function when clicked on the left, but open a new page when clicked in

One feature I've come across on certain websites, like the Jira site, is quite interesting. For instance, if we take a look at the timeline page with the following URL - When you click on the name of an issue (which is an anchor element), it triggers ...

Having trouble understanding the differences between Bootstrap 4's .list-inline class and .list-inline-item class?

I am currently utilizing Bootstrap 4 within Wordpress. Strangely, I am facing an issue where I am unable to have list items appear inline (horizontally) solely by using the class .list-inline on the list itself, as shown below: <ul id="dances" class="l ...

Placing a div with position:absolute inside another div with position:absolute and turning them into position:fixed

HTML <div class="box1"></div> <div class="box2"> <div class="box3"></div> </div> CSS Properties of box1, box2 and box3 are: box1 { position: absolute; z-index: 100; background-color: rgba(39, 39, 39, 0.3) ...

Ways to identify if a resize event was caused by the soft keyboard in a mobile browser

Many have debated the soft keyboard, but I am still searching for a suitable solution to my issue. I currently have a resize function like: $(window).resize(function() { ///do stuff }); My goal is to execute the 'stuff' in that function on ...

Django is refusing to display my CSS and JavaScript files - static files

Within my code in settings.py, I define the static URL like so: STATIC_ROOT = os.path.join(BASE_DIR, "static") STATIC_URL = '/static/' The location of my static files and folders is at /home/myLinuxUsername/myApp/static/interactive-preview-depe ...

What is the best way to eliminate the blank space between div elements on a webpage?

I'm currently working on enhancing my portfolio website, and I'm struggling to eliminate the excessive white space. After inspecting the element, it appears that there is styling related to ul.AMSI, but it's not referenced anywhere in my st ...

Hover effect that smoothly transitions to a background image appearing

I want to achieve a smooth fadeIn effect for the background image of a div. Currently, I have a function that displays the background image when hovering over the relevant div, but the transition is too abrupt. I would like it to fade in and out instead of ...

Leverage dynamically loaded HTML classes using jQuery

My page has dynamically loaded divs with the class name product. The issue I am facing is that Jquery does not seem to recognize this class when using the code below. Clicking on the product divs doesn't trigger any action, while clicking on nav-eleme ...

"Recently, I included a function called 'test' in the code, but I encountered an error stating that it was not recognized as a function. Can someone provide assistance

Issue Detected A 'TypeError' has been thrown: ".goal_test".click is not defined as a function Feel free to collaborate on the code by clicking this link: Please note that my modified code is enclosed within asterisk symbols. ...

Modifying the background image of the <body> element in CSS to reflect the season based on the current month in the calendar

I'm struggling to change my HTML background based on the date. The code I've tried isn't working as expected and I can't seem to find any relevant examples to guide me. My goal is simple - I want the background of my HTML page to be ch ...

A combination of fixed width column and dynamic content area in Bootstrap design

Is there a way to achieve the combination of fixed and fluid width columns using Twitter Bootstrap alone, similar to this example? The HTML and CSS in the example make it seem simple. But can this be done solely with Bootstrap? ...

Extract value from child div using JQuery and update text in another section of the page

Here is a fiddle showcasing the concept I am working on: https://jsfiddle.net/wvz9p3e7/1/ The code is written in PHP and involves a loop for cycling through 7 or 8 different garments. My goal is to have the window on the right side of the fiddle display t ...

Modifying the font style and color of text within the table header - Material Table version 0.19.4

Recently, I began using React and Material UI but have encountered an issue with modifying the color and font of text in the table header. Despite attempting to override it, the default style remains unchanged. It has been mentioned that utilizing "heade ...

Aligning a div within another div with an image

My current setup looks like this: HTML: <div class="wrapper"> <img src="image.jpg" /> <div class="circle"> X </div> </div> CSS: .circle { width:50px; height:50px; border-radius:50%; background: #FF0000; } ...

Modifying color scheme in ASP.NET web application's table

Although I'm not very familiar with this, I'll try to help out. I have an ASP.Net MVC web app in Visual Studio where one of my views, called View Details, contains a table that displays colors based on a specific scale. This scale consists of onl ...

The background colors are not extending to the full width of the screen

I am facing an issue in my CSS code while trying to create a footer. The background color is not filling the width of the screen, and I had encountered the same problem earlier. Here is how it looks: () HTML Code: *{ margin: 0; padding: 0%; b ...

What is the process for transferring a file's contents to my server?

I am currently working on allowing users to import an OPML file that I parse server-side in my Rails application. However, I am facing difficulties as it appears that my server is not receiving the information correctly (neither the success nor error funct ...

Creating custom styles using Material-UI's makeStyles function with both before and after

Currently, I'm tasked with a project that involves utilizing the CSS code below. .hexagon, .hexagon::before, .hexagon::after { width: 67px; height: 116px; border-radius: 18%/5%; } I am wondering if there is a method to apply the specified style ...

Executing a PHP script to initiate a ping transmission

I have a project to complete for my university involving the development of a simple application. However, I lack experience in this area and am unsure how to proceed. The objective is straightforward: I want to send ping requests to 50 IP addresses at t ...