简体   繁体   English

如何在 css `content` 属性中/顶部放置链接(不会弄乱属性的显示)?

[英]How can I place a link in/on top of a css `content` property (without messing up the display of the property)?

I found this very nice floating navbar with a rotating cube effect: https://codepen.io/arjancodes/pen/wtqIr我发现这个非常漂亮的带有旋转立方体效果的浮动导航栏: https://codepen.io/arjancodes/pen/wtqIr

Unfortunately, it does not include working links, and it is not clear to me how to insert them.不幸的是,它不包括工作链接,我不清楚如何插入它们。 I've tried placing the <a> tag a bunch of different ways and monkeying with the CSS based on what I have read about ::after .根据我对::after的阅读,我尝试过以多种不同的方式放置<a>标记并使用 CSS 进行操作。 I also tried changing nth-of-child to nth-of-type , which doesn't seem to have help or hurt.我还尝试将nth-of-child更改为nth-of-type ,这似乎没有帮助或伤害。

The fork I am playing with is demoed here, but as it will change I will post the code in its current state below: https://codepen.io/AwakeAntelope/pen/wvJovrP我正在玩的叉子在这里演示,但由于它会改变,我将在下面的当前 state 中发布代码: https://codepen.io/AwakeAntelope/pen/wvJovrP

I have been able to get the links working in the appropriate places, but for some reason when I do that, each <li> ends up containing the first content property (in my fork of the code, "Google" ).我已经能够让链接在适当的地方工作,但由于某种原因,当我这样做时,每个<li>最终都包含第一个content属性(在我的代码分支中, "Google" )。

HTML HTML

<nav>
  <ul>
        <a href="http://www.google.com">
    <li>
      <div class="home-icon">
        <div class="roof">
          <div class="roof-edge"></div>
        </div>
        <div class="front"></div>
      </div>
    </li>
    </a>
    <a href="http://www.yahoo.com">
    <li>
      <div class="about-icon">
        <div class="head">
          <div class="eyes"></div>
          <div class="beard"></div>
        </div>
      </div>
    </li>
    </a>
    <a href="http://www.bing.com">
    <li>
      <div class="work-icon">
        <div class="paper"></div>
        <div class="lines"></div>
        <div class="lines"></div>
        <div class="lines"></div>
      </div>
    </li>
    </a>
    <a href="http://www.duckduckgo.com">
    <li>
      <div class="mail-icon">
        <div class="mail-base">
          <div class="mail-top"></div>
        </div>
      </div>
    </li>
    </a>
  </ul>
</nav>

<section>
  <h1>3d rotating navigation</h1>
  <h2>+ CSS Icons</h2>
</section>

CSS: CSS:

*,*:before,*:after {
    box-sizing: border-box;
}

:after {
    content: "";
}

section {
  position: relative;
  left: 100px;
}

h1 { 
  margin:80px 0 10px 0;
  font-size: 52px;
  font-family: 'Montserrat', sans-serif;
  text-transform: uppercase;
  text-shadow: 1px 1px 0px #DC143C,
    2px 2px 0px #DC143C,
    3px 3px 0px #DC143C,
    4px 4px 0px #DC143C;
}

h2 {
  font-size: 24px;
}

body {
  padding: 100px 0;
    background: lightblue;
  color: white;
  max-width:640px;
    font-family: 'Montserrat', sans-serif;
    font-size: 14px;
  line-height:1.4;
  font-smoothing: anti-aliased;
}

nav {
  float: left;
    position: relative;
    top: 0;
  left: 0;
    background: transparent;
}

nav ul {
    text-align: center;
}

nav ul li {
    position: relative;
  width: 70px;
  cursor: pointer;
    background: crimson;
    text-transform: uppercase;
    transition:all .4s ease-out;
}

nav ul li:after {
    position: absolute;
    background: white;
    color: crimson;
    top:0;
    left: 70px;
    width: 70px; height: 100%;
  opacity:.5;
  transform: perspective(400px) rotateY(90deg);
    transform-origin: 0 100%;
    transition:all .4s ease-out;
}

nav ul li:nth-of-type(1):after { 
    content: "Google";
    line-height: 88px;
}
nav ul a li:nth-of-type(2):after { 
    content: "Yahoo";
    line-height: 88px;
}
nav ul a li:nth-of-type(3):after { 
    content: "Bing";
    line-height: 85px;
}
nav ul a li:nth-of-type(4):after { 
    content: "DuckDuckGo";
    line-height: 70px;
}

nav ul li:hover {
    transform: translateX(-70px);
}

nav ul li:hover:after {
  opacity: 1;
    transform: perspective(400px) rotateY(0deg) scale(1) ;
}


nav ul li > div {
    display: inline-block;
    padding: 25px 0;
    background: transparent;
}

nav ul li div { position: relative; }

.roof {
    width: 0;
    height: 0;
    top:2px;
    border-style: solid;
    border-width: 0 21px 15px 21px;
    border-color: transparent transparent #ffffff transparent;
}

.roof-edge {
    position: absolute;
    z-index: 20;
    left: -17px;
    top: 3px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 17px 12px 17px;
    border-color: transparent transparent crimson transparent;
}
/*white triangle over red triangle*/
.roof-edge:after {
    position: absolute;
    left: -14.5px;
    top: 3px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 14.5px 10px 14.5px;
    border-color: transparent transparent white transparent;
}

.front {
    position: relative;
    top: 3px;
    width: 28.5px;
    height: 20px;
    margin: 0 auto;
    background: white;
}
/*door*/
.front:after {
    position: absolute;
    background: crimson;
    width: 11px;
    height: 13px;
    bottom: 0;
    left:9px;
}

/*/// About me ////*/

.head {
    width: 32px;
    height: 35px;
    background: white;
    border-radius:8px;
}
/*mouth*/
.head:after {
    width: 4px;
    height: 10px;
    background: white;
    position: absolute;
    border-radius:4px 0 0 4px;
    top:21px;
    left: 14px;
    transform:rotate(270deg);
}

.eyes {
    width: 8px;
    height: 5px;
    border-radius: 50%;
    position: absolute;
    top: 10px;
    left: 5px;
    background: crimson;
}
/*right eye*/
.eyes:after {
    width: 8px;
    height: 5px;
    border-radius: 50%;
    position: absolute;
    top: 0;
    left: 14px;
    background: crimson;
}

.beard {
    width: 32px;
    height: 17px;
    background: crimson;
    border:2px solid white;
    position: absolute;
    bottom: 0;
    left: 0;
    border-radius:0 0 8px 8px;
}
/*nose*/
.beard:after {
    position: absolute;
    top:-2px;
    left: 11px;
    background: white;
    width:6px;
    height: 4px;
    border-left:1px solid crimson;
    border-right:1px solid crimson;
}

/*//work//*/

.paper {
    position: relative;
    height:32px;
    width: 29px;
    background: white;
    border:2px solid white;
}

/*window*/
.paper:after {
    position: absolute;
    top:1px;
    left: 0;
    width: 25px;
    height: 29px;
    background: white;
    border-top:4px solid crimson;
}

.lines {
    position: absolute;
    top: 36px;
    left: 5px;
    width: 11px;
    box-shadow: 0 0 0 1px crimson;
}

.lines:after {
    position: absolute;
    top: 4px;
    left: 3px;
    width: 14px;
    box-shadow: 0 0 0 1px crimson;
}

.lines:nth-child(2) {
    position: absolute;
    top: 44px;
    left: 8px;
    width: 11px;
}

.lines:nth-child(2):after {
    position: absolute;
    top: 4px;
    left: -3px;
    width: 11px;
}

.lines:nth-child(3) {
    position: absolute;
    top: 52px;
    left: 8px;
    width: 14px;
}

.lines:nth-child(3):after {
    display: none;
}

/*//mail //*/

.mail-base {
    position: relative;
    width: 32px;
    height: 18px;
    background: white;
}

.mail-top {
    position: absolute;
    z-index: 20;
    left: 0;
    top: 0;
    width: 0;
    height: 0;
    transform: rotate(180deg);
    border-style: solid;
    border-width: 0 16px 11px 16px;
    border-color: transparent transparent crimson transparent;
}

.mail-top:after {
    position: absolute;
    z-index: 20;
    left: -16px;
    top: 3px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 16px 9px 16px;
    border-color: transparent transparent white transparent;
}

I got it working, just changed this code:我得到了它的工作,只是改变了这个代码:

nav ul a:nth-of-type(1) li:after { 
    content: "Google";
    line-height: 88px;
}
nav ul a:nth-of-type(2) li:after { 
    content: "Yahoo";
    line-height: 88px;
}
nav ul a:nth-of-type(3) li:after { 
    content: "Bing";
    line-height: 85px;
}
nav ul a:nth-of-type(4) li:after { 
    content: "DuckDuckGo";
    line-height: 70px;
}

The nth-of-type needs to target the A element because right now you have it traversing LI elements inside of the first A element, hence the same text every time. nth-of-type需要以A元素为目标,因为现在你让它遍历第一个A元素内的LI元素,因此每次都是相同的文本。 I agree that having ul a li is not a best practice btw.我同意拥有ul a li不是最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CSS链接搞砸了 - Css link messing up 内联块顶部的CSS Padding(?)弄乱了我的div内容 - CSS Padding(?) on inline block top messing up my div content 如何解决 body 属性以在 CSS 中为 Codepen 显示颜色? - How can I solve the body property to display color in CSS for Codepen? 显示用户创建的无效HTML内容而不会弄乱页面 - Display user created invalid HTML content without messing up the page 如何在不弄乱页脚的情况下将两个 div 并排放置 - How do I place two divs next to each other without messing up the footer 如何在顶部修复指定高度的 header 而不将顶部属性添加到同级部分以使其可见? - How Can I fix a header of a specified height in the top without adding top property to the sibling section to make it visible? 尽管我们使用显示 css 属性,但如何使用 [hidden] 属性隐藏 DOM 元素? - How can I hide DOM element using [hidden] property although we use display css property? 如何用表格行显示对多个元素进行分组而不弄乱表格? - How do I group multiple elements with table-row display without messing up the table? 如何使用CSS在内容框的顶部放置三角形“ ^”? - How to place triangle “^” on the top of content box with css? 如何使用CSS属性“ display:flex;”标记布局块? - How to mark up a layout blocks with the CSS property “display:flex;”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM