html - div的格式(响应式)

标签 html css responsive-design

<!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 响应。为此,代码中将 css 称为 city,city2。呈现代码时,前四个 div 不会排成一条直线。另外,当我调整窗口大小时,排列是响应式的,但那 4 个 div 前面有空白。请帮助我如何让它们响应并在屏幕的每个维度占据屏幕。

Responsive when window resized

最佳答案

我想你想要这样的东西,希望它能帮助你。

        .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>

关于html - div的格式(响应式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30881981/

相关文章:

css - 响应式网站布局入门

javascript - 从html调用js命名空间函数

html - 保持网页的基本布局相同

responsive-design - 将 jquery isotope 与 Foundation 集成

css - 左对齐标签 - 右对齐选择元素 (CSS)

css - 在 svg 中的样式标签上添加多个谷歌字体

javascript - 响应式设计和点击事件

HTML/CSS : Bootstrap Slideshow Carousel - Center Align Bubble Numbers

javascript - 在 mouseup 上获取元素下方的 div 目标

jquery - 禁用除选定复选框之外的所有复选框