2017-02-14 1 views
1

나는 원하는 결과를 얻었습니다. 그러나 올바른 방법은 아닙니다. 예를 들어 부모 컨테이너가 너비를 변경하면이 해킹이 작동하지 않습니다. 그러나 브라우저에서 올바른 방법을 시도하고 해결하기 위해 화면에 표시하기 위해이 작업을 수행했습니다.상대 부모 컨테이너 내에 절대 버튼을 중앙에 배치하는 적절한 방법

See screenshot here

HTML 내 엉뚱한 간격에 대한

<div class="container"> 
       <div class="row"> 
        <div class="col-md-4"> 
         <div class="product-wrapper"> 
          <div class="product-card"> 
           <a href="lathes-single.html" class="product-img-wrapper"><img src="../assets/img/46-455.jpg" alt=""></a> 
           <h4> 46-460 12 1/2 in. Variable Speed MIDI-LATHE® </h4> 
           <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p> 
           <a href="lathes-single.html" class="btn btn-lg btn-primary">View Product</a> 
          </div> 
         </div> 
        </div> 
       </div> 
</div> 

죄송합니다. Sublime Text 3을 붙여 넣는 몇 가지 이유 때문에 여기에 모든 것이 다 들어 있습니다. 대신 .btn에이

관련 CSS

.product-img-wrapper { 
    text-align: center; 
} 

.product-img-wrapper img { 
    width: 200px; 
    height: 200px; 
} 

.product-wrapper { 
    position: relative; 
    margin: 50px 0; 
} 

.product-card { 
    position: relative; 
    max-width: 330px; 
    height: 450px; 
    border: 1px solid #eee; 
    margin: 25px auto 0 auto; 
    text-align: center; 
    padding-left: 20px; 
    padding-right: 20px; 
    box-shadow: 7px 7px 5px #838485; 
} 

.product-card .btn { 
    position: absolute; 
    min-width: 200px; 
    bottom: 15px; 
    left: 60px; 
} 
+0

이 필요한 이유는 무엇 는 절대 위치한다? –

+0

다른 제품 제목 링크와 짧은 제품 설명이 항상 동일한 수의 문자를 가지는 것은 아닙니다. .btn 요소에서 위쪽 여백을 수행해야합니다. 모든 버튼이 레이아웃에 정렬되지는 않습니다. 버튼의 절대 위치를 지정하면 사본 및 제목의 길이에 관계없이 이동하지 않습니다. –

+0

그런 다음 버튼에 대한 컨테이너를 절대적으로 배치 한 다음 해당 컨테이너에'text-align : center;'를 적용하면됩니다. –

답변

0

내 의견에, 버튼에 대한 래퍼를하고 position:absolute; 하단에 상기한다. 그러면 래퍼에 text-align:center;이 있기 때문에 버튼에서 스타일을 제거하면 스타일 자체가 중앙에 위치합니다.

.product-img-wrapper { 
 
    text-align: center; 
 
} 
 

 
.product-img-wrapper img { 
 
    width: 200px; 
 
    height: 200px; 
 
} 
 

 
.product-wrapper { 
 
    position: relative; 
 
    margin: 50px 0; 
 
} 
 

 
.product-card { 
 
    position: relative; 
 
    max-width: 330px; 
 
    height: 450px; 
 
    border: 1px solid #eee; 
 
    margin: 25px auto 0 auto; 
 
    text-align: center; 
 
    padding-left: 20px; 
 
    padding-right: 20px; 
 
    box-shadow: 7px 7px 5px #838485; 
 
} 
 

 
.product-card .card-bottom { 
 
    position: absolute; 
 
    bottom: 15px; 
 
    width:100%; 
 
    left:0; 
 
}
<div class="container"> 
 
       <div class="row"> 
 
        <div class="col-md-4"> 
 
         <div class="product-wrapper"> 
 
          <div class="product-card"> 
 
           <a href="lathes-single.html" class="product-img-wrapper"><img src="../assets/img/46-455.jpg" alt=""></a> 
 
           <h4> 46-460 12 1/2 in. Variable Speed MIDI-LATHE® </h4> 
 
           <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p> 
 
           <div class="card-bottom"><a href="lathes-single.html" class="btn btn-lg btn-primary">View Product</a></div> 
 
          </div> 
 
         </div> 
 
        </div> 
 
       </div> 
 
</div>

+0

그게 효과가있다. 그것은 너비 : 100 %를 제외하고 내가 가진 정확한 코드였습니까? 그것은 컨테이너 내부의 중앙 위치에 어떤 영향을 줍니까? –

+0

그것은 컨테이너가 카드의 전체 너비를 차지하게하고, 그 다음에 컨테이너의 중심과 카드의 양쪽 모두가 동일한 너비를 가지기 때문입니다. 수락 된 답으로 표시하는 것을 잊지 마십시오! –

0

사용. 이것은 당신의 btn 센터를 수평하게 만들 것입니다. css3: translateX to center element horizontally는 :

left: 50%; 
-webkit-transform: translateX(-50%); 
-moz-transform: translateX(-50%); 
transform: translateX(-50%); 
+1

나는이 문제를 해결하기 위해 또 다른 해킹을 사용하지 않을 것이다. CSS 변환은 위치 지정 문제를 해결할 수있는 방법이 아니어야합니다. 비록 그것이 결과를 성취하더라도. 귀하의 의견을 주셔서 감사합니다! –

+0

왼쪽 : 50 % 및 여백 - 왼쪽 : -100 픽셀을 사용할 수도 있습니다. 귀하의 .btn 센터에. – Robert

관련 문제