简体   繁体   English

三栏CSS布局

[英]Three Column CSS Layout

I looked at a few other questions before posting this one. 我在发布这个问题之前先看了几个其他问题。 Nothing else does quite what I want. 没有其他事情能做到我想要的。

My goal is to create a three column layout but with a "tout" hanging down from the center of the top of the page in the middle column. 我的目标是创建一个三列布局,但在中间列的页面顶部中心悬挂一个“tout”。 That tout should align with our corporate logo further down the page, with the text of the page flowing under that. 该粉丝应与页面下方的公司徽标保持一致,页面文本在此下方流动。

HTML is straightforward, three divs in a container followed by a div for the logo and then the body copy: HTML非常简单,容器中有三个div,后跟徽标的div,然后是正文副本:

    <div class="container">
    <div class="topcontain">
        <div class="topleft">testing left</div>
        <div class="topcenter"><img class="floatimg" src="toutdemo.png"></div>
        <div class="topright">testing right</div>
    </div>
    <div class="bodypart">
        <div class="logo"><img src="demologo.png"></div>
        <div class="bodycopy">
            <p>Lorem ipsum dolor sit amet...etc</p>
        </div>
    </div>
    </div>

The CSS is based on what I thought was simple alignment: CSS基于我认为的简单对齐:

.container {
    text-align: center;
}
.topcontain {
    width:80%
    text-align:center;
    height:125px;
}
.topleft {
    width:35%;
    float:left;
}
.topcenter {
    width:256px;
    float:left;
}
.topright {
    float:right;
    width:35%;
}

.bodycopy {
    text-align:left;
}

I put together a fiddle that semi-demonstrates my two problems: 我把一个小提琴放在一起,半显示我的两个问题:

  1. The layout works great at a large size but when the viewport narrows the right column drops and/or wants to tuck under the middle (easier to see in a browser than in a fiddle). 布局在大尺寸下工作得很好,但是当视口变窄时,右列会下降和/或想要折叠在中间(在浏览器中比在小提琴中更容易看到)。
  2. Partly because of issue #1 it's sometimes hard keeping the tout and logo aligned. 部分原因是问题#1,有时很难保持兜售和徽标对齐。

Yes, eventually this will use fluid images but for the time being everything is static. 是的,最终这将使用流体图像,但暂时一切都是静态的。 Seems like a simple layout...but not! 看起来像一个简单的布局......但不是!

I have updated your fiddle: http://jsfiddle.net/Znz2P/10/ 我更新了你的小提琴: http//jsfiddle.net/Znz2P/10/

on the left and right floated divs: 在左右浮动div:

width: calc(50% - 128px);

the right and left divs will share the remaining space, minus the set width of the center (divided by 2) 右侧和左侧div将共享剩余空间,减去中心的设定宽度(除以2)

on the logo: 在徽标上:

.logo { clear: left; }

and collapse the borders: 并折叠边框:

.topcontain > div {     
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;}

I aslo added a 'clearfix' class, which is nice to have for containers that only sport floated elements. 我还添加了一个'clearfix'类,对于只运行浮动元素的容器来说这很好。

Its because the center div has fixed width while the left and right have 35% width. 因为中心div有固定宽度,而左边和右边有35%宽度。 You can either remove the width from the "topright" div or set max-width:30% on the "topcenter" class 您可以从“topright”div中删除宽度,也可以在“topcenter”类中设置max-width:30%

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

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