简体   繁体   English

如何让伪元素尊重直接父元素的宽度并忽略兄弟元素的宽度

[英]How to get pseudo element to respect immediate parent's width and ignore sibling's width

I have a few columns of items that when hovered will display an animated underline effect.我有几列项目,当它们悬停时会显示动画下划线效果。 The issue I am having is that the item in each column with the most text is setting the width for all of it's siblings.我遇到的问题是每列中文本最多的项目是为其所有兄弟设置宽度。 For example, two items in the same column will have the same pseudo element width, even if one of the item's content is much shorter than the other.例如,同一列中的两个项目将具有相同的伪元素宽度,即使其中一个项目的内容比另一个短得多。

 .wrap { display: flex; justify-content: center; align-items: flex-start; margin-top: 20px; } .container { display: flex; flex-direction: column; margin: 0 3vw; } .item { margin: 10px 0; text-decoration: none; color: #444; font-weight: bold; font-size: 19px; letter-spacing: 2px; position: relative; display: block; font-size: 12px; } .item::after { position: absolute; display: block; content: ''; width: 100%; left: 0; transform: scaleX(0); bottom: 0; transition: 0.4s transform ease-in-out; border-bottom: solid 2px tomato; transform-origin: 100% 50%; } .item:hover::after { transform: scaleX(1); transform-origin: 0 50%; }
 <div class="wrap"> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> </div>

You can just add align-items: flex-start on .container so the items will take the width of their content:您可以在.container上添加align-items: flex-start以便项目将采用其内容的宽度:

 .wrap { display: flex; justify-content: center; align-items: flex-start; margin-top: 20px; } .container { display: flex; flex-direction: column; margin: 0 3vw; align-items: flex-start; } .item { margin: 10px 0; text-decoration: none; color: #444; font-weight: bold; font-size: 19px; letter-spacing: 2px; position: relative; display: block; font-size: 12px; } .item::after { position: absolute; display: block; content: ''; width: 100%; left: 0; transform: scaleX(0); bottom: 0; transition: 0.4s transform ease-in-out; border-bottom: solid 2px tomato; transform-origin: 100% 50%; } .item:hover::after { transform: scaleX(1); transform-origin: 0 50%; }
 <div class="wrap"> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> </div>

One Possible solution is to add margin-right:auto to all .item一种可能的解决方案是将margin-right:auto添加到所有.item

 .wrap { display: flex; justify-content: center; align-items: flex-start; margin-top: 20px; } .container { display: flex; flex-direction: column; margin: 0 3vw; } .item { margin: 10px 0; text-decoration: none; color: #444; font-weight: bold; font-size: 19px; letter-spacing: 2px; position: relative; font-size: 12px; display: block; /* Not really needed */ margin-right: auto; /* Added */ /* to visualize */ border: 1px solid; padding: 10px; } .item::after { position: absolute; display: block; content: ''; width: 100%; left: 0; transform: scaleX(0); bottom: 0; transition: 0.4s transform ease-in-out; border-bottom: solid 2px tomato; transform-origin: 100% 50%; } .item:hover::after { transform: scaleX(1); transform-origin: 0 50%; }
 <div class="wrap"> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> </div>

display:block on .item is causing this... for the effect that you want, have each item inside a ul li as in the first column/container of the code below: display:block on .item导致了这个......为了你想要的效果,将每个项目放在一个ul li如下面的代码的第一列/容器

 .wrap { display: flex; justify-content: center; align-items: flex-start; margin-top: 20vh; } .container { display: block; flex-direction: column; margin: 0 3vw; } .container:nth-child(2) .item, .container:nth-child(3) .item { display: block; } .item { margin: 10px 0; text-decoration: none; color: #444; font-weight: bold; font-size: 19px; letter-spacing: 2px; position: relative; } .item::after { position: absolute; display: inline-block; content: ''; width: 100%; left: 0; transform: scaleX(0); bottom: 0; transition: 0.4s transform ease-in-out; border-bottom: solid 2px tomato; transform-origin: 100% 50%; } .item:hover::after { transform: scaleX(1); transform-origin: 0 50%; } .container ul li { list-style: none; margin: 10px 0; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <div class="wrap"> <div class="container"> <ul> <li> <a href="#" class="item">Long Phrase Goes Here</a> </li> <li> <a href="#" class="item">Test Test</a> </li> <li> <a href="#" class="item">Test Test</a> </li> <li> <a href="#" class="item">Test Test</a> </li> <li> <a href="#" class="item">Test Test</a> </li> </ul> </div> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> <div class="container"> <a href="#" class="item">Long Phrase Goes Here</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> <a href="#" class="item">Test Test</a> </div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM