简体   繁体   English

带有嵌套div的Div非常灵活

[英]Div with nested divs sized really flexibly

I'm looking for an easy and elegant solution to do the following: 我正在寻找一个简单而优雅的解决方案来执行以下操作:

在此输入图像描述

I've seen a bunch of Q&A here on StackOverflow, however the majority describes some fixed (known) sizes in pixels. 我在StackOverflow上看过一堆Q&A,但是大多数都以像素为单位描述了一些固定(已知)大小。 What I would like to achieve is really flexible layout where: 我想要实现的是灵活的布局,其中:

1) Position the div at certain vertical position and have it a certain height -> solved with setting position: fixed; top: 30%; height: 40%; 1)将div定位在一定的垂直位置并使其具有一定的高度 - >用设定position: fixed; top: 30%; height: 40%;求解position: fixed; top: 30%; height: 40%; position: fixed; top: 30%; height: 40%;

2) Position the div at the center horizontally -< tried width: auto; margin: 0 auto; 2)将div放在水平中心位置 - <尝试width: auto; margin: 0 auto; width: auto; margin: 0 auto; but this didn't work 但这没用

3) Size the nested divs (I'm using the spans right now, but probably that doesn't matter in this context) with their height equal to their width and height of 1 line to be equal to 1/3 (roughly) of the parent div. 3)调整嵌套div的大小(我现在正在使用跨度,但在这种情况下可能无关紧要),它们的高度等于它们的宽度和1行的高度等于1/3(大致)的父div。 Also I would like those nested divs to be content agnostic, ie set the font size based on the size of the div rather than pushing the divs by increasing the text size. 此外,我希望那些嵌套的div是内容不可知的,即根据div的大小设置字体大小,而不是通过增加文本大小来推动div。 Also, so far the whole thing will break if I exclude couple of captions from the nested divs 此外,如果我从嵌套的div中排除几个标题,到目前为止整个事情都会破裂

So far it looks like this: 到目前为止它看起来像这样:

在此输入图像描述

I'm pretty sure this is achievable without any js and I'd be happy to get any advice on this. 我很确定这是可以实现的,没有任何js,我很乐意就此得到任何建议。

Thanks 谢谢

Update 1: I'm updating the post with the link to fiddle , thanks to Sari for the advice. 更新1:我正在用小提琴链接更新帖子,感谢Sari的建议。

You may want to consider using a flexbox for this layout. 您可能需要考虑使用flexbox进行此布局。 By its nature a flexbox is a flexible box . 就其本质而言,flexbox是一种灵活的盒子

Try this: 尝试这个:

HTML (no changes to your fiddle demo ) HTML (没有改变你的小提琴演示

<div id="flexdiv">
    <div id="li1" class="li">
        <span id="l1" class="l">1</span>
        <span id="l2" class="l">2</span>
        <span id="l3" class="l">3</span>
        <span id="l4" class="l">4</span>
    </div>
    <div id="li2" class="li">
        <span id="l5" class="l">5</span>
        <span id="l6" class="l">6</span>
        <span id="l7" class="l">7</span>
        <span id="l8" class="l">8</span>
    </div>
    <div id="li3" class="li">
        <span id="l9" class="l">9</span>
        <span id="l10" class="l">A</span>
        <span id="l11" class="l">B</span>
        <span id="l12" class="l">C</span>
    </div>
</div>

CSS CSS

html, body { height: 100%; } 

body {
    display: flex; /* establish flex container */
    justify-content: center; /* center #flexdiv horizontally */
    align-items: center; /* center #flexdiv vertically */
}

#flexdiv {
    display: flex; /* establish (nested) flex container */
    flex-direction: column; /* align li boxes vertically */
    height: 48vmin; /* height relative to viewport size */
}

.li {
    display: flex;
    text-align: center;
    height: 16vmin; /* height relative to viewport size */
}

.l {
    width: 16vmin; /* boxes maintain aspect ratio */
    line-height: 16vmin; /* vertically center text */
    font-size: 7vmin; /* font size scales */
}

DEMO: http://jsfiddle.net/6bsoze4z/4/ 演示: http//jsfiddle.net/6bsoze4z/4/

尝试使用calc函数设置top:

top: calc(30%);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM