html - 如何在屏幕尺寸变化时更改 CSS

标签 html css screen-size

因此,我研究了我网站的代码,这些代码会在屏幕尺寸发生变化时更改代码某些部分的 css 甚至 html。我开始研究的是,当我在 Windows 10 或 Mac 上将我的网站放入分屏时,我希望标题“Some Title”移动到标题部分的左侧。我希望这能帮助我在屏幕尺寸改变时改变我网站的某些方面。我的代码如下。

 <!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;
}

最佳答案

媒体查询可以帮助您为不同的屏幕尺寸定义不同的样式。此外,我建议对 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>

当屏幕尺寸 < 700px 时,文本将向左对齐,当屏幕尺寸 >= 1000 时,社交媒体框将在右侧添加边距。

关于html - 如何在屏幕尺寸变化时更改 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43264679/

相关文章:

javascript - 如何使用 jquery 或 PHP 更改 img src 目录或图像文件名

javascript - 如何在同一个弹窗中打开不同的内容

css - 无法使用 .htaccess 中的 RewriteRule 访问文件

html - 推到下一行时居中 div 内容

android - 从一侧的屏幕外到另一侧的屏幕外进行 Translate TranslateAnimation

javascript - 防止页面跳转到iframe

javascript - 为什么我的 API 响应没有显示在页面上?

html - 为什么我的标题在两行时给出这个渲染?

html - block 文本旁边的图像