2015-03-13 31 views
2

中區分div內的元素請看看我的代碼JSFIDDLE,我想將所有框放在div的中間。我該怎麼做?如何從下面的代碼

enter image description here

任何幫助,將不勝感激。

+0

你的問題不清楚。 – 2015-03-13 18:06:47

+0

我想將主框中的方框居中 – 2015-03-13 18:07:55

+0

您的意思是標題和標語? – 2015-03-13 18:10:33

回答

1

如果你的意思是水平中心.container,那麼,你應該使用float properti而是通過displayinline-blockinline-table把它們變成直列框。 然後在您的jsfiddle上申請:text-align:center父母,這裏body.grid-mainDEMO

body { 
 
    text-align:center; 
 
} 
 
legend { 
 
    display: block; 
 
    width: 323px; 
 
    margin: 0 auto 0 auto; 
 
    font-size: 20px; 
 
    font-family: sans-serif; 
 
} 
 
body { 
 
} 
 
.grid-main { 
 
    width: auto; 
 
    margin:auto; 
 
    line-height: 12px; 
 
    overflow: hidden; 
 
    /* border: 1px solid #000; */ 
 
} 
 
.grid-sub { 
 
} 
 
.thumb { 
 
    border-top-right-radius: 10px; 
 
    border-top-left-radius: 10px; 
 
    width: 323px; 
 
    height: 160px; 
 
    background-image: url(fashion.jpg); 
 
    background-size: 323px 160px; 
 
} 
 
.clearfix { 
 
    margin: 20px; 
 
} 
 
.container { 
 
    -webkit-box-shadow: 0px 0px 27px -6px rgba(138, 138, 138, 1); 
 
    -moz-box-shadow: 0px 0px 27px -6px rgba(138, 138, 138, 1); 
 
    box-shadow: 0px 0px 27px -6px rgba(138, 138, 138, 1); 
 
    width: 323px; 
 
    height: auto; 
 
    margin: 10pt; 
 
    border: 1px solid #c0c0c0; 
 
    border-radius: 10px; 
 
    border-top: none; 
 
    display:inline-block;/* NOT FLOAT */ 
 
    text-align:left; 
 
} 
 
.top-res-box-overlay { 
 
    background: rgba(0, 0, 0, .3); 
 
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(0, 0, 0, 0)), color-stop(1%, rgba(0, 0, 0, 0)), color-stop(100%, rgba(0, 0, 0, .65))); 
 
    background: -webkit-linear-gradient(top, rgba(0, 0, 0, 0)0, rgba(0, 0, 0, 0)1%, rgba(0, 0, 0, .65)100%); 
 
    background: -webkit-linear-gradient(top, rgba(0, 0, 0, 0)0, rgba(0, 0, 0, 0)1%, rgba(0, 0, 0, .65)100%); 
 
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0)0, rgba(0, 0, 0, 0)1%, rgba(0, 0, 0, .65)100%); 
 
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#a6000000', GradientType=0); 
 
    width: 100%; 
 
    height: 100%; 
 
    /*position: relative; 
 
*/ 
 
    display: block; 
 
    border-radius: 10px 10px 0 0; 
 
} 
 
div.header { 
 
    position: relative; 
 
    height: 160px; 
 
    width: 323px; 
 
} 
 
.box-name { 
 
    font-size: 13pt; 
 
    display: block; 
 
    color: #fff; 
 
    text-shadow: 0 0 1px rgba(0, 0, 0, .5); 
 
} 
 
.box-name-desc { 
 
    font-size: 10pt; 
 
    display: block; 
 
    color: #fff; 
 
    margin-top: 5px; 
 
    font-weight: 100; 
 
    text-shadow: 0 0 1px rgba(0, 0, 0, .5); 
 
} 
 
div.header div.title { 
 
    position: absolute; 
 
    bottom: 0px; 
 
    color: #FFF; 
 
    font-family: sans-serif; 
 
    font-weight: bold; 
 
    padding: 10px; 
 
} 
 
.content { 
 
    width: 100%; 
 
    /*border: 1px solid #c1c1c1; 
 
\t border-bottom-left-radius: 10px; 
 
\t border-bottom-right-radius: 10px;*/ 
 
} 
 
.bottom { 
 
    position: absolute; 
 
    bottom: 0; 
 
    left: 0; 
 
} 
 
.context { 
 
    padding: 10px; 
 
    font-size: 13px; 
 
    line-height: 150%; 
 
    color: #9a9a9a; 
 
    font-family: sans-serif; 
 
}
<body> 
 
    <div class="grid-main"> 
 
     <div class="container"> 
 
      <div class="thumb"> 
 
       <div class="header"> \t <a href="#" class="top-res-box-overlay"></a> 
 

 
        <div class="title"> \t <span class="box-name">Header</span> 
 
\t <span class="box-name-desc">Tagline</span> 
 

 
        </div> 
 
       </div> 
 
      </div> 
 
      <div class="content"> 
 
       <div class="context">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it</div> 
 
      </div> 
 
     </div> 
 
     <div class="container"> 
 
      <div class="thumb"> 
 
       <div class="header"> \t <a href="#" class="top-res-box-overlay"></a> 
 

 
        <div class="title"> \t <span class="box-name">Header</span> 
 
\t <span class="box-name-desc">Tagline</span> 
 

 
        </div> 
 
       </div> 
 
      </div> 
 
      <div class="content"> 
 
       <div class="context">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it</div> 
 
      </div> 
 
     </div> 
 
     <div class="container"> 
 
      <div class="thumb"> 
 
       <div class="header"> \t <a href="#" class="top-res-box-overlay"></a> 
 

 
        <div class="title"> \t <span class="box-name">Title</span> 
 
\t <span class="box-name-desc">Tagline</span> 
 

 
        </div> 
 
       </div> 
 
      </div> 
 
      <div class="content"> 
 
       <div class="context">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it</div> 
 
      </div> 
 
     </div> 
 
     <div class="container"> 
 
      <div class="thumb"> 
 
       <div class="header"> \t <a href="#" class="top-res-box-overlay"></a> 
 

 
        <div class="title"> \t <span class="box-name">Title</span> 
 
\t <span class="box-name-desc">Tagline</span> 
 

 
        </div> 
 
       </div> 
 
      </div> 
 
      <div class="content"> 
 
       <div class="context">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it</div> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</body>

+0

現在,我認爲對父母來說'text-align:center'是不必要的。 – Stickers 2015-03-13 18:56:29

+0

@sdcr它在元素上顯示爲行內框,邊距自動僅適用於表現爲塊元素的元素 – 2015-03-13 19:18:35

+0

OK沒有注意到您建議OP將這些塊轉換爲行內塊。 – Stickers 2015-03-13 19:32:17

-1

如果您知道寬度和「div.title」的高度,你可以把:

div.header div.title { 
    left: 50%; 
    top: 50%; 
    margin-left: -XXpx; 
    margin-top: -YYpx; 
} 

其中xx是寬度的一半,YY是高度的一半。

+0

你誤解了我的問題,我想把框放在中間,而不是標題! – 2015-03-13 18:09:54

2

的問題是.container浮點值,所以像這樣做:

.container { 
    float: left; /* remove this */ 
    margin: 10pt auto; /* add this */ 
} 

如果需要居中對齊inline element<span> <b> <i> ...)或inline- *元素,您可以將text-align: center;添加到其父容器。

如果您需要中心對齊block element<div> <p> ...),您可以將margin-left: auto; margin-right: auto;添加到元素本身。

+0

沒有改進,同樣的輸出 – 2015-03-13 18:31:08

+0

你可以編輯我的jsfiddle – 2015-03-13 18:31:24

+0

http://jsfiddle.net/dkw6vyau/2/ – Stickers 2015-03-13 18:32:08