0

我对 css 中的以下布局有疑问,从下图中可以看出,最右边的两个浮动元素之间有很大的空间。

凌乱的 3 列 css 布局

我正试图让他们像

干净的 3 列 css 布局

我宁愿不使用负边距将“sidebar2”拉到正确的位置,并且“mag link”必须与第二个侧边栏分开。

谢谢

            <!doctype html>
            <html lang="en">
            <head>
            <meta charset="utf-8" />
            <title></title>
            <link rel="stylesheet" href="style.css" />
            <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
            <![endif]-->
            </head>
            <body>
            <div class="wraper clearfix">

            <div class="container clearfix">  

            <header>header</header>

            <div class="carousel">carousel</div>

            <div class="posts">posts
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            </div>

            <div class="sidebar1">sidebar1</div>

            <div class="mag-link">mag link</div>

            <div class="sidebar2">sidebar2</div> 

            </div><!-- container -->
            <footer class="clearfix">footer</footer>      
            </div><!-- wraper -->

            </body>
            </html>

css

            .wraper {
            background-color: rgba(254,139,206,0.27);
            width: 100%;
            position: absolute;
            margin: 0;
            padding: 0;
            }

            .container {
            background-color: rgba(253,184,65,0.27);
            width: 1040px;
            margin: 0 auto;
            padding: 0;
            }

            header {
            background-color: rgba(198,247,73,0.27);
            margin: 0 0 16px 0;
            padding: 0 16px 16px;
            height: 292px;
            }

            .carousel {
            background-color: rgba(96,250,193,0.27);
            margin: 0 8px 16px 0;
            height: 240px;
            width: 720px;
            display: inline-block;
            overflow: hidden;
            float: left;
            }

            .posts {/* page content */
            background-color: rgba(94,93,250,0.27);
            width: 512px;
            float: left;
            display: inline-block;
            padding: 0;
            margin: 0 8px 16px 0;
            }

            .post {
            width: 290px;
            height:200px;
            }

            .sidebar1 {
            background-color: rgba(184,88,250,0.27);
            display: inline-block;
            padding: 0;
            margin: 0 8px 16px 8px;
            float: left;
            position: relative;
            width: 208px;
            height:800px;
            }

            .mag-link {
            background-color: #fd9e90;
            width: 240px;
            height: 200px;
            margin: 0 0 16px 8px;
            float: left;
            position: relative;
            bottom: 389px;
            display: inline-block;
            }

            .sidebar2 {
            background-color: rgba(251,244,57,0.27);
            float: left;
            width: 240px;
            height:1100px;
            margin: 0 0 16px 8px;
            display: inline-block;
            }


            footer {
            width: 100%;
            background: #fd9e10;
            height: 312px;
            float: left;
            }

            .clearfix {
            zoom: 1; }
            .clearfix:before, .clearfix:after {
            content: "";
            display: table; }
            .clearfix:after {
            clear: both;}
4

2 回答 2

1

试试这个:http: //jsfiddle.net/A2XVQ/1/

我只是删除了一些属性

HTML

            <div class="container clearfix">  

            <header>header</header>

            <div class="carousel">carousel</div>

            <div class="posts">posts
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            <div class="post">Post</div>
            </div>

            <div class="sidebar1">sidebar1</div>

            <div class="mag-link">mag link</div>

            <div class="sidebar2">sidebar2</div> 

            </div><!-- container -->
            <footer class="clearfix">footer</footer>      
            </div><!-- wraper -->

CSS

.wraper {
            background-color: rgba(254,139,206,0.27);
            width: 100%;
            position: absolute;
            margin: 0;
            padding: 0;
            }

            .container {
            background-color: rgba(253,184,65,0.27);
            width: 1040px;
            margin: 0 auto;
            padding: 0;
            }

            header {
            background-color: rgba(198,247,73,0.27);
            margin: 0 0 16px 0;
            padding: 0 16px 16px;
            height: 292px;
            }

            .carousel {
            background-color: rgba(96,250,193,0.27);
            margin: 0 8px 16px 0;
            height: 240px;
            width: 720px;
            display: inline-block;
            overflow: hidden;
            float: left;
            }

            .posts {/* page content */
            background-color: rgba(94,93,250,0.27);
            width: 512px;
            float: left;
            display: inline-block;
            padding: 0;
            margin: 0 8px 16px 0;
            }

            .post {
            width: 290px;
            height:200px;
            }

            .sidebar1 {
            background-color: rgba(184,88,250,0.27);
            display: inline-block;
            padding: 0;
            margin: 0 8px 16px 8px;
            float: left;
            position: relative;
            width: 208px;
            height:800px;
            }

            .mag-link {
            background-color: #fd9e90;
            width: 240px;
            height: 200px;
            margin: 0 0 16px 8px;
            position: relative;
            display: inline-block;
            }

            .sidebar2 {
            background-color: rgba(251,244,57,0.27);
            float: left;
            width: 240px;
            height:1100px;
            margin: 0 0 16px 8px;
            }


            footer {
            width: 100%;
            background: #fd9e10;
            height: 312px;
            float: left;
            }

            .clearfix {
            zoom: 1; }
            .clearfix:before, .clearfix:after {
            content: "";
            display: table; }
            .clearfix:after {
            clear: both;}

}

祝你好运

于 2013-06-06T14:50:47.867 回答
0

You need to place the content part (including the carousel, posts and the first sidebar) into one div and the two elements to the right (mag link and sidebar 2) into a second div and float them left.

HTML would look like that:

 <div class="wraper clearfix">

        <div class="container clearfix">  

        <header>header</header>

        <div class="contentwrap">

     <div class="carousel">carousel</div>

        <div class="posts">posts
        <div class="post">Post</div>
        <div class="post">Post</div>
        <div class="post">Post</div>
        <div class="post">Post</div>
        <div class="post">Post</div>
        <div class="post">Post</div>
        </div>

        <div class="sidebar1">sidebar1</div>

     </div>

     <div class="sidebarwrapper">
        <div class="mag-link">mag link</div>

        <div class="sidebar2">sidebar2</div> 
     </div>

        </div><!-- container -->
        <footer class="clearfix">footer</footer>      
        </div><!-- wraper -->

Have a look at my fiddle: http://jsfiddle.net/jW6HW/

于 2013-06-06T14:48:17.520 回答