Exploring the Four Quadrants of CSS

When I use this fiddle https://jsfiddle.net/8kh5Lw9r/, I am getting the expected four quadrants on the screen.

https://i.sstatic.net/O5Q6D.png

HTML

<div id="one"><p>One</p></div>
<div id="two"><p>Two</p></div>
<div id="three"><p>Three</p></div>
<div id="four"><p>Four</p></div>

CSS

div {
    position: fixed;
    border: 1px dashed red;
}

#one {
    height: 40%;
    width: 35%;
}
#two {
    height: 40%;
    width: 65%;
}
#three {
    height: 60%;
    width: 35%;
}
#four {
    height: 60%;
    width: 65%;
}

However, I am facing the following issues:

  1. Why do the four paragraphs stack on top of each other even though the DIVs are properly configured?
  2. Is there a way to prevent redundancy by defining the 40/60 and 35/65 divisions in a single location?

Edit: I would greatly appreciate it if you could identify any misconceptions I have about how CSS functions. Providing a solution is helpful, but correcting my misunderstandings would be even more beneficial.

Answer №1

Your div elements may not be displaying correctly. Adjusting the size and color of the borders could enhance the clarity, as demonstrated in the example below.

div {
  position: fixed;
}
#one {
  border: 2px dashed blue;
  height: 40%;
  width: 35%;
}
#two {
  border: 1px solid red;
  height: 40%;
  width: 65%;
}
#three {
  border: 2px dashed green;
  height: 60%;
  width: 35%;
}
#four {
  border: 2px solid black;
  height: 60%;
  width: 65%;
}
<div id="one"><p>One</p></div>
<div id="two"><p>Two</p></div>
<div id="three"><p>Three</p></div>
<div id="four"><p>Four</p></div>

The issue lies in all elements starting at position (0,0) and forming a grid due to their varying sizes. Although the percentages sum up to 100%, the grid size remains at 65% by 65% as the largest element is 65% by 65%. To address this, consider removing fixed sizes or setting x and y coordinates for a fixed position.

To streamline the code and prevent using IDs, utilize classes for different widths and heights.

div {
  position: fixed;
  border: 1px dashed red;
  box-sizing: border-box;
}
.top    { height: 40%; top:    0; }
.bottom { height: 60%; bottom: 0; }
.left   { width:  35%; left:   0; }
.right  { width:  65%; right:  0; }
<div class="top left"><p>One</p></div>
<div class="top right"><p>Two</p></div>
<div class="bottom left"><p>Three</p></div>
<div class="bottom right"><p>Four</p></div>

The box-sizing: border-box property can be used to incorporate the border into the size of the div, eliminating overlapping borders and potential overflow of elements beyond the page boundaries.

Answer №2

1.) To prevent the fixed positioning of all four DIVs, simply remove the position: fixed; style from your CSS for those elements.

2.) It is recommended to use classes over IDs as they can be applied to multiple elements. For example:

.custom-style {
  height: 40%;
  width: 65%;
}

<div class="custom-style">...</div>

Answer №3

If you're looking for a solution, you might want to consider the following approach:

 .container {
  position: fixed;
  display: block;
  width: 100%;
  height: 100%;
}

#box1, #box2, #box3, #box4 {
  border: 1px dashed red;
  box-sizing: border-box;
  float: left;
}

#box1 {
    height: 40%;
    width: 35%;
}
#box2 {
    height: 40%;
    width: 65%;
}
#box3 {
    height: 60%;
    width: 35%;
}
#box4 {
    height: 60%;
    width: 65%;
}
<div class="container">
  <div id="box1"><p>One</p></div>
  <div id="box2"><p>Two</p></div>
  <div id="box3"><p>Three</p></div>
  <div id="box4"><p>Four</p></div>
</div>

Answer №4

Here is a solution for those issues:

body {
  height: 300px;
}

div {
    border: 1px dashed red;
    float: left;
}

#one, #two {
    height: 40%;
    border-bottom: none
}

#three, #four {
  height: 20%;
}

#one, #three {
    width: 35%;
}

#two, #four {
  width: 30%;
  border-left: none;
}
<div id="one"><p>One</p></div>
<div id="two"><p>Two</p></div>
<div id="three"><p>Three</p></div>
<div id="four"><p>Four</p></div>

Answer №5

It seems like this approach has potential

body{
  margin:0;
}
div {
    position: fixed;
    border: 1px dashed red;
}
#box{
  height: 60%;
    width: 65%;
}
#one {
    height: 40%;
    width: 35%;
}
#two {
    height: 40%;
    left: 35%;
    width: 30%;
}
#three {
    top:40%;
    height: 20%;
    width: 35%;
}
#four {
   top:40%;
   left: 35%;
    height: 20%;
    width: 30%;
}
<div id="box">
<div id="one"><p>One</p></div>
<div id="two"><p>Two</p></div>
<div id="three"><p>Three</p></div>
<div id="four"><p>Four</p></div>
</div>

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

Employ gulp for rebasing CSS URLs

I have a question regarding how to resolve the following issue: In my sass files, I utilize absolute path variables in my main include files. For example, fonts are stored in /assets/fonts/... and icons are located in /assets/icons/... These paths only wo ...

Preventing scrolling in one div while focusing on another div

I am currently in the process of creating a website that functions as a single page site. The main feature of the site is a masonry grid of images. When a user clicks on an item, a detailed panel slides in from the left. Once the panel is closed, it slide ...

Modify vanilla JavaScript carousel for compatibility with Internet Explorer

I am currently in the process of creating a website that incorporates a carousel similar to the one found at the following link: https://codepen.io/queflojera/pen/RwwLbEY?editors=1010 At the moment, the carousel functions smoothly on opera, chrome, edge ...

Make an element in jQuery draggable but always within the viewport

My issue is that when I resize the window, the element stays in its position until I start dragging it. Once I drag it and then resize the window again, it doesn't stay within its container. To better illustrate my problem, you can view a demo on Fid ...

Expand a list item to full width based on the number of items present

Is there a way to make dynamically added list items in a footer stretch across the width of their container? The footer is created in WordPress and the list items are actually widgets. Here is an example: Fiddle I can't manually set the width for e ...

Is there a way to have the menu displayed on a single line and consistently centered?

I am working on a menu that needs to stay centered at the top of the screen, with a consistent appearance and just one line, regardless of the screen width. However, I'm encountering an issue where the menu mysteriously transitions into a two-line for ...

Why does the lazy loading feature keep moving the background image around?

While trying to incorporate lazy loading on my website, I encountered an issue where the image position would change after it was fully loaded and made visible. I experimented with rearranging the order in which my JavaScript and CSS files were called, bu ...

Is your margin not functioning correctly? Is padding taking on the role of margin?

One issue I'm encountering is that the margin isn't behaving as expected in this example. The padding seems to be working like margin should. Why is that? Print Screen: https://i.stack.imgur.com/N1ojF.png Why is it that the margin in the h3 ta ...

:initial selector malfunctioning in CSS

I was hoping to see the :first selector in action with the example below. Unfortunately, the paragraph did not turn red with a background. Can anyone offer some assistance? p:first{background:red;} <p>This is paragraph 1.</p> <p>This ...

Strategies for avoiding unused style tags in React components

Expanding beyond React, I'm unsure if React itself is the culprit of this issue. In a React environment with TypeScript, I utilize CSS imports in component files to have specific stylesheets for each component. I assumed these styles would only be ad ...

"Resolving the duplication issue of a textbox in Phonegap app

I encountered a strange issue recently. While trying to focus on the textbox in my PhoneGap application using touch, I noticed that there are actually 2 textboxes appearing. Please refer to the attached image: ...

Is there a way for me to display the comment count as text directly on the comment bubble image?

In this example on jsfiddle, I am attempting to display the number of comments (12 in this case) as text over the comment bubble image: http://jsfiddle.net/c337Z/ HTML: <ul id="top"> <li><a href="/comments"><div id="commentlink" ...

Relocate the preview division below the button in the Kartik File Input Widget

There are two input file buttons that allow users to upload an image on each button. Upon selecting an image, a preview of the image will be displayed above the button. The current layout is shown here: https://i.sstatic.net/aLAbo.png When images of diff ...

Issue with Sass @use failing to load partial

I'm currently facing an issue while trying to import a sass partial called _variables.scss into my main stylesheet named global.scss. Every time I compile the sass code, I encounter the following error message: Error: Undefined variable. Here is the ...

Determine the hue of the scrollbar

My web page features a customized vertical scrollbar within a div box, and I have managed to create a horizontal scrollbar using CSS and jQuery. Now, I am looking for a way to match the color of the custom horizontal scrollbar with the rest of my design. ...

Troubleshooting problem with text overlaying images upon hovering in CSS

Several months ago, I successfully implemented a cool effect on a Shopify website that I had designed. However, recently the client decided to switch to a new theme without my knowledge, and now one of my CSS tricks is no longer working. If you look at t ...

Styling nested divs in CSS

I am experiencing an issue where the child divs within parent divs are overflowing outside of the parent divs. To get a better understanding of the problem, please try running the code below in a browser: My goal is to align the innermost divs horizontall ...

What is the best way to create a divider between tab panes in JavaFX?

I created a unique vertical tabpane, but I am struggling to insert horizontal lines between each label. Below is the code snippet I have used: .tab *.tab-label { -fx-rotate: 90; } .tab { -fx-padding: 3em 0.5em; } .tab-pane .tab-header-area .tab ...

The functionality of Google Maps code is limited on Android devices because the map feature is not available

After updating the Google Maps embed code to be responsive for mobile devices, I discovered that the map still won't display on Android or iPhone. The following is the modified code. Can anyone assist me in resolving this issue so that the map can sho ...

Elegant CSS background image fade effect

Having a small JS script that functions properly, but encountering an issue when quickly hovering over buttons causing the smooth transition effect to not work as desired. This abrupt change in image is quite unappealing. Any help would be greatly appreci ...