The CSS design morphs as I adjust the browser size. Despite my limited coding experience of just a few weeks, I've encountered a challenging obstacle

Hi there, I've been searching on W3 and various other sources but haven't found a solution to my problem. I have managed to create the layout I want, however, it starts falling apart when I resize the browser window. I'm relatively new to coding, only been at it for six weeks. Can anyone help me understand why the layout is misbehaving?

  div {
  border: 1px solid black;
  padding: 10px;
}

#head {
  background-color: red;
  height: 150px;
  color: white;
  font-size: 50px;
  text-align: center;
}

#left {
  display: inline-block;
  width: 47%;
  height: 300px;
  overflow: auto;
}

#right {
  display: inline-block;
  width: 49%;
  height: 300px;
  float: right;
  overflow: auto;
}

#footer {
  height: 100px;
  background-color: blue;
  color: white;
  }
<div id="head"> The Daily Scumbag</div>
<div id="left">
  <h1>This is left column.
  </h1>
  Lorem Vestibulum
</div>
<div id="right">

Answer №1

While your basic concept was on track, there are some areas that could be improved upon.

Instead of manually specifying one div as width: 47%; and the other as 49%, it would be better to aim for equal widths of 50% each. Additionally, using float eliminates the need for display:inline-block; in this scenario.

To enhance CSS management, I recommend incorporating box-sizing:border-box; into your div's CSS declaration. This ensures that width adjustments will not be affected by borders or padding (more information available here: https://www.w3schools.com/cssref/css3_pr_box-sizing.asp).

Check out the revised example below:

<style>
html,body{
    padding:0;
    margin:0;
}
div {
    border: 1px solid black;
    padding: 10px;
    box-sizing:border-box;
}

#head {
    background-color: red;
    height: 150px;
    color: white;
    font-size: 50px;
    text-align: center;
}
#left {
    float:left;
    width: 50%;
    height: 300px;
    overflow: auto;


}
#right {
    width: 50%;
    height: 300px;
    overflow: auto;
}
#footer {
    clear:left;
    height: 100px;
    background-color: blue;
    color: white;
}
</style>

A quick note regarding:

html,body{
    padding:0;
    margin:0;
}

This prevents browsers from applying their default settings, eliminating unnecessary spacing around elements and the browser window.

Answer №2

Apply the box-sizing: border-box; property to the floating element so that padding and border are included in the width calculation.

  div {
  border: 1px solid black;
  padding: 10px;
}

#head {
  background-color: red;
  height: 150px;
  color: white;
  font-size: 50px;
  text-align: center;
}

#left {
  display: inline-block;
  width: 47%;
  height: 300px;
  overflow: auto;
  box-sizing: border-box;
}

#right {
  display: inline-block;
  width: 49%;
  height: 300px;
  float: right;
  overflow: auto;
  box-sizing: border-box;
}

#footer {
  height: 100px;
  background-color: blue;
  color: white;
  }
<div id="head"> The Daily Scumbag</div>
<div id="left">
  <h1>This is left column.
  </h1>s
  Lorem Vestibulum
</div>
<div id="right">

Answer №3

The reason behind this is the necessity to implement responsive code that allows the webpage to adapt to different browser sizes. Utilizing tools like Twitter Bootstrap can help you achieve this functionality effectively.

Wishing you success in your endeavors.

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 use of DIV tags allows the element to be displayed in an inline

In my project, I decided to add three div tags. The first two are positioned next to each other with the third div tag placed below them. However, when I tried to insert a slideshow into the first div tag, all of the div tags ended up displaying as "block" ...

Components drifting apart following viewport adjustments

Can someone help me with this issue in responsiveness? When I change the viewport, my header and main elements seem to be moving far apart from each other. View the code on JSFiddle <header> <div class="slider"> <img src="picture1" al ...

Can HtmlCleaner be safely used with multiple threads?

I've been searching for information on their website but can't seem to find an answer. Can you confirm if HtmlCleaner is thread safe? I have multiple threads that will be using it and I'm unsure whether reusing an instance of the HtmlCleane ...

The 3D Circle Flip feature on a certain webpage designed by Nordstrom is experiencing issues when viewed on Firefox

Click here to see a 3D Circle Flip effect. It works correctly in Chrome, but in Firefox the text does not change when flipping. ...

The __init__() function in Django encountered an error with the unexpected keyword 'user' being passed

Currently, I am working on establishing a password reset system using the default django libraries. However, I have encountered an error trace while trying to access the site for changing passwords (PasswordChangeConfirm). Internal Server Error: /reset/con ...

Tips for making a div with a single triangular side using Reactjs

Hey there, I'm looking to design a header similar to the one shown in this image. Can someone provide guidance on how I can achieve this UI using React.js? https://i.sstatic.net/zmW5x.jpg ...

Arrange divs next to each other using CSS

I am currently working with a basic layout on my webpage <div id="content-wrap"> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <div ...

Leveraging JavaScript for pricing calculations within asp.net framework

I am currently working with a textbox in a gridview and trying to calculate values using JavaScript. My code seems to be error-free. The goal is to multiply the quantity by the rate to get the total price. function totalise(price, rate, qt) { var qt ...

The Bootstrap carousel spiraled into disarray

I am facing an issue with the basic bootstrap carousel. My goal is to make the slides move every four seconds. The current setup of the carousel code is as follows: $(document).ready(function() { fixCarousel(); }); function fixCarousel() { $('.c ...

Error: Trying to access the parent node of an undefined property

After incorporating this script into my webpage for the newsletter sign-up process, I aimed to reveal customized content post user subscription. <script> function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore(newNode ...

What is the best method for fetching the values of a select element in React.js?

I'm struggling to retrieve the value of a selected element in a dropdown list. I've tried debugging it, but haven't been able to get the value. I attempted to console log e.target.value, but unfortunately, it didn't work. Any thoughts o ...

Tips for toggling the visibility of a revolution slider based on current time using JavaScript

Currently, I am integrating the revolution slider into my WordPress website. My goal is to have the slider change according to the standard time of day. For instance, I want slider1 to display in the morning, slider2 at noon, slider3 in the evening, and sl ...

Error: myFunction has not been declared

Can anyone figure out what's going on here? http://jsfiddle.net/sVT54/ <button onclick="myFunction()">Click me</button> <p id="demo"></p> function myFunction() { document.getElementById("demo").innerHTML="Hello World"; } ...

Creating identical class names for UL elements underneath LI using JavaScript

In my attempt to dynamically generate the class name within the ul element, I successfully achieved the expected result. The outcome can be viewed in the console of the snippet provided for reference. However, I'm facing a challenge when attempting t ...

Tips for saving the visibility status of a <div> in your browser bookmarks?

Currently, I am in the process of developing a single webpage (index.html). The navigation bar at the top includes links to hash references (DIV IDs). If JavaScript is enabled, clicking on these links triggers a JavaScript function with the onclick attribu ...

Fonts displayed on websites may look different on Mac compared to Windows

After carefully selecting a Google Web Font to use as the main font for my website, I noticed that it appears slightly different in terms of dimensions and spatial conformations when displayed on both Mac and Windows computers. For more information about ...

I have a website hosted on Heroku and I am looking to add a blog feature to it. I initially experimented with Butter CMS, but I found it to be too pricey for my budget. Any suggestions on

I currently have a website running on Heroku with React on the front end and Node.Js on the back end. I want to incorporate a blog into the site, but after exploring ButterCMS, I found the pricing starting at $49 to be too steep for my budget. My goal is ...

reason behind text styling: thick 12 pixels height with line spacing of 25 pixels using "Arial" font

Question about CSS Size: Understanding font sizes in CSS {font: 14px/24px Arial, Helvetica, sans-serif} I've noticed some CSS code like this on the website .selector {font: bold 12px/25px "Arial";} I know that 'bold' refers to font w ...

Guide on integrating React.js into an HTML development webpage

I can't seem to get this code to work properly. I have saved it as an .html file on my computer, but it's not displaying anything when I try to open it in Google Chrome. <DOCTYPE! html> <html> <head> <script src= ...

Using a forward slash in the path for the href attribute in a CSS link within an ejs file

Image 1: Configuring express.static for the public folder Image 2: Adding href="/app.css" in post.ejs file Image 3: Final result While experimenting with using /app.css and app.css in the post.ejs file, I noticed that the outcome was consistent with th ...