html - 位置 : fixed gets in way of anchor?

标签 html css

body {
  margin: 0px;
  background-color: rgba(195, 246, 255, 0.48);
}

.nav {
  background-color: rgba(190, 190, 190, 0.72);
  position: fixed;
  top: 0px;
  right: 0px;
  left: 0px;
  text-align: center;
  height: 85px;
  border: 1px solid black;
}

ul {
  position: relative;
  top: 6px;
  left: 360px;
  width: 450px;
  height: 60px;
  margin: 0px auto;
  font-family: "Cairo";
  padding: 5px;
}

li {
  position: relative;
  top: 6px;
  display: inline;
  font-size: 25px;
  text-transform: uppercase;
  letter-spacing: .03em;
}

li.spacing1 {
  margin-right: 25px;
}

li.spacing2 {
  margin-left: 25px;
}

li:hover {
  border: 1px solid red;
  padding: 5px;
}

.content {
  background-color: white;
  width: 1100px;
  margin: 87px auto 0px auto;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="../css/example.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Cairo" rel="stylesheet">
  <title>My Portfolio</title>
</head>

<body>

  <div class="nav">
    <ul>
      <li class="spacing1"><a href="#about">About</a></li>
      <li><a href="#portfolio">Portfolio</a></li>
      <li class="spacing2"><a href="#contact">Contact</a></li>
    </ul>
  </div>

  <div class="content">

    <p id="about">My name is Lawrence Yoon and I graduated from Cal Poly Pomona with a degree in Hospitality Management and minor in Business Marketing. After working in the hospitality industry for 5+ years, I wanted to expand my knowledge by trying out different
      career paths and happened to find out about computer programming. I've been self-learning for 3+ months, and hope to become a front-end web developer soon! I am proficient with HTML and CSS, and have some knowledge of JavaScript. Once I get my first
      job, I hope to continue studying and eventually learn back-end. My goal is to one day become a full-stack developer!</p>
    <p>So far, I have knowledge of HTML, CSS, Bootstrap, JavaScript, and jQuery. I have used StackOverflow a couple times, and although I don't rely on this, it's great to ask questions and receive answers promptly from a loving community. Jon Duckett's
      Introduction to HTML and CSS has been a great teacher to me thus far; although it's a bit dated, it contains detailed images and helped me greatly step foot into the world of web development. Following this book, I got his second book for JavaScript
      and jQuery and have started to read through that. While I'm doing this, I'm learning from FreeCodeCamp, which immensely helped because through their projects, I'm able to make this file on Codepen, which will become my portfolio! I will definitely
      try to finish all FCC challenges, and make a couple of apps to showcase my skills!</p>
    <p>As of right now, I'm not looking for a job because I lack the skills necessary to get my first job in web development. In a couple of months, I hope to polish what I know so far as well as learn new skills to get my first job. In time, I will showcase
      my skills by demonstrating my abilities through the makings of small apps. Thanks for reading! Please don't hesitate to reach out to me to provide tips, or if you want to talk about anything I'm all ears!</p>

    <p id="portfolio">Portfolio:</p>
    <img src="http://res.cloudinary.com/lyoon/image/upload/v1503300012/06_10_16_buqi65.jpg" alt="Beautiful sunset in Redondo Beach" width="300" height="300">
    <img src="http://res.cloudinary.com/lyoon/image/upload/v1503299864/01_30_16_2_d1ntei.jpg" alt="Gray day in Redondo Beach" width="300" height="300">
    <img src="http://res.cloudinary.com/lyoon/image/upload/v1503299922/02_01_16_wbmyow.jpg" alt="Water in Redondo Beach" width="300" height="300">
    <img src="http://res.cloudinary.com/lyoon/image/upload/v1503300012/06_10_16_buqi65.jpg" alt="Beautiful sunset" width="300" height="300">
    <p>Contact me here:</p>
    <input type="text">
    <br>
    <input type="text">
    <br>
    <input type="text">
    <br>
    <input type="submit" value="Send">

    <p id="contact">Here's where you can get in touch with me! Here, you can request for my resume, get more details on my experience, or learn more about my favorite hobbies! I will reply as soon as I am able, thanks!

  </div>
</body>

</html>

两个问题:

  • 我的 position: fixed 导航栏阻止了我的 anchor 标签(关于、投资组合、联系人),这意味着当我点击它们时,它会将我带到它们的位置,但是导航栏阻止启动。当我点击顶部的链接时,我怎样才能让它在我的导航栏下方开始?

  • 我正在使用 CSS li:visited {text-decoration: none;} 但它变为紫色并且仍然有下划线。为什么会这样?

最佳答案

要解决页内 anchor 和固定 header 的问题,您需要做的是创建一个 anchor 元素并将其相对定位在内容部分上方。

fiddle 示例:https://jsbin.com/dosalajotu/edit?html,css,output

例如,如果您的标题是 50px 高:

header { height: 50px; }
.anchor { position: relative; top: -50px; }

<section>
    <div id="about" class="anchor"></div>
</section>

对于 :visited 状态,这将应用于 a 元素,而不是 li 元素。如果您更新 CSS 选择器并覆盖颜色,应该可以解决问题。

li a:active, li a:visited { color: red; text-decoration: none; }

关于html - 位置 : fixed gets in way of anchor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809134/

相关文章:

javascript - 当使用 id 而不是值单击单选按钮时,jQuery 显示 div

javascript - 使 Magellan 侧边栏在移动设备中工作/移至页面底部

html - 如何让 body 取 100% min height 和 height as auto

javascript - 在 HTML5 中创建框架

html - Webkit 浏览器不会填满 div

jquery - 选项卡式 slider 轮播在 Bootstrap 4 中不起作用

html - 使用 django 在模板的各个位置包含常见的 html

javascript - 当 if 条件包含或 (||) 两个变量时,jQuery if/else 语句

php - 将超链接添加到 Wordpress 的 Simplicity-Lite 特色框元素

html - 如何用图像制作复选框