Troubles with element alignment

Hello everyone, I am in need of some assistance with HTML and CSS.
I am a complete beginner and I am trying to create a design that looks like the one shown below:

While I have managed to generate the container itself, I am having trouble with aligning the Blog Title, Blog Post Date, Blog Summary, and Read More button properly. I have only been successful in placing the image container correctly so far.

Here is the output I am currently getting:

  
/* CSS Code */
  body {
    margin: 0;
    font-family: "Bai Jamjuree", sans-serif;
    /* Adjust with the exact font if found */
  }
  
  /* Style for Blog Container */
  
  .blog-container {
    width: 1240px;
    overflow: auto;
    margin-left: auto;
    position: relative;
    left: -3px;
    margin-right: auto;
    max-width: 94%;
    padding: 5px 20px;
    background-color: white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    margin-bottom: 100px;
    box-sizing: border-box;
  }
  
  /* Other CSS Styles... */

Answer №1

I've corrected a few errors in the HTML and CSS, such as removing onclick="';" and adjusting the code for better functionality.

I also eliminated the line .blog-thumbnail { flex: 1; } from the code.

Additional modifications were made to improve other sections of the CSS.

Included some new div elements and classes into the HTML.

Implemented new CSS rules for both existing and newly added classes.

Your current code is not optimized for various screen sizes. I have addressed this issue and aimed to align it with the desired output you provided.

Below is the updated code:

body {
  margin: 0;
  font-family: "Bai Jamjuree", sans-serif;
}

/* Style for Blog Container */

.blog-container {
  width: 1240px;
  overflow: auto;
  margin-left: auto;
  position: relative;
  left: -3px;
  margin-right: auto;
  max-width: 94%;
  padding: 5px 20px;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 100px;
  box-sizing: border-box;
}

.blog-thumbnail img {
  width: 150px;
  height: 150px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-right: 20px;
  object-fit: cover;
}

.blog-post {
  display: flex;
  justify-content: space-between;
  align-items: start;
  height: 150px;
  margin-bottom: 20px;
  margin-top: 20px;
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  max-width: 1200px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.blog-img-and-text {
  display: flex;
}

.blog-post h2 {
  font-size: 1.2em;
  margin: 0 0 15px 0;
  color: #333;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="blog-container" id="blogContainer">
      <div
        class="blog-post"
        data-date="2024-08-30"
        onclick=""
        style="cursor: pointer"
      >
        <div class="blog-img-and-text">
          <div class="blog-thumbnail">
            <img
              src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg"
              alt="Thumbnail"
            />
          </div>

          <div>
            <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
            <p>
              Unlock the Power of Aerial Views & Attract More Buyers to Your
              Listings
            </p>
            <p>
              Unlock the Power of Aerial Views & Attract More Buyers to Your
              Listings
            </p>
          </div>
        </div>

        <div class="date-and-readmore">
          <p>August 30, 2024</p>
          <a href="">Read more</a>
        </div>
      </div>
    </div>
  </body>
</html>

Answer №2

CSS-Grid is recommended for this layout.

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

 ::before,
 ::after {
  box-sizing: inherit;
}


/* Styles for Blog Container */

.blog-container {
  max-width: 1240px;
  overflow: auto;
  margin-left: auto;
  position: relative;
  margin-right: auto;
  padding: 20px;
  /* Responsive design for smaller screens */
  padding: 5px 20px;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 100px;
  box-sizing: border-box;
}

.blog-post {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto 1fr;
  gap: 20px;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.blog-thumbnail {
  grid-row: span 2;
}

.blog-thumbnail img {
  width: 150px;
  height: 150px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  object-fit: cover;
}

.blog-post {
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  max-width: 1200px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  text-align: left;
}

.blog-post h2 {
  font-size: 1.2em;
  color: #333;
}

.blog-post p {
  font-size: 1em;
  color: #666;
}

.blog-post a {
  color: #1e7cd6;
  text-decoration: none;
  font-weight: bold;
  align-self: end;
}

.blog-post a:hover {
  color: #104e8b;
}
<div class="blog-container" id="blogContainer">
  <!-- Example Blog Post -->
  <div class="blog-post" data-date="2024-08-30" onclick="';" style="cursor: pointer;">
    <div class="blog-thumbnail">
      <img src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg" alt="Thumbnail" />
    </div>
    <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
    <p>August 30, 2024</p>
    <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings Lorem ipsum dolor sit amet consectetur adipisicing elit. Incidunt iste rerum perferendis ab voluptate optio repudiandae at, ratione inventore laboriosam officia? </p>
    <a href="">Read more</a>
  </div>
</div>

Answer №3

Consider restructuring the layout and utilizing semantic tags for better organization.

In your design, you have 3 columns that can be aligned side by side using Flexbox.
A more semantically correct approach would involve using aside as containers for the image, date, and "read more" sections. The main column containing your blog post should be wrapped in an article tag.

You can make the article element expand to fill the available space by applying flex-grow: 1 to it.

To achieve your desired layout with the date and "read more" columns, utilize flexbox with flex-direction: column. This allows you to use margin-top: auto on the "read more" link to position it at the bottom.

Note: Avoid using margins to separate columns within a flexbox container; opt for gap instead!

section.blog-post {
  display: flex;
  gap: 1.25em;
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  max-width: 1200px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  text-align: left;
  
  aside.blog-thumbnail img {
    width: 150px;
    height: 150px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    object-fit: cover;
  }
  
  article {
    flex-grow: 1;
    h2 {
      font-size: 1.2em;
      margin-bottom: 20px;
      color: #333;
      margin-top: 0;
    }
    p {
      font-size: 1em;
      color: #666;
      margin-bottom: 100px;
    }
  }
  
  aside.blog-date {
    display: flex;
    flex-direction: column;
    time {
      color: #666;
    }
    a {
      color: #1e7cd6;
      text-decoration: none;
      font-weight: bold;
      margin-top: auto;
    }
  }
}
<main class="blog-container" id="blogContainer">
  <!-- Example Blog Post -->
  <section class="blog-post" data-date="2024-08-30" onclick="';" style="cursor: pointer;">
    <aside class="blog-thumbnail">
      <img src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg" alt="Thumbnail" />
    </aside>
    <article>
      <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
      <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings</p>
    </article>
    <aside class="blog-date">
      <time datetime="2024-08-30">August 30, 2024</time>   
      <a href="">Read more</a>
    </aside>
  </section>
</main>

Answer №4

Consider adding one more wrapper for handling typography elements.

body {
  margin: 0;
  font-family: "Bai Jamjuree", sans-serif;
  /* Adjust with the exact font if found */
}

/* Style for Blog Container */

.blog-container {
  width: 1240px;
  overflow: auto;
  margin-left: auto;
  position: relative;
  left: -3px;
  margin-right: auto;
  max-width: 94%;
  /* Responsive for smaller screens */
  padding: 5px 20px;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 100px;
  box-sizing: border-box;
}

.blog-thumbnail img {
  width: 150px;
  height: 150px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-right: 20px;
  object-fit: cover;
}

.blog-post {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  max-width: 1200px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  text-align: left;
  display: flex;
  margin-bottom: 20px;
  margin-top: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ddd;
}

.blog-post h2 {
  font-size: 1.2em;
  margin-bottom: 20px;
  color: #333;
}

.blog-post p {
  font-size: 1em;
  color: #666;
}

.blog-post a {
  color: #1e7cd6;
  text-decoration: none;
  font-weight: bold;
}

.blog-post a:hover {
  color: #104e8b;
}

a {
  text-decoration: none;
}

/* Responsive Styles */

@media (max-width: 768px) {
  .blog-post {
    flex-direction: column;
    gap: 10px;
  }
}
<div class="blog-container" id="blogContainer">
  <!-- Example Blog Post -->
  <div class="blog-post" data-date="2024-08-30" onclick="';" style="cursor: pointer;">
    <div class="blog-thumbnail">
      <img src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg" alt="Thumbnail" />
    </div>
    <div>
      <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
      <p>August 30, 2024</p>
      <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings</p>
      <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings</p>

      <a href="">Read more</a>
    </div>
  </div>
</div>

In addition, there is excessive CSS in your code, such as hardcoding values like 94%. Consider making your layout fluid instead of relying on fixed measurements. Read this article for guidance:

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

Declare a jQuery variable containing HTML elements

Greetings! I am facing an issue while attempting to create a jQuery variable that stores a table meant for displaying in different sections of a website. Unfortunately, I am encountering an error and struggling to comprehend the reason behind it. Below is ...

Incorrect placement of rectangle on a dynamic canvas

After clicking on two different positions on the canvas rectangle, I noticed that it was being drawn in the wrong location due to issues with the responsive canvas size. Despite attempting to scale the pointer position based on both the canvas and window w ...

Events Unavailable for Viewing - Full Calendar - Database Error Encountered

I am currently developing a calendar application. While I have managed to successfully display the calendar and add events, I am facing an issue with displaying events on the monthly view. Here is a snippet of the HTML code being used: <head> &l ...

What is the best way to have my program switch out a string with the pressed key in a particular portion of the string?

I'm currently developing a hangman game using JavaScript. I am facing an issue where I need to replace the "-" with the guessed letter by the user in the correct position it belongs to within the word. While I can add the letter at the beginning or en ...

Instructions for turning an HTML table cell into an editable text box

Essentially, I'm looking to enable users to click on the table and edit the text within it. I found inspiration from this Js Fiddle: http://jsfiddle.net/ddd3nick/ExA3j/22/ Below is the code I've compiled based on the JS fiddle reference. I tho ...

The functionality of Jquery UI is not compatible with version 1.12

Incorporating jQuery UI into my current project has presented some challenges. Both the jquery-ui.min.css and jquery-ui.min.js files are version 1.12, so I opted for the latest jQuery version, jquery-3.2.1.min.js. Specifically, I decided to test the datep ...

The functionality of the Delete and Edit buttons has been disabled

As I am developing a CRUD (Create, Read, Update, Delete) application that involves managing a list of students and their grades, I have encountered issues while trying to implement the delete and edit functionalities on the front end. Specifically, I am st ...

Difficulty in modifying a global variable within an event handler

I am working on an ionic4 app that includes a button. The goal is to display the accelerometer alpha value when the button is pressed. However, I am encountering an issue where the event handler invoked by the event listener does not seem to update the g ...

Troubleshooting problem with video recording functionality using HTML 5 media streams

My HTML 5 code for video recording and uploading is encountering a JavaScript error when the start button is clicked. The error messages I am receiving are: "TypeError: webcamstream.record is not a function streamRecorder = webcamstream.record();" "TypeE ...

Image not being shown by ng-src

I'm struggling to get my picture to display using ng-src. <img ng-src="http://localhost:3000/images/{{image.image}}" class="img-circle" alt="User" style="width: 130px; height: auto;"> Although the data is present when I console log this: ...

Struggling to eliminate the unseen padding or white space preventing the text from wrapping under the button

Just starting out with html and css and could use some assistance with a small issue I've come across. While redesigning a website, I have a three-button form (Donate, Find, Subscribe). I'm trying to get the paragraph underneath the Donate and Fi ...

Enhancing readability in a vertical CSS menu with proper spacing

I'm looking to increase the spacing between each menu item as they appear too close together. Can someone help me with managing this? The menu names are currently right under one another, almost touching. What is the best way to create more space betw ...

Encountering an error while trying to execute `$(document).ready(function() {}

Recently, I delved into the world of JavaScript, particularly Jquery. As I encountered the $(document).ready(function() {}); statement and added it to my code, I faced an issue when placing it within the header tag of my HTML. The script tag would display ...

Using CSS3 to shift a div in relation to another moving div

Take a look at this JSfiddle. I am trying to make the "Push this!" div move when the menu resizes/expands on hover. Is it possible to achieve this effect using only CSS? <div id="nav"> <ul> <li><a href=""><span>01</spa ...

Tips for incorporating inline styling into the body element

Can someone help me with adding inline style to the body element using jQuery? I want to set the background color to white (#FFFFFF). Your assistance would be highly appreciated. Thank you! ...

Picture is not appearing on web browser within Node application

Recently, while working with Visual Studio Code, I decided to incorporate an image into my Node project. After downloading the image from the internet and placing it in the directory, I renamed it to 'file_2.jpeg'. I then included <img src = " ...

Table within a table does not occupy full height within a table cell

I encountered an issue while nesting a table within a td element. The behavior differs between browsers: in Firefox, the nested table fills the entire cell from top to bottom, but in Edge and Chrome, it is centered within the td cell and does not occupy th ...

Creating a new form upon clicking

By clicking the "Add New Job" link, a new form with three fields will appear. This is the primary form: <h2>JOB EXPERIENCE 1</h2> <div class="job-content"> <input type="text" value="Company" name="company" /> <input type="te ...

SVG: spin the entire text

I need assistance rotating a list of words at an angle, specifically tilting only the characters: https://i.sstatic.net/r96Mt.png Here is my code: <svg width="2000" height="130"> <g *ngFor="let fruit of fruits"> <g> ...

Ways to ensure a div matches the size of the div above it within a Boostrap grid column

Hello there, this is my very first question on this platform. I'm still in the early stages of learning all these concepts, so please be patient with me ...