gpt4 book ai didi

html - 控制 flexbox 中的链接包装元素

转载 作者:太空宇宙 更新时间:2023-11-04 07:40:02 29 4
gpt4 key购买 nike

我有一个设置为 display: flex 的容器,在这个容器中是用链接包裹的元素。我想用它们的 div 类名而不是它们的 a 属性来控制元素。这是我的代码:

.container {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
width: 100%;
margin-bottom: 2rem;
justify-content: space-between;
flex-flow: wrap;
}

.container a {
background-color: red;
width: 48%;
margin-bottom: 2%;
}
<div class="container">
<a href="google.com"><div class="item">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div></a>
<a href="google.com"><div class="item">It is a long established fact that a reader will be distracted by the readable content of a page.</div></a>
</div>

这很完美,但是,我想用 div 类名项替换 .container a。这样做的原因是在维护代码时,我认为这会使查看我的 CSS 的人感到困惑。我怎样才能做到这一点,以便我可以将选择器 .container a 替换为 .item 并且它仍然可以正常工作?注意:整个 div block 必须是一个链接。这是 jsFiddle

最佳答案

您可以将这些类添加到您的 A 标签中。甚至完全放弃 div。

.container {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
/*width: 100%; by default*/
margin-bottom: 2rem;
justify-content: space-between;
flex-flow: wrap; /* has no effect since you're using % for flex-items width */
}

.item {
background-color: red;
width: 48%;
margin-bottom: 2%;
}
<div class="container">
<a class="item" href="google.com">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</a>
<a class="item" href="google.com">It is a long established fact that a reader will be distracted by the readable content of a page.</a>
</div>

关于html - 控制 flexbox 中的链接包装元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48614120/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com