我将我的div
与左边对齐,但是我对齐的div有些问题我不明白。当我调整我的div时,我发现他们没有正确对齐。 #39;我的代码错了?
body {
background: #ccc;
}
#box {
width: 960px;
margin: 20px auto;
}
.otel-list-box {
position: relative;
background: #FFF;
border-radius: 5px;
width: 47%;
float: left;
margin-bottom: 30px;
}
figure {
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.hotel-links {
padding: 15px 15px 18px 25px;
}
ul {
padding: 0;
margin: 0;
list-style-type: none;
}
.hotel-links ul li {
padding: 7px 0;
border-bottom: 1px solid #d6d6d6;
}
.hotel-links ul li a {
display: block;
font-size: 13px;
color: #000;
-webkit-transition: color linear 0.4s;
-moz-transition: color linear 0.4s;
transition: color linear 0.4s;
}
.otel-list-box .otel-list-content figure img {
border-radius: 5px 5px 0 0;
width: 100%;
border-bottom: 1px solid #faa82b;
height: 172px;
}
.otel-list-box:nth-child(2n) {
float: right;
}

<div id="box">
<div class="otel-list-box">
</div>
<!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div>
<!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div>
<!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div>
<!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div>
<!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div>
<!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div>
<!-- each item-->
</div>
&#13;
答案 0 :(得分:3)
这个问题有一些解决方案。
将display:flex
添加到#box
(框的容器),然后将width:calc(50% - 30px)
添加到otel-list-box
。 30px因为你将左右边距设置为每个框15px
请参阅下面的代码段或jsFiddle
body{
background:#ccc;
}
#box{
width:960px;
margin:20px auto;
display:flex;
flex-wrap:wrap;
}
.otel-list-box{
position: relative;
background: #FFF;
border-radius: 5px;
width: calc(50% - 30px);
margin:0 15px 30px;
}
figure{
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.hotel-links {
padding: 15px 15px 18px 25px;
}
ul {
padding: 0;
margin: 0;
list-style-type: none;
}
.hotel-links ul li {
padding: 7px 0;
border-bottom: 1px solid #d6d6d6;
}
.hotel-links ul li a {
display: block;
font-size: 13px;
color: #000;
-webkit-transition: color linear 0.4s;
-moz-transition: color linear 0.4s;
transition: color linear 0.4s;
}
.otel-list-box .otel-list-content figure img {
border-radius: 5px 5px 0 0;
width: 100%;
border-bottom: 1px solid #faa82b;
height: 172px;
}
.otel-list-box:nth-child(2n){
}
<div id="box">
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
<li><a href="#">Item 7</a></li>
<li><a href="#">Item 8</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
</div>
body{
background:#ccc;
}
#box{
width:960px;
margin:20px auto;
}
.otel-list-box{
position: relative;
background: #FFF;
border-radius: 5px;
}
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
flex-wrap:wrap;
}
figure{
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.hotel-links {
padding: 15px 15px 18px 25px;
}
ul {
padding: 0;
margin: 0;
list-style-type: none;
}
.hotel-links ul li {
padding: 7px 0;
border-bottom: 1px solid #d6d6d6;
}
.hotel-links ul li a {
display: block;
font-size: 13px;
color: #000;
-webkit-transition: color linear 0.4s;
-moz-transition: color linear 0.4s;
transition: color linear 0.4s;
}
.otel-list-box .otel-list-content figure img {
border-radius: 5px 5px 0 0;
width: 100%;
border-bottom: 1px solid #faa82b;
height: 172px;
}
.otel-list-box:nth-child(2n){
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="box" class=" row row-eq-height">
<div class="otel-list-box col-sm-6">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box col-sm-6">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
<div class="otel-list-box col-sm-6">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box col-sm-6">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
<div class="otel-list-box col-sm-6">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
<li><a href="#">Item 7</a></li>
<li><a href="#">Item 8</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box col-sm-6">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
</div>
display:table
。这更加棘手,因为你需要插入另一个div,它包含2 otel-list-box
es display:table
上的 #box
; display:table-row
上的boxRow
和display:table-cell
上的.otel-list-box
body{
background:#ccc;
}
#box{
width:960px;
margin:20px auto;
table-layout: fixed;
display:table;
}
.otel-list-box{
position: relative;
background: #FFF;
border-radius: 5px;
width: calc(50% - 30px);
margin:0 15px 30px;
display:table-cell;
}
.boxRow {
display:table-row
}
figure{
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.hotel-links {
padding: 15px 15px 18px 25px;
}
ul {
padding: 0;
margin: 0;
list-style-type: none;
}
.hotel-links ul li {
padding: 7px 0;
border-bottom: 1px solid #d6d6d6;
}
.hotel-links ul li a {
display: block;
font-size: 13px;
color: #000;
-webkit-transition: color linear 0.4s;
-moz-transition: color linear 0.4s;
transition: color linear 0.4s;
}
.otel-list-box .otel-list-content figure img {
border-radius: 5px 5px 0 0;
width: 100%;
border-bottom: 1px solid #faa82b;
height: 172px;
}
.otel-list-box:nth-child(2n){
}
<div id="box">
<div class="boxRow">
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
</div>
<div class="boxRow">
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
</div>
<div class="boxRow">
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
<li><a href="#">Item 7</a></li>
<li><a href="#">Item 8</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
</div>
</div>
var otel = $(".otel-list-box"),
highest = 0;
$(otel).each(function(){
if($(this).height() > highest){
highest = $(this).height();
}
});
$(otel).height(highest);
body{
background:#ccc;
}
#box{
width:960px;
margin:20px auto;
}
.otel-list-box{
position: relative;
background: #FFF;
border-radius: 5px;
width: calc(50% - 30px);
margin:0 15px 30px;
float:left;
}
figure{
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.hotel-links {
padding: 15px 15px 18px 25px;
}
ul {
padding: 0;
margin: 0;
list-style-type: none;
}
.hotel-links ul li {
padding: 7px 0;
border-bottom: 1px solid #d6d6d6;
}
.hotel-links ul li a {
display: block;
font-size: 13px;
color: #000;
-webkit-transition: color linear 0.4s;
-moz-transition: color linear 0.4s;
transition: color linear 0.4s;
}
.otel-list-box .otel-list-content figure img {
border-radius: 5px 5px 0 0;
width: 100%;
border-bottom: 1px solid #faa82b;
height: 172px;
}
.otel-list-box:nth-child(2n){
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box">
<div class="boxRow">
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
</div>
<div class="boxRow">
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
</div>
</div><!-- each item-->
</div>
<div class="boxRow">
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
<li><a href="#">Item 7</a></li>
<li><a href="#">Item 8</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
<div class="otel-list-box">
<div class="hotel-list-content">
<figure>
<img src="http://betacontent.anitur.com.tr/web/images/300x172/2017-04/otel_anasayfa_YBlzcqup5KUoDQG7CE8o2-cocuk-ucretsiz.png" />
</figure>
<div class="hotel-links">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
</ul>
</div>
</div>
</div><!-- each item-->
</div>
</div>