Home > Back-end >  Problem in sizing image when using z-index
Problem in sizing image when using z-index

Time:01-14

When i don't specify the position of my sections, the phone image perfectly follows the assigned width.

Which is - width: 60%;

img1

img2



But when i apply position and z-index to the first two sections the width: 60%; doesn't get applied, as the phone image becomes way too large.

img3

img4



HTML & CSS code:

body {
    font-family: 'Montserrat', sans-serif;
    font-weight: 600;
}

#title {
    background-color: #ff4c68;
    color: #FFFFFF;
}

h1 {
    font-family: 'Montserrat', sans-serif;
    font-weight: 800;
    font-size: 3.5rem;
    Line-height: 1.5;
}

h2 {
    font-family: 'Montserrat', sans-serif;
    font-weight: 700;
    font-size: 2rem;
}

h3 {
    font-family: 'Montserrat', sans-serif;
    font-weight: 800;
    font-size: 1.5rem;
}

.container-fluid {
    padding: 3% 15% 7%;
}

p {
    font-family: "Ubuntu";
    color: #8f8f8f;
}


/* Navigation Bar */

.navbar-brand {
    font-family: "Ubuntu";
    font-size: 2.5rem;
}

.navbar {
    padding-bottom: 4.5em;
}

.nav-item {
    padding: 0 18px;
}

.nav-link {
    font-size: 1.2rem;
    font-family: "Montserrat";
    font-weight: 100;
}


/* Download buttons */

.download-button {
    margin: 5% 3% 5% 0;
}


/* Title image */

.title-image {
    width: 60%;
    transform: rotate(25deg);
    position: absolute;
}


/* feature section  */

#features {
    padding: 7% 15%;
    background-color: #FFFFFF;
    position: relative;
    z-index: 1;
}

.feature-box {
    text-align: center;
    padding: 5%;
}

.icon {
    margin-bottom: 1rem;
    color: #ef8172;
}

.icon:hover {
    color: #ff4c68;
}


/* testimonials */

#testimonials {
    text-align: center;
    background-color: #ef8172;
    color: #fff;
}

.carousel-item {
    padding: 7% 15%;
}

.testimonial-image {
    width: 10%;
    border-radius: 100%;
    margin: 20px;
}


/* Press */

#press {
    background-color: #ef8172;
    text-align: center;
    padding-bottom: 3%;
}

.press-logo {
    width: 15%;
    padding: 20px 20px 50px;
}


/* Pricing  */

#pricing {
    padding: 100px;
    text-align: center;
}

.pricing-col {
    padding: 3% 2%;
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>TinDog</title>

    <!-- Bootstap css  -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <!-- CSS Stylesheets  -->
    <link rel="stylesheet" href="css/styles.css">

    <!-- Google fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;800&family=Ubuntu:wght@500&display=swap" rel="stylesheet">
    <!-- Bootstrap scripts  -->
    <script src="jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-7 zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG 2QOK9T ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>



    <!-- font awesome  -->
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg p" crossorigin="anonymous" />


</head>

<body>

    <section id="title">

        <div >

            <!-- Nav Bar -->
            <nav >
                <a  href=""> tindog</a>
                <button  type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span ></span>
          </button>
                <div  id="navbarSupportedContent">
                    <ul >
                        <li >
                            <a  href="">Contact</a>
                        </li>
                        <li >
                            <a  href="">Pricing</a>
                        </li>
                        <li >
                            <a  href="">Download</a>
                        </li>
                    </ul>
                </div>
            </nav>



            <!-- Title -->

            <div >
                <div >
                    <h1>Meet new and interesting dogs nearby.</h1>
                    <button type="button" ><i ></i> Download</button>
                    <button type="button" ><i ></i> Download</button>
                </div>

                <div >
                    <img  src="images/iphone6.png" alt="iphone-mockup">
                </div>
            </div>
        </div>
    </section>


    <!-- Features -->

    <section id="features">
        <div >
            <div >
                <i ></i>
                <h3>Easy to use.</h3>
                <p>So easy to use, even your dog could do it.</p>
            </div>
            <div >
                <i ></i>
                <h3>Elite Clientele</h3>
                <p>We have all the dogs, the greatest dogs.</p>
            </div>
            <div >
                <i ></i>
                <h3>Guaranteed to work.</h3>
                <p>Find the love of your dog's life or your money back.</p>
            </div>
        </div>

    </section>


    <!-- Testimonials -->

    <section id="testimonials">
        <div id="carouselExampleControls"  data-bs-ride="carousel" data-bs-interval="3000">
            <div >
                <div >
                    <h2>I no longer have to sniff other dogs for love. I've found the hottest Corgi on TinDog. Woof.</h2>
                    <img  src="images/dog-img.jpg" alt="dog-profile">
                    <em>Pebbles, New York</em>
                </div>
                <div >
                    <h2 >My dog used to be so lonely, but with TinDog's help, they've found the love of their life. I think.</h2>
                    <img  src="images/lady-img.jpg" alt="lady-profile">
                    <em>Beverly, Illinois</em>
                </div>
            </div>
            <button  type="button" data-bs-target="#carouselExampleControls" data-bs-slide="prev">
              <span  aria-hidden="true"></span>
              <span >Previous</span>
            </button>
            <button  type="button" data-bs-target="#carouselExampleControls" data-bs-slide="next">
              <span  aria-hidden="true"></span>
              <span >Next</span>
            </button>
        </div>





    </section>


    <!-- Press -->

    <section id="press">
        <img  src="images/techcrunch.png" alt="tc-logo">
        <img  src="images/tnw.png" alt="tnw-logo">
        <img  src="images/bizinsider.png" alt="biz-insider-logo">
        <img  src="images/mashable.png" alt="mashable-logo">

    </section>


    <!-- Pricing -->

    <section id="pricing">

        <h2>A Plan for Every Dog's Needs</h2>
        <p>Simple and affordable price plans for your and your dog.</p>
        <div >
            <div >
                <div >
                    <div >
                        <h3>Chihuahua</h3>
                    </div>
                    <div >
                        <h2>Free</h2>
                        <p>5 Matches Per Day</p>
                        <p>10 Messages Per Day</p>
                        <p>Unlimited App Usage</p>
                        <div >
                            <button type="button" >Sign Up</button>
                        </div>
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <h3>Labrador</h3>
                    </div>
                    <div >
                        <h2>$49 / mo</h2>
                        <p>Unlimited Matches</p>
                        <p>Unlimited Messages</p>
                        <p>Unlimited App Usage</p>
                        <div >
                            <button type="button" >Sign Up</button>
                        </div>
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <h3>Mastiff</h3>
                    </div>
                    <div >
                        <h2>$99 / mo</h2>
                        <p>Pirority Listing</p>
                        <p>Unlimited Matches</p>
                        <p>Unlimited Messages</p>
                        <p>Unlimited App Usage</p>
                        <div >
                            <button type="button" >Sign Up</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>


    <!-- Call to Action -->

    <section id="cta">

        <h3>Find the True Love of Your Dog's Life Today.</h3>
        <button type="button">Download</button>
        <button type="button">Download</button>

    </section>


    <!-- Footer -->

    <footer id="footer">

        <p>© Copyright 2018 TinDog</p>

    </footer>


</body>

</html>



I want the end result to be like this:

img5



image of the phone:

enter image description here

CodePudding user response:

Hey buddy I really don't know if this is the correct way of doing it, but it seems to work just fine. What I did was change the width to max width the make it from 60% to 18%. It looks good and like the lesson. then when you switch it to tablet or mobile, I had the @media function change it back to 60% so it didn't shrink all the way down and look weird. idk if I'm explaining it correctly but here's the code:

    .title-image {
      max-width: 18%;
      transform: rotate(25deg);
      position: absolute;
      right: 22%;
      }

      @media (max-width: 991px) {
       .title-image {
       position: static;
       transform: rotate(0deg);
       max-width: 60%;
        }
         }
    

I'm a complete beginner like you but I hope this helped in any way.

CodePudding user response:

You only really need to set the position of your first section to achieve this, if you set the title's position: relative; and z-index: -1;, you won't need to set the position on your .title-image class. See the snippet(Full page) below for your reference.

body {
  font-family: 'Montserrat', sans-serif;
  font-weight: 600;
}

#title {
  background-color: #ff4c68;
  color: #FFFFFF;
  height: 90%;
  max-height: 900px;
  display: flex;
  justify-content: space-evenly;
  padding-inline: 10%;
  position: relative;
  z-index: -1;
}

h1 {
  font-family: 'Montserrat', sans-serif;
  font-weight: 800;
  font-size: 3.5rem;
  Line-height: 1.5;
}

h2 {
  font-family: 'Montserrat', sans-serif;
  font-weight: 700;
  font-size: 2rem;
}

h3 {
  font-family: 'Montserrat', sans-serif;
  font-weight: 800;
  font-size: 1.5rem;
}

.container-fluid {
  padding: 3% 15% 7%;
}

p {
  font-family: "Ubuntu";
  color: #8f8f8f;
}


/* Navigation Bar */

.navbar-brand {
  font-family: "Ubuntu";
  font-size: 2.5rem;
}

.navbar {
  padding-bottom: 4.5em;
  margin-bottom: 3rem;
}

.nav-item {
  padding: 0 18px;
}

.nav-link {
  font-size: 1.2rem;
  font-family: "Montserrat";
  font-weight: 100;
}


/* Download buttons */

.download-button {
  margin: 5% 3% 5% 0;
}


/* Title image */

.title-image {
  width: 60%;
  transform: rotate(25deg);
}


/* feature section  */

#features {
  padding: 7% 15%;
  background-color: #FFFFFF;
}

.feature-box {
  text-align: center;
  padding: 5%;
}

.icon {
  margin-bottom: 1rem;
  color: #ef8172;
}

.icon:hover {
  color: #ff4c68;
}


/* testimonials */

#testimonials {
  text-align: center;
  background-color: #ef8172;
  color: #fff;
}

.carousel-item {
  padding: 7% 15%;
}

.testimonial-image {
  width: 10%;
  border-radius: 100%;
  margin: 20px;
}


/* Press */

#press {
  background-color: #ef8172;
  text-align: center;
  padding-bottom: 3%;
}

.press-logo {
  width: 15%;
  padding: 20px 20px 50px;
}


/* Pricing  */

#pricing {
  padding: 100px;
  text-align: center;
}

.pricing-col {
  padding: 3% 2%;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>TinDog</title>

  <!-- Bootstap css  -->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

  <!-- CSS Stylesheets  -->
  <link rel="stylesheet" href="css/styles.css">

  <!-- Google fonts -->
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;800&family=Ubuntu:wght@500&display=swap" rel="stylesheet">
  <!-- Bootstrap scripts  -->
  <script src="jquery-3.5.1.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-7 zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG 2QOK9T ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>



  <!-- font awesome  -->
  <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg p" crossorigin="anonymous" />


</head>

<body>

  <section id="title">

    <div >

      <!-- Nav Bar -->
      <nav >
        <a  href=""> tindog</a>
        <button  type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span ></span>
          </button>
        <div  id="navbarSupportedContent">
          <ul >
            <li >
              <a  href="">Contact</a>
            </li>
            <li >
              <a  href="">Pricing</a>
            </li>
            <li >
              <a  href="">Download</a>
            </li>
          </ul>
        </div>
      </nav>
      <!-- Title -->
      <div >
        <div >
          <h1>Meet new and interesting dogs nearby.</h1>
          <button type="button" ><i ></i> Download</button>
          <button type="button" ><i ></i> Download</button>
        </div>

        <div >
          <img  src="https://i.stack.imgur.com/uz7Pc.png" alt="iphone-mockup">
        </div>
      </div>
    </div>
  </section>
  <!-- Features -->
  <section id="features">
    <div >
      <div >
        <i ></i>
        <h3>Easy to use.</h3>
        <p>So easy to use, even your dog could do it.</p>
      </div>
      <div >
        <i ></i>
        <h3>Elite Clientele</h3>
        <p>We have all the dogs, the greatest dogs.</p>
      </div>
      <div >
        <i ></i>
        <h3>Guaranteed to work.</h3>
        <p>Find the love of your dog's life or your money back.</p>
      </div>
    </div>
  </section>
  <!-- Testimonials -->
  <section id="testimonials">
    <div id="carouselExampleControls"  data-bs-ride="carousel" data-bs-interval="3000">
      <div >
        <div >
          <h2>I no longer have to sniff other dogs for love. I've found the hottest Corgi on TinDog. Woof.</h2>
          <img  src="images/dog-img.jpg" alt="dog-profile">
          <em>Pebbles, New York</em>
        </div>
        <div >
          <h2 >My dog used to be so lonely, but with TinDog's help, they've found the love of their life. I think.</h2>
          <img  src="images/lady-img.jpg" alt="lady-profile">
          <em>Beverly, Illinois</em>
        </div>
      </div>
      <button  type="button" data-bs-target="#carouselExampleControls" data-bs-slide="prev">
              <span  aria-hidden="true"></span>
              <span >Previous</span>
            </button>
      <button  type="button" data-bs-target="#carouselExampleControls" data-bs-slide="next">
              <span  aria-hidden="true"></span>
              <span >Next</span>
            </button>
    </div>
  </section>
  <!-- Press -->
  <section id="press">
    <img  src="images/techcrunch.png" alt="tc-logo">
    <img  src="images/tnw.png" alt="tnw-logo">
    <img  src="images/bizinsider.png" alt="biz-insider-logo">
    <img  src="images/mashable.png" alt="mashable-logo">

  </section>


  <!-- Pricing -->

  <section id="pricing">

    <h2>A Plan for Every Dog's Needs</h2>
    <p>Simple and affordable price plans for your and your dog.</p>
    <div >
      <div >
        <div >
          <div >
            <h3>Chihuahua</h3>
          </div>
          <div >
            <h2>Free</h2>
            <p>5 Matches Per Day</p>
            <p>10 Messages Per Day</p>
            <p>Unlimited App Usage</p>
            <div >
              <button type="button" >Sign Up</button>
            </div>
          </div>
        </div>
      </div>
      <div >
        <div >
          <div >
            <h3>Labrador</h3>
          </div>
          <div >
            <h2>$49 / mo</h2>
            <p>Unlimited Matches</p>
            <p>Unlimited Messages</p>
            <p>Unlimited App Usage</p>
            <div >
              <button type="button" >Sign Up</button>
            </div>
          </div>
        </div>
      </div>
      <div >
        <div >
          <div >
            <h3>Mastiff</h3>
          </div>
          <div >
            <h2>$99 / mo</h2>
            <p>Pirority Listing</p>
            <p>Unlimited Matches</p>
            <p>Unlimited Messages</p>
            <p>Unlimited App Usage</p>
            <div >
              <button type="button" >Sign Up</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
  <!-- Call to Action -->
  <section id="cta">
    <h3>Find the True Love of Your Dog's Life Today.</h3>
    <button type="button">Download</button>
    <button type="button">Download</button>
  </section>
  <!-- Footer -->
  <footer id="footer">
    <p>© Copyright 2018 TinDog</p>
  </footer>
</body>

</html>

You can familiarize yourself with css position property here.

PS. I won't be including any @media queries as it isn't part of the question.

  •  Tags:  
  • Related