最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Align position absolute element to the end of the relative element - Stack Overflow

programmeradmin1浏览0评论

I have a position absolute element which has a min-width. What I want to do is to align the absolute element's end to the relative element's end.

The relative element DOES NOT have a fixed width. The width can be vary depending on the content inside.

The use case here is, I'm building a custom dropdown. The relative element is the label which has the selected text and the position absolute element is the dropdown.

<div class="container">
 <div class="text">Text from list</div>
 <ul class="list">...</ul>
</div>

The image above has the look I'm expecting. What should I do to get this alignment? Can it be done with pure CSS?

I have a position absolute element which has a min-width. What I want to do is to align the absolute element's end to the relative element's end.

The relative element DOES NOT have a fixed width. The width can be vary depending on the content inside.

The use case here is, I'm building a custom dropdown. The relative element is the label which has the selected text and the position absolute element is the dropdown.

<div class="container">
 <div class="text">Text from list</div>
 <ul class="list">...</ul>
</div>

The image above has the look I'm expecting. What should I do to get this alignment? Can it be done with pure CSS?

Share Improve this question asked Feb 23, 2018 at 11:37 user3607282user3607282 2,5556 gold badges37 silver badges65 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

.relative-div {
	position:relative;
	min-height: 50px;
	background:#BB9A9B;
}
.abs-div {
	position: absolute;
	right: 0;
    top: calc(100% + 10px);
	min-width: 100px;
	min-height: 50px;
	background: red;
}
<div class="container">
	<div class="relative-div">
	relative-div
		<div class="abs-div"></div>
	</div>
</div>

.element {
    padding: 0.75rem 1rem;
    border: 1px solid navy;
    background-color: dodgerblue;
    color: white;
    margin: 10px 0px;
}

.parent-class {
    position: absolute;
    right: 0px;
}

.relative-element {
    position: relative;
 }

.absolute-element {
    position: absolute;
    min-width: 200px;
    right: 0px;
}

    <div class="parent-class">
      <div class="element relative-element">Relative Element</div>
      <div class="element absolute-element">Absolute Element</div>
    </div>

i would remend to use flex-box. here is a cheat-sheet https://css-tricks./snippets/css/a-guide-to-flexbox/

发布评论

评论列表(0)

  1. 暂无评论