简体   繁体   English

CSS,相对定位元素中的绝对定位元素混乱

[英]css, absolute positioning elements within relative positioning elements cluttering up

I'm trying to position my elements with absolute positioning within my relative div element. 我正在尝试在相对div元素内以绝对定位来定位元素。 But it's cluttering up on eachother, as shown in this picture: 但是,彼此之间却杂乱无章,如下图所示:

在此处输入图片说明

Probably not the best picture. 可能不是最好的情况。 But there's two identical divs on top of eachother. 但是彼此之间有两个相同的div。

I wanted the second copy of place itself under the first one, and so on if there were more. 我希望将地点的第二个副本本身放在第一个副本之下,如果还有更多副本,依此类推。 And thought relative was the way to do it. 并且认为亲戚是这样做的方式。 But it seems like I'm getting complications of the divs inside the relative container, if they have absolute positioning? 但是,如果它们具有绝对位置,似乎在相对容器内的div越来越复杂了?

 <div class="industryoutter">
        <div class ="industryinnerleft">
        Agricultural Chemicals
        </div>
        <div class ="industryinnerright">
        C
        </div>
   </div>
   <div class="industryoutter">
        <div class ="industryinnerleft">
        Agricultural Chemicals
        </div>
        <div class ="industryinnerright">
        C
        </div>
   </div>

css CSS

.industryoutter {
    position: relative;
    background-color: #c0c0c0;
    width: 210;
}

.industryinnerleft {
    position: absolute;
    left: 5px;
}

.industryinnerright {
    position: absolute;
    right: 5px;
}

Using absolute positioning for things like this is just bad practice, it makes everything harder. 对这样的事情使用绝对定位只是不好的做法,这会使一切变得更困难。 The easiest thing you could do is to just float .industryinnerright right, like: 您最容易做的就是将.industryinnerright向右浮动,例如:

 .industryoutter { position: relative; background-color: #c0c0c0; width: 210px; overflow: hidden; } .industryinnerright { float: right } 
 <div class="industryoutter"> Agricultural Chemicals <span class="industryinnerright">C</span> </div> <div class="industryoutter"> Agricultural Chemicals <span class="industryinnerright">C</span> </div> 

Make sure you add overflow: hidden; 确保添加了overflow: hidden; to .industryoutter so it will grow to fit its content's height. .industryoutter以便它可以根据其内容的高度增长。 You also no longer need to wrap the left text in a div. 您也不再需要将左文本包装在div中。

Just set a fixed height in the container to work with your inner divs. 只需在容器中设置固定高度即可与内部div配合使用。

.industryoutter {
    position: relative;
    background-color: #c0c0c0;
    width: 210px;
    height: 50px;
}

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

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