Tips for aligning icons and text correctly within a table

I can't seem to align the icon vertically with the text. Currently, the image icon is positioned in the middle of the <td> tag, but I want it at the top, as shown in the jsfiddle: http://jsfiddle.net/TheAmazingKnight/N6fLp/

To help visualize the spacing of each element, I added border:1px dotted blue;. By default, the td tag that encloses the img tag is centered. How can I align the img tag and title header to the very top of the table's td tag and have them aligned horizontally with the text?

I tried using vertical-align:text-top to mimic the desired result, but it didn't work. I also attempted to use position:relative on the table and set the image to top:0;, but that didn't work either. What am I missing?

HTML:

<table>
    <tr>
        <td><img src="http://www.newtekinsurance.com/wp-content/uploads/2012/08/icon-audience.png" alt="img.png"/></td>
        <td>
            <strong>Title header</strong>
            <br>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
            <ul class="arrowLink">
                <li><a href="javascript:showNotice('#')">View Lorem ipsum</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td><img src="http://www.newtekinsurance.com/wp-content/uploads/2012/08/icon-audience.png" alt="img.png"/></td>
        <td>
            <strong>Title Header</strong><br>
            Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
            <ul class="arrowlink">
                <li><a href="javascript:showNotice('#')">View Lorem ipsum</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td><img src="http://www.newtekinsurance.com/wp-content/uploads/2012/08/icon-audience.png" alt="img.png"/></td>
        <td>
            <strong>Title Header</strong><br>
            Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
            <ul class="arrowlink">
                <li><a href="javascript:showNotice('#')">View Lorem ipsum</a></li>
            </ul>
        </td>
    </tr>
    <tr>
        <td><img src="http://www.newtekinsurance.com/wp-content/uploads/2012/08/icon-audience.png" alt="img.png"/></td>
        <td>
            <strong>Title Header</strong><br>
            Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

            <p>Lorem ipsum</p>

            <ul>
                <li>Hola <a href="javascript:showNotice('#')">Google</a><br>
                    <br>
                </li>

                <li>Include completed <a href="#" target="_blank">Lorem ipsum</a></li>
            </ul>

            <p>Mail to:<br>
            Lorem Ipsum<br>
            Attn: Lorem Ipsum<br>
            1234 Main Street<br>
            NY NY 12345</p>
        </td>
    </tr>
    <tr>
        <td><img src="http://www.newtekinsurance.com/wp-content/uploads/2012/08/icon-audience.png" alt="img.png"/></td>
        <td>
            <strong>Title Header</strong><br>
            Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
        </td>
    </tr>
    <tr>
        <td><img src="http://www.newtekinsurance.com/wp-content/uploads/2012/08/icon-audience.png" alt="img.png"/></td>
        <td>
            <strong>Title Header</strong><br>
            Lorem <a href="javascript:showNotice('#')">Lorem</a> website Lorem ipsum

            <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>

            <p class="small">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
        </td>
    </tr>
</table>

CSS:

*{border:1px dotted blue;}
ul,li{padding-left:15px !important;padding:0;margin:0;}
/*table, tr, td, p{margin:0;padding:0;}*/
img{margin-right:10px;}
img{width:50px;height:50px;}

Answer №1

To align an element to the top of a table cell, you can achieve this by using the CSS property vertical-align:top.

td {
    vertical-align:top;
}

Official Documentation

Live Example

Answer №2

After experimenting with your jsfiddle, I made a simple addition:

td {
    padding: 6px;
    text-align: left;
    vertical-align: top;
    border-bottom:1px solid #ddd;
}

That was the only change I made to the CSS.

Answer №3

Employing tables for layout purposes is considered outdated and restricts your options. In the event that you opt for utilizing a table, you can insert the necessary html code as follows:

<td valign="top"><img ... /></td>

Alternatively, you can utilize CSS (which will impact all td elements):

td {
    vertical-align:top;
}

If you decide against using tables, I suggest checking out this informative article:

http://css-tricks.com/centering-in-the-unknown/

Answer №4

<div class="container">
   <td><img /></td>
   <td>Content</p>
</div>
.container {
   display: flex;
   align-items: center;
   justify-content: center;
}

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

Is it possible to refresh HTML content using jQuery?

Currently, I am attempting to iterate through response data and integrate the values into the HTML of my webpage. It's crucial for me to clear any existing values in the HTML so that only the new ones are displayed, keeping it up-to-date. The structu ...

expanding the div based on the filtered content inside

My current setup involves using jQuery filters to refine the selection of products. All the products are contained within their own individual divs, which are then placed inside a single "large div". When a filter is applied, the divs containing products ...

Is there a way to prevent my Header links from wrapping during window size testing?

https://i.stack.imgur.com/YTc3F.png The image above showcases my standard header layout, while the one below illustrates what occurs as I resize the window. https://i.stack.imgur.com/re44T.png Is there a specific CSS solution to prevent the Text navLink ...

Performing condition checks within the foreach loop and displaying numerous results in a single row

I have a list of results from searching certain parameters and I need to print them out. When using a foreach loop to print the results, I want to ensure that if the purchase dates are the same, they should be printed in the same row. How can I achieve thi ...

How can I eliminate the error message "WARNING: no homepage content found in homepage.md"?

Starting my journey with KSS utilizing Keith Grant's CSS in Depth as a guide. In section 10.1.4, the author suggests: To begin, create a new markdown file named homepage.md inside the css directory. This file will serve as an introduction to the patt ...

What is the process for setting the opacity of the background in ::selection to 1?

Is it possible to have selected text with a solid background rather than a transparent one? Using opacity: 1 does not achieve the desired effect due to some standard override by the browser or other factors. ::-moz-selection { background: #fff; colo ...

Display full lines of nested tree view HTML lists upon hovering using HTML, CSS, Angular, and Bootstrap

I currently have an Angular 4 application where a left-side div displays a tree of items as recursive HTML lists. The issue I am facing is that long texts within this div get cut off by the right border, with a scrollbar appearing instead. What I would lik ...

Smooth scrolling in JavaScript can lead to unexpected jumps when using scroll-margin-top

I am currently working on implementing smooth scrolling into my website using JavaScript. However, I have run into a problem with CSS property scroll-margin-top causing a sudden jump at the end. The CSS I am using for scroll margins looks like this: class ...

Attempting to create a JavaScript calculator from scratch

After spending hours typing and debugging my code for a school assignment, I have created a calculator that still isn't working. I'm hoping someone can help me find the errors in my code. function myStory() { window.alert ("Very doge"); } // ...

Using v-model with an input file is not supported

Is there a solution for not being able to use v-model in an input tag with type="file"? Here is an example of the HTML code causing this issue: <input v-model="imageReference" type="file" name="file"/> ...

What is the best way to trigger the selection options in a dropdown menu with several buttons?

Is it possible to display the options from a select-option tag using a different button? I have either a div or another button. I want to be able to show the list of options from my select tag by clicking this button. Here is my select tag: <select&g ...

I need help getting rid of the unwanted text and objects that are appearing on my website

I recently designed a basic website and everything was running smoothly until I decided to insert a new ul division in the HTML page as a spacer between two elements: <div> <ul> <li><a></a></li> </u ...

I am looking for JavaScript or jQuery code that allows me to incorporate a "Save This Page As" button

Is there a way to enhance the saving process for users visiting an HTML page, rather than requiring them to go through File > Save As? I am interested in implementing a "Save Page As" button on the page that would trigger the save as dialog when clicke ...

HTML is unable to recognize the CSS file. Maven and Spring are the solutions

I'm having some trouble connecting my CSS to my HTML. This is the structure of my folders: https://i.sstatic.net/B9bHx.png Here is my HTML code: https://i.sstatic.net/XyE4E.png I've tried several methods to make it work. Interestingly, every ...

Is there a way to dynamically apply the "active" class to a Vue component when it is clicked?

Here is the structure of my Vue component: Vue.component('list-category', { template: "#lc", props: ['data', 'category', 'search'], data() { return { open: false, categoryId: this.category ...

Slider UI handle in jQuery sliding out of the div container

Could you assist me with a technical issue I am facing? I need to know how and where to prevent the .ui-slider-handle from exceeding the div when it reaches 100%. Feel free to check out the live preview at https://codepen.io/Melwee/pen/Exjwoyo I have inc ...

Using relative URLs in HTML to navigate a specific group of requests via nodejs middleware

Alright, so let me dive into the details of my current dilemma. Essentially, I have a project implemented in node that serves a specific purpose. It's a website utilizing express, jade, and stylus. I have configured the routing for static content in ...

Adding an iframe with inline script to my Next.js website: A step-by-step guide

For my Next.js project, I have this iframe that needs to be integrated: <iframe class="plot" aria-label="Map" id="datawrapper-chart-${id}" src="https://datawrapper.dwcdn.net/${id}/1/" style="width: ...

Execute PHP and HTML code stored in a string

I have a string stored in a variable within my PHP code that looks like this: <?php $string = " <?php $var = 'John'; ?> <p>My name is <?php echo $var; ?></p> "; ?> When using the $string variable elsewhere, I ...

Aligning buttons in an inline form with Bootstrap

Issue with Button Alignment Post Validation. <form class="form-inline form-padding"> <div class="form-group"> <div class="input-group"> <input class="form-control" placeholder="First Name" name="fir ...