簡體   English   中英

如何使這些div底部對齊?

[英]How to bottom-align these divs?

小提琴

HTML

<div id="page"><div id="results"><div class="result">1+2</div>
<div class="result">3*4</div><div class="result">5/9</div>
<div class="result">y=mx+b</div></div><input id="eq" type="text" autofocus=""></div>

CSS

@import url(http://fonts.googleapis.com/css?family=Rokkitt);
body {
  background-color: #444047;
  margin: 0;
  padding: 0;
}
#page {
  width: 40%;
  background-color: #ececec;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 50%;
  margin-left: -20%;
}
#results {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 75px;
}
#eq {
  width: 96%;
  margin-left: -48%;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -webkit-border-radius: 20px;
  -moz-border-radius: 20px;
  -ms-border-radius: 20px;
  -o-border-radius: 20px;
  border-radius: 20px;
  border-width: 2px;
  border-color: #9A9A9A #EEEEEE #EEEEEE #9A9A9A;
  border-style: solid;
  font-family: 'Rokkitt', serif;
  letter-spacing: 1px;
  font-size: 24pt;
  padding: 5px 10px;
  position: absolute;
  bottom: 10px;
  left: 50%;
}
#eq:focus {
  border-color: #4E9FF2 #84B2E0 #84B2E0 #4E9FF2;
  -webkit-box-shadow: 0 0 10px #007eff;
  -moz-box-shadow: 0 0 10px #007eff;
  -ms-box-shadow: 0 0 10px #007eff;
  -o-box-shadow: 0 0 10px #007eff;
  box-shadow: 0 0 10px #007eff;
  outline: none;
}
#results {
  font-family: 'Rokkitt', serif;
  font-size: 16pt;
  vertical-align: bottom;
  text-align: left;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  padding: 5px;
  display: table-cell;
  overflow: auto;
}
.result {
  padding: 2px;
}
.result + .result {
  border-top: 1px dotted gray;
}

我該如何調整的.result的div的底部#result容器?

編輯:如果有太多.result不能容納,則#results div需要獲得一個滾動條。 overflow:auto似乎與下面的一些解決方案不兼容。

讓我們清理混亂,包裹內部容器, position: absolute; position: relative; 容器,我是從頭開始制作的,您可以看一下

演示

<div class="wrap">
    <div class="inner_wrap">
        <span>1+2</span>
        <span>3*4</span>
        <span>5/9</span>
        <span>y=mx+b</span>
        <span><input id="eq" type="text" autofocus="" /></span>
    </div>
</div>

CSS(不包括input元素樣式,但我已在演示小提琴中將其包括在內)

html, body {
    background-color: #444047;
    height: 100%;
}

.wrap {
    width: 230px;
    background: #ECECEC;
    height: 100%;
    margin: auto;
    position: relative;
}

.wrap span {
    display: block;
    border-bottom: 1px dotted #515151;
    padding: 5px;
    font-size: 18px;
    font-family: Arial;
}

div span:last-child {
    border-bottom: 0;    
}

.inner_wrap {
    position: absolute;
    bottom: 0;
}

這是一個不會破壞您的overflow: auto的版本overflow: auto

這是一個jsFiddle

基本上,我正在做的是將您的標記包裝在足夠的div ,這樣我就可以通過display: table;使包裝器div表現得像一個display: table; display: table-row; display: table-cell; CSS屬性。

現在,包裝標記的行為類似於表格,我可以將div絕對定位在頂部“行”的“單元格”內,並將其設置為max-height100%並固定在底部,並且其overflow設置為auto 這與我在此處發布的原始答案中使用的技術完全相同。 但是現在max-height 100%將保留在table的第一行內,從而消除了您在我之前發布的解決方案中指出的問題。

以下是相關的CSS類:

#page {
    width: 40%;
    background-color: #ececec;
    position: fixed;
    bottom: 0;
    left: 50%;
    margin-left: -20%;
    display: table;
    height: 100%;
}
#results, #input {
    display: table-row;
}
#results > div {
    display: table-cell;
    height: 100%;
    position:relative;
}
#results > div > div {
    overflow: auto;
    max-height: 100%;
    width: 100%;
    position: absolute;
    bottom: 0px;
}
#input {
    height: 75px;
}
#results {
    font-family:'Rokkitt', serif;
    font-size: 16pt;
    vertical-align: bottom;
    text-align: left;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    overflow: auto;
    height: 100%;
}

這是經過編輯的HTML:

<div id="page">
    <div id="results">
        <div>
            <div>
                <div class="result">1+2</div>
                <div class="result">3*4</div>
                <div class="result">5/9</div>
                <div class="result">y=mx+b</div>
                <div class="result">y=mx+b</div>
                <div class="result">y=mx+b</div>
                <div class="result">y=mx+b</div>
                <div class="result">y=mx+b</div>
                <div class="result">y=mx+b</div>
                <div class="result">y=mx+b</div>
                <div class="result">y=mx+b</div>
                <div class="result">1+2</div>
            </div>
        </div>
    </div>
    <div id="input">
        <input id="eq" type="text" autofocus="" />
    </div>
</div>

演示: http : //jsfiddle.net/H2qBH/1/您想要這樣嗎.....

  #results {
  position: absolute;
top: 275px;
right: 0;
left: 0;
bottom:0px;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM