锚点标记下划线未在具有突出显示动画的文本下对齐 编辑

如何解决锚点标记下划线未在具有突出显示动画的文本下对齐 编辑

我试图让我的锚标签执行动画处理,其中下划线将鼠标悬停在上面时突出显示上面的文本。不知道为什么下划线这么远位于左侧。关于可能导致此问题的原因有什么想法?

我尝试将容器类更改为position:absolute,然后将内容设置为相对,但这也不起作用。

.section-title {
  display: block;
  font-size: 2em;
  text-align: center;
  padding-bottom: 1.5%;
  font-size: 2em;
  transform: translateX(0%) translateY(-25%);
  /* z-index: -1; */
}

.section-title-highlight {
  color: black;
}

.section-title-highlight:before {
  content: '';
  display: block;
  height: 20px;
  width: 20%;
  background: #35FCCE;
  position: absolute;
  bottom: 0;
  left: 0;
  transition: height .7s;
  z-index: -1;
}

.section-title-highlight:hover:before {
  height: 85%;
}

a {
  text-decoration: none;
  cursor: default;
  display: block;
}

.container {
  padding-top: 3%;
  padding-left: 5%;
  padding-right: 5%;
  padding-bottom: 7%;
  margin-top: 0;
  margin-left: 5%;
  margin-right: 5%;
  margin-bottom: 5%;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  background-color: rgb(236,236,236);
}

.container-item {
  display: flex;
  flex-direction: column;
}

#home {
  height: 500px;
  /* margin-bottom: 0; */
  position: relative;
}

#projects {
  height: 600px;
  position: relative;
}
<div class="container">
  <div id="projects">
    <a href="#" class="section-title-highlight section-title">Projects</a>
    <div class="container-item">
      <p>
        asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
      </p>
    </div>
  </div>

解决方法

您正在突出显示块上使用position:absolute,并将其放置在您指定的确切位置,相对于具有{em> position:relative的父容器。

您的突出显示应该相对于section-title-highlight中的文本,因此您只需要像这样相对设置即可:

.section-title-highlight {
  position:relative;
}

您的突出显示元素现在位于文本的左下方:

.section-title-highlight:before {
  height: 2px;      /* or whatever your starting height should be */
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  /* rest of your css here... */ 
}

您遇到的另一个问题是突出显示的内容比文本要宽。这是因为标题是一个块元素,所以它会拉伸其容器的整个宽度。

您可以通过将高亮显示为inlineinline-block元素来解决此问题。在下面的示例中,我将您的section-title设置为一个块元素,并将您的section-title-highlight内联:

 <div class="section-title">
     <a class="section-title-highlight">Projects</a>
 </div>

工作示例:

.section-title {
  display: block;
  text-align: center;
  padding-bottom: 1.5%;
  font-size: 2em;
  transform: translateX(0%) translateY(-25%);
  /* z-index: 0; */
}

.section-title-highlight {
  position: relative;
  color: black;
}

.section-title-highlight:before {
  content: '';
  display: block;
  height: 2px;  /* or whatever height you want to start */
  width: 100%;
  background: #35FCCE;
  position: absolute;
  bottom: 0;
  left: 0;
  transition: height .7s;
  z-index: -1;
}

.section-title-highlight:hover:before {
  height: 85%;
}

a {
  text-decoration: none;
  cursor: default;
  display: block;
}

.container {
  padding-top: 3%;
  padding-left: 5%;
  padding-right: 5%;
  padding-bottom: 7%;
  margin-top: 0;
  margin-left: 5%;
  margin-right: 5%;
  margin-bottom: 5%;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  background-color: rgb(236,236,236);
}

.container-item {
  display: flex;
  flex-direction: column;
}

#home {
  height: 500px;
  /* margin-bottom: 0; */
  position: relative;
}

#projects {
  height: 600px;
  position: relative;
}
<div class="container">
  <div id="projects">

    <a href="#" class="section-title-highlight section-title">Projects</a>
      <p>The heading above is using a block display (as in your code),so it is appearing the full width of the container.</p>
    <div class="section-title">
      <span class="section-title-highlight">Projects</span>
    </div>
    <div class="container-item">
      <p>
        This heading is using an inline element so the highlight is only the width of the text
      </p>
    </div>
  </div>
</div>

,

我相信您只需更改以下内容即可得到结果:

.section-title-highlight:before {
  width: 20%;
}

对此:

.section-title-highlight:before {
  width: 100%;
}

关于“伪造文本宽度”的讨论可以归结为:div.projects的宽度取决于文本的宽度。 a::before的宽度由div.project的宽度确定。如此,将a::before的{​​{1}}属性设置为100%会将其设置为文本的宽度。

如您所料,也有必要绝对定位width元素。如果不这样做,它将在流中,占据其容器的100%,然后将文本包装到下一行。如果您尝试从其中删除a::before,这对您将很明显。

将容器类更改为绝对容器,将元素更改为相对容器,则只能将容器绝对定位在相对放置的DOM树中的下一个元素中(如果更有意义,则为“容器层次结构”)。所以不,那是行不通的。

这是代码:

position: absolute
    .section-title {
      display: inline-block;
      font-size: 2em;
      text-align: center;
      padding-bottom: 1.5%;
      font-size: 2em;
      transform: translateX(0%) translateY(-25%);
      /* z-index: -1; */
    }

    .section-title-highlight {
      color: black;
    }

    .section-title-highlight:before {
      content: '';
      display: inline-block;
      height: 20px;
      width: 100%;
      background: #35FCCE;
      position: absolute;
      bottom: 0;
      left: 0;
      transition: height .7s;
      z-index: -1;
    }

    .section-title-highlight:hover:before {
      height: 85%;
    }

    a {
      text-decoration: none;
      cursor: default;
    }

    .container {
      padding: 3% 5% 7%;
      margin: 0 5% 5%;
      display: flex;
      flex-direction: column;
      align-items: center;
      background-color: rgb(236,236);
    }

    .container-item {
      display: flex;
      flex-direction: column;
    }

    #projects {
      height: 600px;
      position: relative;
    }  

    .projects-header {
      text-align: center;
    }

<div class="container"> <div id="projects"> <div class="projects-header"> <a href="#" class="section-title-highlight section-title">Projects</a> </div> <div class="container-item"> <p> Edit: the problem was that the highlight on the title would expand to be as wide as this paragraph,which was not apparent when only "asdf" was in it.. </p> </div> </div> </div>上进行一些澄清可能会有所帮助。 position的默认值为position。静态定位的元素将放置在页面流中的任何位置,就像它们是文本段落一样。相对定位的元素相对于其静态位置进行定位,其中statictop偏离静态位置。因此,例如,假设您给left赋予了div.projects,那么它的位置将比没有指定任何top: -10px值(即位置类型)的位置高10px是静态的。)

除非另有说明,否则绝对定位的元素位于positiontop: 0相对定位的容器内。因此,您不必严格按照left: 0风格使用top: 0。您必须使用a::before,否则栏将位于文本的顶部而不是底部。

编辑

OP和FluffyKitten在注释中描述的问题的修复程序(很好,我的修复程序)需要两个步骤。

  1. HTML的结构使得锚元素被视为flex单元。因此,其宽度与段落的宽度相关。因此,第一步是将锚标记包装在div中。 (作为bottom: 0。)
  2. anchor标签处于块显示状态,这意味着它会占据其容器的整个宽度。解决此问题的方法是将div.project-header风格的display:block更改为.section-title
,

您所要做的就是不要使用亲戚的头衔,您会没事的。

这将使用问题的原始代码,并更改框的宽度以缩小高度,并添加一个从下到下的过渡。

    .section-title {
      display: block;
      font-size: 2em;
      text-align: center;
      padding-bottom: 1.5%;
      font-size: 2em;
      transform: translateX(0%) translateY(-25%);
      /* z-index: -1; */
    }

    .section-title-highlight {
      color: black;
    }

    .section-title-highlight:before {
      content: '';
      display: block;
      height: 5px;
      width: 100%;
      background: #35FCCE;
      position: absolute;
      margin-bottom: .2em;
      bottom: 0;
      left: 0;
      transition: margin-bottom .5s,height .7s;
      z-index: -1;
    }

    .section-title-highlight:hover:before {
      height: 85%;
      margin-bottom:0;
    }

    a {
      text-decoration: none;
      cursor: default;
      display: block;
    }

    .container {
      padding-top: 3%;
      padding-left: 5%;
      padding-right: 5%;
      padding-bottom: 7%;
      margin-top: 0;
      margin-left: 5%;
      margin-right: 5%;
      margin-bottom: 5%;
      display: flex;
      flex-direction: column;
      align-items: center;
      height: 100%;
      background-color: rgb(236,236);
    }

    .container-item {
      display: flex;
      flex-direction: column;
    }

    #home {
      height: 500px;
      /* margin-bottom: 0; */
      position: relative;
    }

    #projects {
      height: 2em;
      position: relative;
    }
    <div class="container">
      <div id="projects">
        <a href="#" class="section-title-highlight section-title">Projects</a>
        <div class="container-item">
        </div>
      </div>
      <p>
            asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
          </p>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-