What is the best way to position an image at the top of a responsive container?

Seeking guidance on aligning two responsive containers in a row. One container has an image set to width: 100% and height: auto. How can I vertically align the image at the top of its parent container?

<div class="top-row">
  <figure class="product-image col-sm-6">
    <img src="http://rastenis.lt/coocoo/image/cache/catalog/Demo/Untitled.Oil%20on%20canvas.45x55%20cm.2011-570x600.JPG" class="img-r" alt="img">
  </figure>
  <article class="description col-sm-6">
    <h1 class="p-heading">ELEPHANTS ARE COMING BACK</h1>
    <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes </p>
  </article>
</div>

CSS

.top-row {
  width: 100%;
}
figure img {
  width: 100%;
  height: auto;
}

Check out my example here: https://jsfiddle.net/2sg209o0/

Answer №1

The example code provided here: https://jsfiddle.net/2sg209o0/ includes most of the necessary CSS for your needs. To confirm, you can add the following CSS to the appropriate selectors in order to achieve the desired look:

.top-row {
    width: 100%;
    border:1px dotted blue;
}
figure img {
    width: 100%;
    height: auto;
    border:1px dashed red;
}

If you inspect the image element by right-clicking, you'll notice white pixels at the top and bottom of the image. The image aligns with the top edge of its immediate container as requested:

How to vertically align image at the top of its parent responsive container ?

However, it seems that you want the image to align at the top of the containing div, "top-row", along with other content. For this alignment, additional styling is needed, mainly related to floats and the use of the "Figure" element.

The browser assigns pre-existing styles to tags like the figure, including a margin. Once you review the code below, you can visit the JSFiddle link provided for the final live example. I have adjusted styles and cropped the image (hosted on imgur) for your convenience. Feel free to customize further based on your requirements, particularly if it involves showcasing products or similar purposes.

.top-row {
  width: 100%;
  border:1px dotted red; /* visualizing top-row dimensions */
}

figure{
  margin:0 1em 1em 0; /* overriding default browser styles */
  width:50%;   /* allocating space within top-row */
  float:left;  /* useful for layout design */
}

figure img {
  width: 100%;
  height: auto;
}

https://jsfiddle.net/pa04oxmt/

Note: The image may exceed its container's boundaries, but adjustments can be made accordingly :) edit: corrected errors and typos

Answer №2

I'm a bit unsure on the specifics of your request. Are you seeking assistance with aligning divs in a horizontal row or vertically positioning an image within its own row? If it's the latter, utilizing the vertical-align property may be beneficial (http://www.example.com/cssref/pr_pos_vertical-align.html).

Answer №3

The image seems to have an issue. Try updating the image link and reviewing it once more. It appears that the top white area of your image is causing some display problems, as it should ideally be situated at the top of its parent container. Consider cropping the image for better alignment.

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

How come the text input on my website automatically displays a "#" in the URL?

Here is the code snippet I am working with: <div class="container text-center"> <h1 class="display-3">What's the weather</h1> <p class="lead"><strong>Please enter a city name:</strong></p> ...

What's the best way to display a component once a function has been executed?

My service controls the visibility of components using *ngIf! Currently, when I click a button, the service sets to true and the components appear instantly! I want the components to only show after a specific function has finished executing. This means I ...

Implementing a hamburger menu and social media sharing buttons on websites

Currently working on my personal website and delving into the world of Web Development, I have some inquiries for seasoned developers. My first query revolves around incorporating a hamburger menu onto my site for easy navigation to other pages. After att ...

Menu that collapses with Ionic's flexbox styling

Recently delving into the world of Ionic and flexbox, I'm faced with a challenge in organizing my layout effectively. I aim to create a vertical menu where users can choose from various topics, each with its own set of subtopics. Upon selecting a top ...

When clicking, expand the li element to 100% width and move the other siblings aside

I'm looking to arrange li elements in multiple columns with flexibility. https://i.sstatic.net/HJCZu.png <ul> <li class="yellow"> Yellow Block </li> <li class="red"> Red Block ...

How to toggle hidden links with AngularJS or vanilla JavaScript with a click事件

When the "Show all" button is clicked, I would like to display all elements. If the "1st half" link is clicked, I want to show "abc", "def", and "ghi". When the 2nd link "2nd half" is clicked, I want to display "jkl", "mno", and "pqr". Then, when the "show ...

An AngularJS directive designed to execute after a text input has been modified

I have integrated a bootstrap calendar datepicker that interacts with and updates an <input type="text"> field. As of now, when I use ng-change="myAngularExpression()", the function gets executed the moment the text box is selected. Is there a way t ...

Is storing text in data-content and utilizing bootstrap-popover.js compatible?

Is it possible to display HTML content in a popover using ? How reliable is it to store text in data-content? Can we expect it to function properly across all browsers? ...

Guide on Creating a Smooth jQuery Scroll Animation from Top to Bottom and Bottom to Top for a Landing Page Navigation Menu

I'm in the process of developing a landing page website where my navigation links smoothly scroll to different sections on the page. Currently, when I click on a nav link, it instantly takes me to the bottom and then back to the top. However, I want t ...

What is the best way to retrieve a particular value from a database using PHP?

$sql="Select chanceNo From gm_gacha_token1 where token_code='$mytoken'"; $result=mysql_query($sql); $count=mysql_num_rows($result); Language: PHP I am attempting to retrieve a single value of `chanceNo. Is there a way to get this value and stor ...

Serve an HTML file with CSS/JS directly from the server disk to the client's browser

I'm currently facing a challenge that involves securely rendering and serving HTML files (along with CSS, JS) from the local disk on the server to a web application running in the client's browser. APPLICATIONS A front-end web app is built usi ...

Firefoxy can't seem to get through the navigation keys within an element that has a hidden

Check out this fiddle I created: http://jsfiddle.net/c11khcak/13/. My goal is to hide the scrollbar of an element while still allowing it to be scrollable using the mouse wheel and up/down navigation keys. I've been able to achieve this using some bas ...

Ensure that the header logo is in perfect alignment with the body of the

I am facing an issue with the alignment of my header logo on a webpage. The body of the page has white space on each side, and I want the header logo to be aligned on the left so that it starts vertically at the same point as the body regardless of the win ...

Tips for preventing labels from overlapping in JavaScript

After texturing an image to a sphere in 3D (as shown below), I encountered an issue where the planegeometry labels were overlapping. I am looking for a way to separate these labels using the three.js library. 2 labelBox.prototype.update = function() { ca ...

Is it possible to create a query selector that is able to find an element based on its attributes while also excluding elements with a specific class name?

Imagine you have elements that look like this: <div class="a b" data-one="1" data-two="2"></div> <div class="c" data-one="1" data-two="2"></div> <div class="b" data-one="1" data-two="2"></div> Is there a way to selec ...

Adjust the line selection color in datatables with the power of jQuery

When I click on a row in my datatable, I want the selected row to have a background color of red and all other rows to have a background color of white. I attempted to achieve this with the following code: $(document).on('click', "#table_user t ...

Troubleshooting Spacing Issues in jQuery Dropdowns

I'm experiencing difficulties with aligning text in a dropdown menu vertically. Any suggestions on how to resolve this issue? Below is the HTML and C# code: <ul class="dropdown"> <li> <p class="MenuHeading"> <a hr ...

The concept of CSS div wrapping and managing vertical whitespace in web design

My goal is to make multiple div elements wrap to the next line when the width of their container is changed. The wrapping part is working fine, as the boxes drop to the next line when there isn't enough width for them to fit. However, I am facing an i ...

Identify Horizontal Swipe Gestures on Page-level

I am currently focused on ensuring accessibility for users utilizing voiceover technology. While navigating their phone, these individuals rely on right and left swipes to interact with elements on a page. I am seeking to implement swipe detection at the ...

The chosen date is not preserved within the select box

I have these specific items: $scope.items = [{"date":"2014-12-26T05:00:00.000Z","display":"6:00"}, {"date":"2014-12-26T05:15:00.000Z","display":"6:15"}, {"date":"2014-12-26T05:30:00.000Z","display":"6:30"}] When I use the following code: <select ...