Jump to content

How do you apply :before CSS when row has background image?


simona6

Recommended Posts

.color-top-border-pink-green:before {
    background: linear-gradient(to right,#e0218a 17.85%,#bff010 53.28%,#96BD0C 100%);
    content: "";
    height: 4px;
    width: 100%;
    display: block;
}

I'm trying to apply this gradient border at the top of a row.

It works if the background is coloured or even if it has a Gradient Colour set in the row options.

But it will not work if there is a background image assigned.  I assumed that as a "before", it would simply place this literally, before the row, so it appears as a top border.

I tried it on a row that has a background image set to contain, and to the right.  And applied it "after", and it added it, but only up to the point where the background image began.

Any ideas?

Simon

Link to comment
Share on other sites

Works for me - what exactly are you seeing?

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Testing before</title>
	<style>
		.color-top-border-pink-green:before {
			background: linear-gradient(to right,#e0218a 17.85%,#bff010 53.28%,#96BD0C 100%);
			content: "";
			height: 4px;
			width: 100%;
			display: block;
		}
		.color-top-border-pink-green{
			height: 350px;
			width: 99vw;
			background: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2073&q=80') center center no-repeat;
		}
	</style>
</head>
<body>
	<div class="color-top-border-pink-green"></div>
</body>
</html>

Note that there is a small chance I'm breaking copyright rules with that background image - I honestly don't know. It comes from here: https://unsplash.com/photos/KMn4VEeEPR8

Edited by maxxd
Link to comment
Share on other sites

This is the web page:

https://www.79design.org.uk/

This is the second where, for this test, I am adding the top :before code.

I think the background image is set within the DIV itself, rather than external Style.

If you look at one of the upper rows you see the top bar working, as the DIV it is "before" has only a background color or gradient set.

top.jpg

Link to comment
Share on other sites

For the section of the page you're showing, the problem is that your background issue is absolutely positioned over top of the ::before element.  You can work around this by adding to your style

position: relative;
z-index: 10;

That will make the gradient show, but it won't be positioned correctly due to the padding on the parent element.  You'll need to adjust that styling to get what you want, and probably other page styles.

 

Link to comment
Share on other sites

You might be misunderstanding what ::before does, or maybe my usage of parent.  The selector ::before is a pseudo-element that is placed before the content of the selected element, not before the actual element. 

For example, the html:

<div class="hello">World</div>

With the CSS

.hello::before {
   content: "Hello";
}

Would be rendered as if you had written this HTML

<div><span>Hello</span>World</div>

The pseudo-element is a child of the selected element.  As such, any padding on the selected (parent) element affects where the pseudo-element is rendered.

 

 

Edited by kicken
Link to comment
Share on other sites

Sorted.  I applied positon: absolute, with top: 0, and index: 4 (so it goes over any color overlay, but not over any image that is 'overlapping'.

But I assumed that applying it as ::before, means this comes immediately before that row - not "inside it"...

Link to comment
Share on other sites

As kicken explained, ::before will come before the content inside the specified element, not before the specified element itself. ::after works the same way, only, you know - after the content of the specified element.

Link to comment
Share on other sites

Oooo wow.  By sheer logic I assumed ::before was before the item you select.  Ie.  Before ::title (inside the container), or ::before the inner DIV.

So what code would you use to place it ::before the ACTUAL Div??

Unless I got it right with the Absolute positioning?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.