Setting CSS margins to properly align a list next to a float-left image

Here's my initial attempt at coding a reddit-style comment using html and css. I have some inquiries about the css and overall structure:

  1. How can I align the comment body ("The King took off his hat...") with the comment head ("Nathan, posted...") and the comment tail ("reply permalink ...")? I experimented with adjusting the margin-bottom for .comment-left but it didn't resolve the issue.
  2. I may have gone overboard with the
    tags. Which ones are unnecessary?
  3. Is there a more efficient way to achieve the same layout?

Thank you all, Nathan

ps - I utilized helpful details from here to stack my voting arrows on top of each other.


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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>CSS sandbox: comments</title>

    <style type="text/css">

      .vote {
        width: 15px;        
        float: left;
        clear: left;      
      }
      .vote img {
        display: block;
        float: none;
        clear: both;
        width: 15px;
      }
      .userpic img {
        width: 60px;
      }
      .comment-contents li {
        list-style-type: none;
        margin-bottom: 10px;
      }
      .comment-left {
        float: left;
      }
      .head {
        margin-left:10px;
      }
      .tail-list li {
        display: inline;
      }
      img {
        border: 0;
      }
      a {
        text-decoration: none;
      }
    </style>
  </head>

  <body>
    <div class="comment">
       <span class="comment-left">
        <span class="vote">
          <a href="#"><img alt="^" title="vote up" src="http://www.reddit.com/static/aupgray.gif"></a>
          <a href="#"><img alt="v" title="vote down" src="http://www.reddit.com/static/adowngray.gif"></a>
        </span>
        <span class="userpic">
          <a href="#">
          <img src="http://www.gravatar.com/avatar/550deada0ac679dfc3c9103b674760af?s=128&d=identicon&r=PG" height="60" width="60">
          </a>
        </span>
       </span>

       <span class="comment-main">
        <ul class="comment-contents">
          <li class="head">
           <a href="#">Nathan</a>, posted 2 hours ago
           <a href="#" class="comment-collapse">[-]</a>
          </li>
          <li class="middle">
            <p>The king took off his hat and looked at it. Instantly an immense
            crowd gathered. The news spread like wildfire. From a dozen leading
            dailies, reporters and cameramen came rushing to the scene pellmell in
            highpowered monoplanes. Hundreds of reserves, responding without
            hesitation to a riot call, displayed with amazing promptness quite
            unparalleled inability to control the ever-increasing multitude, but
            not before any number of unavoidable accidents had informally
            occurred.</p>

            <p>Chapter 1 - untitled (eecummings)</p>
          </li>
          <li class="tail">
           <ul class="tail-list">
            <li><a href="#">reply</a></li>
            <li><a href="#">permalink</a></li>
            <li><a href="#">offensive?</a></li>
           </ul>
          </li>
        </ul>
      </span>
    </div>
  </body>
</html>

Answer №1

Revise your CSS by adding left padding to ".comment-contents li" and removing the margin from ".head"

.comment-contents li {
list-style-type: none;
margin-bottom: 10px;
padding-left: 60px;
}

.head {
margin-left:0px;
}

Additionally, consider using padding instead of margin for ".comment-contents li":

.comment-contents li {
list-style-type: none;
padding: 0 0 10px 60px;
}

I suggest avoiding excessive use of padding and margin within a class unless absolutely necessary.

Answer №2

Currently, I don't have the time to work on CSS (maybe later), but you can achieve the desired appearance using this code snippet:

<div class="comment">
     <p class="user">
        <a href="#">
          <img src="http://www.gravatar.com/avatar/550deada0ac679dfc3c9103b674760af?s=128&d=identicon&r=PG" height="60" width="60">
        </a>
        <a href="#">Nathan</a>, posted 2 hours ago
        <a href="#" class="comment-collapse">[-]</a>
     </p>
     <p>The king took off his hat and looked at it. Instantly an immense
            crowd gathered. The news spread like wildfire. From a dozen leading
            dailies,reporters and cameramen came rushing to the scene pellmell in
            highpowered monoplanes. Hundreds of reserves,responding without 
            hesitation to a riotcall,displayed with amazing promptness quite 
            unparalleled inability to control the everincreasing multitude,but 
            not before any number of unavoidable accidents had informally 
            occurred.
     </p>
     <p>Chapter 1 - untitled (eecummings)</p>
  <ul class="commentTools">
    <li class="voteUp"><a href="#">vote up</a></li>
    <li class="voteDown"><a href="#">vote down</a></li>
    <li><a href="#">reply</a></li>
    <li><a href="#">permalink</a></li>
    <li><a href="#">offensive?</a></li>
  </ul>
</div>

Additionally, here is the accompanying CSS:

.comment {
   position:relative;
    padding-left:75px;
}
.comment p {

}
.comment .user img {
  float:left;
  margin-left:-60px;
}
.comment .commentTools {
    padding-left:0;
}
.comment .commentTools li {
   display:inline;
}
.comment .commentTools .voteUp, .comment .commentTools .voteDown {
   position:absolute;
   display: block;
   left:0;
}
.comment .commentTools .voteUp {
    top:0;
}
.comment .commentTools .voteDown {
    top:15px;
}
.comment .commentTools .voteUp a, .comment .commentTools .voteDown  a {
  display:block;
  width: 15px;
  height: 15px;
  text-indent:-5000em;
}
.comment .commentTools .voteUp a {
    background: url(http://www.reddit.com/static/aupgray.gif) no-repeat;
}
.comment .commentTools .voteDown a {
    background: url(http://www.reddit.com/static/adowngray.gif) no-repeat;
}

I haven't tested this thoroughly, so there may be some bugs present. Feel free to reach out if you need further clarification on any aspect of the CSS.

Answer №3

Would it be possible to adjust the margin-left of the paragraph to 30px?

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

Searching for a particular string, selector, or XPath on numerous web pages using the "inspect element" browser tool - what is the best approach?

Hey everyone, I hope you're all having a fantastic day. I'm wondering, is there a way to search for an element across multiple web pages simultaneously, rather than just one page at a time? The goal is to locate a specific string or code (like ...

Stop animation on mobile devices by modifying the CSS file

Using jQuery, I added an animation class to a div on my webpage. The animation.css file has been included as well. The class animated slideInLeft is currently in use. My question is whether it is possible to disable this class for mobile devices in order ...

When utilizing the tab key on Chrome, the iFrame fails to scroll

We are currently facing an issue with our web application that is embedded inside an iFrame. On one of our HTML pages, there are numerous textboxes with a large amount of content, requiring users to scroll down to navigate through it all. When using the ...

The default height setting for React MUI Avatar div is a fixed value of 40 pixels

I am working with a MUI Avatar component in my project: <Avatar sx={{ width: 180, height: "auto", maxHeight: 180, margin: 2 }} variant="square" alt={""} src={ global.config.baseUrl + "/image/logo" ...

What is the best way to convert a string from a csv file into an integer using Python?

I'm currently working on a code that involves processing data obtained from this source () and organizing it into a CSV file. My goal is to create a program that calculates the BMI of each player, then identifies them as obese if their BMI exceeds 30 ...

I'm puzzled as to why the navigation bar isn't staying fixed

I recently created a landing page using Bootstrap, but I'm having an issue with the navbar not being fixed. Here is the code for the navbar: <nav class="navbar fixed-top navbar-inverse bg-primary navbar-toggleable-md navbar-light bg-faded"> < ...

Optimizing Safari for the Best Screen Size Experience

I seem to be encountering issues with optimizing my website. Everything appears to be working correctly in terms of widths, heights, margins, etc., on my laptop. However, when I view the site on an iMac or any other computer with a larger screen size, the ...

Embedded HTML code within a JSON structure

When attempting to send HTML content within a JSON object as a servlet response, Internet Explorer 10 fails to display the HTML tags along with the content. The response type being used is application/json. Java: String summaryRptHTML = sum.generateSumma ...

How to Fetch Data Using jQuery AJAX in PHP Without Page Refresh

I am trying to prevent the page from refreshing when clicking on a select option, but when I do so, the data does not get updated. Here is the code: echo "<form name='frmtopdevotees' method='post' action='topuser_load.php&apos ...

When scrolling down, the popup window shifts its position

I have implemented a popup window which displays a list on hover. It works perfectly on the default page, but when I scroll down, the popup also moves with the scrollbar. Below is the HTML code that creates the hidden popup list initially and shows it on ...

Unable to render text onto an html5 canvas

Currently delving into JS after having experience in other programming languages. My focus right now is on creating a canvas that can display text. https://jsfiddle.net/b5n2rypn/ The issue at hand: Despite using the fillText method, no text appears on th ...

The Angular Google map fails to show up when embedded into a SweetAlert2 popup

I am attempting to showcase a segment of Google Maps within a popup using sweetalert2. The HTML code snippet provided below works fine when directly placed in the HTML page, but fails to work within the alert. export function showGoogleMap() { swal({ ...

Steps for making a grid within a bootstrap 3 modal

I am new to HTML and CSS I'm working on creating a modal content similar to this: https://ibb.co/RvrhmRs Most of the work is done, but my modal currently looks like this: https://ibb.co/1zG0y2t I just need help arranging an icon next to the text. ...

Confirming the information before submitting the form

My submit button is set up like this: <input class="create_button" name="commit" onclick="return validate_activity();" type="submit" value="Save"> I have noticed that this button always sends a request to the server regardless of wh ...

What is the best way to send information from an MVC controller to JavaScript?

Being new to programming, I am working on 2 projects that function as client-server via Rest API. I am looking to create a column chart using data from a List of ID (participant) and Votes (Total votes) obtained from the server's connection with the d ...

Closing out with page content wrap-up

I am currently working on a personal project using Django for the back-end and Bootstrap for the front-end. I have encountered an issue where my footer is covering part of the content below "Diadema" in the list, and a scroll bar does not appear as it shou ...

The external hyperlink is not functional in Firefox and Chrome browsers, but it operates properly in Internet Explorer 9

For security reasons, it seems that embedding an external link for live scoring from itftennis.com on our website is blocked. If you try to click on the jsfiddle LIVE SCORING link, it will open a page with an error. However, if you click on the address ba ...

What is stopping me from utilizing ES6 template literals with the .css() method in my code?

I am currently working on a project where I need to dynamically create grid blocks and change the color of each block. I have been using jQuery and ES6 for this task, but I am facing an issue with dynamically changing the colors. Below is the snippet of m ...

Achieve maximum use of vertical and horizontal space for items in a flexbox

I have a container with elements, each containing an image. I want to arrange these images in the four corners of the container using flexbox. Currently, they are distributing correctly horizontally but not taking up all the available space vertically. He ...

Tips for sending a string as HTML content in VueIn Vue, you can send a string as

I am attempting to pass the value for exp by using this code snippet. However, the form of selectedChannel.explanation appears as "channel name". Is there a way for me to display exp as channel name? computed: { channel: { get() { ...