Center an image vertically within its container

Here is an example of some HTML/CSS code:

.section {
  width: 100%;
  height: 200px;
  overflow: hidden;
}
.section img {
  max-width: 100%;
  height: auto;
}
<div class="section">
  <img src="http://www.example.com/image.jpeg" alt="image" />
</div>

JSFiddle Link

How can I center the image vertically in its container?

Answer №1

.box {
  width: 100%;
  height: 300px;
  overflow: hidden;
  object-fit: contain;
}
.box img {
  max-width: 100%;
  height: auto;
  position: relative;
  top: 50%;
  left: 0;
  transform: translate(0, -50%);
}
<div class="box">
  <img src="http://www.example.com/image.jpg" alt="image" />
</div>

Answer №2

It seems like your image is larger than the container it's placed in. Is this intentional?

To address this issue, I recommend utilizing a background image instead with specific styling such as background-position: center and background-size: cover.

Answer №3

Modify the styling with CSS

CSS

div.horizontal {
    display: flex;
    justify-content: center;
}

div.vertical {
    display: flex;
    flex-direction: column;
    justify-content: center;
}




.container {
  width: 100%;
  height: 200px;
  overflow: hidden;
  object-fit: cover;
}

.container img {
  max-width: 100%;
  height: auto;
}

Update the HTML structure

<div class="horizontal container div1">
    <div class="vertical">
        <img src="http://www.wallpapereast.com/static/images/HD-Wallpapers1_Q75eDHE.jpeg" alt="img" />
    </div>
</div>

Check out the live demo here

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

Tips for setting HTML content in a text field using SOAP API in Tuleap

We are using the Tracker SOAP API to create fresh tracker artifacts. While making changes to an artifact in Tuleap, there is an option to choose between plain text and html in a dropdown menu for a text field. Our goal is to input html-formatted text into ...

Is it possible to stack images on top of each other on a webpage?

I'm looking to create a dress-up game website where users can click on different accessories to layer them on top of each other. It's a bit tricky to explain, but I've found some examples that demonstrate what I have in mind: This site is ...

Modifying the src attribute of an object tag on click: A step-by

So I have an embedded video that I want to dynamically change when clicked on. However, my attempt at doing this using JavaScript doesn't seem to be working. <object id ="video" data="immagini/trailer.png" onclick="trailer()"></object> H ...

Achieve consistent card body heights in Bootstrap 4 with the card group feature

I am working towards achieving a specific layout using Bootstrap 4's card-group feature, as shown in the image below: The goal is to have everything at the same height within each card (including card-header, card-title, and small subtext if present) ...

Guide to designing a bar graph using HTML5 for a web app optimized for iPad

I am in the process of converting my iOS native app for iPad to a web app using HTML5. However, I am encountering an issue with creating a bar graph for the HTML5 app. Is there any documentation or built-in API available for this specific task? On iOS, we ...

Create a collapsible menu using only CSS that expands when the down arrow is clicked, specifically designed for

I am currently working on creating a mobile-friendly version of a menu. I have a list of items, and I want only the item with the class nav-current to be displayed initially. Using pseudo :after, I added an arrow after the link of that specific li element. ...

3D Animation for All Browsers

I'm currently working on creating a small browser-based game and I've been experimenting with animations to get the desired effect. So far, the animation works well in Opera and Edge, although there is a slight cropping issue in Edge. Unfortunat ...

What is the best way to align the inner content to the left and right within a border-radius shape?

.intro h1{ font-family: 'Cambria'; font-size: 16pt; font: bold; text-align: left; } .intro p{ font-family: 'Calibri'; font:italic; font-size: 12pt; padding: 0px 690px 0px 20px; text-align: left; ...

Utilize the drag-and-drop feature on an HTML5 canvas

When I drag an HTML element and drop it onto a canvas, the object is supposed to appear at the same location on the canvas where it was dropped. However, it is showing up at a different location instead. You can view my code for reference. Script functio ...

JS script for clicking various icons on a webpage that trigger the opening of new tabs

I am working on a webpage where I have 9 icons with the same class. I am trying to write JavaScript code that will automatically open all 9 icons when the webpage loads. I tried using the code below, but it only clicks on the first icon and stops. let ...

The color of the link remains blue when using Firefox

Check out my website at kenastonchiropractic.com When viewing the site in Chrome, the "Home" link remains the same color as the other links and they all turn white when hovered over. However, in Firefox, the "Home" link stays blue even after being clicked ...

Ways to create more space between text and images with Bootstrap 5

Is there a way to adjust the spacing between descriptive text and an image on a webpage? I've tried fixing it, but it didn't work. How can I ensure that my text is properly separated from the image? Thank you. <!DOCTYPE html> <html lang ...

Leveraging the ReactJS Hook useState for detecting Key press events

As I am in the process of constructing a piano, I want it to showcase certain CSS styles when the user "presses" specific keyboard buttons (via keydown event) - even enabling the simultaneous clicking of multiple different buttons. Once the user releases t ...

Smarty is failing to generate Javascript on all files

I have a traditional PHP/Smarty Website setup. It consists of an index.php home base and a templates folder containing .tpl Files. The key templates include header.tpl, footer.tpl, index.tpl, and subpage.tpl. Both index.tpl and subpage.tpl include the hea ...

Implementing alignment adjustment for search bar with bootstrap 5.0

Is there a way I can adjust the positioning of the search bar and button to align left after the navbar-brand element in a continuous manner? I have tried using the bootstrap classes justify-content-start and text-left, but they did not yield the desired r ...

Email-box appears in a seemingly "random" location

I am currently engrossed in my informatics project, which involves building a sample dating site. However, I have encountered some difficulties in getting everything to align correctly. Everything was working fine until my email box appeared in the wrong p ...

Dividing a string into an array and displaying it in a table using Laravel

Retrieving a string from the database and using the explode function to split the values. Below is the code snippet: $data = DoctorRegistration::select('products') ->where('doctorid','=',$doctorid) ->get(); ...

The controller remains unresponsive when attempting to activate the button on the webpage

I'm a beginner in frontend development and I'm currently working on a website that uses Thymeleaf. On this website, there is a div block with a button. Please take a look at the HTML code below, specifically the button at the last three rows. &l ...

establish the parent child size with CSS

In my current project, I am faced with a challenge regarding the CSS and HTML code. I am attempting to have a <div> element positioned after an <img> element inherit the width and height of the image. This <div> is meant to act as an over ...

Unable to resolve CSS positioning issue

There have been instances where I noticed that the CSS positioning property does not function as intended. Here is a snippet of my code: .position { position:absolute; left:50%; } According to the above code, the element with the specified class should b ...