cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>이미지의 가로세로비를 유지하면서 틀 안에 맞추기</h1>

<h2>세로로 긴 이미지 맞추기</h2>
<ul class="clearfix">
  <li>
    <p>1:1 비율</p>
    <div class="aspect_1_1"><img src="http://dimg.donga.com/wps/NEWS/IMAGE/2014/02/26/61229543.2.jpg"></div>
  </li>
  <li>
    <p>4:3 비율</p>
    <div class="aspect_4_3"><img src="http://dimg.donga.com/wps/NEWS/IMAGE/2014/02/26/61229543.2.jpg"></div>
  </li>
  <li>
    <p>4:5 비율</p>
    <div class="aspect_4_5"><img src="http://dimg.donga.com/wps/NEWS/IMAGE/2014/02/26/61229543.2.jpg"></div>
  </li>
  <li>
    <button id="btnToggleOverflow" type="button">이미지 자름 토글</button>
  </li>
</ul>

<br><br>
<h2>가로로 긴 이미지 맞추기</h2>
<ul class="clearfix">
  <li>
    <p>1:1 비율</p>
    <div class="aspect_1_1"><img src="http://dimg.donga.com/wps/NEWS/IMAGE/2014/02/21/61082243.3.jpg"></div>
  </li>
  <li>
    <p>4:3 비율</p>
    <div class="aspect_4_3"><img src="http://dimg.donga.com/wps/NEWS/IMAGE/2014/02/21/61082243.3.jpg"></div>
  </li>
  <li>
    <p>4:5 비율</p>
    <div class="aspect_4_5"><img src="http://dimg.donga.com/wps/NEWS/IMAGE/2014/02/21/61082243.3.jpg"></div>
  </li>
</ul>

<div class="footer">
<a href="https://start.goodtime.co.kr/2014/02/%EC%9D%B4%EB%AF%B8%EC%A7%80%EC%9D%98-%EA%B0%80%EB%A1%9C%EC%84%B8%EB%A1%9C%EB%B9%84%EB%A5%BC-%EC%9C%A0%EC%A7%80%ED%95%98%EB%A9%B4%EC%84%9C-%ED%8B%80-%EC%95%88%EC%97%90-%EB%A7%9E%EC%B6%94%EB%A0%A4/" target="_blank">2014, 이동련</a></div>
            
          
!
            
              body, button { font-family: 맑은 고딕; font-size: 11pt; text-decoration: none; }

li { float: left; margin-right: 50px; list-style: none; }
li > div { border: solid 3px blue; }

/* 아래 세 줄이 중요한 스타일임 */
div.aspect_1_1 { width: 80px; height: 80px; }
div.aspect_4_3 { width: 100px; height: 75px; }
div.aspect_4_5 { width: 80px; height: 100px; }

.clearfix:after {
   content: " ";
   visibility: hidden;
   display: block;
   height: 0;
   clear: both;
}
.footer {
  width: 600px; text-align: center; margin-top: 5em;
}
            
          
!
            
              window.onload = function() {
  var divs = document.querySelectorAll('li > div');
  for (var i = 0; i < divs.length; ++i) {
    var div = divs[i];
    var divAspect = div.offsetHeight / div.offsetWidth;
    div.style.overflow = 'hidden';
    
    var img = div.querySelector('img');
    var imgAspect = img.height / img.width;

    if (imgAspect <= divAspect) {
      // 이미지가 div보다 납작한 경우 세로를 div에 맞추고 가로는 잘라낸다
      var imgWidthActual = div.offsetHeight / imgAspect;
      var imgWidthToBe = div.offsetHeight / divAspect;
      var marginLeft = -Math.round((imgWidthActual - imgWidthToBe) / 2)
      img.style.cssText = 'width: auto; height: 100%; margin-left: '
                      + marginLeft + 'px;'
    } else {
      // 이미지가 div보다 길쭉한 경우 가로를 div에 맞추고 세로를 잘라낸다
      img.style.cssText = 'width: 100%; height: auto; margin-left: 0;';
    }
  }
  
  var btn = document.querySelector('#btnToggleOverflow');
  btn.onclick = function() {
    var val = divs[0].style.overflow == 'hidden' ? 'visible' : 'hidden';
    for (var i = 0; i < divs.length; ++i)
      divs[i].style.overflow = val;
  };
};

            
          
!
999px
Loading ..................

Console