How to change CSS when the screen size changes
Asked Answered
W

4

9

So I have looked into code for my website that would changes the css and even the html of certain sections of my code when the screen size changes. What I am looking into as a start is when I put my website into split screen on Windows 10, or on a Mac, I want the header "Some Title" to move to the left side of the header section. I hope that this will help me with changing certain aspects of my website when the screen size changes. My code is below.

 <!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;
}
Weald answered 6/4, 2017 at 19:56 Comment(7)
Learn about media queries: w3schools.com/css/css_rwd_mediaqueries.aspVamp
I know I read about them I just didn't understand what max-width: to use for mobile phone and for split screen on a computerWeald
For mobile phone something like 480px is generic (that's what Bootstrap uses for mobile breakpoint), but I don't know about split screens.Vamp
You're already including Bootstrap 4. They've done all the hard work for you. Just use their grid system. v4-alpha.getbootstrap.com/layout/gridSounder
Im a little confused on how to implement the bootstrap code into my website, I know that I need to use the <link rel> tag. I had some help with it last time because I didn't know where to find the css stylesheets.Weald
read "Getting started" v4-alpha.getbootstrap.com/getting-started/introductionSounder
Yes, media queries or simpler, use a framework like Bootstrap or Foundation.Tombstone
H
16

Media queries can help you to define different styles for different screen sizes. Also, I'd recommend a different accomodation of HTML items, e.g.:

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

When screen size < 700px then text will align to the left, and when screen size is >= 1000 then social media box will add a margin to the right.

Hershberger answered 6/4, 2017 at 20:44 Comment(1)
worked fine for me too. Just need to add !importantKobarid
T
6

Well we can do this in two ways:-

1) Using media queries. please refer here

2) You can also achieve this using simple JavaScript. See Below:-

$(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>

I have also created a demo on codepen. See This

Thanks for bearing with me.

Transcalent answered 19/3, 2019 at 7:24 Comment(0)
E
5

Example

  @media screen and (max-width: 600px){
    ul.topnav li.right,
    ul.topnav li {float: none;}
    .seged{position: relative;}
    #lab{border-right: 0px;}
}
Erb answered 6/4, 2017 at 20:27 Comment(0)
R
2

As mentioned in the comments, you want media queries:

@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;
}
Rigel answered 6/4, 2017 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.