简体   繁体   English

容器底部的 Position 元素没有 flex 或 position absolute

[英]Position element at the bottom of container without flex or position absolute

I'm trying to make a component that uses both a background color for half of the component and an image for the other half, with the image acting as an element with a margin.我正在尝试制作一个组件,该组件的一半使用背景颜色,另一半使用图像,图像充当带有边距的元素。 The problem I'm having is that I cannot use display flex to space elements correctly in the content area, it breaks the component with the current solution I'm using and I have no idea why.我遇到的问题是我无法使用 display flex 在内容区域中正确地分隔元素,它破坏了我正在使用的当前解决方案的组件,我不知道为什么。

I'm specifically trying to position.service__link at the bottom of.service__content.我专门尝试 position.service__link 在.service__content 的底部。 I've tried using paddings/margins to get it in place, but at smaller screen sizes it ends up overflowing out of the box.我试过使用填充/边距来固定它,但在较小的屏幕尺寸下它最终会溢出盒子。

 *, *::before, *::after { box-sizing: border-box; } * { padding: 0; margin: 0; } body { min-height: 100vh; font-family: Arial; }.service-container { max-width: 1800px; margin-inline: auto; padding: 40px 15px; display: flex; flex-wrap: wrap; justify-content: center; gap: 1.5rem; }.service { flex: 1 1 calc(33.3336% - 1.5rem); display: inline-block; position: relative; background: #144088; min-height: 325px; border: 2px solid #144088; color: white; }.service::before { content: ""; float: right; width: 100%; height: 100%; background: url(https://images.unsplash.com/photo-1645069258059-6f5a71256c4a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2076&q=80) left center/150%; shape-outside: polygon(79% 0, 100% 0, 100% 100%, 42% 100%); clip-path: polygon(79% 0, 100% 0, 100% 100%, 42% 100%); shape-margin: .3em; }.service::after { content: "01"; position: absolute; top: 0; font-size: 60px; font-weight: bold; line-height: 1; opacity: 25%; }.service__content { padding: 3rem 0 1rem 1rem; height: 100%; }.service__title { font-size: 26px; }.service__description { font-size: clamp(12px, 3vw +.25rem, 16px); }.service__title, .service__description { margin-bottom: 1rem; }.service__link { display: inline-flex; justify-content: space-between; align-items: center; width: 35%; }.service__link a { color: white; }.service__link img { filter: brightness(0)invert(1); } @media screen and (max-width: 1200px) {.service { flex: 0 1 518px; } }
 <body> <div class="service-container"> <div class="service"> <div class="service__content"> <h4 class="service__title">Lorem</h4> <p class="service__description">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Placeat doloremque molestiae eligendi voluptas veniam repellendus, aperiam tempora suscipit consectetur sint?</p> <div class="service__link"> <a href="#">Link</a> <img src="https://img.icons8.com/ios/26/000000/image.png"/> </div> </div> </div> <div class="service"> <div class="service__content"> <h4 class="service__title">Lorem</h4> <p class="service__description">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Placeat doloremque molestiae eligendi voluptas veniam repellendus, aperiam tempora suscipit consectetur sint?</p> <div class="service__link"> <a href="#">Link</a> <img src="https://img.icons8.com/ios/26/000000/image.png"/> </div> </div> </div> <div class="service"> <div class="service__content"> <h4 class="service__title">Lorem</h4> <p class="service__description">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Placeat doloremque molestiae eligendi voluptas veniam repellendus, aperiam tempora suscipit consectetur sint?</p> <div class="service__link "> <a href="#">Link</a> <img src="https://img.icons8.com/ios/26/000000/image.png"/> </div> </div> </div> </div> </body>

What is going on when display: flex is added to.service__content?将 display: flex 添加到 .service__content 时发生了什么?

Also, if anybody has a better idea of how to make this component in general I'd love to hear it.另外,如果有人对一般情况下如何制作此组件有更好的想法,我很想听听。

Thanks!谢谢!

I think you could use this: position: absolute; bottom: 30px我想你可以使用这个: position: absolute; bottom: 30px position: absolute; bottom: 30px to put it in the bottom part of the div. position: absolute; bottom: 30px放在div的底部。

If you set service__link to flex instead of inline flex and add margin-top auto it should stay on the bottom如果您将 service__link 设置为 flex 而不是 inline flex 并添加 margin-top auto 它应该留在底部

you have to give position: relative for container你必须给position: relative对于容器

than for service__link position: absolute;比 service__link position: absolute; left:10px; bottom:10px; there will be no deterioration in smaller screen.较小的屏幕不会有任何恶化。

.service__content {
padding: 3rem 0 1rem 1rem;
height: 100%;
position: relative; // Added
}

.service__link {
display: inline-flex;
justify-content: space-between;
align-items: center;
width: 35%;
position: absolute; // Added
left:10px;          // Added
bottom:10px;        // Added    
}

 *, *::before, *::after { box-sizing: border-box; } * { padding: 0; margin: 0; } body { min-height: 100vh; font-family: Arial; }.service-container { max-width: 1800px; margin-inline: auto; padding: 40px 15px; display: flex; flex-wrap: wrap; justify-content: center; gap: 1.5rem; }.service { flex: 1 1 calc(33.3336% - 1.5rem); display: inline-block; position: relative; background: #144088; min-height: 325px; border: 2px solid #144088; color: white; }.service::before { content: ""; float: right; width: 100%; height: 100%; background: url(https://images.unsplash.com/photo-1645069258059-6f5a71256c4a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2076&q=80) left center/150%; shape-outside: polygon(79% 0, 100% 0, 100% 100%, 42% 100%); clip-path: polygon(79% 0, 100% 0, 100% 100%, 42% 100%); shape-margin: .3em; }.service::after { content: "01"; position: absolute; top: 0; font-size: 60px; font-weight: bold; line-height: 1; opacity: 25%; }.service__content { padding: 3rem 0 1rem 1rem; height: 100%; position:relative; }.service__title { font-size: 26px; }.service__description { font-size: clamp(12px, 3vw +.25rem, 16px); }.service__title, .service__description { margin-bottom: 1rem; }.service__link { display: inline-flex; justify-content: space-between; align-items: center; width: 35%; position:absolute; left:10px; bottom:10px; }.service__link a { color: white; }.service__link img { filter: brightness(0)invert(1); } @media screen and (max-width: 1200px) {.service { flex: 0 1 518px; } }
 <body> <div class="service-container"> <div class="service"> <div class="service__content"> <h4 class="service__title">Lorem</h4> <p class="service__description">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Placeat doloremque molestiae eligendi voluptas veniam repellendus, aperiam tempora suscipit consectetur sint?</p> <div class="service__link"> <a href="#">Link</a> <img src="https://img.icons8.com/ios/26/000000/image.png"/> </div> </div> </div> <div class="service"> <div class="service__content"> <h4 class="service__title">Lorem</h4> <p class="service__description">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Placeat doloremque molestiae eligendi voluptas veniam repellendus, aperiam tempora suscipit consectetur sint?</p> <div class="service__link"> <a href="#">Link</a> <img src="https://img.icons8.com/ios/26/000000/image.png"/> </div> </div> </div> <div class="service"> <div class="service__content"> <h4 class="service__title">Lorem</h4> <p class="service__description">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Placeat doloremque molestiae eligendi voluptas veniam repellendus, aperiam tempora suscipit consectetur sint?</p> <div class="service__link "> <a href="#">Link</a> <img src="https://img.icons8.com/ios/26/000000/image.png"/> </div> </div> </div> </div> </body>

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

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