Employing transitions with the :target selector

Seeking guidance on transitioning with a :target-selector. I've searched extensively online for ways to implement the transition in my code, but I've only managed to get animations to work (which is not permitted).

Here's my HTML code:

    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>BluShop</title>
    <link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="container">
    <header id="header">
        <h1>BluShop</h1>
    </header>

    <section id="leftContent">
        <div id="pacificrimContent">
            <h2>Pacific Rim</h2>
            <img src="../bilder/pacificRim.jpg">
            <p>blahblah</p>
        </div>
        <div id="startrekContent">
            <h2>Star Trek</h2>
            <img src="../bilder/starTrek.jpg">
            <p>blahblah</p>
        </div>
        <div id="worldwarzContent">
            <h2>World War Z</h2>
            <img src="../bilder/worldWarZ.jpg">
            <p>blahblah </p>
        </div>
    </section>
    <aside id="rightContent">
        <div id="pacificrimPoster">
            <a href="#pacificrimContent"><img src="../bilder/pacificRim.jpg"></a>
        </div>
        <div id="startrekPoster">
            <a href="#startrekContent"><img src="../bilder/starTrek.jpg"></a>
        </div>
        <div id="worldwarzPoster">
            <a href="#worldwarzContent"><img src="../bilder/worldWarZ.jpg"></a>
        </div>
    </aside>

    <footer id="footer">
    </footer>
</div>  
</body>
</html>

and the CSS code:

    @charset "utf-8";
/* CSS Document */

body{
    margin: 0;
    }

#container{
    width: 960px;
    height: 600px;
    margin: auto;
    background-color: rgb(78, 80, 85);
    border: solid 1px rgb(213, 214, 215);
    }

#header{
    height: 60px;
    background-color: rgb(66, 69, 74);
    margin-bottom: 10px;
    }   

#header h1{
    float: left;
    margin: 0px 10px 0px 10px;
    color: rgb(14, 177, 238);   
    }

#leftContent{
    float: left;
    position: relative;
    margin: 0px 10px 10px 10px;;
    height: 460px;
    width: 780px;
    background-color: rgb(230, 231, 232);
    }

#leftContent h2{
    font-size: 20px;
    margin: 10px 0px 0px 10px;
    }

#leftContent img{
    float: left;
    margin: 0px 20px 10px 10px;
    width: 310px;
    height: 420px;
    }

#leftContent p{
    margin: -4px 20px 10px 0px;
    }           

#pacificrimContent, #startrekContent, #worldwarzContent{
    display: none;
    }   

#rightContent{
    float: right;
    width: 140px;
    height: 460px;
    margin: 0px 10px 10px 10px;
    background-color: rgb(230, 231, 232);
    }   

#rightContent img{
    display: block;
    width: 100px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 20px    
    }

#pacificrimContent:target{
    display:block;
    position: absolute;
    }   

#startrekContent:target{
    display:block;
    position: absolute;
    }

#worldwarzContent:target{
    display:block;
    position: absolute;
    }   

#footer{
    height: 60px;
    margin: 0;  
    background-color: rgb(16, 163, 210);
    clear: both;
    }   

The images that will appear when using :target need to be hidden at first, hence why I used display: none under #pacificrimContent, etc.

I am looking to trigger a fade-in effect when #pacificrimContent appears. I've experimented with adding 'transition: all 2s fadein;' under display: none, but haven't been successful.

If someone could assist me and explain the process so I can learn from it rather than just copying and pasting, that would be greatly appreciated!

Thank you!

Answer №1

When you alter the display property (like changing it from none to block or vice-versa), transitions always end up breaking.

Instead of using the method above, one alternative is setting the initial height of the block to 0 and then transitioning it to 100% when targeted. Check out the sample code below:

#pacificrimContent, #startrekContent, #worldwarzContent {
    height: 0px;
    overflow: hidden;
    transition: height 1s ease-in-out;
}
#pacificrimContent:target, #startrekContent:target, #worldwarzContent:target {
    height: 100%;
    position: absolute;
}

body {
  margin: 0;
}
#container {
  width: 960px;
  height: 600px;
  margin: auto;
  background-color: rgb(78, 80, 85);
  border: solid 1px rgb(213, 214, 215);
}
#header {
  height: 60px;
  background-color: rgb(66, 69, 74);
  margin-bottom: 10px;
}
#header h1 {
  float: left;
  margin: 0px 10px 0px 10px;
  color: rgb(14, 177, 238);
}
#leftContent {
  float: left;
  position: relative;
  margin: 0px 10px 10px 10px;
  height: 460px;
  width: 780px;
  background-color: rgb(230, 231, 232);
}
#leftContent h2 {
  font-size: 20px;
  margin: 10px 0px 0px 10px;
}
#leftContent img {
  float: left;
  margin: 0px 20px 10px 10px;
  width: 310px;
  height: 420px;
}
#leftContent p {
  margin: -4px 20px 10px 0px;
}
#pacificrimContent,
#startrekContent,
#worldwarzContent {
  height: 0px;
  overflow: hidden;
  -webkit-transition: height 1s ease-in-out;
  -moz-transition: height 1s ease-in-out;
  transition: height 1s ease-in-out;
}
#rightContent {
  float: right;
  width: 140px;
  height: 460px;
  margin: 0px 10px 10px 10px;
  background-color: rgb(230, 231, 232);
}
#rightContent img {
  display: block;
  width: 100px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 20px
}
#pacificrimContent:target,
#startrekContent:target,
#worldwarzContent:target {
  height: 100%;
  position: absolute;
}
#footer {
  height: 60px;
  margin: 0;
  background-color: rgb(16, 163, 210);
  clear: both;
}
<div id="container">
  <header id="header">
    <h1>BluShop</h1>

  </header>
  <section id="leftContent">
    <div id="pacificrimContent">
      <h2>Pacific Rim</h2>

      <img src="../bilder/pacificRim.jpg">
      <p>blahblah</p>
    </div>
    <div id="startrekContent">
      <h2>Star Trek</h2>

      <img src="../bilder/starTrek.jpg">
      <p>blahblah</p>
    </div>
    <div id="worldwarzContent">
      <h2>World War Z</h2>

      <img src="../bilder/worldWarZ.jpg">
      <p>blahblah</p>
    </div>
  </section>
  <aside id="rightContent">
    <div id="pacificrimPoster">
      <a href="#pacificrimContent">
        <img src="../bilder/pacificRim.jpg">
      </a>

    </div>
    <div id="startrekPoster">
      <a href="#startrekContent">
        <img src="../bilder/starTrek.jpg">
      </a>

    </div>
    <div id="worldwarzPoster">
      <a href="#worldwarzContent">
        <img src="../bilder/worldWarZ.jpg">
      </a>

    </div>
  </aside>
  <footer id="footer"></footer>
</div>

Check out these links for a list of Animatable/Transitionable properties:

  1. W3C List
  2. MDN List

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

I am looking to reposition the navbar span tag so that it appears beneath the icon, utilizing both HTML and CSS

I'm currently working on a navbar design with 5 items, each containing an icon and a span tag. However, I'm facing difficulties in aligning the span tag below the icon for all 5 items in the navbar. Despite my best efforts, I have been unable to ...

Tips for customizing icons when hovering in CSS

I'm trying to make a default icon light up on hover using CSS, but I'm having trouble getting it to work. Any suggestions on how I can achieve this effect? Thank you! HTML <ul class='nav nav-main nav-sidebar'> <li role=&apos ...

What is the best way to incorporate text below grid columns in html?

Can you show me how to insert text below a column using the grid system? Here is what I am trying to accomplish Here is the code I have created: .grid{ display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-auto-rows: 240px 240px; row-g ...

Choosing an option from an HTML dropdown menu within an Android WebView

Can someone provide me with an example of how to use JavaScript to select an item from an HTML drop-down menu after downloading a webpage to WebView? ...

What is the best way to position a button beside a heading?

The issue remains even after applying float: right; as it causes the button to extend beyond the div. How can I resolve this problem? HTML <div id="main"> <h1>Title</h1> <button>Button</button> </div> CSS #main { ...

What is the best way to ensure the header spans the entire width of a webpage using the display: flex; property and flex-direction: column styling within the body element

Click here for the CSS code snippet Hello everyone, this is my very first query on Stack Overflow. I know it might be a simple question but it's my first time posting here so kindly bear with me. Below is the CSS code snippet provided: *{ ...

Switch ng-model in Angular to something different

I am looking to transform my own tag into a template <div><input .../><strong>text</strong></div> My goal is to have the same values in both inputs. Check out the plunker here If I change the scope from scope: {value:' ...

When I click on "Submit", it redirects me to the PHP page

I've followed a tutorial on creating custom PHP contact forms at this link: and made some modifications to the form for my specific requirements. However, when I click the Submit button on the form, it redirects me to the PHP page. What I actually w ...

Assess JavaScript for Fetch API implementation

Currently, I am utilizing node.js on Windows with the express module to create an HTML page where I need to retrieve data from a server-side function called getfiledata() (I want to keep my Javascript and text file private). I have attempted to use fetch( ...

Limiting the displayed portion of a table and implementing scrolling instead

I am currently working with a static HTML template that contains the following code: <table> <thead></thead> <tbody></tbody> </table> Using jQuery and AJAX, I am dynamically adding and removing rows and columns ...

tips for remaining in modal window post form submission

I have a form within a modal window. I am using ajax to load content in the same modal window, but the issue is that it redirects to the main page after submitting the form. How can I ensure that the modal window stays open even after the form submission? ...

How can I open a new window, redirect the current one, and bring focus to the new window using JavaScript?

Trying to troubleshoot a problem I'm having with the following setup: - Using SAP Portal, I am launching an HTML page containing this code. - The goal is for the HTML page to open a new window. - Once the new window opens, the original HTML page ...

"div p" or "div * p"? Do they have the same meaning or is there a difference between them?

My knowledge of the "grandchild" selector in CSS was limited until I came across this insightful article. Intrigued, I decided to experiment with it and found that it resembles the universal selector (which targets all elements). Let's take a look at ...

Utilizing the value selected in a form to trigger an action method without the use of a query

I am currently utilizing pug to generate pages for my nodeJS server. In this particular case, I am attempting to use the value selected in a form select option to dynamically change the action method. Essentially, the goal is to utilize the group name sel ...

Organizing JSON Data into Paragraphs

I'm working on a basic web application using Angular JS. Within my JSON object, I have several paragraphs that I need to split onto separate lines. I tried looking for a way to force a line break within the JSON object, but couldn't find a solut ...

Take off the wrapping from the package

I need help with my code on how to remove the wrapper span tag without removing the text. <ul> <li> <a href="#"> </a> <ul> <li> <a href="#"> ...

Navigating discreetly with invisible scroll bar

I am attempting to design a webpage where the scroll bar is hidden, but users can still scroll or click on links to navigate down the page. I have managed to hide the scroll bar, but I am facing an issue where the page jumps to 100% when scrolling. How can ...

object stored in an array variable

I am looking to find out if there is a way to access a variable or function of an object that has been added to an array. I want to display all the objects in the array on a web page using a function that will return a string containing their content. Thi ...

What is the best way to generate an extensive table with HTML and AngularJS?

I am looking to implement an expandable table feature where a single header with a plus icon can be clicked to reveal the child rows beneath it. Currently, I have been able to achieve this functionality. However, I am facing an issue where the child rows ...

The <footer> element refuses to be placed at the bottom of the page

Struggling to keep my <footer> tag at the bottom of my page despite trying various techniques. Check out my website below to see the issue in action: ...