Practicing the basics of a simple 3-column layout before delving into the world of

Currently, I am working on a three column layout that appears to be functioning well on the surface. However, upon inspecting the page using developer tools, it becomes apparent that the margin of the third middle column is overlapping with the neighboring columns that have been floated on either side. Additionally, my attempts at incorporating media queries to optimize the layout for mobile devices seem to be ineffective. While I understand that frameworks like flexbox or bootstrap could resolve these issues, my main goal is to master the basics before relying on them. I want to be able to tackle similar challenges without the aid of frameworks in case it comes up in a coding interview.

In summary: - Adjust the margin size in a three column layout. - Implement a responsive column layout for mobile devices. Here's a snippet of my code below:

 `<!DOCTYPE html>
<html>
  <head>
    <title>Nav Page </title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="le.css">
  </head>
  <body>
<h1>Column Layout</h1>
<div class="col1">
  <h2>Column 1</h2>
    <p>
    "Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua.
    Ut enim ad minim veniam, quis
    nostrud exercitation ullamco
    laboris nisi ut aliquip ex ea
    commodo consequat. Duis aute
    irure dolor in reprehenderit in
    voluptate velit esse cillum dolor
     e eu fugiat nulla pariatur.
     Excepteur sint occaecat cupidatat
     non proident, sunt in culpa qui
     officia deserunt mollit anim id
     est laborum."
      </p>
</div>

<div class="col2">
  <h2>Column 2</h2>
  <p>
  "Lorem ipsum dolor sit amet,
  consectetur adipiscing elit,
  sed do eiusmod tempor incididunt
  ut labore et dolore magna aliqua.
  Ut enim ad minim veniam, quis
  nostrud exercitation ullamco
  laboris nisi ut aliquip ex ea
  commodo consequat. Duis aute
  irure dolor in reprehenderit in
  voluptate velit esse cillum dolor
   e eu fugiat nulla pariatur.
   Excepteur sint occaecat cupidatat
   non proident, sunt in culpa qui
   officia deserunt mollit anim id
   est laborum."
    </p>
</div>
<div class="col3">
  <h2>Column 3</h2>
  <p>
  "Lorem ipsum dolor sit amet,
  consectetur adipiscing elit,
  sed do eiusmod tempor incididunt
  ut labore et dolore magna aliqua.
  Ut enim ad minim veniam, quis
  nostrud exercitation ullamco
  laboris nisi ut aliquip ex ea
  commodo consequat. Duis aute
  irure dolor in reprehenderit in
  voluptate velit esse cillum dolor
   e eu fugiat nulla pariatur.
   Excepteur sint occaecat cupidatat
   non proident, sunt in culpa qui
   officia deserunt mollit anim id
   est laborum."
    </p>
</div>
  </body>

</html>
`

Below is my CSS:

* {
  box-sizing: border-box;
}
.col1 {
  width: 25%;
  float: left;
  margin: auto;
  border: 1px solid black;
}

.col2 {
  width: 25%;
  float: right;
  margin: auto;
  border: 1px solid black;
}

.col3 {

width:25%;
heigth: 50px;
margin: auto;
border: 1px solid black;

}

h1 {
  text-align: center;
}

@media screen and (max-width: 575px) {
  .col1 {
    float:none;

  }

  .col2 {
    float:none;

  }

  .col3 {
    float:none;

  }
}

Answer №1

Utilize CSS Grid instead of floats, as floats are considered outdated for layout designs.

/* Common styles */

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

h1{
    text-align: center;
    margin: 1rem 0;
}

/* CSS grid layout */

.layout{
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 4rem;

    padding: 0 2rem;
}

.layout div{
    border: 1px solid black;
}
  
@media (max-width: 575px) {
    .layout{
        grid-template-columns: 1fr;

        grid-gap: 2rem;

        padding: 0 1rem;
    }
    
}
    <h1>Column Layout</h1>

    <div class="layout">
        <div>
            <h1>Column 1</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolor e eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>

        <div>
            <h1>Column 2</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolor e eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>

        <div>
            <h1>Column 3</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolor e eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</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

Adaptive website backdrop

I have created a unique background for my website design, which includes 3 slices labeled as div id top, middle, and bottom. You can view the design in this image. I am interested in making this background responsive. While I have come across examples of ...

Buttons to maximize, minimize, and close individual sections on a webpage

I am fairly new to front end development and I must say, I am absolutely enjoying every bit of it. Here is a little challenge that came my way. It may seem simple to some but it got me thinking. I have three sections on the right side of my website. I wa ...

Remove elements generated by the .after method with Jquery

In my HTML file, I have a table with hard-coded column headers: <table id="debugger-table"> <tr> <th>Attribute</th> <th>Computed</th> <th>Correct</th> </tr> </table&g ...

Utilizing various CSS classes based on screen resolutions

In my UL, there is a span class called user-name. Depending on the screen resolution, I want to either remove or apply a different class. <link href="media-queries.css" rel="stylesheet"> <span class="user-name">Shane</span> When I try t ...

Is there a way for me to modify the color of the currently selected button?

I've been attempting to change the color of the active button when clicked, but my CSS class "activePage" isn't doing the trick. Ideally, clicking the "projects" button should turn it red, and clicking the "about" button should change it to red w ...

Is there a way to retrieve the width and height of a parent element within a nested declaration in SASS?

Is there a way for a child element to automatically adopt the dimensions of its parent? How can I retrieve the width and height values of the parent element in order to achieve this effect? Here is an example code snippet: .thumb { width: 120px; ...

Guide on implementing event listener for right click using pure JavaScript (VANILLA JS)

I need the div to appear wherever the cursor is holding down the right mouse button. In my scenario, I am using the following code: <div class="d-none" id="item"></div> #item{ position: absolute; top: 0; left: 0; w ...

"Aligning content in Bootstrap with vertical centering

I am currently working on aligning a simple piece of code vertically. Check out the code on JS Fiddle: https://jsfiddle.net/3kj44net/ .container-fluid { padding-left: 20px; padding-right: 20px; } <link href="https://maxcdn.bootstrapcdn.com/boots ...

Issue arising with the resizing of the image in the main section, as it is shrinking prematurely before reaching the designated width

Desired Outcome Current Situation Currently, I am in the process of creating a responsive hero section where I aim to maintain the image at full size until the window width reaches below 1300px. However, I have observed that the image starts to shrink mu ...

Ways to invoke jQuery(this) within an anchor tag's onclick event handler

My task involves working with HTML code and I need to apply a class name to the parent li tag using jQuery. Here is the HTML code snippet: <ul> <li><a onclick="setSelectedTestPlan();" href="javascript:void(0);">New Test</ ...

translating creates vacant spaces on both sides

I am working with a <div class="scrollable"> that has the CSS property overflow: scroll;. Additionally, I have another <div class="line1"></div> which should not trigger any scrolling on the <div class="scrolla ...

What is the best way to align sections side by side using Bootstrap?

Hello, I'm currently trying to customize the layout of this Bootstrap webpage to resemble the design shown in this image https://i.sstatic.net/w7i6Z.png. Specifically, I want to display the blog section and the team section side by side. As a beginne ...

Animation of disappearing blocks covering the entire screen

I am currently working on creating a slider with fading blocks animation similar to the one shown here. The challenge I am facing is making it fullscreen, where the height and width will be variable. This makes using the background-position trick ineffecti ...

issues with the handler not functioning properly in html and javascript

<form method="post" action="."> <label class="input-label">Enter Your First Name</label><input field="first_name" class="input-edit" maxlength="30" type="text" value="{{ user.first_name }}" /> <label class="i ...

Displaying a video in fullscreen on a webpage

Struggling to achieve the desired result, seeking advice from experts. Looking to create a classic example like this: http://codepen.io/anon/pen/rWwaRE But want it embedded within blocks of text and possibly triggered by scrolling over it similar to this ...

The width of the Div element is not determined by the parent CSS grid, leading to a lack of ellipsis

I am working on a grid layout where each column contains a div with text. My goal is to have the text show ellipsis when it overflows the column, even though the number of columns is random. I attempted to use the following CSS for the text-containing div ...

What is the best way to set a stationary background that scrolls on a responsive webpage?

I'm working on a responsive website with alternating sections featuring fixed background images where the content is meant to scroll over the image. However, I'm facing an issue on iOS where the background image zooms into a small section and sca ...

Using htaccess to eliminate PHP and HTML files from your website

I've been scouring the Internet for quite some time now, trying to find a working htaccess file for my website. I'm using CuteNews, a news CMS that offers URL rewrite functionality. However, when I enable URL rewriting, it causes my news pages to ...

unusual behavior observed in addEventListener

I have recently delved into learning about the DOM. I have a project in mind where I want to create a website with buttons that, when clicked, play different drum sounds. As part of my experimentation with JavaScript, I wanted to explore the this keyword. ...

Extract the price value from the span element, then multiply it by a certain number before adding it to a div element

On the checkout page of my website, I have the following HTML: <tr class="order-total"> <th>Total</th> <td><strong><span class="woocommerce-Price-amount amount"> <span class="w ...