CSS Media查询和定位元素

时间:2019-01-03 15:53:47

标签: html css

我正在尝试做2件事情。

  1. 让带有id鸡肉,牛肉和寿司的元素粘贴到其父元素的右上角。
  2. 根据屏幕尺寸使页面具有响应性,而无需使用引导程序等外部框架。我正在尝试使用媒体查询,但是页面仍然没有更改以匹配匹配的图像。

谢谢。

不同屏幕尺寸的布局图:

desktop

tablet

mobile

* {
  box-sizing: border-box;
}

h1 {
  margin-bottom: 15px;
}

p {
  font-family: Helvetica;
  color: white;
  text-align: center;
}

hr {
  border: solid 3px;
  black;
}

#chicken {
  float: right;
  padding: 10px;
  background-color: #ff6666;
  display: block;
  color: black;
}

#beef {
  float: right;
  padding: 10px;
  background-color: darkred;
  display: block;
  color: white;
}

#sushi {
  float: right;
  padding: 10px;
  background-color: #e6ac00;
  display: block;
  color: black;
}


/*Simple Responsive Framework.*/

.row {
  width: 100%;
}


/********** Desktop devices **********/

@media (min-width: 992px) {
  .col-lg-1,
  .col-lg-2,
  .col-lg-3,
  .col-lg-4,
  .col-lg-5,
  .col-lg-6,
  .col-lg-7,
  .col-lg-8,
  .col-lg-9,
  .col-lg-10,
  .col-lg-11,
  .col-lg-12 {
    float: left;
    border: 1px solid black;
    background-color: gray;
    margin: 20px;
  }
  .col-lg-1 {
    width: 33.33%;
  }
  .col-lg-2 {
    width: 33.33%;
  }
  .col-lg-3 {
    width: 33.33%;
  }
}


/********** Tablet devices *********/

@media (min-width: 768px) and (max-width: 991px) {
  .col-md-1,
  .col-md-2,
  .col-md-3,
  .col-md-4,
  .col-md-5,
  .col-md-6,
  .col-md-7,
  .col-md-8,
  .col-md-9,
  .col-md-10,
  .col-md-11,
  .col-md-12 {
    float: left;
    border: 1px solid black;
    background-color: gray;
    margin: 20px;
  }
  .col-md-1 {
    width: 50%;
  }
  .col-md-2 {
    width: 50%;
  }
  .col-md-3 {
    width: 100%;
  }
}


/********* Mobile devices *********/

@media (max-width: 767px) {
  .col-sm-1,
  .col-sm-2,
  .col-sm-3,
  .col-sm-4,
  .col-sm-5,
  .col-sm-6,
  .col-sm-7,
  .col-sm-8,
  .col-sm-9,
  .col-sm-10,
  .col-sm-11,
  .col-sm-12 {
    float: left;
    border: 1px solid black;
    background-color: gray;
    margin: 20px;
  }
  .col-sm-1 {
    width: 100%;
  }
  .col-sm-2 {
    width: 100%;
  }
  .col-sm-3 {
    width: 100%;
  }
}
<h1>Our Menu</h1>
<div class="row">
  <div class="col-lg-3 col-md-6">
    <p id="chicken">Chicken</p>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
      in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>
  </div>
  <div class="col-lg-3 col-md-6">
    <p id="beef">Beef</p>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
      in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>
  </div>
  <div class="col-lg-3 col-md-6">
    <p id="sushi">Sushi</p>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
      in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>
  </div>
</div>
<div class="row">
  <hr>
</div>

1 个答案:

答案 0 :(得分:0)

您正在使用margin来容器div,其宽度以百分比表示。例如台式机,占33.33%。通常,为了做出响应,请尽量避免使用父容器的边距,并使用填充或其他技巧来实现设计。我已经修复了台式机和移动设备的代码。平板电脑非常简单。 `

   * {
    box-sizing: border-box;
}
h1 {
    margin-bottom: 15px;
}

p {
    font-family: Helvetica;
    color: white;
    text-align: center;
}

hr {
    border: solid 3px black;
}
#chicken {
    float: right;
    padding: 10px;
    background-color: #ff6666;
    display: block;
    color: black;
    margin: 0;
}

#beef {
    float: right;
    padding: 10px;
    background-color: darkred;
    display: block;
    color: white;
    margin: 0;
}

#sushi {
    float: right;
    padding: 10px;
    background-color: #e6ac00;
    display: block;
    color: black;
    margin: 0;
}
.chickenContainer, .beefContainer, .sushiContainer{
    border: 1px solid black;
    background-color: gray;
} 

/*Simple Responsive Framework.*/
.row {
    width: 100%;
}

/********** Desktop devices **********/
@media (min-width: 992px) {
    .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6,
    .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12 {
        float: left;
        box-sizing: border-box;
        padding: 20px;
    }
    .col-lg-1 {
        width: 33.33%;
    }
    .col-lg-2 {
        width: 33.33%;
    }
    .col-lg-3 {
        width: 33.33%;
    }

}

/********** Tablet devices *********/
@media (min-width: 768px) and (max-width: 991px) {
    .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6,
    .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12 {
        float: left;
        box-sizing: border-box;
        padding: 20px;
    }
    .col-md-1 {
        width: 50%;
    }
    .col-md-2 {
        width: 50%;
    }
    .col-md-3 {
        width: 100%;
    }
}

/********* Mobile devices *********/
@media (max-width: 767px) {
    .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6,
    .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
        float: left;      
        box-sizing: border-box;
        padding: 20px;
    }
    .col-sm-1 {
        width: 100%;
    }
    .col-sm-2 {
        width: 100%;
    }
    .col-sm-3 {
        width: 100%;
    }

}
 <h1>Our Menu</h1>
    <div class="row">
        <div class="col-lg-3 col-md-6">
          <div class="chickenContainer">
            <p id="chicken">Chicken</p>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
                <div class="beefContainer">
            <p id="beef">Beef</p>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
                <div class="sushiContainer">
            <p id="sushi">Sushi</p>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
            </div>
        </div>
    </div>

`