【问题标题】:formatting of the divs(responsive)div的格式(响应式)
【发布时间】:2015-06-17 13:12:43
【问题描述】:
<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Responsive</title>
    <style>
        .city {
            float: left;
            margin: 5px;
            padding: 15px;
            width: 100%;
            height: 300px;
            border: 1px solid black;
        }

        .city2 {
            float: left;
            margin: 5px;
            padding: 15px;
            width: 100%;
            height: 100px;
            border: 1px solid red;
        }
    </style>
</head>
<body background="page-bg.jpg">

    <h1>W3Schools Demo</h1>
    <h2>Resize this responsive page!</h2>

    <div style="width:100%">
        <div style="width:21%;" >
            <div class="city2">
                <h2>London</h2>
                <p>London is the capital city of England.</p>

            </div>
        </div>
        <div style="width:21%;float:left;">
            <div class="city2">
                <h2>Paris</h2>

            </div>
        </div>
        <div style="width:21%;">
            <div class="city2">
                <h2>Tokyo</h2>

            </div>
        </div>


        <div style="width:21%;">
            <div class="city2">
                <h2>Istanbul</h2>

            </div>
        </div>
    </div>





    <div style="width:100%">

        <div style="width:33.3%">
            <div class="city">
                <h2>London</h2>
                <p>London is the capital city of England.</p>
                <p>It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
            </div>

        </div>

        <div style="width:33.3%">
            <div class="city">
                <h2>Paris</h2>
                <p>Paris is the capital and most populous city of France.</p>
            </div>
        </div>

        <div style="width:33.3%">
            <div class="city">
                <h2>Tokyo</h2>
                <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.</p>
            </div>
        </div>
    </div>
</body>
</html>

上面是我的代码,它在第 1 行有 4 个 div,在第 2 行有 3 个 div。我试图让 div 响应。为此,代码中提到了 city,city2 的 css。呈现代码时,前四个 div 不是直线。另外,当我调整窗口大小时,排列是响应式的,但是这 4 个 div 前面有空白。请帮助我如何让它们响应并占据屏幕的各个维度。

【问题讨论】:

    标签: html css responsive-design


    【解决方案1】:

    我想你想要这样的东西,希望对你有帮助。

            .city {
                float: left;
                margin: 5px;
                padding: 15px;
                width: 100%;
                height: 300px;
                border: 1px solid black;
                box-sizing:border-box;
            }
    
            .city2 {
                float: left;
                margin: 5px;
                padding: 15px;
                width: 100%;
                min-height: 120px;
                border: 1px solid red;
                box-sizing:border-box;
    
            }
        <h1>W3Schools Demo</h1>
        <h2>Resize this responsive page!</h2>
    
        <div >
            <div style="width:21%;float:left;margin-right: 10px;">
                <div class="city2">
                    <h2>London</h2>
                    <p>London is the capital city of England.</p>
    
                </div>
            </div>
            <div style="width:21%;float:left;margin-right: 10px;">
                <div class="city2">
                    <h2>Paris</h2>
                    <p>London is the capital city of England.</p>
    
                </div>
            </div>
            <div style="width:21%;float:left;margin-right: 10px;">
                <div class="city2">
                    <h2>Tokyo</h2>
                    <p>London is the capital city of England.</p>
    
                </div>
            </div>
    
    
            <div style="width:21%;float:left;margin-right: 10px;">
                <div class="city2">
                    <h2>Istanbul</h2>
    				<p>London is the capital city of England.</p>
                </div>
            </div>
        </div>
    
    
    
    
    
        <div>
    
            <div style="width:32%;float:left;margin-right:8px">
                <div class="city">
                    <h2>London</h2>
                    <p>London is the capital city of England.</p>
                    <p>It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
                </div>
    
            </div>
    
            <div style="width:32%;float:left;margin-right:8px">
                <div class="city">
                    <h2>Paris</h2>
                    <p>Paris is the capital and most populous city of France.</p>
                </div>
            </div>
    
    		<div style="width:32%;float:left;">
                <div class="city">
                    <h2>Tokyo</h2>
                    <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.</p>
                </div>
            </div>
        </div>

    【讨论】:

    • 感谢您的回复。这对我有一点帮助。请查看我的编辑(添加了新图片)。
    【解决方案2】:

    简单的改进,希望能帮助你开始并且你会改进这个例子:)

    *{
    	box-sizing:border-box;
    	margin:0;
    	padding:0;
    }
    body{
    	background-image: url("page-bg.jpg");
    	font-size:80%;
    	max-width:1200px;
    	margin:0 auto;
    }
    h1, h2{
    	padding:5px;
    }
    .row1{
    	width:100%;
    }
    .one{
    	width:21%;
    	min-width:300px;
    	float:left;
    	padding:5px;
    	margin:0 auto;
    }
    .inside {
        float: left;
        padding:5px;
        width:100%;
        min-height:120px;
        border: 1px solid red;
    }
    .row2{
    	width:100%;
    	clear:both;
    }
    .two{
    	width:33%;
    	min-width:400px;
    	float:left;
    	padding:5px;
    	margin:0 auto;
    }
    .city {
    	float: left;
        padding: 15px;
        width: 100%;
        min-height: 300px;
        border: 1px solid black;
    }
    <!DOCTYPE html>
    <html lang="en-US">
    <head>
        <title>Responsive</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
    
        <h1>Demo</h1>
        <h2>Responsive page</h2>
    
        <div class="row1">
            <div class="one">
                <div class="inside">
                    <h2>London</h2>
                    <p>London is the capital city of England.</p>
                </div>
            </div>
            <div class="one">
                <div class="inside">
                    <h2>Paris</h2>
                </div>
            </div>
            <div class="one">
                <div class="inside">
                    <h2>Tokyo</h2>
                </div>
            </div>
            <div class="one">
                <div class="inside">
                    <h2>Istanbul</h2>
                </div>
            </div>
        </div>
    
    
        <div class="row2">
            <div class="two">
                <div class="city">
                    <h2>London</h2>
                    <p>London is the capital city of England.</p>
                    <p>It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
                </div>
            </div>
            <div class="two">
                <div class="city">
                    <h2>Paris</h2>
                    <p>Paris is the capital and most populous city of France.</p>
                </div>
            </div>
            <div class="two">
                <div class="city">
                    <h2>Tokyo</h2>
                    <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, and the most populous metropolitan area in the world.</p>
                </div>
            </div>
        </div>
    </body>
    </html>

    【讨论】:

    • 好的,你只需要为.one类更改一件事:删除width:21%并粘贴width:100%。多余的空间现在被 div 占据了。
    猜你喜欢
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2019-07-18
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多