중앙 요소의 높이가 창 높이보다 길 때 수직 중심을 비활성화하는 방법은 무엇입니까? (자바 스크립트 제외)

고슴도치

display: flex; align-items: center;일반 모드에서 사이트 콘텐츠를 세로 중앙에 배치하려고 합니다. 그러나 내용이 변경되고 중앙 요소 높이가 창보다 길어지면 align-items: flex-start;.

유스 케이스는 그 것이고 나는 당신과 함께 샘플 바이올린 사랑을 나눌 것입니다.

실제 문제는 이 문제에 자바 스크립트사용 해서는 안된다는 것입니다. 순수한 CSS 코드로 해결해야합니다. (콘텐츠를 늘리고 문제를 확인하는 방법을 보여주기 위해 자바 스크립트를 추가했습니다.) 바이올린에서 버튼을 클릭하면 내 문제를 더 많이 클릭하고 오버플로가 제대로 실행되지 않고 상위 요소가 보이지 않는 것을 확인할 수 있습니다. 이걸 실행하려면 현명한 옵션이 필요합니다. 도와 주셔서 감사합니다.

html :

<div id="box">
    <div>
      <button id='click'>
        click me
      </button>
    </div>
  </div>

css :

html,body{
  min-height:100%;
  height:100%
}
#box {
   height:100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#box div {
  background:tomato;
  width: 100px;
  height: auto;
}

자바 스크립트 :

document.addEventListener("click", function(){

    var para = document.createElement("P");    
    para.innerHTML = "This is a paragraph.";  
    document.getElementById("box").children[0].appendChild(para);

});

실제 샘플 이미지를 보여주고 더 깔끔하게 만들기 위해 바이올린을 공유하겠습니다.

일반 모드 :여기에 이미지 설명 입력

긴 콘텐츠 모드 : 여기에 이미지 설명 입력

jsfiddle : 링크

이 이미지에 표시된 것처럼 오버플로도 사실이 아니라 상단을 표시하지만 창 상단이 아닙니다.

cjc

I'm not sure if this will work for your requirements, but you need to start by changing the direction of the flexbox (you are using the default flex-direction: row but you'll need to switch to flex-direction: column). This will allow you to really take advantage of the dynamic sizing nature of flexboxes in the way that you need.

Since you are switching the orientation of the flexbox, you'll also need to switch your align-items and justify-content values. Since the effect you want is to end up with is justify-content: flex-start, go ahead and switch that now. You'll simulated justify-content: center with some placeholder elements.

Once you are using the flexbox vertically, you can then put in some placeholders that will shrink as the actual content grows. Put an empty div with this placeholder class above an below the actual content. The placeholder class then needs to be styled so that it can only shrink using flex-grow: 0 and flex-shrink: 1 (this works best if all other elements have flex-shrink: 0). It will also need to be styled with an initial height (I used height: 50vh but that might vary based on your needs).

With the flex-direction switched and the placeholders added, the last thing you need to do is set a height or max-height on the flexbox so that the placeholders will actually shrink. I recommend using height: 100vh but you can use something different based on the needs of your project.

다음은 최종 결과입니다 (샘플 코드 기준). 예제 코드에 매우 구체적이고 실제 솔루션에 필요한 것이 아닌 몇 가지 다른 조정 (예 : 자리 표시 자에 배경 설정)을 만들었습니다. 또한 불필요한 코드 (예 : HTML 및 본문의 스타일)를 제거했습니다.

document.addEventListener("click", function() {

  var para = document.createElement("P");
  para.innerHTML = "This is a paragraph.";
  document.getElementById("box").children[1].appendChild(para);

});
#box {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-direction: column;
  height: 100vh;
  overflow: auto;
}

#box>* {
  background: tomato;
  width: 100px;
  margin: 0;
  flex-grow: 1;
  flex-shrink: 0;
}

#box>.placeholder {
  height: 50vh;
  flex-grow: 0;
  flex-shrink: 100;
  background: none;
}
<div id="box">
  <div class="placeholder"></div>
  <div>
    <button id='click'>
    click me
    </button>
  </div>
  <div class="placeholder"></div>
</div>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관