Understanding the concept of margins and managing tile layouts

I'm facing a few issues that I can't seem to resolve, and I would appreciate some guidance on how it all works. Firstly, I have a container with the ID "kafle" which I've positioned as desired, but now I want to add multiple tiles into it. My goal is to have tile 1 take up 50% of the width of the "kafle" container, while tile 2 occupies another 50%. Despite setting the margin and padding of both tiles to 0, there seems to be an issue causing tile 2 to go under tile 1, and I'm unsure why this is happening. Additionally, I'm uncertain about how to position tile 3, 4, 5, and 6 below tile 1, but in line with tile 2. When I set all tiles to display: inline-block, tiles 3, 4, 5, and 6 end up exceeding the height of tile 2. How can I address this? Any help in understanding this would be greatly appreciated.

html, body{
    height: 100%;
    min-height: 100%;
    margin:0;
}


#kafle{
    position:relative;
    height: 80%;
    width: 60%;
    top:10%;
    left:20%;
    background-color: aquamarine;
}

#onas{
    background-color: coral;
    height:33%;
    width:49%;
    display: inline-block;
    margin:0;
    padding:0;
}

#aktualnosci{
    background-color: firebrick;
    height:90%;
    width:49%;
    display: inline-block;
    margin:0;
    padding:0;
}

#galeria{
    background-color: forestgreen; 
    height:10%;
    width:15%;
    display: inline-block;
}

#wspierajanas{
    background-color: khaki; 
    height:10%;
    width:15%;
    display: inline-block;
}

#kontakt{
    background-color: fuchsia; 
    height:10%;
    width:15%;
    display: inline-block;
}

#minigra{
    background-color: fuchsia; 
    height:10%;
    width:15%;
    display: inline-block;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <script src="javascript.js"></script>
  
    <div id="kafle">
        <div id="onas">1</div>
        <div id="aktualnosci">2</div>
       <div id="galeria">3</div>
        <div id="wspierajanas">4</div>
        <div id="kontakt">5</div>
        <div id="minigra">6</div>
    </div>
  
</body>
    
</html>

Answer №1

Due to my undying self-loathing, I made the decision to construct this layout using CSS Grid (CSS Tricks article), even though it comes with limited compatibility and no uniformity.

Nevertheless, with some persistence, you can achieve your desired design using this relatively new feature in CSS. The key is to find a syntax that works consistently across different browsers. Below is one that has proven to be effective on most major browsers.

html, body{
  height: 100%;
  min-height: 100%;
  margin:0;
}


#kafle{
  position:relative;
  height: 80%;
  width: 60%;
  top:10%;
  left:20%;
  background-color: aquamarine;

  display: -ms-grid;
  display: grid;
  -ms-grid-columns: 1fr 1fr 1fr 1fr 1fr 1fr;
  -ms-grid-rows: 1fr 1fr 1fr;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(3, 1fr);
  -webkit-box-align: stretch;
      -ms-flex-align: stretch;
          align-items: stretch;
  justify-items: stretch;
}

#onas{
  background-color: coral;
  grid-column: 1 / 4;
  grid-row: 1 / 2;
  -ms-grid-column: 1;
  -ms-grid-column-span: 3;
  -ms-grid-row: 1;
  -ms-grid-row-span: 1;
}

#aktualnosci{
  background-color: firebrick;
  grid-column: 4 / 7;
  grid-row: 1 / 4;
  -ms-grid-column: 4;
  -ms-grid-column-span: 6;
  -ms-grid-row: 1;
  -ms-grid-row-span: 3;
}

#galeria{
  background-color: forestgreen; 
  grid-column: 1 / 4;
  grid-row: 2 / 3;
  -ms-grid-column: 1;
  -ms-grid-column-span: 3;
  -ms-grid-row: 2;
  -ms-grid-row-span: 1;
}

#wspierajanas{
  background-color: khaki; 
  -ms-grid-column: 1;
  -ms-grid-column-span: 1;
  -ms-grid-row: 3;
  -ms-grid-row-span: 1;
}

#kontakt{
  background-color: fuchsia; 
  -ms-grid-column: 2;
  -ms-grid-column-span: 1;
  -ms-grid-row: 3;
  -ms-grid-row-span: 1;
}

#minigra{
  background-color: fuchsia; 
  -ms-grid-column: 3;
  -ms-grid-column-span: 1;
  -ms-grid-row: 3;
  -ms-grid-row-span: 1;
}
<div id="kafle">
  <div id="onas">1</div>
  <div id="aktualnosci">2</div>
  <div id="galeria">3</div>
  <div id="wspierajanas">4</div>
  <div id="kontakt">5</div>
  <div id="minigra">6</div>
</div>

Answer №2

I have just completed a Weave in response to your query.
HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Tiles</title>
  </head>

  <body>
    <div class="tile1">
      <h1>Tile 1</h1>
    </div>

    <div class="tile2">
      <h1>Tile 2</h1>
    </div>

    <div class="tile3">
      <h1>Tile 3</h1>
    </div>

    <div class="bottomLeftTiles">
      <div class="tile4">
        <h1>Tile 4</h1>
      </div>

      <div class="tile5">
        <h1>Tile 5</h1>
      </div>

      <div class="tile6">
        <h1>Tile 6</h1>
      </div>
    </div>
  </body>
</html>

CSS:

div {
  background-color: rgb(240, 240, 240);
  border: 1px solid rgb(0, 0, 0);
}

h1 {text-align: center;}

.tile1 {
  width: 50%; height: 33%;
  position: absolute;
  left: 0; top: 0;
}

.tile2 {
  width: 50%; height: 100%;
  position: absolute;
  right: 0; top: 0;
}

.tile3 {
  width: 49.6%; height: 33%;
  position: absolute;
  left: 0; top: 33%;
}

.bottomLeftTiles {
  width: 49.6%; height: 33%;
  position: absolute;
  left: 0; top: 66%;
}
.tile4 {
  width: 33%; height: 100%;
  position: relative;
  left: 0; top: 0;
}
.tile5 {
  width: 33%; height: 100%;
  position: absolute;
  left: 33%; top: 0;
}
.tile6 {
  width: 33%; height: 100%;
  position: absolute;
  left: 66%; top: 0;
}

You may need to adjust some elements for proper sizing, but I believe this will be beneficial to you.

Answer №3

It seems like the arrangement of the objects in relation to each other is causing some concern for you.

  1. Try setting #id{display:inline:block;} This will make the div behave like a block element.
  2. Use #id{position:relative widthtomove px lengthtomove px;} Adjust the two relative parameters accordingly for each box or division, and continue making adjustments until they align perfectly.

You might want to explore more about using position:relative in detail, but the tips above should get the job done!

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 dynamically inserted nested division within a bootstrap grid does not occupy the entire width of the parent division

I am working with a parent div where I am dynamically adding smaller divs with content to achieve a specific layout. Here's how the structure looks: https://i.sstatic.net/tN1s1.png The code for the parent div is shown below: <div class="row" sty ...

What is the best method to override the CSS transition effect with JavaScript?

I am trying to implement a CSS transition where I need to reset the width of my box in advance every time my function is called. Simply setting the width of the box to zero makes the element shrink with animation, but I want to reset it without any animati ...

JavaScript code is functioning properly on Chrome and Internet Explorer, however, it may not be working on FireFox

Despite searching through the console, I am unable to find a solution to this issue. There are two images in play here - one is supposed to appear at specific coordinates while the other should follow the mouse cursor. However, the image intended to track ...

"Text that appears on a clear background when you hover over it

I'm attempting to have hidden text display inside a table cell upon hovering. I managed to achieve the hidden-until-hovered effect, but I am struggling to make the text appear solid in color (it currently decreases in opacity when hovered over). Is th ...

Creating a display of divs that flow from left to right in each row, and then stacking the rows from bottom to top using CSS and jQuery

My current dilemma involves a collection of divs that must be displayed in a specific order, but with a rather intricate layout. Essentially, this is the structure of my HTML: <div> <div class="box">1 (first in list)</div> <di ...

Troubleshooting issue: Django integration with inline Bootstrap datepicker is not functioning

I'm new to using bootstrap datepicker in inline mode, but I'm having trouble getting my divs to display anything once rendered. Can you help me figure out what's wrong with my code? <html> <head> <title>bootstr ...

Glitch in Safari 6: Round Corners Issue

Greetings! Upon observing the image provided, an unusual behavior can be noticed in Safari 6 concerning round corners. { border-radius: 5px 5px 5px 5px; } Upon hovering over the elements, a thin line appears that seems to accumulate with each subsequent ...

Javascript Function : Toggle visibility of div when user clicks anywhere on the page

I have implemented a feature where clicking on the 3 dots reveals a div, and clicking again hides it. However, I would like the div to hide when clicking anywhere else on the document. Imagine two circles - clicking on one circle displays a div, while cli ...

Ways to eliminate a Collection of CSS2DObject from the environment

I have a dynamic msg_arr array of strings that I want to display visually, and I need to clear the existing errs Group of CSS2DObject from the scene before creating a new errors Group. The issue here is that although the new CSS2DObjects are added success ...

Divide an HTML file into separate documents upon submitting a form

Is there a way to input HTML into a text area, then upon submission, have the system read the file, identify the class selector, and separate the HTML into multiple files saved in a directory? If you have any thoughts on how this could be accomplished, pl ...

Searching for the nearest label to an input element when they are located at the same hierarchy level

I am dynamically inserting input elements into my form. The structure of each code block is as follows: <input .. /> <input .. /> <label></label> The purpose of the label is to display an error message. For example, if one of the ...

I am experiencing an issue with my css dropdown menu appearing incorrectly on both Internet Explorer and Firefox

Chrome and Safari are working perfectly fine, but I encountered an issue with IE and FF where the submenu menu appears on the left side of the main navigation. The website in question is cedumilam.php.cs.dixie.edu. Below is the CSS code I am using: # ...

Is there a way to dynamically apply the "active" class to a Vue component when it is clicked?

Here is the structure of my Vue component: Vue.component('list-category', { template: "#lc", props: ['data', 'category', 'search'], data() { return { open: false, categoryId: this.category ...

I am looking to update the background color of my Bootstrap navigation bar

I am looking to update the background color of my bootstrap navigation bar. Here is the current look: https://i.sstatic.net/szUXD.png The HTML code I have been using is as follows: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

Guide to setting up a custom js file in Laravel admin template

Currently working with Laravel 5.8 and utilizing the laravel-admin Template for administrative purposes. There are times when I require custom JavaScript and CSS files specifically for certain admin controllers. How can I include these JS and CSS in lara ...

Unfixed body border encircling my entire webpage

My goal is to have a border around all the content on my page, but I am facing issues with the positioning of the bottom border. I want it to always be at the bottom of the page. html, body { min-height: 100%; height: 100%; } body { margin: 0; ...

Create custom sign board buttons using CSS styling

Check out the button design linked below: I attempted to recreate this design using before and after, but I want the arrow to be softer. I tried skewing it as well. I also searched for references, but couldn't find similar examples online even though ...

The NextJS Link component does not seem to be receiving the styles from Tailwindcss

I am currently working on a nextjs frontend project that has tailwindcss integrated. Below is an example of the code I am using: import Link from 'next/link' return( <div> <Link className="text-sm hover:text-gray-600" ...

Update the content of an HTML element without having direct access to the HTML code

I am currently in the process of creating a website using a website builder, and I am interested in customizing the default message that is displayed when a required field in the website form is left empty. The form in question is an integral part of the w ...

Convert HTML tr elements into a PHP array

After realizing that my previous thread lacked clarity, I decided to delete it and start fresh. In my php script, I send a cURL request to a website which generates an HTML table. This table contains 7 TR elements, each containing 6 TD elements. Essentia ...