How to change content of a div after link click using CSS :target?


Tags: html,css,hyperlink,navigation

Problem :

I am trying to show different tab content when clicking on a link using pure CSS by following this W3Schools tutorial. However, it is not working for me.

Again, it has to be pure CSS, no JavaScript!

Here's my code:

/* landscape-screen.css */


/*** General Settings ***/

* {
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
}


/*Main Wrapper Styling*/

#main-wrapper {
  min-width: 960px;
  height: 100%;
}


/* Banner styling*/

.header {
  width: 100%;
  height: 10%;
  background-color: #7a6868;
}


/* "Welcome" photo in the banner position */

#welcome {
  margin-left: 30%;
}


/* General wrapper settings */

.inner-wrapper-settings {
  width: 100%;
  height: 100%;
}


/* Left navigation menu styling */

.aside {
  width: 180px;
  height: 100%;
  background-color: #cd4409;
  /*display: inline-block; */
  /* So that Nav Bar will be next to its section */
  text-align: center;
  float: left;
}


/* Right main Section styling */

.section {
  /*display: inline-block; */
  /* So that the section will be next to Nav Bar */
  width: calc(100% - 180px);
  height: 100%;
  background-color: #eec712;
  float: right
}


/*Footer styling*/

.footer {
  background-color: black;
  width: 100%;
  height: 80px;
  clear: both;
  color: white;
}


/*** Tooltip***/

.tooltip {
  display: inline;
  position: relative;
}

.tooltip:hover:before {
  border: solid;
  border-color: #333 transparent;
  border-width: 6px 6px 0 6px;
  bottom: 20px;
  content: "";
  left: 50%;
  position: absolute;
  z-index: 99;
}

.tooltip:hover:after {
  background: #333;
  background: rgba(0, 0, 0, .8);
  border-radius: 5px;
  bottom: 26px;
  color: #fff;
  content: attr(title);
  left: 20%;
  padding: 5px 15px;
  position: absolute;
  z-index: 98;
  width: 220px;
}


/* sidebar-links.css */

ul {
  padding: 0;
  margin-top: 30px;
}

li {
  margin-bottom: 30px;
  padding: 0px;
}

a {
  color: white;
  text-decoration: none;
}

a:hover {
  color: #f0d248;
}

a:visited {
  color: white;
}


/*form-responsive.css:*/

@import url(http://fonts.googleapis.com/css?family=Montserrat:400,700);
html {
  /*background:url(http://thekitemap.com/images/feedback-img.jpg) no-repeat;*/
  background-size: cover;
  height: 100%;
}

#feedback-page {
  text-align: center;
}

#form-main {
  width: 100%;
  float: left;
  padding-top: 0px;
}

#form-div {
  background-color: rgba(72, 72, 72, 0.4);
  padding-left: 35px;
  padding-right: 35px;
  padding-top: 35px;
  padding-bottom: 50px;
  width: 450px;
  float: left;
  left: 50%;
  position: absolute;
  margin-top: 30px;
  margin-left: -260px;
  -moz-border-radius: 7px;
  -webkit-border-radius: 7px;
}

.feedback-input {
  color: #3c3c3c;
  font-family: Helvetica, Arial, sans-serif;
  font-weight: 500;
  font-size: 18px;
  border-radius: 0;
  line-height: 22px;
  background-color: #fbfbfb;
  padding: 13px 13px 13px 54px;
  margin-bottom: 10px;
  width: 100%;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  box-sizing: border-box;
  border: 3px solid rgba(0, 0, 0, 0);
}

.feedback-input:focus {
  background: #fff;
  box-shadow: 0;
  border: 3px solid #3498db;
  color: #3498db;
  outline: none;
  padding: 13px 13px 13px 54px;
}

.focused {
  color: #30aed6;
  border: #30aed6 solid 3px;
}


/* Icons ---------------------------------- */

#name {
  background-image: url(http://rexkirby.com/kirbyandson/images/name.svg);
  background-size: 30px 30px;
  background-position: 11px 8px;
  background-repeat: no-repeat;
}

#name:focus {
  background-image: url(http://rexkirby.com/kirbyandson/images/name.svg);
  background-size: 30px 30px;
  background-position: 8px 5px;
  background-position: 11px 8px;
  background-repeat: no-repeat;
}

#email {
  background-image: url(http://rexkirby.com/kirbyandson/images/email.svg);
  background-size: 30px 30px;
  background-position: 11px 8px;
  background-repeat: no-repeat;
}

#email:focus {
  background-image: url(http://rexkirby.com/kirbyandson/images/email.svg);
  background-size: 30px 30px;
  background-position: 11px 8px;
  background-repeat: no-repeat;
}

#comment {
  background-image: url(http://rexkirby.com/kirbyandson/images/comment.svg);
  background-size: 30px 30px;
  background-position: 11px 8px;
  background-repeat: no-repeat;
}

textarea {
  width: 100%;
  height: 150px;
  line-height: 150%;
  resize: vertical;
}

input:hover,
textarea:hover,
input:focus,
textarea:focus {
  background-color: white;
}

#button-blue {
  font-family: 'Montserrat', Arial, Helvetica, sans-serif;
  float: left;
  width: 100%;
  border: #fbfbfb solid 4px;
  cursor: pointer;
  background-color: #3498db;
  color: white;
  font-size: 24px;
  padding-top: 22px;
  padding-bottom: 22px;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
  margin-top: -4px;
  font-weight: 700;
}

#button-blue:hover {
  background-color: rgba(0, 0, 0, 0);
  color: #0493bd;
}

.submit:hover {
  color: #3498db;
}

.ease {
  width: 0px;
  height: 74px;
  background-color: #fbfbfb;
  -webkit-transition: .3s ease;
  -moz-transition: .3s ease;
  -o-transition: .3s ease;
  -ms-transition: .3s ease;
  transition: .3s ease;
}

.submit:hover .ease {
  width: 100%;
  background-color: white;
}

@media only screen and (max-width: 580px) {
  #form-div {
    left: 3%;
    margin-right: 3%;
    width: 88%;
    margin-left: 0;
    padding-left: 3%;
    padding-right: 3%;
  }
}

.section div {
  display: none;
}

.section:target div {
  display: inline-block;
}
<!--Layout Main Wrapper-->
<div id="main-wrapper">

  <div class="header">
    <!-- The banner-->
    <img id="logo" src="Resources/Images/logo.png" width="184" height="47" alt="logo" />
    <img id="welcome" src="Resources/Images/Welcome.png" width="180" height="40" alt="Welcome" />
  </div>
  <!--
            -->
  <div id="content-wrapper" class="inner-wrapper-settings">
    <div class="aside">
      <!--The Navigation Menu (left side)-->
      <ul>
        <li><a title="Link for blog" class="tooltip" href="#blog">Blog</a></li>
        <li><a title="Link for Contacts" class="tooltip" href="#contacts">Contacts</a></li>
      </ul>
    </div>
    <!--
                    -->
    <div class="section">
      <!--The Section of the layout (right side)-->
      <div id="blog">
        <span>Blog</span>
      </div>

      <div id="contacts">
        <div id="form-main">
          <div id="form-div">
            <form class="form" id="form1">

              <p class="name">
                <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
              </p>

              <p class="email">
                <input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
              </p>

              <p class="text">
                <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
              </p>


              <div class="submit">
                <input type="submit" value="SEND" id="button-blue" />
                <div class="ease"></div>
              </div>
            </form>
          </div>
        </div>
      </div>

    </div>
  </div>
  <!--
            -->
  <div class="footer">
    <!--The foot of the layout (down side)-->
    bla bla
  </div>
</div>

jsFiddle

Currently, when I click on "Blog" or on "Contacts", it doesn't show anything. Can you please show me how to fix it?



Solution :

change the css like below

.section > div {display: none;}
.section > div:target  { display: block;}

and remove # from id name

DEMO


    CSS Howto..

    How to use css linear gradients in IE10?

    How to center div within another div?

    How can I force an image to be no larger than the section it's in?

    how to avoid bullets showing in main menu

    How to properly apply WinJS.Binding.Template ListView GridLayout CSS?

    How is `outline: inherit 0` interpreted?

    How to change expand div after click and collapse it after another click?

    css how to make this checkbox smaller

    3x3 CSS grid: how to make center div the only one with a fixed height/width?

    How Do I Wrap Text So My Accordions Image Does Go OutSide My aAccordion

    how to write .html with .css to custom color text

    How to apply padding to a column using and CSS in Firefox?

    How to make div position fixed without floating

    How to overlay images with css (cross-browser)

    how to validate IE 7/8 CSS hacks?

    How to distribute floated elements evenly with a dynamic column and row count in CSS?

    custom styles not effecting the nested accordion jquery - How to fix

    How to make this double line shape with css?

    How do I style a Javascript button using d3?

    How do I keep my background image at the bottom of the page?

    How to divide a border into 4 equal parts?

    How to animate transitions triggered by links

    How at least approximately imitate col-md-1.5?

    How to style a