【问题标题】:How to change CSS when the screen size changes屏幕大小变化时如何更改 CSS
【发布时间】:2017-09-02 01:04:31
【问题描述】:

所以我研究了我的网站的代码,当屏幕尺寸发生变化时,这些代码会改变我的代码某些部分的 css 甚至 html。我首先要研究的是,当我在 Windows 10 或 Mac 上将我的网站放入分屏时,我希望标题“某些标题”移动到标题部分的左侧。我希望这将有助于我在屏幕尺寸发生变化时更改网站的某些方面。我的代码如下。

 <!DOCTYPE html>
    <head>
        <meta charset="UTF-8">
        <title>Some Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" href= "Logo.png" type="img/SVG" style="width: 100%; height: 100%">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
        <link rel = "stylesheet" href="stylesheet.css">
    </head>
    <body>
    
    <div class="Header" id="myHeader">
        <a class = "headerLogo">
            <a href="file:///C:/Noah's%20stuff/Home.html" ><h1 style="color:white; font-family: Verdana; font-style: italic; font-size: x-large;
            text-align: center; padding-top: 20px">Some Title</h1></a>
            <style>
                a{text-decoration: none;}
                a:hover{
                    text-decoration:none;
                }
            </style>
    
        </a>
        <div class="socialmedia">
            <a class = "Facebook">
                <a href="https://www.facebook.com/" target="_blank"><img src = "https://images.seeklogo.net/2016/09/facebook-icon-preview-1.png" width="50px" height="50px"></a>
            </a>
            <a class = "Instagram">
                <a href="https://www.instagram.com/"><img src = "https://images.seeklogo.net/2016/06/Instagram-logo.png"  width="50px" height="50px"></a>
            </a>
            <a class = "Youtube">
                <a href="https://www.youtube.com/channel/" target="_blank"><img src = "https://images.seeklogo.net/2016/06/YouTube-icon.png" width="50px" height="50px"></a>
            </a>
            <a class = preorder>
                <button style = "background-color: white;">Pre-Order</button>
    
            </a>
        </div>
    </div>

My CSS

body {
    margin: 0;

}

.Header {
    position: fixed;
    z-index:1;
    width: 100%;
    height: 70px;
    background-color: black;
    text-align: right;
}
.headerLogo {

}

.socialmedia {
    position: fixed;
    right: 100px;
    top: 35px;
    transform: translate(0, -50%);
    display: flex;
    /* add this */
    align-items: center;
    /* add this */
}

.preorder button {
    background-color: white;
    border: 0;
    height: 35px;
    width: 110px;
    margin-left: 35px;
}

.footer {
    display: flex;
    align-items: center;
    width: 100%;
    height: 90px;
    background-color: black;
}

.img-fluid{
    width: inherit;
    height: 782px;
}

.mySlides~.mySlides {
    position: absolute;
    top: 0;
    left: 100%;
    transition: 0.7s;
}

【问题讨论】:

  • 我知道我读过它们我只是不明白什么是最大宽度:用于手机和计算机上的分屏
  • 对于手机来说,像 480px 这样的东西是通用的(这就是 Bootstrap 用于移动断点的东西),但我不知道分屏。
  • 您已经包含了 Bootstrap 4。他们已经为您完成了所有艰苦的工作。只需使用他们的网格系统。 v4-alpha.getbootstrap.com/layout/grid
  • 我对如何在我的网站中实现引导代码有点困惑,我知道我需要使用 标签。上次我得到了一些帮助,因为我不知道在哪里可以找到 css 样式表。

标签: html css screen-size


【解决方案1】:

媒体查询可以帮助您为不同的屏幕尺寸定义不同的样式。另外,我建议使用不同的 HTML 项目,例如:

<!DOCTYPE html>
<head>
  <meta charset="UTF-8">
  <title>Some Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href= "Logo.png" type="img/SVG" style="width: 100%; height: 100%">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
  <style>
    body {
        margin: 0;

    }

    .Header {
        background-color: black;
        text-align: right;
    }

    .socialmedia {
        transform: translate(0, -50%);
        align-items: center;
    }

    .socialmedia a{
      display:inline;
    }

    h1 {
      color:white; 
      font-family: Verdana; 
      font-style: italic; 
      font-size: x-large;
      text-align: center; padding-top: 20px;
    }

    @media (max-width:700px){
        .headerLogo h1 {
            text-align:left;
        }

    }
    @media (min-width:1000px){
      .socialmedia {
        margin-right:100px;
      }
    }
    .preorder button {
        background-color: white;
        border: 0;
        height: 35px;
        width: 110px;
        margin-left: 35px;
    }

    .footer {
        display: flex;
        align-items: center;
        width: 100%;
        height: 90px;
        background-color: black;
    }

    .img-fluid{
        width: inherit;
        height: 782px;
    }

    .mySlides~.mySlides {
        position: absolute;
        top: 0;
        left: 100%;
        transition: 0.7s;
    }
    a{text-decoration: none;}
    a:hover{
        text-decoration:none;
    }
  </style>
</head>
<body>    
<div class="Header" id="myHeader">
  <a class="headerLogo" href="file:///C:/Noah's%20stuff/Home.html" ><h1>Some Title</h1></a>
  <div class="socialmedia">
      <a class="Facebook" href="https://www.facebook.com/" target="_blank"><img src = "https://images.seeklogo.net/2016/09/facebook-icon-preview-1.png" width="50px" height="50px"></a>
      <a class="Instagram" href="https://www.instagram.com/"><img src = "https://images.seeklogo.net/2016/06/Instagram-logo.png"  width="50px" height="50px"></a>
      <a class="Youtube" href="https://www.youtube.com/channel/" target="_blank"><img src = "https://images.seeklogo.net/2016/06/YouTube-icon.png" width="50px" height="50px"></a>
      <button style = "background-color: white;">Pre-Order</button>
  </div>
</div>
</body>
</html>

当屏幕尺寸 = 1000 时,社交媒体框将在右侧添加边距。

【讨论】:

  • 对我也很好。只需要添加 !important
【解决方案2】:

例子

  @media screen and (max-width: 600px){
    ul.topnav li.right,
    ul.topnav li {float: none;}
    .seged{position: relative;}
    #lab{border-right: 0px;}
}

【讨论】:

    【解决方案3】:

    我们可以通过两种方式做到这一点:-

    1) 使用媒体查询。请参考here

    2) 您也可以使用简单的 JavaScript 来实现这一点。见下文:-

    $(window).on('resize', function() {
        if($(window).width() > 600) {
            $('#body').addClass('limit1200');
            $('#body').removeClass('limit400');
        }
        else {
            $('#body').addClass('limit400');
            $('#body').removeClass('limit1200');
        }
    })
    .limit400 h1 { font-size:12px; }
    .limit1200 h1 { font-size:50px; }
    <div id="body" class="limit400">
        <h1>Hello :Professor</h1>
        <span>[ change the screen width to understant the effect ]</span>
    </div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    我还在 codepen 上创建了一个演示。 See This

    感谢您对我的包容。

    【讨论】:

      【解决方案4】:

      如 cmets 中所述,您需要媒体查询:

      @media (max-width: 800px) {
            h1 {
              text-align: left !important;
            }
          }
      <!DOCTYPE html>
          <head>
              <meta charset="UTF-8">
              <title>Some Title</title>
              <meta name="viewport" content="width=device-width, initial-scale=1">
              <link rel="icon" href= "Logo.png" type="img/SVG" style="width: 100%; height: 100%">
              <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
              <link rel = "stylesheet" href="stylesheet.css">
          </head>
          <body>
          
          <div class="Header" id="myHeader">
              <a class = "headerLogo">
                  <a href="file:///C:/Noah's%20stuff/Home.html" ><h1 style="color:blue; font-family: Verdana; font-style: italic; font-size: x-large;
                  text-align: center; padding-top: 20px">Some Title</h1></a>
                  <style>
                      a{text-decoration: none;}
                      a:hover{
                          text-decoration:none;
                      }
                  </style>
          
              </a>
              <div class="socialmedia">
                  <a class = "Facebook">
                      <a href="https://www.facebook.com/" target="_blank"><img src = "https://images.seeklogo.net/2016/09/facebook-icon-preview-1.png" width="50px" height="50px"></a>
                  </a>
                  <a class = "Instagram">
                      <a href="https://www.instagram.com/"><img src = "https://images.seeklogo.net/2016/06/Instagram-logo.png"  width="50px" height="50px"></a>
                  </a>
                  <a class = "Youtube">
                      <a href="https://www.youtube.com/channel/" target="_blank"><img src = "https://images.seeklogo.net/2016/06/YouTube-icon.png" width="50px" height="50px"></a>
                  </a>
                  <a class = preorder>
                      <button style = "background-color: white;">Pre-Order</button>
          
                  </a>
              </div>
          </div>
      
      My CSS
      
      body {
          margin: 0;
      
      }
      
      .Header {
          position: fixed;
          z-index:1;
          width: 100%;
          height: 70px;
          background-color: black;
          text-align: right;
      }
      .headerLogo {
      
      }
      
      .socialmedia {
          position: fixed;
          right: 100px;
          top: 35px;
          transform: translate(0, -50%);
          display: flex;
          /* add this */
          align-items: center;
          /* add this */
      }
      
      .preorder button {
          background-color: white;
          border: 0;
          height: 35px;
          width: 110px;
          margin-left: 35px;
      }
      
      .footer {
          display: flex;
          align-items: center;
          width: 100%;
          height: 90px;
          background-color: black;
      }
      
      .img-fluid{
          width: inherit;
          height: 782px;
      }
      
      .mySlides~.mySlides {
          position: absolute;
          top: 0;
          left: 100%;
          transition: 0.7s;
      }

      【讨论】:

        猜你喜欢
        • 2020-09-04
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 2016-12-28
        • 1970-01-01
        相关资源
        最近更新 更多