简体   繁体   English

如何进行流体布局

[英]How to Make a Fluid Layout

So I was assigned in class to code this redesigned homepage in a week. 因此,我在课堂上被分配在一周内编写这个重新设计的首页的代码。 It has to me responsive so after I got it all coded (which is a miracle in and of itself) I started to research how to make it responsive. 它对我来说必须具有响应性,因此在我将所有代码都编码后(这本身就是一个奇迹),我开始研究如何使其具有响应性。 I know this probably wasn't the best way of doing things, but hey, this is really my first time to code anything so live and learn I suppose. 我知道这可能不是最好的处理方式,但是,嘿,这确实是我第一次编写任何我认为应该如此生动有趣的代码。 Anyway, I've gone through my CSS and set all of widths to be percentages to give it fluidity (a good place to start I think). 无论如何,我已经遍历了CSS并将所有宽度设置为百分比,以使其具有流动性(我认为这是一个不错的起点)。 I assigned a max-width to almost everything because I have a slider set up with 600px images and I don't want them to scale above that. 我为几乎所有内容分配了最大宽度,因为我有一个设置了600像素图像的滑块,但我不希望它们在此范围内缩放。 So everything works fine until I get below 1300. You can visit the site here to see what happens. 因此一切正常,直到我跌至1300以下。您可以在此处访问该网站以查看发生了什么。 I do not understand why the two boxes on the right jump down if they are simply told to take up a certain percentage of the parent div. 我不明白,如果只告诉它们占据父div一定比例的位置,为什么右边的两个框会跳下来。 Interestingly enough, at the same point the last footer item also drops down. 有趣的是,在同一点,最后的页脚项目也下降了。 Is there something I am missing or what can I do to make it scale properly? 有什么我缺少的东西,或者我该怎么做才能正确缩放?

I know that in actuality I do not want it to scale indefinitely and plan to use media queries to handle it at smaller resolutions, but it is bothering me that it isn't scaling properly at even such a high resolution of 1300. I wasn't planning to do a media query until I got down to 1024. 我知道实际上我不希望它无限扩展并计划使用媒体查询以较小的分辨率处理它,但是令我困扰的是,即使在这样的1300高分辨率下,它也无法正确扩展。计划进行媒体查询,直到降至1024。

If you have any other advice or resources for making a site responsive feel free share. 如果您有任何其他建议或资源可以使站点快速响应,请随意共享。 Thanks in advance. 提前致谢。

Here's my html 这是我的HTML

<div class="container">
    <div id="captioned-gallery">

    </div><!--captioned gallery-->

    <div id="menu-ad">
        <div>
            <p class="titles">Our Fare</p>
            <p id="ad">Our lunch and dinner menus feature European inspired comfort food accompanied by an extensive bar.</p>
            <a href="#" id="button">VIEW MENU</a>
        </div>
    </div><!--end menu ad-->

      <div id="hours">
        <div>
        <p class="titles">Hours</p>
            <p id="down">
                <span class="subtitles">Lunch</span>
                <span class="hours">Mon-Fri 11-4</span>
            </p>

            <p>
                <span class="subtitles">Dinner</span>
                <span class="hours">Mon-Sat 4-12</span>
            </p>

            <p>
                <span class="subtitles">Bar</span>
                <span class="hours">Mon-Sat 4-12</span>
            </p> 
        </div>
</div><div style="clear:both;"></div><!--end hours-->
</div><!--end container-->

And the CSS 和CSS

/*Container*/

div.container {
    margin: 0 auto;
    position: relative;
    width: 70%;
    max-width: 910px;
    border: 2px solid #b9aea3;
}

/*slider*/

div#captioned-gallery {
    width: 65.934066%;
    max-width: 600px;
    margin: 10px;
    overflow: hidden;
    height: auto;
    float: left;




/*menu ad*/

div#menu-ad {
    position: relative;
    width: 31.648352%;
    max-width: 288px;
    height: auto;
    float: right;
    border-left: 2px solid #b9aea3;
    border-bottom: 2px solid #b9aea3;
    overflow: hidden;
}

div#menu-ad div {
    background: #f9f4df;
    margin: 10px;
    padding: 1.9rem 4rem 2.5rem 2.5rem;
    height: 200px;
    display: inline-block;
}


a#button {
    padding: .7rem 1.3rem .5rem 1.3rem;
    font-family: "Montserrat", "Helvetica", sans-serif;
    font-size: 1.8rem;
    color: #f8f8f1;
    background: #d6832e;
    text-align: center;
    vertical-align: middle;
    text-decoration: none;
    position: absolute;
    float: left;
    bottom: 3.5rem;
}


/*hours*/

div#hours {
    position: relative;
    width: 31.648352%;
    max-width: 288px;
    height: auto;
    float: right;
    border-left: 2px solid #b9aea3;
}

div#hours div {
    background: #f9f4df;
    margin: 10px;
    padding: 1.9rem 2.5rem 2.5rem 2.5rem;
    width: auto;
    height: 150px;
}


#down span{
    margin-top: 1rem;
}

#hours p {clear:both;}

The reason why the right boxes "jump down" is that you set a 10px margin on the #captioned-gallery and a 2px border-left on #menu-ad . 右侧框“跳下来”的原因是您在#captioned-gallery上设置了10px的边距,在#menu-ad上设置了2px border-left


EXPLANATION 说明


On a 1400px wide screen 在1400像素宽的屏幕上

#container has 70% width = 1400 * 70 / 100 = 980px #container 1400 * 70 / 100 = 980px宽度为70%= 1400 * 70 / 100 = 980px

#captioned-gallery has 65.9..% width = 980 * 65.9 / 100 = 645.42px #captioned-gallery宽度为65.9 ..%= 980 * 65.9 / 100 = 645.42px
AND a 10px margin so the width of #captioned-gallery is 645.42 + 2*10 = 665.42px 并且有10px的边距,因此#captioned-gallery的宽度为645.42 + 2*10 = 665.42px

#menu-ad has 31.6..% width = 980 * 31.6 / 100 = 309.68px #menu-ad宽度为31.6 ..%= 980 * 31.6 / 100 = 309.68px
AND a 2px border-left so #menu-ad is 309.68+2px=311.68px 并且左边框为2px,因此#menu-ad为309.68 + 2px = 311.68px

So the width of #captioned-gallery + #menu-ad = 665.42 + 311.68 = 977,36px which is less than 980px (width of #container ) so it can fit . 因此, #captioned-gallery + #menu-ad = 665.42 + 311.68 = 977,36px宽度小于980px( #container宽度),因此可以容纳


On a 1000px wide screen 在1000像素宽的屏幕上

If you do the same calculation you end up with these results 如果进行相同的计算,最终将得到这些结果

#container is 700px wide #container容器的宽度为700像素
#captioned-gallery is 481.3 wide #captioned-gallery的宽度为481.3
#menu-ad is 223.2 wide #menu-ad宽223.2

481.3 + 223.2 = 704,5px So #captioned-gallery + #menu-ad can't fit in the #container anymore so it "jumps down" which is the behavior for floating elements that can't fit in their container. 481.3 + 223.2 = 704,5px所以#captioned-gallery + #menu-ad 不再适合 #container ,因此它“跳下来”,这是无法容纳在其容器中的浮动元素的行为。



SOLUTION

Besides from using media-queries as stated by Sebsemillia you can change the margins to % (including them in the total with of the elements so it isn't larger than 100%) and use the box-sizing CSS property to include the left-border in the #menu-ad , #hours width : 除了使用Sebsemillia所述的媒体查询外,您还可以将边距更改为%(将它们添加到元素的总数中,因此不大于100%),并使用box-sizing CSS属性将left-border #menu-ad , #hours中的left-border#menu-ad , #hours宽度:

The width and height properties include the padding and border, but not the margin width和height属性包括填充和边框,但不包括边距

You CSS could look like this : 您的CSS可能看起来像这样:

#captioned-gallery{
    width:65%;
    margin:10px 1%;
}
#menu-ad,#hours{
    width:33%;
    border-left:2px solid #b9aea3;
    box-sizing: border-box;
      -moz-box-sizing: border-box;     //for firefox support
}

33% + 65% + 2*1% = 100%

You should look into media-queries. 您应该调查媒体查询。 I guess if you read this article it will help you a lot to understand the issue. 我想,如果您阅读这篇文章,将会对您有所帮助。

CSS Tricks - css media queries CSS技巧-CSS媒体查询

For example when the viewport is under your max-width of 960px, you should set the width of your footer to 100%, instead of 70 %. 例如,当视口在960px的最大宽度以下时,应将页脚的宽度设置为100%,而不是70%。

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

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