What is the best way to position a title and subheading alongside an image?

I am currently working on developing a blog using php and html to enhance my coding skills. On my website, I have a special category where I display posts, the number of which can vary.

Each post is composed of a title, a subheading, and a placeholder div called "fakeimg" for images that are not yet available. I want the subheading to appear below the title with the fakeimg positioned next to them at the same height as the title.

                             _________
title, usually short       |         |   
                            | fakeimg |
subheading, one or two rows|         |
                           |_________| 

---------hr between posts---------
                             _________
title, usually short      |         |   
                            | fakeimg |
subheading, one or two rows|         |
                           |_________| 

I have tried various solutions found online to align them as desired using properties like display: flex and block, but haven't been successful.

Below is the CSS code for the fakeimg, the container div for title, subheading, and fakeimg, as well as separate divs for only the title and only the subheading.

.fakeimg {
  width: 100px;
  height: 100px;
  padding: 3em 2em;
  margin: 0.5em;
  background-color: lightgrey;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  float: right;
  display: block;
}

 .title-and-subheading-onecat{
  margin-right: 1.5em;
  display: flex;
  align-items: start;
}

.one-post  {
  clear: both;
  margin: 0;
}

.post-description {
  margin-bottom: auto;
}

Here is an excerpt of the page's code:

<?php foreach($post as $p):?>
  <div class="title-and-subheading-onecat">
    <div class="one-post">
      <a href="http://localhost:8888/blog/public/index.php/post?id=<?php echo e($p->id); ?>">
        <?php echo (e($p['title'])); ?>
      </a>
      <br />
    </div>
    <div class="post-description">
        <?php echo nl2br(e($p['subheading']));?>
    </div>
    <div class="fakeimg">
      Image
    </div>
  </div>
<?php endforeach;?>

The fakeimg now starts at the same height as the title, as intended. However, all fakeimgs need to be aligned on the right side with consistent horizontal positioning, instead of being directly next to the subheading.

I would greatly appreciate any assistance from experienced developers as I am still learning the ropes in coding.

Answer №1

To achieve the desired layout, you will need to utilize separate div elements. One div should contain the title and subtitle, while the other should hold the image. Set the widths of both divs so they total 100%, and float them left to ensure they appear side by side. I have made some adjustments to your code for better presentation.

CSS

.img-parent {
  width: 50%;
  float: left;
}
.fakeimg {
  width: 100px 100px;
  padding: 3em 2em;
  margin-top: 0.5em;
  margin-bottom: 0.5em;
  margin-right: 0.5em;
  background-color: lightgrey;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  position: relative;
  display: block;
}

.title-and-subheading-onecat{
  margin-right: 1.5em;
  display: block;
  align-items: start;
}

.one-post  {
  width: 50%;
  float:left;
}

.post-description {
  float: left;
  margin-bottom: auto;
}

HTML:

<div class="title-and-subheading-onecat">
  <div class="one-post">
    <div class="title">
      <a href="http://localhost:8888/blog/public/index.php/post?id=1">
        asdasd asd asd asd asd asd asd
      </a>
    </div>
    <div class="post-description">
      asdasdasd asd asd ad as dasd asdas das asd asdas dasd asd asda das dasd asds
    </div>
  </div>
  <div class="img-parent">
    <div class="fakeimg">
      Image
    </div>
  </div>
</div>

JSFiddle: https://jsfiddle.net/ashhaq12345/Lgsa0r6f/

Answer №2

I successfully achieved the desired layout using flex. By wrapping the title and subtitle in a div and applying display:flex to .title-and-subheading-onecat, I was able to create the desired structure. Hopefully, this solution works for you as well.

.fakeimg {
                    width: 100px 100px;
                    padding: 3em 2em;
                    background-color: lightgrey;
                    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
                    display: block;
                }

                .title-and-subheading-onecat{
                    margin-right: 1.5em;
                    display:flex;
                    align-items: start;
                }

                .one-post  {
                    clear: both;
                    margin: 0;
                }

                .post-description {
                    margin-bottom: auto;
                }
                
            
<div class="title-and-subheading-onecat ">
                    <div class="">
                        <div class="one-post">
                            <a href="">
                                TITLE HERE
                            </a>
                            <br />
                        </div>
                        <div class="post-description">
                            TITLE HERE
                        </div>
                    </div>
                    <div class="fakeimg">
                        Image
                    </div>
                </div>
            

Answer №3

If you're looking to achieve this, bootstrap 3/4 can make it simple for you.

<div class="media">
    <div class="media-body">
      <h4 class="media-heading">Title</h4>
      <p>Subtitle/ Description</p>
    </div>
    <div class="media-right">
      <img src="img_avatar1.png" class="media-object" style="width:60px">
    </div>
 </div>

I hope this information is useful for you.

Answer №4

To achieve the desired outcome, switch all elements (image, title, and subheading) to display as inline-block. If this approach proves ineffective, consider altering them to table. Either method should yield the results you seek.

Answer №5

For this situation, you can utilize the following straightforward code.

CSS

div {
  border: 3px solid #4CAF50;
  padding: 5px;
}  
.clearfix {
  overflow: auto;
}
h1{
  margin:0;
}   
.img2 {
  float: right;
}

HTML

<div class="clearfix">
  <img class="img2" src="x.jpg" width="170" height="170">
  <h1>Header</h1>
  <p>SubHeader/Description</p>
</div>

We hope you find this solution helpful and easy to implement. Enjoy!

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

What is the best way to arrange these elements and a sidebar to achieve this specific layout?

Need help aligning items in CSS to achieve this layout using flexbox. Thank you. | Text A | text B | text C | video | | text d | text E | text F | video | The video is the same in both rows and should extend along the side as a sidebar. ...

Create a circle at the point where two table cells intersect in an HTML document

How can I create a circular shape at the intersections of HTML tables? I am struggling to figure out a way to incorporate shapes into HTML tables. My goal is to achieve a design similar to the image shown below. I have attempted using text and spans, as we ...

Ensuring divs are centered when resizing the page

Is there a way to center an unordered list of tables while ensuring that it remains centered even when the window is resized? The list should move each table to a new line if they can no longer fit. Check out the code snippet below for reference: <d ...

Is there a way I can detect a browser timeout and display a custom error message?

My webpage in classic asp contains a link to a local IP address, shown below: <a href="http://192.168.1.89">Link</a> When the local IP address is not available, the web browser eventually times out and shows its own error message. I ...

I'm having trouble with my Typescript file in Vscode - every time I try to edit the css code, all the text turns red. Can someone

Check out this visual representation: [1]: https://i.stack.imgur.com/9yXUJ.png Followed by the corresponding code snippet. export const GlobalStyle = createGlobalStyle` html { height: 100%; } body { background-image: url(${BGImage}); ba ...

I am in search of a specialized 3D camera with a unique effect

I am seeking assistance with animating a camera to replicate or closely resemble the effect seen in this demo: Any help would be greatly appreciated. ...

Expanding the height of an image in a multi-select dropdown using CSS

I have an image that is included in all text fields and drop downs like so: However, when it comes to a multiple select drop down box, the image appears differently: I would like the image to display as shown above for multiple select drop downs. I atte ...

conceal parent window element upon clicking an image within an iframe

I'm facing a challenge with hiding certain buttons in a parent window when opening a modal by clicking an image inside an iframe. Below is the code snippet that I am using: In the parent window - <iframe id="gallery" src="links/gallery.html" wid ...

HTML5 Division Element Dimensions

I am currently modifying an HTML5 template. The default Bootstrap parameters were set as: col-sm-12 col-md-4 col-lg-4 col-sm-12 col-md-8 col-lg-8 This configuration allotted 20% of the screen width to text and 80% to images, but I want it reversed. Tex ...

stop the leakage of CSS and JS from the subtree to the document through the inverse shadow DOM mechanism

My page contains dynamic HTML content that I want to incorporate. The dynamic content consists of only HTML and CSS, without any JavaScript. However, I have some custom global CSS styles and JS logic that need to be implemented along with this dynamic con ...

"Encountering an Ajax error 419 when using radio buttons and a form

I'm attempting to use AJAX to send radio button values to a Laravel route when a button is clicked, but I keep receiving error code 419 even though I am sending the CSRF token. $(document).ready(function(){ $("#valor").on('click' ...

CSS: Handling Background Images Responsively

I'm experiencing an unusual issue where the image is not appearing, even though the div box shows up when I highlight elements on the page. Validation and inspection also show no issues. Here is the code: <div class="mainImage"></div> A ...

Why do my paths encounter issues when I alter the manner in which I deliver my index.html file?

I recently made a decision to change the method of serving my index.html file from app.use('/', express.static(__dirname + '/..')); to app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/../index.htm ...

Troubleshooting drag-and-drop functionality in a JavaScript HTML5 application resembling a Gmail upload interface

Here is a snapshot of my user interface: Each node in the tree structure is represented by an <li> element along with an <a> link. Furthermore, each folder serves as a dropzone for file uploads similar to the drag-and-drop feature found in Gm ...

Displaying HTML with extracted message form URL

I have a link that redirects to this page Is there a way for me to extract the message "Message Sent Successfully" from the URL and display it in the form below? <form action="send_form_email.php" name="contactForm" method="post"> //I want to d ...

Troubleshooting textarea resize events in Angular 6

In the development of my Angular 6 application, I have encountered an issue with a textarea element. When an error occurs, an asterisk should be displayed next to the textarea in the top-right corner. However, there is a gap between the textarea and the as ...

Switching from HTML to PHP redirection

I'm encountering an issue with my PHP and HTML setup. I want to create a link that redirects to a PHP page containing a login form. Here's the code snippet I've used: <p><a href="LoginPage.php">Login</a></p> However ...

HTML/CSS Selecting Tables: First, Last, and More

Is there a way to combine selector tags in order to style the first <td> inside the first <tr>? I have attempted various methods to merge these selectors, albeit unsuccessfully. I am simply seeking guidance on whether or not this is even possi ...

What is causing my anchor tags not to reflow properly?

I've been working on my new website and I'm facing a challenge that I just can't seem to solve. Here's the link: When I resize my browser to "mobile width", making it narrower than the row of oval "Tags" below my site menu, I want the ...

Issues with custom logo and menu/sidebar toggle on Wordpress

Hello everyone, I am currently facing some difficulties with my new WordPress blog that utilizes the Skilt-Theme. Specifically, I'm having trouble with the custom-logo and sidebar-toggle features. When there is no logo present, you can see the layout ...