响应式设计断点

时间:2016-04-16 15:24:53

标签: html css responsive

我正试图摆脱引导程序,实际上是自己控制断点。在继续学习之前,我想知道我是否走在正确的轨道上。所以这样做是正确的:

HTML:

<!DOCTYPE html>
<html>

<head>
    <link type="text/css" rel="stylesheet" href="style.css"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
    <div id="container">
        <img src="banner.png"/>
            <section id="left-column">
                <p>This is the left column</p>
            </section>
            <aside id="right-column">   
                <p>This is the right column</p>
            </aside>
    </div>
    <!-- /Container -->

</body>
</html>

CSS:

#container {
    width: 960px;
    margin: 0px auto;
}

#left-column {
    width: 700px;
    float: left;
    background-color: red;
}
#right-column {
    width: 260px;
    float: left;
    background-color: blue;
}

/* Tablet & Computer */
@media screen and (max-width:959px) {
    #container {
        width:100%;
    }
    #left-column {
        width: 70%;
    }
    #right-column {
        width: 30%;
    }
    img {
        width: 100%
    }
}   

/* Phone */
@media screen and (max-width:640px) {
    #left-column {
        width: 100%;
    }
    #right-column {
        width: 100%;
    }
}
/* Stop Scaling */
@media screen and (max-width:320px) {
    #container {
        width: 320px;
}

3 个答案:

答案 0 :(得分:2)

我不认为使用媒体查询有好/坏的方式,只是你想要的方式:)

Bootstrap通过提供不同的断点来应用于移动设备/平板电脑/桌面以及不同的类名来帮助您实现布局风格,但您可以通过使用您想要的断点而无需Bootstrap来自行完成。 (请参阅medias查询有关Bootstrap的信息:http://getbootstrap.com/css/#grid-media-queries

所以你的方法是正确的。请关闭最后一个括号:

/* Stop Scaling */
@media screen and (max-width:320px) {
    #container {
        width: 320px;
    }
}

此外,我更喜欢使用移动宽度(%),因为它将占据屏幕的整个宽度。在你上一次媒体查询的情况下,选择的宽度是没用的(我不知道很多设备的宽度是320px)

请参阅此处用于帮助您选择媒体查询的常用设备: http://mydevice.io/devices/

答案 1 :(得分:1)

你可以摆脱Bootstrap但你可以在断点中使用他们的方法

超小型设备电话 <768px

小型设备平板电脑 ≥768px

中型设备桌面 ≥992px

大型设备桌面 ≥1200px

您需要为列指定网格系统,而不是硬编码列宽度

喜欢

.col1{
  width:8.33333333%
}

.col2{
  width:16.66666667%
}

.col3{
  width:25%
}

.col4{
  width:33.33333333%
}

.col5{
  width:14.666667%
}

.col6{
  width:50%
}

.col7{
  width:58.33333333%
}

.col8{
  width:66.6666667%
}

.col9{
  width:75%
}

.col10{
  width:83.33333333%
}

.col11{
  width:91.6666666%
}
.col12{
  width:100%
}

最后,您必须为列定义行,以避免因浮动列而导致高度冲突

.row{
  margin-left:-15px;
  margin-right:-15px;
 }
.row{:after{
  content:'';
  display:block;
  clear:both
}
.col{   //general properties of columns
  padding:15px;
  float:left; 
}

更新:如果您没有通过row清除clear:both;课程后的花车,您会发现列容器的高度始终为0,即使列有内容这是因为float从容器的高度中减去元素的高度

您还需要提供margin-left:-15px;margin-right:-15px;,以使该行的第一列和最后一列与页面内容保持一致

答案 2 :(得分:0)

你可以这样做,但我建议使用less或sass,否则会非常乏味(你也应该使用autoprefixer!)。从技术上讲,你应该设置类而不是id,因为它的破坏性较小。您可能希望添加最小宽度,并且您应该验证由于四舍五入问题,70%+ 30%不能超过100%。

Flexbox是一种新的/酷炫的网格方式,例如, https://css-tricks.com/snippets/css/a-guide-to-flexbox/,但直到我们在css中获得正确的网格布局(例如https://css-tricks.com/snippets/css/complete-guide-grid/)。

相关问题