Set inline-svg as the background of a div

225 Views Asked by At

I got a place on my CSS Grid where I am inserting a paragraph of about 15 characters long, I would like to add a background element to it and I want it to have some personality so am using a inline SVG content.

I tried to place it at the bottom with a position:relative but when you change the view-port size it doesn't scale well. I wrapped the SVG code in a container:

.marquee-container {
    height: 0;
    position: absolute;
    width: 500px;
}

And the styles of the SVG:

.svg-marquee {
    fill: teal;
    stroke-width: 4;
    stroke-miterlimit: 10;
    cursor: pointer;
    transition: .5s;
}

This is the HTML Markup

<div class="home-works">
    <div class="head">
        <h1>Entries</h1>
    </div>
    <img class="thumbnail" src="img/profile-picture.png" width="100%"/>
    <div class="main-content">                
        <div class="marquee-container">
            <svg version="1.1"
                        xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
                        x="0px" y="0px" viewBox="0 0 634 175" style="enable-background:new 0 0 634 175;"
                        xml:space="preserve">
                            <path class="svg-marquee" d="M595.9,173C317,173,317,153,38.1,153C27.3,153,2,157.6,2,151C2,87.5,34.8,87.5,34.8,24c0-6.6-7.5-22,3.3-22
                            C317,2,317,22,595.9,22c10.8,0,36.1-4.6,36.1,2c0,63.5-32.8,63.5-32.8,127C599.2,157.6,606.7,173,595.9,173z"/>
            </svg>
        </div>
        <div class="post">
            <h3>title</h3>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore placeat
                                maiores ad ullam illo, blanditiis ipsam libero! Aspernatur, mollitia suscipit?
            </p>
        </div>
    </div>
</div>  

My item is inside a CSS Grid Layout where post is the class I am using to style the contents of the paragraph. Which is not inside any grid-template-areas, its just a class within an area already defined.

.post {
    text-align: left;
    position: relative;
    z-index: 1;
}

So the scaling doesn't go well, I would like to place the element at the background of the paragraph to be contained, what is the best approach?

.marquee-container {
  height: 0;
  position: absolute;
  width: 500px;
}

.svg-marquee {
  fill: teal;
  stroke-width: 4;
  stroke-miterlimit: 10;
  cursor: pointer;
  transition: .5s;
}

.post {
  text-align: left;
  position: relative;
  z-index: 1;
}
<div class="home-works">

  <div class="head">
    <h1>Entries</h1>
  </div>
  <img class="thumbnail" src="img/profile-picture.png" width="100%" />
  <div class="main-content">

    <div class="marquee-container">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" x="0px" y="0px" viewBox="0 0 634 175" style="enable-background:new 0 0 634 175;" xml:space="preserve">
                            <path class="svg-marquee" d="M595.9,173C317,173,317,153,38.1,153C27.3,153,2,157.6,2,151C2,87.5,34.8,87.5,34.8,24c0-6.6-7.5-22,3.3-22
                            C317,2,317,22,595.9,22c10.8,0,36.1-4.6,36.1,2c0,63.5-32.8,63.5-32.8,127C599.2,157.6,606.7,173,595.9,173z"/>
                    </svg>
    </div>
    <div class="post">
      <h3>title</h3>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore placeat maiores ad ullam illo, blanditiis ipsam libero! Aspernatur, mollitia suscipit?
      </p>
    </div>
  </div>
</div>

1

There are 1 best solutions below

3
On BEST ANSWER

Since you are positioning the .marquee-container absolutely. You need a parent to have position set to relative, So, add position: relative on the parent .main-content.

I don't understand why you set the .marquee-container height to 0. I would suggest set it's height to auto and have a bottom: 0;, (As you wanted it to be aligned to bottom of the container).

Set the width to 100% since you want it to be scalable. also add a negative z-index so that the element doesn't cover up the contents.

Check the snippet below.

.main-content {
  position: relative;
}
.marquee-container {
  position: absolute;
  bottom: 0;
  height: auto;
  width: 100%;
  z-index: -5;
}

.svg-marquee {
  fill: teal;
  stroke-width: 4;
  stroke-miterlimit: 10;
  cursor: pointer;
  transition: .5s;
}

.post {
  text-align: left;
  position: relative;
  z-index: 1;
}
<div class="home-works">

  <div class="head">
    <h1>Entries</h1>
  </div>
  <img class="thumbnail" src="img/profile-picture.png" width="100%" />
  <div class="main-content">

    <div class="marquee-container">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" x="0px" y="0px" viewBox="0 0 634 175" style="enable-background:new 0 0 634 175;" xml:space="preserve">
                            <path class="svg-marquee" d="M595.9,173C317,173,317,153,38.1,153C27.3,153,2,157.6,2,151C2,87.5,34.8,87.5,34.8,24c0-6.6-7.5-22,3.3-22
                            C317,2,317,22,595.9,22c10.8,0,36.1-4.6,36.1,2c0,63.5-32.8,63.5-32.8,127C599.2,157.6,606.7,173,595.9,173z"/>
                    </svg>
    </div>
    <div class="post">
      <h3>title</h3>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore placeat maiores ad ullam illo, blanditiis ipsam libero! Aspernatur, mollitia suscipit?
      </p>
    </div>
  </div>
</div>