Is there a way to arrange two columns in a horizontal line?

The alignment of the columns is off:
https://i.sstatic.net/1l4CO.png

The issue: https://i.sstatic.net/Iokm9.jpg

I am looking to create a blog layout with 2 columns:

  1. one column for the main content, displaying articles
  2. the other column as a menu

To achieve this, I am using CSS code as I couldn't think of any other method (refer to the code below).

In terms of my HTML structure, I aim to keep it simple and tidy (check out the code below).

How can I align the second column properly with the first one?

There seems to be some padding around 20px but it isn't present in my code. I am at a loss on how to fix this.

.column {
  float: left;
  padding-top: 1%;
  font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, "sans-serif";
}


/* Left Column (main) */

.column.main {
  width: 76%;
  background-color: grey;
  padding-bottom: 1%;
  padding-right: 1%;
  padding-left: 1%;
}


/* Right Column (menu) */

.column.menu {
  width: 20%;
  background-color: lightgrey;
  padding-bottom: 1%;
  padding-right: 1%;
  padding-left: 1%;
}
<div class="column main">
  <div class="card">
    Content
  </div>
</div>

<div class="column menu">
  <div class="card">
    <h3 align="center">Main Menu</h3>
    <li>Main Menu</li>
    <li>English</li>
    <li>German</li>
    <li>Spanish</li>
  </div>
</div>

Answer №1

Eliminate any default margins by using a CSS Reset and make sure to fix the invalid structure in your HTML. Remember that li elements should be wrapped within a ul.

.column {
  float: left;
  padding-top: 1%;
  font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, "sans-serif";
}


/* Left Column (main) */

.column.main {
  width: 76%;
  background-color: grey;
  padding-bottom: 1%;
  padding-right: 1%;
  padding-left: 1%;
}


/* Right Column (menu) */

.column.menu {
  width: 20%;
  background-color: lightgrey;
  padding-bottom: 1%;
  padding-right: 1%;
  padding-left: 1%;
}

* {
  margin: 0;
}
<div class="column main">
  <div class="card">
    Content
  </div>
</div>

<div class="column menu">
  <div class="card">
    <h3 align="center">Main Menu</h3>
    <ul>
      <li>Main Menu</li>
      <li>English</li>
      <li>German</li>
      <li>Spanish</li>
    </ul>
  </div>
</div>

Answer №2

To resolve this issue, simply reset all margins to zero.

* {
   margin: 0;
   padding: 0;
}

This is necessary because certain HTML elements come with default margins such as body, h1 to h6, p, fieldset, form, ul, ol, dl, dir, menu, blockquote, and dd.

Answer №3

Check out this code snippet for a simple HTML and CSS layout:

<div class="wrapper">
    <div class="header">Header</div>
    <div class="content">
        <div class="main">Content</div>
        <div class="sidebar">
            <ul>
                <li>Home</li>
                <li>About</li>
                <li>Contact</li>
                <li>Services</li>
            </ul>
        </div>
    </div>
    <div class="footer">Footer</div>
</div>

CSS Styling:

.wrapper {
    display: grid;
    grid-template-columns: 1fr 2fr;
}
.header, .footer {
    background: #333;
    color: white;
    text-align: center;
}
.content {
    display: flex;
}
.main {
    flex: 2;
    background: #f4f4f4;
    padding: 10px;
}
.sidebar {
    flex: 1;
    background: #ddd;
    padding: 10px;
}

Answer №4

If you want to create two columns side by side in HTML, you should give this simple code a try!

   <div class="row">
   <div class="column"></div>
   <div class="column"></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

Using Python, Scrapy, and Selenium to extract dynamically generated content from websites utilizing JavaScript

I am currently utilizing Python in combination with Selenium and Firefox to extract specific content from a website. The structure of the website's HTML is as follows: <html> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"> ...

Implementing uniform 1px borders across Bootstrap columns

Is there a way to achieve a consistent 1px border around columns using only CSS, without creating a 2px border when two columns are side by side? I want the outer edges to have a 1px border while the inner borders have a double width due to being adjacent. ...

Crafting a dynamic CSS 'trail' effect when hovering

I am attempting to create a visually appealing CSS menu using primarily CSS, with just a touch of jQuery as well: Here is the concept: +------------------------+ | | | | | +---+ | | | ...

Discovering the maximum value in AngularJS using an expression (complete with a bug demonstration)

How can I set a maximum value for an input that can be either a numeric amount or a percentage? Is it possible to use ng-max to set the max value to 100 if the input is a percentage, and leave it blank if it's a numeric amount? I attempted the follo ...

Positioning an HTML div

Currently, I am diving into the world of HTML and have included a relative div in my code. Although there are two additional divs positioned before it, they do not overlap; unfortunately, the first two still occupy space above the relative one. My desire ...

A method for displaying information in HTML by implementing ng-repeat in AngularJS

I have retrieved JSON data in my controller as a response and now I need to display it in the HTML. Here is what I have implemented: Inside my controller: .controller('DataImportControl', ['$scope','$http', '$location& ...

Picking the following div using a button and updating its content

I am currently working on a project that involves 4 div elements, each with its own value (for example: 0). There is also a button with plus and minus functionality to change the values in each box. Additionally, there is a "set next" button to move on to ...

Looking to slide a form on top of an image carousel in Bootstrap - any tips?

How can I move this form over the "carousel"? <html> <head> <title>test</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http ...

Retrieve the AJAX response data to be utilized in future operations

It appears that there is a bug in using INNERHTML where my SELECT OPTION tag becomes blank, based on the research I have done. I am trying to figure out how to create a variable containing my OPTION tags for later use, based on the JSON data below, but I a ...

What is the most effective method for transitioning between states in Angular when it comes to modifying the HTML display?

When faced with the decision of choosing between two different approaches for managing component view during loading, the "best way" refers to performance and minimizing UI blinking (reducing DOM renderizations). Approach 1: Utilize a loading variable to ...

Extract the image URL from the href attribute using Xpath

My goal is to extract all images enclosed in href attributes from the given code snippet <div class="jcarousel product-imagethumb-alt" data-jcarousel="true"> <ul> <li> <a href="https://domain/imagefull.jpg" onclick="return false;" cla ...

Unable to overlay a div on top of an image

Hello, I'm currently attempting to overlay a div on top of an image. Since the image needs to be responsive, it cannot be set as a background image. Here is the CSS I am using: #slider { margin:0 33%; width:67%; position:relative; } #sli ...

Display endless data within a set window size

I am looking to create a fixed-size window for displaying text from the component <Message/>. If the text is longer, I want it to be scrollable within the fixed window size. See screenshot below: Screenshot export default function AllMessages(){ ...

The button in the flex container is not wide enough due to center alignment

For my project, I implemented a CSS grid layout consisting of 20 rows and 20 columns (5% of screen for each cell). One particular page includes a button that initially fit within grid columns 5-8 and rows 6-9 without any issues. However, I wanted to center ...

HTML text has been partly processed

When Xml is parsed using a SAX Parser, there is one specific tag called <Panchanga> which contains HTML contents. The structure of the tag is as follows: <Panchanga><table style='font-size:14px;'> <tr> <td ><img ...

Can I combine the col and d-flex classes in Bootstrap 5 without breaking any rules?

Is it possible to use both col and d-flex classes together? I want to center the element with the class "description" by combining them. <section class="container-fluid mb-5"> <div class="row"> <div class=&q ...

Customize the appearance of the date input box in MUI KeyboardDatePicker

Currently, I am attempting to customize the appearance of the KeyboardDatePicker component including board color, size, font, and padding. However, none of the methods I have tried seem to be effective. Here is what I have attempted so far: 1 . Utilizing ...

Is it possible for audio to be activated and played while the phone is locked?

I am currently developing a web chat application and I have encountered an issue regarding playing notification sounds when a new message arrives. Initially, I decided to utilize howler.js for playing the sounds. While howler allows us to start playing a ...

JavaScript allows you to create matrices from squares

Hey everyone, I'm facing an issue with creating matrices in HTML. I need to display a 5x5 matrix where each index is represented by a square. I tried using a span template for the box and then wrote a script to populate the matrices, but it's no ...

Using Jquery to set values for css properties

I've recently started using jquery and I have a task related to a drop-down menu that I'm struggling with: if (scrnwidth <= 761) { if (display was block) { //Defaultly testi has display:none property. testi = m ...