多栏布局的设置方法

最近面试的时候被问到如何设置一栏定宽,一栏自适应的两栏布局,回答得不好,因此又回去阅读了相关文档,现整理一下以作记录。


1. float方法

两栏布局

对于两栏布局,只需给左栏设置宽度,再设置float:left,右栏元素设置margin-left:为左边元素的宽度(右栏元素不可设置宽度,不然不能实现自适应),这样就完成了左栏固定,右栏自适应的两栏布局。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>float两栏布局</title>
    <style>
        header, footer { height: 100px;margin: 4px 0;background-color: #409BF7;clear: both; }
        nav  { background-color: #F4EA26;width: 200px;height: 100px;float: left; }
        aside { background-color: #01F1A4;height: 250px;margin-left: 204px; }
    </style>
</head>
<body>
    <header>header</header>
    <nav>nav1</nav>
    <aside>aside1</aside>
    <footer>footer</footer>
</body>
</html>

这里需要强调的是右栏不可设置宽度,不管用什么单位,都不会随着浏览器窗口自适应变化。如在此处给右栏设置width: 100%; 则右栏的宽度将是父元素的宽度,且由于设置了margin-left: 204px ,因此右栏会超出窗口204px。margin占据的空间并不算在width中。

另外需要注意的是,当浮动的那一栏的高度大于自适应的那一栏的高度时,页脚会填充在浮动元素(此处即左栏)四周,因此需要给footer 设置 clear: both;清除两侧的浮动,独占一行。

在浏览器窗口比较小的时候,左侧的浮动元素的宽度会大于右侧元素的宽度,但这样显示的效果很差,因此,我考虑利用媒体查询的特性来处理小屏幕的布局方式。

    @media (max-width: 768px){
        nav, aside { float: none;width: auto;margin: 0; }
    }

这里只需要去除浮动并将各自的width设置为auto,两个元素都会恢复到原来的文档流,即成为占据父元素全部宽度的盒子。由于右侧的盒子之前制定了外边距,因此需要在此处指定外边距为0。

三栏布局

对于三栏布局也是相同的原理,左右的盒子设置好宽度之后,左边的盒子设置float:left; 右边设置float:right;中间设置左右margin即可,这样就完成了左右固定,中间自适应的三栏浮动布局。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>float三栏布局</title>
    <style>
        header, footer { height: 100px;margin: 4px 0;background-color: #409BF7;clear: both; }
        #content { background-color: #E4E5E5;margin: 5px 0; }
        #content .nav  { background-color: #F4EA26;width: 200px;height: 200px;float: left; }
        #content .aside { background-color: #ED4EF2;width: 200px;height: 250px;float: right; }
        #content .article { background-color: #01F1A4;height: 450px;margin: 0 204px; }
    </style>
</head>
<body>
    <header>header</header>
    <div id="content">
        <div class="nav">nav</div>       
        <div class="aside">aside</div>
        <div class="article">article</div>
    </div>
    <footer>footer</footer>
</body>
</html>

如果不给中间的元素设置左右margin值,那么该元素也会围绕在左右两个浮动的盒子四周。
注意,如果仔细观察的话,发现id为content的div元素上实际只包含着article元素,另外两个浮动元素已经脱离了文档流,不再受div#content的宽高限制。

同样,利用媒体查询来解决屏幕空间不足的问题。

        @media (max-width: 768px){
            #content .nav, #content .aside, #content .article { float: none;width: auto;margin: 0; }
        }
        @media (min-width: 768px) and (max-width: 979px) {
            #content .aside { display: none; }
            #content .article  { margin-right: 0; }
        }

这里另外制定了在屏幕宽度为 768px到979px之间的处理方式,将右侧的aside隐藏掉(右侧的侧边栏起辅助说明的作用,可以暂时不显示)。但但屏幕再小的话,就恢复成正常的位置,即从上到下排列,并布满整个父元素的宽度。


先暂时写这么多,下一篇再讲讲flex布局。欢迎大家留言指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码: 1. HTML 页面代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>多布局</title> <style type="text/css"> body { margin: 0; padding: 0; } #header { height: 50px; background-color: #ccc; } #nav { height: 30px; background-color: #999; } #content { width: 70%; float: left; background-color: #f5f5f5; } #sidebar { width: 30%; float: left; background-color: #eee; } #footer { height: 50px; background-color: #ccc; clear: both; } </style> </head> <body> <div id="header">这是头部</div> <div id="nav"> <ul> <li><a href="#section1">目一</a></li> <li><a href="#section2">目二</a></li> <li><a href="#section3">目三</a></li> </ul> </div> <div id="content"> <section id="section1"> <h2>目一</h2> <p>这是目一的内容。</p> </section> <section id="section2"> <h2>目二</h2> <p>这是目二的内容。</p> </section> <section id="section3"> <h2>目三</h2> <p>这是目三的内容。</p> </section> </div> <div id="sidebar"> <h3>侧</h3> <p>这是侧的内容。</p> </div> <div id="footer">这是底部</div> </body> </html> ``` 2. Servlet 代码: ```java import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HomePageServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.getWriter().print(getHomePageHtml()); } private String getHomePageHtml() { // 读取 HTML 文件 String html = ""; try { html = new String(Files.readAllBytes(Paths.get("index.html")), StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } return html; } } ``` 3. 部署到 Tomcat 上: 将 HTML 页面和 Servlet 代码打包成 WAR 文件,然后将 WAR 文件部署到 Tomcat 上即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值