Position image in the center with two lines of text that adjust gracefully based on screen size

I am struggling to position an image alongside two lines of centered text. Currently, the image appears to the left of the text in my example, but what I really want is for the image to be centered with respect to the text and have a perfectly aligned image/text layout.

CSS:

.center-class{
  text-align:center;
}
.righty img{
  max-width:100px;
  float:left;
}
.vid-open{
}

HTML:

<section class="">
  <div class="row pull-down">
    <div class="center-class">
      <div class="righty">
        <img src="http://www.psdgraphics.com/file/white-egg.jpg" >
        <h2>This is a header.</h2>
        <h5 class="vid-open">some text some text some text<span class="icon-right-left-01-011" ></span></h5>
      </div>
    </div>
  </div>
</section>

VIEW DEMO

Answer №1

To make the text appear centered and inline-block, you can enclose it in a div element and style it with CSS:

.center-class {
  text-align: center;
}

.righty > * {
  display: inline-block;
  vertical-align: middle;
}

.righty img {
  max-width: 100px;
}
<section class="power-of-egg">
  <div class="row pull-down">
    <div class="center-class">
      <div class="righty">
        <img src="http://www.psdgraphics.com/file/white-egg.jpg">
        <div class="con">
          <h2>This is an egg.</h2>
          <h5 class="vid-open">eggs are very nutritious<span class="icon-right-left-01-011" ></span></h5>
        </div>
      </div>
    </div>
  </div>
</section>

Check out the updated Codepen here

Answer №2

To align the entire block in the center, you can use the following CSS code:

.centered-block{
  text-align:center;
}
.float-right img{
  max-width: 100px;
  float:right;
}
.video-open{
}

.float-right {
  width: 300px;
  margin: 0 auto;
}

Answer №3

The issue arises from nesting your image within a block-level div element, causing it to stretch across the full width of its parent element.

To resolve this, simply remove the image from the div container and set the display property of the text-containing div to inline-block. This will constrain the div's width to match its content.

Check out the updated code snippet here: http://codepen.io/anon/pen/LNNJRQ

Answer №4

To center an element horizontally, you can utilize display: block; and margin: auto;. Although there may be alternative methods, this is the css I employed to align the image in the center with text positioned to its right:

.righty > .con {
  position: absolute;
  top:0;
  left: 55%;
}

.righty img {
  display: block;
  vertical-align: middle;
  margin: auto;
  max-width: 100px;
}

Please note: the positioning of the .con class may need adjustment based on screen size.

You can view the updated codepen for reference.

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

Conceal the ::before pseudo-element when the active item is hovered over

Here is the code snippet I am working with: <nav> <ul> <li><a href="javascript:void(0)" class="menuitem active">see all projects</a></li> <li><a href="javascript:void(0)" class="menuitem"> ...

What is the best way to eliminate the underline text decoration from a link element?

Take a look at this JS Bin. I want to change the link Project name in there. By default, it is underlined when hovered over. How can I remove that effect? I attempted using a class project-name: a:hover, a:focus .project-name { text-decoration: non ...

The embedded video on Youtube is not extending to fill the entire screen

Currently, I am in the process of creating a website for a friend's upcoming wedding using Bootstrap 5. However, I have encountered an issue where the embedded video is not expanding to fill the entire screen size. For reference, you can view the live ...

Could someone provide an example to illustrate the concept of "em is relative to the font size and % is relative to the parent element"?

Could someone provide an example of how "em is relative to the font size and % is relative to the parent element" works? What exactly do "relative to the font size" and "relative to the parent element" mean? ...

What is the most effective way to alter the text color using jQuery?

Whenever I want to create a text hover animation, jQuery is my go-to tool. Is there a snippet of code that can change the color or size of the text in this case? ...

Display a loading image during the execution of the Exec_Shell Script

Looking to add a loading image while script.sh is executing Check out the code snippet below: <?php if (isset($_POST['restartserver'])) { shell_exec("my-script.sh"); } ?> <html> <body> &l ...

What is the method for formatting within the <textarea> element?

While working on developing a comment system, I recently made the discovery that text areas cannot be formatted to retain paragraphs, line breaks, etc. This was particularly noticeable when comparing sites like StackOverflow, which uses a text editor inste ...

How to set a variable in a Python Selenium script based on webpage content

I attempted this method but it did not yield the desired results Is there a way to capture the value of an element from a web page and assign it to a variable using Selenium with Python in PyCharm? 250 a = driver.get.element(By.ID, "rate").tex ...

What could be the reason why the color of pixels X and Y is not being retrieved successfully in this

Seeking to extract the color of pixel X,Y from an image using the code example provided in this response (). $('#map').mousemove(function(e) { if(!this.canvas) { this.canvas = $('<canvas/>') ...

Determining if a Website is Responsive with PHP

One way to determine if a website is responsive or not is by analyzing its HTML code. A previous inquiry on this topic can be found here: . This method only focuses on checking for the <meta name="viewport" content="width=device-width"> tag. It&apos ...

The bootstrap table did not meet my expectations as I had hoped

I am currently using Bootstrap to create a basic two-column table similar to the one on the Bootstrap website here: http://getbootstrap.com/css/#tables. To achieve this, I have implemented a javascript function to display the table as shown below: $(&bso ...

Improving the efficiency of rendering multiple objects on a canvas

I'm currently working on developing a simulation for ants using the basic Javascript canvas renderer. Here is a snippet of the rendering code: render(simulation) { let ctx = this.ctx; // Clearing previous frame ctx.clearRect(0, ...

The box on my form is leaking my background color outside the 1px border, but only in Internet Explorer

While experimenting with background gradients and borders one day just for the fun of it, I made an interesting observation in Internet Explorer. The issue I encountered was that the background color was overflowing outside the radius border, but this onl ...

Why isn't a password appearing in the "generated-password" div from the password generator?

The function toIncludeCharacters() appears to be functioning correctly as it returns lowercase letters upon upload, but generatePassword() is currently producing a blank output. After clicking the "Generate Password" button, three blank outputs are return ...

Can you explain the contrast between onsubmit="submitForm();" and onsubmit="return submitForm();"?

Is it possible that the form below is causing double submissions? <form name="myForm" action="demo_form.asp" onsubmit="submitForm();" method="post"> function submitForm(){ document.myForm.submit(); } I've noticed a bug where sometimes two ...

utilize javascript to style iframe from an external domain without access to its DOM structure

I have two websites, example.com and example1.com. I'm trying to display the content of example1.com within an iframe on example.com. For instance: Add this code to example.com: <iframe src='http://example1.com'> Then add the follow ...

What are the most effective strategies for creating cross-platform components using Vue.js 2.0?

In my current project, I am looking to create a universal component library using Vue.js 2.0 that can be seamlessly integrated across various platforms. The focus of this query lies in establishing the most effective styling practices for components. Typi ...

"What is the best way to transform tab navigation into a dropdown menu with the help

I have a collection of tabs currently on display. I am looking to transform them into a dropdown menu when the screen size changes, making them more responsive. Each set of tabs corresponds to different content. Despite trying various solutions found on s ...

How to Style Your ASP.NET Website: Utilizing a Stylesheet with Visual Studio 2010

I am having trouble adding a stylesheet to the master page of my asp.net web form. I want to create an inline navigation menu at the top of the page, but I can't seem to get it working. I have created the stylesheet in the same way I would for an HTML ...

Grid numbering with CSS in jQuery mobile design

Looking to create a numbered grid where the user inputs a number and the grid is generated. I am considering using CSS, HTML, or Jquery Mobile for an iPad project. I'm unsure of the best approach at the moment. I would like the grid and numbers to aut ...