简体   繁体   中英

Css: paragraph at bottom of a div

i have this html structure:

 .grid-container { display: grid; grid-template-columns: auto auto; } .grid-left { border-bottom: 3px solid #f2f2f2; margin-bottom: 8px; padding-top:4px; padding-bottom:4px; } .grid-right{ border-bottom: 3px solid #f2f2f2; margin-bottom: 8px; padding-bottom:4px; padding-left: 15px; } .sub-grid-container { display: inline-grid; grid-template-rows: 26px auto auto; border-bottom: 3px solid #f2f2f2; margin-bottom: 8px; padding-bottom: 4px; padding-top: 3px; } .grid-text{ padding-top: 10px; font-size: 13px; color: #505050; } 
 <div class="grid-container"> <div class="grid-left"> <div class="sub-grid-container"> <div class="negozio"> Amazon</div> <div class="img">Image</div> <div class="fill"></div> </div> </div> <div class="grid-right"> some text here .... <p> Date xx/xx/xx</p> </div> </div> 

I want to have the paragraph p with the date at the bottom of the div, i tried using position absolute and relative but i have some problem like you see in the image 在此处输入图片说明 how can i fix this?

You will need to use position:relative so that the div knows you are going to position another element inside that div.

Give your .grid-container a height (just for demo purposes)

.grid-cotainer { 
  display: grid;
  grid-template-columns: auto auto;
  height: 200px }

Then tell the 'container' div .grid-right it needs relative positioning.

.grid-right {  
  border-bottom: 3px solid #f2f2f2;
  margin-bottom: 8px;
  padding-bottom: 4px;
  padding-left: 15px;
  position: relative }

Then you can use what you used, position:absolute to keep it at the bottom.

.grid-right p:not(.not-at-bottom) {
  bottom: 0;
  position:absolute }

You could even make use of not . An identifier so that if you do not want to place a p tag at the bottom inside your .grid-right div, it knows not to.

https://jsfiddle.net/9rdbd4j6/

Try this:

<div class="grid-container">
  <div class="grid-left">
    <div class="sub-grid-container">
      <div class="negozio"> Amazon</div>
      <div class="img">Image</div>
      <div class="fill"></div>
    </div>
  </div>
  <div class="grid-right"> some text here ....
    <p> Date xx/xx/xx</p>
  </div>
</div>

And this CSS:

.grid-container {
  display: grid;
  grid-template-columns: auto auto;
}

.grid-left {
  border-bottom: 3px solid #f2f2f2;
  margin-bottom: 8px;
  padding-top: 4px;
  padding-bottom: 4px;
}

.grid-right {
  position: relative;
  border-bottom: 3px solid #f2f2f2;
  margin-bottom: 8px;
  padding-bottom: 24px;
  padding-left: 15px;
}

.grid-right p {
  position: absolute;
  bottom: 0;
  margin-bottom: 0;
}

.sub-grid-container {
  display: inline-grid;
  grid-template-rows: 26px auto auto;
  border-bottom: 3px solid #f2f2f2;
  margin-bottom: 8px;
  padding-bottom: 4px;
  padding-top: 3px;
}

.grid-text {
  padding-top: 10px;
  font-size: 13px;
  color: #505050;
}

Or run it here: https://jsfiddle.net/ur4jb9pc/ Basically:

  • Set parent div with position: relative
  • Set child p with position: absolute
  • Set child p with bottom: 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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