如何通过媒体查询来适应较小的设备。

huangapple go评论62阅读模式
英文:

how to adapt this website for smaller devices via media

问题

我的网站在较小的设备上没有响应。我尝试在我的代码中使用@media,但没有效果,所以我真的不知道接下来该怎么办,有任何帮助吗?

我的CSS代码:

/* 基本导航栏样式 */
.navbar {
  background-color: #333;
  overflow: hidden;
}
/* 导航栏链接样式 */
.navbar a {
  float: left;
  color: white;
  text-align: left;
  padding: 10px 126.7px;
  text-decoration: none;
  font-size: 1.25em;
  text-size-adjust: center;
  line-height: 70px;
  transform: rotate(-10);
}
/* 鼠标悬停链接样式 */
.navbar a:hover {
  background-color: #ddd;
  color: black;
}
.container {
  background: linear-gradient(45deg, blueviolet, lightgreen);
  padding: 15px 9%;
  padding-top: 20px;
}
.container .card-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  padding-top: 80px;
}
.grid-item:nth-child(4) {
  grid-row-start: 3;
  padding: 50px;
}
.container .card-container .card {
  border-radius: 5px;
  background: #fff;
  width: 450px;
  height: 600px;
  border-style: solid;
  padding-top: 35px;
}
.container .card-container .card:nth-child(1) {
  background: #333;
}
.container .card-container .card:first-child h1:first-child {
  color: white;
}
.container .card-container .card:first-child h3 {
  color: white;
}
h1:nth-of-type(2) {
  color: chocolate;
}
h1 {
  display: inline;
  margin: 15px;
  margin-left: 50px;
  margin-right: 50px;
}
.container .card-container .card .text {
  padding-top: 45px;
  font-size: 1em;
  text-align: left;
  margin-left: 50px;
  margin-right: 50px;
}
.container .card-container .card:first-child .text {
  padding-top: 45px;
  font-size: 1em;
  text-align: left;
  margin-left: 50px;
  margin-right: 50px;
  color: white;
}
h3 {
  margin-left: 50px;
  margin-right: 50px;
  text-align: left;
}
.container .card-container .card:first-child h2:nth-of-type(1) {
  color: white;
}
h2 {
  display: inline;
  text-align: center;
  margin-left: 50px;
  margin-right: 50px;
}
h2:nth-of-type(2) {
  color: chocolate;
}
@media (max-width: 768px) {
  .container {
    padding: 20px;
  }
}

我尝试写了一些媒体查询,但没有效果,不具备响应性,如果有人知道还可以做什么,请告诉我。我不知道还应该写些什么,所以只是随便输入一些东西。

英文:

Hi My website is not being responsive on smaller devices. I tried to use @media in my code but that doesn't work so I don't really knwo what to do next any help.

my css code:

 /* Základní styly pro navbar */
 .navbar {
  background-color: #333;
  overflow: hidden;
}
/* Styly pro odkazy v navbaru */
.navbar a {
  float: left ;
  color: white;
  text-align:left ;
  padding: 10px 126.7px;
  text-decoration: none;
  font-size: 1.25em;
  text-size-adjust: center;
  line-height: 70px;
  transform: rotate(-10);
}
/* Styly pro odkazy při najetí myší */
.navbar a:hover {
  background-color: #ddd;
  color: black;
}
.container{
  background:linear-gradient(45deg, blueviolet, lightgreen);
  padding:15px 9%;
  padding-top: 20px; 
}
.container .card-container{
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap:15px;
  padding-top: 80px;
}
.grid-item:nth-child(4) {
  grid-row-start: 3;
  padding: 50px;
}
.container .card-container .card{
  border-radius: 5px;
  background: #fff;
  width: 450px;
  height: 600px;
  border-style: solid;
 padding-top: 35px; 
}
.container .card-container .card:nth-child(1){
background: #333;
} 
.container .card-container .card:first-child h1:first-child{ 
  color: white;
}
.container .card-container .card:first-child h3{ 
  color: white;
}
h1:nth-of-type(2){ 
  color: chocolate;
}
h1{  
  display: inline;
  margin: 15px;
  margin-left: 50px;
margin-right: 50px; 
}
.container .card-container .card .text{
padding-top: 45px;
font-size: 1em ;
text-align: left;
margin-left: 50px;
margin-right: 50px;
}
.container .card-container .card:first-child .text{
  padding-top: 45px;
  font-size: 1em ;
  text-align: left;
  margin-left: 50px;
  margin-right: 50px;
  color: white; 
  }
h3{
  margin-left: 50px;
margin-right: 50px; 
  text-align: left; 
}
.container .card-container .card:first-child h2:nth-of-type(1){ 
  color: white;
}
h2{
  display: inline; 
  text-align: center;
  margin-left: 50px;
margin-right: 50px;
}
h2:nth-of-type(2){
  
  color: chocolate;
}
@media (max-width:768px){
  .container{
      padding:20px;
  }
}

I
tried to write some media forms but that doesn't work, its not responsive, if anyone know what to do else. I dont know what should I write more so I am just typing some things.

答案1

得分: -1

尝试像这样编写媒体查询:(它将在屏幕宽度小于等于768px时生效)

@media only screen and (max-width: 768px){
    .container{
      padding:20px;
    }
    
    //尝试在这些花括号内修改其余选择器的样式。
}

如果你想从另一个更小的屏幕尺寸开始进行更改,那么可以再次编写另一个媒体查询,像这样:

@media only screen and (max-width: 500px){
    //编写你的代码(它将在屏幕宽度小于等于500px时生效)
}

如果你想将所有卡片显示在单列中(每行一个),那么我认为这样可以实现:

@media only screen and (max-width: 768px){
    .container .card-container{
        grid-template-columns: 1fr;
    }
}

你可能需要进行一些其他调整。

英文:

Try writing media queries like this: (It will take effect from 768px screen width and below that)

@media only screen and (max-width: 768px){
    .container{
      padding:20px;
    }
    
    //Try with the rest of your selectors and make the changes within these curly brackets.
}

If you want to make changes from another smaller screen size then you can again write another media query like this:

@media only screen and (max-width: 500px){
    //Write your codes (It will take effect from 500px screen width and below that)
}

If you want to show all the cards in a single column (one in each row) then I think this will do it:

@media only screen and (max-width: 768px){
    .container .card-container{
        grid-template-columns: 1fr;
    }
}

You may need to make some other adjustments.

huangapple
  • 本文由 发表于 2023年6月25日 23:19:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定