0

I'm working on the following layout issue: the page heading is center aligned. It should have a background image on its left side, and the background should also be made semi-transparent. The heading content may be of various lengths; the positioning of the background needs to take this into account. The layout should be the same on both desktop and mobile. For example:

logo behind text

So far, I've been able to make the background image semitransparent and center it by using the ::after pseudo-element. The image is set as the background of the ::after, and justification on the parent also positions the background. Code so far:

.heading-bg-logo {
    display: flex;
    justify-content: center;
    color: blue;
}

.heading-bg-logo::after {
    content: "";
    display: inline-block;
    width: 1em;
    height: 1em;
    background: url(https://images.unsplash.com/photo-1581079289196-67865ea83118?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=120&q=80);
    background-repeat: no-repeat;
    background-size: 100%;
    position: absolute;

    z-index: -1;
    opacity: 0.2;
}
<h2 class="heading-bg-logo">
   Test Heading
</h2>
  • The issue at this point is that the logo positioned in the center behind the heading, rather than the left side of the heading text. How can I position it on the left side of the content?
  • Second Issue which i have is that the Logo Size should be customizable.

Note: I've used a demo image here from Unsplash.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
DnD2k21
  • 221
  • 1
  • 6
  • 25
  • Other than the image being stretched, this seems to work. What exactly is it you are trying to solve? – DBS May 06 '22 at 09:23
  • This is why i am asking here. Because the image which will be replaced by a logo should be placed like in screenshot. My Problem is, that the logo is positioned center behind the Heading and not like the beginning of heading as in screenshot. – DnD2k21 May 06 '22 at 09:26
  • I've updated my code and my explaination with the problem. – DnD2k21 May 06 '22 at 09:28
  • Why is a pseudo-element being used? Why not just use background properties on `.heading-bg-logo`? – outis May 06 '22 at 09:57
  • It would be really helpful, if you can show me a working example for that. I have no issue to accept that as an answer. – DnD2k21 May 06 '22 at 10:07
  • It looks like the reason for the pseudo-element is the requirement that the background image have transparency (which should probably be called attention to in the question). – outis May 06 '22 at 10:37
  • Why were the width & height of the pseudo-element set to a different aspect ratio than the image? This distorted the image. – outis May 06 '22 at 11:20

2 Answers2

2

Background sizing and positioning by themselves shouldn't require more than background properties on the element itself. As long as you can get the content box to shrink to fit the content, background-origin can be used to position the background relative to the content box (or the padding box, if you want the background to extend beyond the content box). However, requiring additional transparency on the background steers this towards a pseudo-element.

An alternative to a pseudo-element that can be used in certain circumstances is to use an inset box-shadow with a partially transparent color to wash-out the image. This only works if the background image is on top of a solid color; the same color is then used as the box-shadow color. Note the transparency of the box-shadow is the inverse of the transparency for the image: the more "transparent" the background is supposed to be, the less transparent the box-shadow color.

The other tricky aspect is in how you shrinkwrap the content. The simplest is to use a width of fit-content, though it's not supported in older browsers:

.heading-bg-logo {
color: blue;
width: fit-content;
margin: auto;
padding: 0 0.5em;

/* The spread-radius needs to be large enough to cover the background */
box-shadow: 0 0 0 1000px inset rgba(255, 255, 255, 0.8);
background: url("https://images.unsplash.com/photo-1581079289196-67865ea83118?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3269&q=80");
background-origin: padding-box;
background-repeat: no-repeat;
background-size: contain;
}
<span>content before</span>
<h2 class="heading-bg-logo">Test Heading</h2>
<span>content after</span>

Note that a content-size of contain is used with the sample image to scale it to fit within the element without distorting the aspect ratio.

If using a pseudo-element to achieve semitransparency, then you can position it relative to the content using the usual approach: relatively position the parent element and absolutely position the pseudo-element:

.heading-bg-logo {
color: blue;
width: fit-content;
margin: auto;
position: relative;
}

.heading-bg-logo::after {
content: " ";
opacity: 0.2;
background: url("https://images.unsplash.com/photo-1581079289196-67865ea83118?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3269&q=80");
background-repeat: no-repeat;
background-size: contain;
position: absolute;
top: 0;
left: -0.5em;
height: 100%;
width: 100%;
z-index: -1;
}
<span>content before</span>
<h2 class="heading-bg-logo">Test Heading</h2>
<span>content after</span>
outis
  • 75,655
  • 22
  • 151
  • 221
  • I prefer using ur second solution. But the logo size is not customizable. As i already defined logo size in my post which is width: 120px; height: 40px; this should be considered too.. – DnD2k21 May 06 '22 at 11:14
  • I just want to customize the size of the image which is currently not possible in ur example. Please let me know how to do that. And ofcourse this answer can be acceptable. – DnD2k21 May 06 '22 at 11:35
  • @DnD2k21: customizing the size is entirely possible. The `::after` pseudo-element's position and size don't affect each other; you can set each as needed. – outis May 06 '22 at 11:39
1

Fast fix. Add margin-right: 150px; to your .heading-bg-logo::after class.

Update (responsive behavior)

.c {
  display: flex;
  justify-content: center;
  align-items: center;   
}
.c h2 {
  color: blue;  
}

.heading-bg-logo {  
  position: relative;
}

.heading-bg-logo::after {     
  position: absolute;
  top:-20px;
  content: "";
  margin-left: -80px;  
  width: 120px;
  height: 40px;  
  background: url(https://images.unsplash.com/photo-1581079289196-67865ea83118?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3269&q=80);
  background-repeat: no-repeat;
  background-size: 100% 95%;  
  z-index: -1;
  opacity: 0.2;
}
<div class="c">
  <div class="heading-bg-logo"></div>
  <div>
    <h2>Test Heading Test Heading</h2>  
  </div>
</div>

<div class="c">
  <div class="heading-bg-logo"></div>
  <h2>Test Heading </h2>  
</div>

.heading-bg-logo {
    display: flex;
    justify-content: center;
    color: blue;
}
.heading-bg-logo::after {
   
        content: "";
    display: inline-block;
    width: 120px;
    height: 40px;
  margin-right: 150px;
    background: url(https://images.unsplash.com/photo-1581079289196-67865ea83118?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3269&q=80);
    background-repeat: no-repeat;
    background-size: 100% 95%;
    position: absolute;

    z-index: -1;
    opacity: 0.2;
}
<h2 class="heading-bg-logo">
   Test Heading
</h2>
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • The Text can be any longer. So this solution is not responsive and also not flexible. I cannot give fix position to logo as i do not know the width of the text. – DnD2k21 May 06 '22 at 10:45
  • @DnD2k21 You mean that is dynamic header, right? You an edit the margin value... – Maik Lowrey May 06 '22 at 10:46
  • @DnD2k21 Can you explain what you understand with responsive, pelase? – Maik Lowrey May 06 '22 at 10:49
  • yes i mean there can be more text than this or even lesser than this. Yes you can refer it as a dynamic text. – DnD2k21 May 06 '22 at 10:50
  • Responsive means it should work within any device without breaking the layout grid. But if u put more text in ur example it is no more responsive and doesn't work as i want. – DnD2k21 May 06 '22 at 10:51
  • @DnD2k21 Thanks for answering. I update my answer. With this solution the position of logo is undependent from the length from text. Then you can use media-queries to change style depends from device. – Maik Lowrey May 06 '22 at 11:05