Connect the common background of an absolute SVG element and a relative element

As I am not very familiar with CSS, I may be facing an XY problem where I have been able to find information about the individual parts involved but struggle to combine them effectively.

The challenge is to add a background to a navbar while also incorporating an after svg element that fills the image underneath it. Both the navbar and the svg need to use the same background.

I came across this blog post which covers using an image as an SVG fill, but I haven't been successful in combining it with using the same image as a background for another element.

Below is the code I have been working on so far:

body, p {
    margin: 0;
}

.navbar {
    padding: 10 10 5 15;
}

.navbar a {
    margin-right: 10;
}

nav::after {
    content: "";
    position: absolute;
    z-index: 2;
    height: 15px;
    width: 100%;
    left: 0;
    background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/23618/rip.svg) bottom;
    background-size: 50%;
}
<nav>
    <div class="navbar">
        <a class="active" href="#home">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
    </div>
</nav>
<body>
    <img src="https://cdn.pixabay.com/photo/2013/07/13/13/36/triangle-161210_1280.png">
</body>

It results in this visualization (observe the rip effect appearing over the ruler):

https://i.sstatic.net/0K32J.png

If I wanted to incorporate this image (another royalty-free image) as the background image of the navbar, the final look would be similar to this (crudely edited with GIMP):

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

Answer №1

If you're looking for a unique approach, consider incorporating the triangle design into a png image rather than masking it with a separate svg. You can then position the rippled triangle inside the png to achieve the desired effect:

/*new CSS code from here*/

nav{
height: 40px;
background: url('https://cdn.pixabay.com/photo/2019/08/21/05/24/vintage-4420182_1280.jpg') repeat-x 6px;
display: block;
padding: 20px;
width: 1280px;
}
.triangle{
top:63px;
position: absolute;
left: -10px;
}
<head>
    <style>
        body, p {
            margin: 0;
        }

        .navbar {
            padding: 10 10 5 15;
        }

        .navbar a {
            margin-right: 10;
        }

        nav::after {
            content: "";
            position: absolute;
            z-index: 2;
            height: 15px;
            width: 100%;
            left: 0;
            background-size: 50%;
        }
    </style>
</head>
<nav>
    <div class="navbar">
        <a class="active" href="#home">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
    </div>
</nav>
<body>
    <img class="triangle" src="https://i.sstatic.net/fGW2R.png">
</body>

The image: https://i.sstatic.net/fGW2R.png

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

How to convert a Django template table to an Excel file (.xlsx)

Exploring the world of Django templates for the first time and in need of exporting data. Seeking guidance on how to export a table from a Django template to a .xlsx file. Is there a specific method for achieving this task? Here is a snippet from my views. ...

What is the best way to implement a loading cursor when the Submit button is clicked?

Is there a way to incorporate a progress cursor into my code in order to notify the user to wait when they click the Submit button or the Upload Button while uploading multiple files? Below is an example of my form: <form action="" method="post" enct ...

CSS allows for the creation of fixed bars that remain at the top of the

I am looking to create a fixed bar that is off-center with a scrolling background. The code I have so far either places the bar on the surface but fixes it to the background, or it positions the bar beneath the image and fixes it to the window - neither ...

Innovative dropdown menu featuring images and informative text snippets

I have a question about creating a menu similar to the one on http://www.resellerclub.com that includes images and text details. Are there any ideas on which technologies were used or if there are any commercial solutions available for this? Thank you. ...

Rearranging the order of Div elements within a main container using JavaScript DOM manipulation

How can I move elements within a div from the start to the end in the same div, for example, changing the order from 1-2-3 to 2-3-1? My code: const cards = document.querySelectorAll(".card"); const firstCard = document.querySelectorAll(".card")[0].inne ...

Using Javascript, retrieve a custom attribute specific to a checkbox option

How can I access the custom attribute "mem_name" value? <input class="messageCheckbox" type="checkbox" value="3" mem_name='ABC' name="checkmember" > <input class="messageCheckbox" type="checkbox" value="1" mem_name='PQR' name ...

Only by refreshing can submission be prevented effectively

My website has a page that redirects to another page. On the second page, I have prevented the default form submission and implemented my own custom functionality using the following code: <!DOCTYPE html> <html> <head> <ti ...

Preventing the creation of redundant database records in Django

Currently, I have a form in the settings that saves user entries into the Django SQLite database. It is essential to prevent users from entering duplicate settings with the same name. I attempted to use a for loop to verify if the data already exists but ...

Is there a way to pass an object as a parameter when calling a function in JavaScript?

Is it possible to pass an object as a parameter to a function that is a value of another object? I know how to pass a string easily and even a variable as a string. But passing an object seems to be tricky. Let's consider the following 2 objects: var ...

when the keyboard appears on mobile, ensure that the input element is in focus

Is there a way to automatically scroll the page/form to focus when a user clicks on an input field and the keyboard pops up? I want the input field to be displayed within the current view. I've attempted two solutions, but neither seems to be effecti ...

Multi-line auto-suggest list with typeahead functionality

Currently, I am utilizing typeahead with Twitter Bootstrap and AngularJS. The autocomplete suggestions are displayed in a single line, as seen in the screenshot below. However, I would like to have the big cities' names displayed on separate lines. Fo ...

The dimensions of the HTML table have expanded excessively following the execution of mysql_fetch_assoc

I have encountered an issue where, after inserting the database values into an HTML table, the table becomes too large. I've tried using CSS code to adjust it, but it doesn't help. Should I consider changing something in the database value type? ...

Executing JavaScript function by clicking on <img>

I've been developing a website similar to YouTube, and I'm facing difficulties with the Like/Dislike feature for comments. Currently, I have implemented it in the following way: when a user clicks on an image (thumbsUp.png or thumbsDown.png), a ...

Tips for creating a scrollable section within a div row using clarity design instead of bootstrap formatting

My current project combines Angular with .NET Core, and I am encountering difficulties in styling one particular component. This component consists of a row with two columns. The first column is empty while the second column contains another row divided i ...

Alter a portion of the style using jQuery

After attempting to use jQuery to change the style of a specific element, I was surprised to find that it didn't work as expected. The typical process for changing the style involves the following code: $(document).ready(function(){ $("p").css({" ...

Issue with Dropdown functionality in Navigation Bar when using Bootstrap and Angular

Currently, I am delving into the realms of Angular and Bootstrap in my learning journey. The challenge I am facing involves code generated by Yeoman. In this code, normal links in my Nav bar function properly, but the drop down items are not behaving as ex ...

The getElementById function is unable to locate any matching results

Currently, I am attempting to extract data from an XML file based on the ID of each tag listed within. Below is an example of the XML structure: <?xml version="1.0" encoding="UTF-8"?> <zones> <bedroom id="0"> <fan i ...

code in html/javascript that is securely implemented for adobe air applications

Creating a desktop application using Adobe Air is my goal, but I have concerns about the security of the HTML/JavaScript source code. My application will feature paid media content, and I am hesitant to expose any URLs associated with it. What methods ca ...

Achieving Dynamic Center Alignment for One or Two DIVs

My goal is to create a page layout with three DIVS arranged as Left|Center|Right, while still being able to display different combinations such as Center, Left|Center, or Center|Right in the center of a wrapper div. I have been using jQuery toggle to swit ...

Anchor tag buttons do not affect the font color and variables remain unchanged

I've been struggling to change the font color and background color of the <a> element, but nothing seems to work. I've attempted using hexadecimal notation and styling the .btn class without pseudo-classes, with no luck. Check out my CodeP ...