如何在链接点击后使用CSS更改div的内容:target?

时间:2016-02-25 10:24:16

标签: html css hyperlink navigation

按照此W3Schools tutorial,点击使用纯CSS的链接时,我试图显示不同的标签内容。但是,它对我不起作用。

同样,它必须是纯CSS,没有JavaScript!

这是我的代码:



/* 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>
&#13;
&#13;
&#13;

jsFiddle

目前,当我点击&#34; Blog&#34;或者在&#34;联系人&#34;上,它没有显示任何内容。 你能告诉我如何解决它吗?

2 个答案:

答案 0 :(得分:2)

更改下面的CSS

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

并从ID名称

中删除#

<强> DEMO

答案 1 :(得分:0)

尝试删除角色&#34;#&#34;从您的div ids:

<div id="blog">
<div id="contacts">