简体   繁体   English

如何在 div 的顶部和底部显示两个元素,即 css 中的自动高度

[英]How to show two element at the top and bottom of the div which is auto height in css

I have two div one is left and one is right.我有两个 div,一个在左边,一个在右边。 and have two element in right div and I want to place one at the top of the div and another one is at the bottom.并在右侧 div 中有两个元素,我想将一个放在 div 的顶部,另一个放在底部。 the height of left element is auto so is right one and we can not fix the height because of dynamic content.左元素的高度是自动的,右元素的高度也是自动的,由于动态内容,我们无法固定高度。

 #mainDiv.list_desc { width: 80%; height: 100%; display: inline-block; vertical-align: top; } #mainDiv.list_desc.title { font-size: 44px; color: #000066; padding: 15px; } #mainDiv.list_desc.tag { width: 100%; padding: 15px; } #mainDiv.list_desc.tag.facility_tag { display: inline-block; text-align: left; width: 25%; font-size: 22px; color: #000066; } #mainDiv.list_desc.desc { font-size: 26px; padding: 15px; } #mainDiv.list_price { width: 19%; height: 100%; display: inline-block; border: 1px solid blue; } #mainDiv.list_price.top_price { height: 90%; border: 1px solid black; font-size: 48px; text-align: right; padding: 15px; } #mainDiv.list_price.bottom_button { height: 10%; border: 1px solid blue; } #mainDiv.list_price.bottom_button.select_room_btn { width: 100%; border: 1px solid #000066; text-align: center; font-size: 26px; }
 <div id="mainDiv"> <div class="list_section"> <div class="singlelist"> <div class="list_desc"> <div class="title">My title</div> <div class="tag"> <div class="facility_tag tag1">Tag1</div> <div class="facility_tag tag2">Tag2</div> <div class="facility_tag tag3">Tag3</div> </div> <div class="desc">If you have ever looked for new fonts to download, then 'the quick brown fox jumps over the lazy dog' is a sentence that you have probably seen before. This pangram is often seen next to fonts that are on display. The font's style is applied to the pangram, giving an example of what the font would look like in a sentence and also for the individual letters. This is one of the common uses of pangrams these days; to display different styles of fonts.If you have ever looked for new fonts to download, then 'the quick brown fox jumps over the lazy dog' is a sentence that you have probably seen before. This pangram is often seen next to fonts that are on display. The font's style is applied to the pangram, giving an example of what the font would look like in a sentence and also for the individual letters. This is one of the common uses of pangrams these days; to display different styles of fonts.If you have ever looked for new fonts to download, then 'the quick brown fox jumps over the lazy dog' is a sentence that you have probably seen before. This pangram is often seen next to fonts that are on display. The font's style is applied to the pangram, giving an example of what the font would look like in a sentence and also for the individual letters. This is one of the common uses of pangrams these days; to display different styles of fonts.</div> </div> <div class="list_price"> <div class="top_price">$100</div> <div class="bottom_button"> <div class="select_room_btn">Select This</div> </div> </div> </div> </div> </div>

Js fiddle: https://jsfiddle.net/pbvfr1hq/ js小提琴: https://jsfiddle.net/pbvfr1hq/

Select this button should be at the bottom of description in right div. Select 此按钮应位于右侧 div 的描述底部。

On the div that wrapps the items you want to adjust, say display: flex;在包装您要调整的项目的 div 上,说 display: flex; flex-direction: column;弹性方向:列; justify-content: space-between; justify-content: 之间的空格; *the first and last div will be placed on top and bottom *第一个和最后一个div将放在顶部和底部

(EDITED) Your CSS should look like this: (已编辑)您的 CSS 应如下所示:

  #mainDiv .list_desc{
   height:100%;display:inline-block;vertical-align:top;
  }
  #mainDiv .list_desc .title{
   font-size:44px;color:#000066;padding:15px;
  }
  #mainDiv .list_desc .tag{
   width:100%;padding:15px;
  }
  #mainDiv .list_desc .tag .facility_tag{
   display:inline-block;text- 
   align:left;width:25%;font-size:22px;color:#000066;
  }
  #mainDiv .list_desc .desc{
   font-size:26px;padding:15px;
  }

  #mainDiv .list_price{
   height:100%;display:inline-block;border:1px solid blue;
  }
  #mainDiv .list_price .top_price{
   border:1px solid black;font-size:48px;text-align:right;padding:15px;
  }
  #mainDiv .list_price .bottom_button{
    border:1px solid blue;
  }
  #mainDiv .list_price .bottom_button .select_room_btn{
    width:100%;border:1px solid #000066;text-align:center;font-size:26px;
  }

  #mainDiv .singlelist {
    display: -webkit-flex;
    display: -ms-flex;
    display: flex;
    justify-content: space-between;
  }

  #mainDiv .list_desc {
    -ms-flex: 0 0 80%;
    flex: 0 0 80%;
    max-width: 80%;
  }

  #mainDiv .singlelist .list_price{
    display: -webkit-flex;
    display: -ms-flex;
    display: flex;
    flex-direction: column;
    height: auto;
    -ms-flex: 0 0 20%;
    flex: 0 0 20%;
    max-width: 20%;
    border: none;
  }

  #mainDiv .singlelist .list_price .top_price{
    height: auto;
  }
  #mainDiv .singlelist .list_price .bottom_button{
    margin-top: auto;
  }

I think you can use display:grid to solve your problem.我认为您可以使用display:grid来解决您的问题。

 .singlelist { display: grid; grid-template-columns: 80% auto; grid-template-row: auto; column-gap: 0; row-gap: 0; } #mainDiv.list_desc { width: 100%; height: 100%; display: inline-block; vertical-align: top; } #mainDiv.list_desc.title { font-size: 44px; color: #000066; padding: 15px; } #mainDiv.list_desc.tag { width: 100%; padding: 15px; } #mainDiv.list_desc.tag.facility_tag { display: inline-block; text-align: left; width: 25%; font-size: 22px; color: #000066; } #mainDiv.list_desc.desc { font-size: 26px; padding: 15px; } #mainDiv.list_price { width: 100%; height: 100%; display: inline-block; border: 1px solid blue; display: flex; flex-direction: column; justify-content: space-between; flex-grow: 1; } #mainDiv.list_price.top_price { border: 1px solid black; font-size: 48px; text-align: right; padding: 15px; } #mainDiv.list_price.bottom_button { border: 1px solid blue; } #mainDiv.list_price.bottom_button.select_room_btn { width: 100%; border: 1px solid #000066; text-align: center; font-size: 26px; }
 <div id="mainDiv"> <div class="list_section"> <div class="singlelist"> <div class="list_desc"> <div class="title">My title</div> <div class="tag"> <div class="facility_tag tag1">Tag1</div> <div class="facility_tag tag2">Tag2</div> <div class="facility_tag tag3">Tag3</div> </div> <div class="desc">If you have ever looked for new fonts to download, then 'the quick brown fox jumps over the lazy dog' is a sentence that you have probably seen before. This pangram is often seen next to fonts that are on display. The font's style is applied to the pangram, giving an example of what the font would look like in a sentence and also for the individual letters. This is one of the common uses of pangrams these days; to display different styles of fonts.If you have ever looked for new fonts to download, then 'the quick brown fox jumps over the lazy dog' is a sentence that you have probably seen before. This pangram is often seen next to fonts that are on display. The font's style is applied to the pangram, giving an example of what the font would look like in a sentence and also for the individual letters. This is one of the common uses of pangrams these days; to display different styles of fonts.If you have ever looked for new fonts to download, then 'the quick brown fox jumps over the lazy dog' is a sentence that you have probably seen before. This pangram is often seen next to fonts that are on display. The font's style is applied to the pangram, giving an example of what the font would look like in a sentence and also for the individual letters. This is one of the common uses of pangrams these days; to display different styles of fonts.</div> </div> <div class="list_price"> <div class="top_price">$100</div> <div class="bottom_button"> <div class="select_room_btn">Select This</div> </div> </div> </div> </div> </div>

You will want to write media queries in order to make it responsive.您将需要编写媒体查询以使其具有响应性。

You can learn about grid property here: https://css-tricks.com/snippets/css/complete-guide-grid/您可以在此处了解grid属性: https://css-tricks.com/snippets/css/complete-guide-grid/

You can edit it at codepen .你可以在codepen编辑它。

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

相关问题 CSS:如何调整两个div对齐在底部,它们使用float属性以及自动的width和height - CSS: How to adjust two divs align at bottom which use float property and auto width and height CSS - 如何将页面的 div 高度从顶部拉伸到底部? - CSS - How to stretch div height from top to bottom of the page? 根据top()和bottom()在CSS中设置DIV的高度 - Set height of DIV in CSS based on top() and bottom() 如何使用jquery将div移动到元素的顶部,将另外两个移动到元素的底部 - How to use jquery to move a div to the top of a element and the other two to the bottom CSS相对div在自动高度中显示空间 - css relative div show space in auto height 如何保持顶部div高度固定,底部div高度为%age - How to keep the top div height fixed and bottom div height in %age CSS 自动填充顶部和底部以始终覆盖高度 - CSS auto padding top and bottom to always have the height covered CSS:仅使用“顶部”和“底部”属性设置内部div的高度 - css : set the height of inner div just by using 'top' and 'bottom' propreties CSS:是否有可能获得100%高度的div,而不是顶部和底部边距? - CSS: Is it possible to get a div that is 100% height, less a top and bottom margin? 如何仅使用CSS将DIV拉伸到高度为100%并溢出的网页底部? - How to keep DIV stretched to the bottom of the webpage with height 100% and overflow:auto using CSS only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM