Designing my website to resize and adapt to different screen resolutions using CSS

As I navigate my first week of CSS, HTML, and PHP programming, I encountered a problem with text overlapping and causing issues on my site when scaled according to window size.

I'm seeking clarity on what mistakes I may be making. Despite trying media queries, I still struggle to grasp the concept entirely.

Take a look at my current code:

/* Reset body padding and margins */

body {
  margin: 0px;
  padding: 0px;
}


/* Make Header Sticky */

#header_container {
  background: black;
  border: 1px solid #666;
  height: 40px;
  left: 0;
  position: fixed;
  width: 100%;
  top: 0;
}

#header {
  line-height: 5px;
  margin: 10px;
  auto: width:940px;
  text-align: left;
}

#headertext {
  font-family: "sans-serif";
  size: 20px;
  padding: 2px;
  font-size: 120%;
  text-decoration: none;
}


/* CSS for the content of page. I am giving top and bottom padding of 80px to make sure the header and footer do not overlap the content.*/

#container {
  margin: auto;
  overflow: auto;
  padding: 80px;
  width: 100%;
  height: auto;
}

#content {}


/* CSS FOR HOME PAGE CONTENTS */

#hometext {
  font-family: "arial";
  padding: 1px;
  font-size: 100%;
  text-decoration: none;
  position: absolute;
  top: 5em;
  left: 4em;
  color: #585858;
  margin-right: 850px;
}

#hometext2 {
  font-family: "arial";
  padding: 10px;
  font-size: 80%;
  text-decoration: none;
  position: absolute;
  top: 7em;
  left: 4.24em;
  color: #585858;
  margin-right: 1200px;
}

#hometext3 {
  font-family: "arial";
  padding: 1px;
  font-size: 100%;
  text-decoration: none;
  position: absolute;
  top: 5m;
  left: 65em;
  color: #585858;
  margin-right: 0;
}

#hometext4 {
  font-family: "arial";
  padding: 18px;
  font-size: 80%;
  text-decoration: none;
  position: absolute;
  top: 10m;
  left: 80em;
  color: #585858;
  margin-right: 0;
}

#home_container
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<html>

<head>
  <link rel="stylesheet" type="text/css" href="style/style.css" />

  <title>replay.sc</title>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  <style type="text/css">
a:link {
      color: grey;
    }
    
    a:visited {
      color: grey;
    }
    
    a:hover {
      color: white;
    }
    
    a:active {
      color: grey;
    }
    
    text-decoration: none;
  }
}
  </style>


</head>

<body font="sans-serif" id="container">


  <!--- BEGIN: STICKY HEADER -->
  <div id="header_container">
    <div id="header">
      <p><a id="headertext" a href="replay.sc"> replay.sc </a>
        <p>
    </div>
  </div>
  <!-- END: STICKY HEADER -->

  <!-- BEGIN: Sticky Footer -->
  <div id="footer_container">
    <div id="footer">

    </div>
  </div>
  <!-- END: Sticky Footer -->
  <div id="home_container">
    <h1 id="hometext"> Upload your replay here to generate a page containing a download link and various information on the replay. </h1>
    <p id="hometext2"> When the page is generated you will have the option to select which information on the replay you want to display to the public, if you are logged in you will be able to edit this in the future.</p>

    <h1 id="hometext3"> Upload a replay pack here to generate a page containing download links for every replay or for a .rar file of every replay. </h1>
    <p id="hometext4"> Only basic information for each replay will be made to conserve server load. </p>
  </div;
</body>

</html>

Answer №1

It's important to understand the difference between relative and absolute measurements when designing websites. If you're new to web design, it's best to stick to one type of measurement at first and gradually learn how to effectively use both.

For instance, a website with absolute positioning will not resize when the browser window is resized, while a site with relative positioning can adapt to different window sizes. Both methods can create good websites, but mastering both techniques is essential for creating successful and flexible designs.

Many beginners find absolute positioning easier to grasp initially, but it may limit your ability to create layouts that work well across various screen sizes. To start with absolute positioning, you typically set a fixed width for the page using pixels like 960px or 800px. Some key steps to creating an absolute positioned site include:

  1. Use position: absolute for elements
  2. Specify top/bottom and left/right or height/width properties
  3. Consistently use the same unit of measurement (e.g. pixels)

An example of absolute positioning can be seen in this JSFiddle demo.

The downside of absolute positioning is evident when users zoom in/out or have different window sizes, leading to wasted space or horizontal scrolling. A more modern approach is using floated positioning for responsive and elastic designs, which involves utilizing percentages, margins, paddings, and layout algorithms skillfully.

  1. Use percent units for most elements
  2. Carefully manage margins and paddings
  3. Understand different layout algorithms

An example of an elastic layout can be found in this JSFiddle demo. Learning these concepts may be challenging, but practice leads to a natural understanding of how to create versatile layouts.

Answer №2

If you're looking to enhance your CSS skills, consider implementing a container to manage the layout effectively. By resizing the container, you can easily avoid any overlapping elements. Feel free to explore and adapt the code snippet below, which includes a designated container with specific CSS properties to prevent overlap. Check out how the #advertisement CSS ID plays a crucial role in achieving the desired effect.


<html>
<body>
  <div id="container">
    <div id="advertisement">
      <img src="Desert.jpg" width="200px" height="200px">
    </div>
    <div id="transparent">
      <img src="black.jpg" width="200px" height="200px">
    </div>
    <div id="regularborder">
      <img src="Desert.jpg" width="200px" height="200px">
      <img src="Desert.jpg" width="200px" height="200px">
      <img src="Desert.jpg" width="200px" height="200px">
    </div>
  </div>
</body>
</html>

<style>
  #container {
    position: relative;
  }
  
  #advertisement {
    z-index: -1;
    border-left: solid;
  }

  #transparent {
    opacity: 0.4;
    filter: alpha(opacity=40);
    position: absolute;
    left: 0px;
    top: 0px;
  }

  #regularborder {
    border-style: solid;
  }
</style>

Answer №3

You can experiment with CSS by using code similar to the following.

#fullviewport { position:absolute; margin:0px; width:100%; }

Answer №4

for a responsive design, consider the following styling:

CSS

.header{background-color:#000;color:#fff;width:100%;min-height:40px;}
a{color:#fff;font-size:120%;line-height:2em;margin:10px;}
h1{ color: #585858; font-family: "arial";font-size: 100%;line-height: 20px;}
p{margin-top:1em;font-family: "arial";font-size:80%;color:#585858;}
.table{display:table;width:55%;margin:0 auto;}
.row{display:table-row;}
.cell{display:table-cell;padding:2em;width:50%;}

HTML

<div class="header">
        <a href="/" id="headertext"> replay.sc </a>
     </div>
     <div class="table">
        <div class="row">
            <div class="cell">
                <h1> Upload your replay here to generate a page containing a download link and various information on the replay. </h1>
                <p> When the page is generated you will have the option to select which information on the replay you want to display to the public, if you are logged in you will be able to edit this in the future.</p>
            </div>
            <div class="cell">
                <h1> Upload your replay here to generate a page containing a download link and various information on the replay. </h1>
                <p> Only basic information for each replay will be made to conserve server load. </p>
            </div>
        </div>
     </div>

You can customize content's margin and padding according to your preferences.

Answer №5

Utilize the vw and vh units for responsive font sizing:

font-size: 1vw /* = 1% of viewport width */
font-size: 1vh /* = 1% of viewport height */
font-size: 1vmin /* = 1vw or 1vh, whichever is smaller */
font-size: 1vmax  /* = 1vw or 1vh, whichever is larger */

Learn more about this technique here:

Viewport Sized Typography

Answer №6

Achieving true responsiveness requires a desire for predictability in the behavior of your content. Utilizing media queries on your website tailored to specific screen sizes can help with this. By using @media screen and {max-width:400px}{.....}, you are essentially instructing the content to behave in a certain way when the screen width is at its maximum of 400px. This allows you to control how the content adjusts within the {....}. Experimenting with different browsers other than Internet Explorer can also be beneficial, as IE8 may not support media queries. In such cases, adding a JavaScript file called respond.js from their developer website could serve as a solution.

Reference:

W3C media queries and their standards

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The popup is unable to remain in the center of the screen because the background page keeps scrolling back to the top

Whenever I click a button to open a popup window, the page unexpectedly scrolls to the top. It's frustrating because if I'm at the bottom of the page and press the 'submit' button, the popup doesn't display in the center as intende ...

The jQuery toggle function seems to be skipping alternate items

I have recently started learning Javascript and JQuery. Currently, I am working on creating a comment system where you can click reply to display the form. However, I'm facing an issue where the form only shows up for the first comment reply, not for ...

What is the method used to incorporate the radial gradient in this design?

As I was browsing the internet, I came across several websites with beautiful natural gradients on their backgrounds. However, I'm puzzled as to how they achieved this effect. It doesn't seem like a simple image file because the gradient adjusts ...

Opacity effect on input text when it exceeds the input boundaries

I'm having an issue: I need to create inputs with different states, including when the text is longer than the input itself. In this case, the extra text should fade out with a gradient transparency effect: Here is my current code snippet: .Input { ...

What is the process of decoding a URL in JavaScript?

Is there a better method to decode this URL in order to use it with JavaScript? URL: https://www.example.com/post.php?v=1&text=it's-me&20hello%0Aworld%0A At present, any occurrence of ' in the URL is causing an error and blank lines ar ...

Load image in browser for future display in case of server disconnection

Incorporating AngularJS with HTML5 Server-Side Events (SSE) has allowed me to continuously update the data displayed on a webpage. One challenge I've encountered is managing the icon that represents the connection state to the server. My approach inv ...

JavaScript issue with confirm/redirect feature not functioning as expected

A demonstration of JavaScript is utilized for deleting an employee in this scenario... <script type="text/javascript"> function deleteEmployee(employee) { var confirmation = confirm('Are you sure?'); if(confirmation) { ...

Parallax Effect Slows Down When Scrolling In Web Page

Currently in the process of creating a website with a scrolling parallax effect using Stellar.js on the header and three other sections. However, I'm experiencing lag when scrolling, especially at the top of the page. I've attempted to reduce la ...

Here is a guide on how to ensure that the div elements automatically fill the available space

Looking at this html page: Check out the jsFidlle Demo here I am aiming to have the boxes fill up the space effortlessly, each with equal width but varying heights. This is similar to the layout in Google Keep notes ...

How to Utilize Raw HTML in GWT with the Power of Jquery/Javascript?

Below is the HTML code snippet: <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function( ...

Angular: Troubleshooting blank pages in HTML - What's causing it?

This is the content of my heroes component: <!--The content below is only a placeholder and can be replaced.--> <div style="text-align:center"> <h1> <h2>{{hero.name}} Details</h2> <div><s ...

Displaying or concealing fields through Javascript depending on the category type of the results item

I am facing an issue with displaying certain fields in each individual property search result based on the property type. For example, if a property is classified as Land, I do not want the bedrooms and bathrooms fields to be visible, but if it is a Villa, ...

What causes jQuery to not work correctly during keydown events?

I've been working on this external jQuery code: jQuery(document).one('keydown', 'g',function (evt){ if ($("#tb").html() == "0") { $("#tb").html("Testing the chicken.") } else {$("#tb").html("Chickens fart too." ...

What is the best way to incorporate two banners on the Magento homepage alongside my flexslider?

I recently made the switch from using a camera.js slider on my website to a Flexslider. The reason for this change was that I couldn't make the camera.js slider clickable, and I wanted to eliminate the 'Click here' button. However, now that ...

Centering text underneath bootstrap image design

After trying various methods, I still couldn't get the text to display directly below my images in the center. Below is the code I attempted: <div class="our_partners"> <div class="container"> <div class="row"> ...

Aligning the canvas resolution to match the video resolution for superimposition purposes

Within a div, I have both a canvas and a video element: <div id="videos"> <canvas id="my-canvas"></canvas> <video id="remote-video" autoplay></video> </div> Below is the css styling for both elements: #my-canv ...

Is it possible to recognize when the mouse button is held down and the cursor is outside the viewport by using mouseleave detection?

Is there a way to detect when a user moves the mouse outside of the view-port, even if they are holding down the mouse button (for example, if the mouse is on the browser address bar)? In the code below, I am currently using mouseout and mouseleave to det ...

change the css back to its original state when a key is pressed

Is there a way to retrieve the original CSS of an element that changes on hover without rewriting it all? Below is my code: $(document).keydown(function(e) { if (e.keyCode == 27) { $(NBSmegamenu).css('display', 'none');} ...

Changing the color of Material UI pagination: a step-by-step guide

I have integrated Material UI to create a pagination bar for my website. While everything is functioning properly, I am looking to personalize the appearance. After reviewing their documentation, I tried implementing a theme but encountered an issue with c ...

Adjust image size while maintaining aspect ratio

Currently, I am implementing a resize function for an image by using the following code snippet: $('.image_resize').each(function(){ var ww = $(window).width() - 80 - 400; var wh = $(window).height() - 60; var iar = $(this).attr(&apo ...