skip to Main Content

Whenever I change my screen size, the text and image is not responsive to that change.

Also whenever I want to add a scroll – the background acts weird and doesn’t show the whole page.

Image 1:
Scrolling horitzontally

Image 2:
Changing screen size and text isn’t showing properly

Html:

<meta name="viewport" content="width=device-width, initial-scale=1"> 

<div class='comicContainer'>

    <div class='comicBackground'>
        <img src='Images/test1.jpg'></img>
    </div>

    <div class='comicImage'>
        <img src='Images/test1.jpg'></img>
    </div>

    <div class='comicText'>
    <h1>Impact Winter</h1>
    <h2>Published: 26 June 2016</h2>
    <h3 style='font-size: 22px;'>
        Writer: Travis Beacham
    </h3>
    <h4 style='font-size: 22px;'>
        Penciler: Stephen Green
    </h4>
    <h5 style='font-size: 22px;'>
        Cover Artist: Stephen Green
    </h5>
    <p class='breakLine'>In the British countryside, a band of survivors has formed a resistance in the fallout shelter of a medieval castle. Among them is Darcy, a young, headstrong fighter waiting for the chance to prove she can be on the front lines.</p>
    <br>
    <button><center>BUY</center></button>

  </div>
  </div>

CSS:


body{
    margin: 0px;
    background-color: rgb(237, 237, 237);
    overflow-x: hidden;
}

header {
    font-family: 'Anton';
    font-size: 45px;
    color: white;
    transition: 0.5s;
}


.comicBackground img {
    width: 100%;
    height: 85%;
    background-position: center;
    background-repeat: no-repeat;
    object-fit: cover;

    filter: brightness(20%);
  }

.comicImage img {
    width: 250pv;
    height: 350px;

    position: relative;

    top: -530px;
    right: -20px;
}

.comicText {
    float: left;
    position: relative;
    top: -890px;
    right: -350px;

    color: white;
    font-family: 'Roboto Condensed';
}

.comicText button {
  width: 160px;
  height: 44px;

  border: 3px solid purple;
  background-color: transparent;

  color: white;
  font-family: 'Roboto Condensed';
  font-size: 20px;
  font-weight: 700;
  transition: 0.2s;
}

.comicText button:hover {
  background-color: purple;
}

  @media screen and (max-width: 2000px) {
    .comicBackground img {
        width: 100%;
        height: 105%;
        background-position: center;
        background-repeat: no-repeat;
        filter: brightness(20%);
      }
  
      .comicImage img {
        width: 350px;
        height: 450px;
    
        position: relative;
    
        top: -710px;
        right: -100px;
    }
    
    .comicText {
        float: left;
        position: relative;
        top: -1170px;
        right: -500px;
        color: white;
        font-family: 'Roboto Condensed';
    }
      
  }
  
  @media screen and (max-width: 600px) {
    .comicBackground img {
      width: 100%;
      height: 120%;
      background-position: center;
      background-repeat: no-repeat;
      filter: brightness(20%);
    }

    .comicImage img {
        width: 250px;
        height: 350px;

        position: relative;
         top: -120px;
         right: 120px;
    
        transform: translate(65%, -200%);
    }

    .comicText {
        float: left;
        position: relative;
        top: -820px;
        right: -50px;
    
        color: white;
        font-family: 'Roboto Condensed';
    }
  }


  .breakLine {  
    width: 700px;
    word-wrap: break-word;
}

I tried to change the properties based on screen size – i.e for mobile screen size, I changed the orientation of the text + image – but it doesn’t really work at all.

2

Answers


  1. First of all, except for the smallest size, you need to set your @media queries to have both a maximum and a minimum size. So for example, change the @media screen and (max-width: 2000px) to something like @media screen and (min-width: 601px) and (max-width: 2000px).

    You may also have a typo in your CSS. pv isn’t a recognized measurement unit.

    Login or Signup to reply.
  2. I think a lot of your issues are related to using positioned elements. It’s more difficult than it needs to be to get them to be responsive. Your best bet is to use grid or flex layouts and the CSS background-image prop for your .comicBackground instead of an image.

    I’ve added this JS Fiddle with an example of what that might look like.

    The key take-aways are:

    Changing the way your HTML elements are grouped — .comicBackground now wraps .comingImage and .comicText

    <div class="comicBackground">
      <div class='comicContainer'>
        <img class='comicImage' src='https://images.pexels.com/photos/7809122/pexels-photo-7809122.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2' />
        <div class='comicText'>
          <h1>Impact Winter</h1>
          <h2>Published: 26 June 2016</h2>
          <h3 style='font-size: 22px;'>
            Writer: Travis Beacham
          </h3>
          <h4 style='font-size: 22px;'>
            Penciler: Stephen Green
          </h4>
          <h5 style='font-size: 22px;'>
            Cover Artist: Stephen Green
          </h5>
          <p class='breakLine'>In the British countryside, a band of survivors has formed a resistance in the fallout shelter of a medieval castle. Among them is Darcy, a young, headstrong fighter waiting for the chance to prove she can be on the front lines.</p>
          <br>
          <button>
            BUY
          </button>
        </div>
      </div>
    </div>
    

    Removing positioned elements and using flex for the layout.

    .comicContainer, .comicBackground {
      display: flex;
    }
    
    .comicBackground {
      justify-content: center;
      align-items: center;
    }
    
    .comicContainer {
      justify-content: flex-start;
      height: max-content;
      max-width: 800px;
    }
    

    Using the background-image CSS prop to create a responsible background image using the url function

    .comicBackground {
      width: 100vw;
      height: 100vh;
      background-image: linear-gradient(to bottom, rgba(0, 0, 0, .85), rgba(18, 42, 66, .85)), 
         url(https://images.pexels.com/photos/7809122/pexels-photo-7809122.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2);
      background-position: center;
      background-size: cover;
      background-repeat: no-repeat;
    }
    

    Note: You can define multiple values for the background-image if you pass a comma-delimited list. The linear-gradient here is just a way of replacing the filter: brightness(20%); you had.

    There are some other, unrelated, changes in the fiddle that might be of some value.

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search