gpt4 book ai didi

html - CSS 媒体查询和定位元素

转载 作者:行者123 更新时间:2023-11-28 02:25:49 25 4
gpt4 key购买 nike

我正在尝试做两件事。

  1. 让 id 为 chicken、beef 和 sushi 的元素粘在它们父元素的右上角。
  2. 根据屏幕尺寸使页面响应,而不使用 Bootstrap 或其他任何外部框架。我正在尝试使用媒体查询,但我的页面仍然没有更改以匹配匹配的图像。

谢谢。

不同屏幕尺寸的布局图:

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>

最佳答案

您正在为容器 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>

`

关于html - CSS 媒体查询和定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54025670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com