响应式CSS浮动以垂直对齐,无需在顶部留出任何空白。

3
我正在创建一个响应式网页。我有两篇文章浮动在左侧,两个旁注浮动在右侧。我希望这两个旁注与第一篇文章在顶部对齐。最初,旁注与文章完美对齐,因为我将旁注放在两篇文章之间的HTML中。
但是,在制作响应式页面时,我发现当我尝试将旁注折叠到页面底部,位于文章下方时,它们会显示在两篇文章的中间,而我想要旁注出现在两篇文章下面。因此,我将旁注移到了HTML页面底部,从而导致了问题。
我已经尝试清除浮动,并使用inline-block显示文章和旁注,但无法使其正常工作。
编辑:有人建议(然后删除了他们的回复?)绝对定位旁注。我已经尝试过这样做,但我觉得它会破坏响应式设计,所以如果可能的话,我想避免使用它。
这里是JSfiddle

@charset"utf-8";

/* CSS Document */

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/
 html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
 article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after, q:before, q:after {
    content:'';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
/* End of CSS reset */

/* apply a natural box layout model to all elements, but allowing components to change */
 html {
    box-sizing: border-box;
}
*, *:before, *:after {
    box-sizing: inherit;
}
/* General styles */

/* Background pattern from Subtle Patterns, by Atle Mo: http://subtlepatterns.com/green-cup/ */
 body {
    line-height: 1.4;
    background-image: url(../images/green_cup.png);
}
.wrapper {
    width: 960px;
    /* 960px; */
    margin: 0 auto;
}
/* Header styles */
 header {
    font-family:'Playfair Display', serif;
    font-size: 23px;
}
.header-text {
    margin: 0 auto;
    width: 800px;
    font-family:'Playfair Display', serif;
    text-align: center;
    color: #1d464f;
    font-size: 2.1em;
    /* 50px; */
}
.header-text-below {
    margin: 0 auto;
    width: 800px;
    text-align: center;
    font-family:'Merriweather', serif;
    font-size: 1.1em;
    /* 25px; */
    color: #256139;
    padding-bottom: 10px;
}
/* End Header styles */

/* Nav */
 nav {
    width: 100%;
    background-color: white;
    border-bottom: 4px solid #78605C;
    height: 60px;
}
nav ul {
    padding-top: 7px;
    margin-left: 19%;
}
nav li {
    float: left;
    display: block;
    text-align: center;
    list-style-type: none;
    width: 19%;
}
nav a {
    display: block;
    font-family:'Playfair Display', serif;
    color: #2A879C;
    text-decoration: none;
    font-size: 30px;
}
nav a:hover, nav a:focus, nav a:active {
    color: #78605C;
}
/* Typography */
 h1 {
    color: #1d464f;
}
.article-header {
    color: #1d464f;
    padding-top: 4px;
    font-size: 35px;
}
p {
    font-family:'Merriweather', serif;
    line-height: 1.4em;
    font-size: 1em;
}
.date-circle {
    font-family:'Merriweather', serif;
    border-radius: 50%;
    background-color: #78605C;
    color: white;
    height: 60px;
    width: 60px;
    float: left;
    font-size: 14px;
    text-align: center;
    padding-top: 20px;
    margin-right: 10px;
    margin-bottom: 5px;
    clear: both;
}
.read-more a {
    color: #78605C;
    font-family:'Merriweather', serif;
    text-decoration: none;
    font-weight: bold;
}
.featured {
    color: #256139;
    font-family:'Merriweather', serif;
    font-weight: bold;
    font-size: 20px;
}
/* End Typography */
 img {
    max-width: 100%;
    height: auto;
}
/* Articles */
 article {
    float: left;
    display: inline-block;
    width: 67.39%;
    /* 620px / 960px = 67.39%, target / context = result */
    padding: 10px;
    padding-top: 10px;
    background-color: white;
}
article:nth-of-type(2) {
    margin-bottom: 20px;
}
.clear {
    clear: left;
}
/* Borders of articles and asides */
 article:nth-of-type(1) {
    border-top-style: solid;
    border-right-style: solid;
    border-bottom-style: none;
    border-left-style: solid;
    border-color: #DED8AA;
    border-width: 4px;
}
article:nth-of-type(2) {
    border-top-style: none;
    border-right-style: solid;
    border-bottom-style: solid;
    border-left-style: solid;
    border-color: #DED8AA;
    border-width: 4px;
}
aside:nth-of-type(1) {
    border-top-style: solid;
    border-right-style: solid;
    border-bottom-style: none;
    border-left-style: solid;
    border-color: #DED8AA;
    border-width: 4px;
}
aside:nth-of-type(2) {
    border-top-style: none;
    border-right-style: solid;
    border-bottom-style: solid;
    border-left-style: solid;
    border-color: #DED8AA;
    border-width: 4px;
}
/* End borders */

/* Aside, right side div */
 aside {
    float: right;
    width: 31.25%;
    /* 300px / 960px = 31.25%, target / context = result */
    padding: 10px;
    padding-top: 10px;
    background-color: white;
}
/* Font awesome styling, social media icons from: http://fortawesome.github.io/Font-Awesome/ */
 .iconContact {
    width: 235px;
    margin: 0 auto;
}
.iconContact a {
    color: #2A879C;
    transition: color 0.3s ease;
    text-align: center;
    float: left;
    display: inline-block;
    padding: 10px;
}
.iconContact a:hover {
    color: #78605C;
}
/* Media queries 


Your blog web page should be usable at the following sizes at a minimum: 
i. Smartphone – portrait (320px wide) 
ii. Smartphone – landscape (480px wide) 
iii. Tablet – portrait (768px wide) 
iv. Tablet – landscape(1024px wide) 
v. Desktop – widths wider than 1024px 



*/
 @media only screen and (max-width: 1080px) {
    main {
        width: 90%;
        margin: 0 auto;
    }
    .article-header {
        padding-top: 8px;
        font-size: 1.2em;
        /* 35px; */
    }
    @media only screen and (max-width: 980px) {
        .wrapper {
            width: 80%;
            /* 960px; */
            margin: 0 auto;
        }
        nav ul {
            padding-top: 7px;
            margin-left: 0;
        }
        nav li {
            float: left;
            display: block;
            text-align: center;
            list-style-type: none;
            width: 25%;
        }
        .header-text {
            margin: 0 auto;
            width: 80%;
            /* 800px; */
            font-family:'Playfair Display', serif;
            text-align: center;
            color: #1d464f;
            font-size: 2.1em;
            /* 50px; */
        }
        .header-text-below {
            margin: 0 auto;
            width: 80%;
            /* 800px; */
            text-align: center;
            font-family:'Merriweather', serif;
            font-size: 1.1em;
            /* 25px; */
            color: #256139;
            padding-bottom: 10px;
        }
        main {
            width: 100%;
            margin: 0 auto;
        }
        .article-header {
            padding-top: 8px;
            font-size: 1.2em;
            /* 35px; */
        }
        .iconContact a {
            padding: 6px;
        }
    }
    @media only screen and (max-width: 920px) {
        .iconContact a {
            padding: 4px;
        }
    }
    @media only screen and (max-width: 890px) {
        .header-text-below {
            font-size: 1em;
            /* 25px; */
        }
    }
    @media only screen and (max-width: 860px) {
        .iconContact a {
            padding: 3px;
        }
        @media only screen and (max-width: 830px) {
            .iconContact a {
                padding: 2px;
            }
        }
        @media only screen and (max-width: 800px) {
            .header-text-below {
                font-size: 0.9em;
                /* 25px; */
            }
            .iconContact a {
                padding: 1px;
            }
            @media only screen and (max-width: 890px) {
                .header-text-below {
                    font-size: 1em;
                    /* 25px; */
                }
            }
            @media only screen and (max-width: 768px) {
                aside {
                    float: none;
                    width: 100%;
                }
                article {
                    width: 100%;
                }
            }
<!doctype html>
<html>
    
    <head>
        <meta charset="utf-8">
        <title>A Healthy Zest; A food blog begins</title>
        <link rel="stylesheet" type="text/css" href="styles/styles.css">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
        <link href='http://fonts.googleapis.com/css?family=Playfair+Display:400,700' rel='stylesheet' type='text/css'>
        <link href='http://fonts.googleapis.com/css?family=Merriweather' rel='stylesheet' type='text/css'>
        <!-- Responsive media queries -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <!--[if lt IE 9]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
    </head>
    
    <body>
        <nav>
            <ul>
                <li><a href="#">Home</a>

                </li>
                <li><a href="#">About</a>

                </li>
                <li><a href="#">Recipes</a>

                </li>
                <li><a href="#">Contact</a>

                </li>
            </ul>
        </nav>
        <div class="wrapper">
            <header>
                
<h1 class="header-text"> A Healthy Zest </h1>

                
<h2 class="header-text-below"> Carefully curated recipes for those on the go </h2>

            </header>
            <main>
                <!-- Articles, left hand side -->
                <article>
                    <header>
                        <div class="date-circle">May 02</div>
                         <h1 class="article-header">Organic Italian pizza</h1>

                    </header>
                    <!--- Image in blog post, figure -->
                    <figure>
                        <img src="images/pizza-600.jpg" alt="Delicious Italian Cheese and Tomato Pizza" width="600" height="400">
                    </figure>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent volutpat lectus quis tellus venenatis pellentesque. Vestibulum et aliquam ipsum. Suspendisse dapibus nibh arcu. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed posuere urna ut tortor vulputate porttitor. Maecenas eget ultrices felis, at luctus justo. Etiam blandit placerat lacus vel tristique. Suspendisse ultrices iaculis eros sed molestie. Nulla eget ante eleifend, vulputate nunc in, congue orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin id tellus dolor. Aenean maximus eros porttitor erat consequat, vel iaculis dui faucibus. Vestibulum aliquet lorem at mi pretium ultricies. Suspendisse tempor pulvinar rutrum. Morbi consequat eget nunc id lobortis. Pellentesque ut tincidunt lectus. In hac habitasse platea dictumst. Duis gravida neque sit amet nunc vestibulum, non interdum ante dictum. Fusce semper pharetra lectus nec auctor. Sed dignissim cursus leo, et porttitor sem porta quis. Etiam facilisis volutpat neque, vitae rutrum purus fermentum vitae. Donec sed nunc dolor. Sed sed rutrum mi. Sed malesuada lobortis aliquet. Praesent vel nisl in massa dictum varius. Nullam nec magna arcu. Vivamus at dolor id neque iaculis rhoncus. Suspendisse aliquet libero felis, vitae tincidunt lectus placerat a. Duis a dictum lacus. Donec ut libero diam. Phasellus scelerisque mattis felis, ut malesuada neque pellentesque et. In arcu sem, convallis ac fermentum sed, consequat et tellus. Donec eu orci quis sapien aliquet porttitor sit amet eu metus. Etiam non sodales risus. Suspendisse placerat sem eu dolor porta tempor.</p>
                    <p class="read-more"> <a href="#"> Read more...</a> 
                    </p>
                </article>
                <article>
                    <header>
                        <div class="date-circle">April 08</div>
                         <h1 class="article-header">Healthy whole-grain sandwich</h1>

                    </header>
                    <!--- Image in blog post, figure -->
                    <figure>
                        <img src="images/sandwich-600.jpg" alt="Healthy sandwich with lettuce, egg and cream cheese" width="600" height="400">
                    </figure>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent volutpat lectus quis tellus venenatis pellentesque. Vestibulum et aliquam ipsum. Suspendisse dapibus nibh arcu. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed posuere urna ut tortor vulputate porttitor. Maecenas eget ultrices felis, at luctus justo. Etiam blandit placerat lacus vel tristique. Suspendisse ultrices iaculis eros sed molestie. Nulla eget ante eleifend, vulputate nunc in, congue orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin id tellus dolor. Aenean maximus eros porttitor erat consequat, vel iaculis dui faucibus. Vestibulum aliquet lorem at mi pretium ultricies. Suspendisse tempor pulvinar rutrum. Morbi consequat eget nunc id lobortis. Pellentesque ut tincidunt lectus. In hac habitasse platea dictumst. Duis gravida neque sit amet nunc vestibulum, non interdum ante dictum. Fusce semper pharetra lectus nec auctor. Sed dignissim cursus leo, et porttitor sem porta quis. Etiam facilisis volutpat neque, vitae rutrum purus fermentum vitae. Donec sed nunc dolor. Sed sed rutrum mi. Sed malesuada lobortis aliquet. Praesent vel nisl in massa dictum varius. Nullam nec magna arcu. Vivamus at dolor id neque iaculis rhoncus. Suspendisse aliquet libero felis, vitae tincidunt lectus placerat a. Duis a dictum lacus. Donec ut libero diam. Phasellus scelerisque mattis felis, ut malesuada neque pellentesque et. In arcu sem, convallis ac fermentum sed, consequat et tellus. Donec eu orci quis sapien aliquet porttitor sit amet eu metus. Etiam non sodales risus. Suspendisse placerat sem eu dolor porta tempor.</p>
                    <p class="read-more">
<a href="#">Read more...</a> 
                    </p>
                </article>
                <!-- Aside, right sidebar, author bio-->
                <aside>
                    <!-- Social media icons -->
                    <div class="iconContact">
<a href="#"> 
<i class="fa fa-facebook fa-3x"></i> 
</a>

                    </div>
                    <div class="iconContact">
<a href="#"> 
<i class="fa fa-twitter fa-3x"></i> 
</a>

                    </div>
                    <div class="iconContact">
<a href="#"> 
<i class="fa fa-instagram fa-3x"></i> 
</a>

                    </div>
                    <div class="iconContact">
<a href="#"> 
<i class="fa fa-pinterest fa-3x"></i> 
</a>

                    </div>
                    <!-- Social media icons end -->
                    <figure>
                        <img src="images/author-300.png" alt="Picture of the author of the food blog" width="300" height="294">
                    </figure>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent volutpat lectus quis tellus venenatis pellentesque. Vestibulum et aliquam ipsum. Suspendisse dapibus nibh arcu. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed posuere urna ut tortor vulputate porttitor. Maecenas eget ultrices felis, at luctus justo. Etiam blandit placerat lacus vel tristique.</p>
                </aside>
                <!-- Aside, right sidebar, featured posts -->
                <aside>
                    
<h1 class="featured">Featured Posts</h1>

<a href="#"> 
<figure>
  <img src="images/smoothie-300.jpg" alt="Delicious Green Smoothie with bananas and strawberries on the side" width="300" height="200">
</figure>
</a>

<a href="#"> 
<figure>
  <img src="images/oatmeal-300.jpg" alt="Healthy oatmeal with bananas and nuts sprinkled on top" width="300" height="200">
</figure>
</a>

                </aside>
                <div class="clear"></div>
        </div>
        <!-- end wrapper -->
        </main>
        <footer class="clear"></footer>
    </body>

</html>

1个回答

2

我建议使用弹性盒模型来实现您想要的效果。

删除所有浮动。

将包含元素<main>转换为弹性盒容器:

main {
    display: flex;
}

将所有左侧内容放入<div id="left">中,将所有右侧内容放入<div id="right">中。
现在让我们从这些div中移除grow和shrink属性。
#left, #right {
    flex-grow: 0;
    flex-shrink: 0;
}

现在,当您想要使用移动布局时,您可以将媒体查询设置为flex-direction: column;flex-grow: 1;,这样右侧内容将下移到左侧内容下方。
您可能需要将分配给每个文章和旁边的某些属性移动到它们各自的左/右列标记中。
这是一个jsfiddle 这是一个有关flexbox的好参考。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接