Tips for aligning the image and text in the center of the page

Struggling to center this image on my website. I've attempted various methods like margin: 0 and absolute positions, but nothing seems to work. Being a beginner in html and css doesn't help either. My attempts to center it have been futile.

The challenge is to keep the image centered even when the site window width changes, rendering padding ineffective.

My CSS code:

/* Image and text setup container */
.container {
float: left;
position: relative;
width: 100%;
left: 0%;
right: 0%;
}

.imagetext {
text-align: left;
width: 5%;
position: absolute;
top: 8px;
right: 60px;
font-size: 18px;
}

img { 
padding-right: 5px;
padding-right: 5px;
padding-bottom: 15px;
}

HTML Code:

<!--Front page image and text-->
    <div class="container">
        <img src="Aberlady_Church.png" alt="Church" width="400" height="200">
        <div class="imagetext">Hasellus tempus pretium efficitur mauris non magna volutpat</div>
    </div>

Currently, the image appears like this:

Answer №1

If you're looking to create a layout similar to this example

<!--Code for front page image and text-->
    <div class="container">
      <figure>
        <img src="http://lorempicsum.com/futurama/350/200/1" alt="Church" width="400" height="200">
        <div class="imagetext">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </div>
      </figure>
    </div>

CSS Styles

/* Styles for image and text container */
.container {
position: relative;
width: 100%;
}

.imagetext {
text-align: left;
width: 5%;
position: absolute;
top: 8px;
color: #fff;
right: 80px;
font-size: 18px;
}
figure { 
position: relative; 
width: 400px; 
margin: auto; /* adjust according to your image size */}
img { 
padding-right: 5px;
padding-left: 5px;
padding-bottom: 15px;
}

The text is positioned within a figure element using absolute positioning relative to the tag.

Answer №2

When aiming to horizontally center a block level element within its container, the most effective method is often

margin-left: auto;
margin-right: auto;

It's important to note, however, that introducing float and position properties to the containing block can complicate the layout unnecessarily, so it's best to avoid them unless necessary.

Answer №3

When it comes to styling that specific markup, I recommend experimenting with the following CSS styles.

.wrapper {
    width: 80%;
}

.textimage {
    width: 7%;
    font-size: 20px;
    margin-left: 10px;
}

img { 
    margin: 0;
    padding-right: 5px;
    padding-bottom: 15px;
}

Answer №4

Your code contains numerous errors and unnecessary lines. I suggest delving into some HTML and CSS resources to enhance your skills.

One way to simplify your issue is by using an image wrapper .image_wrapper to center images and text within it.

CSS

.image_wrapper {
    text-align: center;
}

.imagetext {
    font-size: 18px;
}

img { 
    padding: 5px;
}

HTML

<div class="container">
  <div class="image_wrapper">
    <img src="http://www.theimaginativeconservative.org/wp-content/uploads/2016/02/Pretty-Church.jpg" alt="Church" width="400" height="200" />
    <div class="imagetext">Hasellus tempus pretium efficitur mauris non magna volutpat</div>
  </div>
</div>

Give it a try!

https://jsfiddle.net/r1rh7wn4/

Answer №5

Consider enclosing your image and its accompanying text within a <div> tag to isolate it from the surrounding page content. This will also enable you to center both elements, regardless of the page's width. Here's a demonstration:

Example Link

.container {
  float: left;
  position: relative;
  width: 100%;
  left: 0%;
  right: 0%;
}
.imagecontainer {
  width: 75%;
  height: auto;
  margin: 0 auto;
  position: relative;
}
.imagecontainer img {
  width: 100%;
  height: auto;
  padding-right: 5px;
  padding-bottom: 15px;
}
.imagecontainer .imagetext {
  width: 20%;
  height: 100%;
  position: absolute;
  top: 0;
  right: 0;
}
<div class="container">
  <p>Sample text here.</p>
  <div class="imagecontainer">
    <img src="sample-image.jpg" alt="Sample Image" />
    <div class="imagetext">Image text goes here</div>
  </div>
  <p>Additional text content.</p>
</div>

If you prefer the image text to be centered below the image:

Centered Text Example Link

.container {
  float: left;
  position: relative;
  width: 100%;
  left: 0%;
  right: 0%;
}
.imagecontainer {
  width: 75%;
  height: auto;
  margin: 0 auto;
  position: relative;
}
.imagecontainer img {
  width: 100%;
  height: auto;
  padding-right: 5px;
  padding-bottom: 15px;
}
.imagecontainer .imagetext {
  width: 100%;
  height: 100%;
  position: relative;
  text-align: center;
}
<div class="container">
  <p>Sample text here.</p>
  <div class="imagecontainer">
    <img src="sample-image.jpg" alt="Sample Image" />
    <div class="imagetext">Centered image text</div>
  </div>
  <p>Additional text content.</p>
</div>

Answer №6

To center your image, simply apply the CSS properties "display:block;" and "margin:0 auto;".

/* styling for image and text container */
.container {
  float: left;
  position: relative;
  width: 100%;
  left: 0%;
  right: 0%;
}

.imagetext {
  text-align: left;
  width: 5%;
  position: absolute;
  top: 8px;
  right: 60px;
  font-size: 18px;
}

img { 
  /* add these properties to center the image */
     display:block;
     margin:0 auto;
  /* add these properties to center the image */
  padding-right: 5px;
  padding-right: 5px;
  padding-bottom: 15px;
  
}
<div class="container">
  <img src="Aberlady_Church.png" alt="Church" width="400" height="200">
  <div class="imagetext">Hasellus tempus pretium efficitur mauris non magna volutpat
</div>

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

Leaflet OSM: Adjust map focus to the polygon's center

Looking to create an HTML file that integrates the Leaflet library to display an OpenStreetMap view with a centered polygon? I've been following a helpful discussion, but still unsure about how to center and autozoom an arbitrary polygon on the map. A ...

Scaling Vuetify icons dimensions

During the development of an app using Vuetify, I encountered challenges in changing the default colors set by Vuetify. After some trial and error, I came across a solution on: https://github.com/vuetifyjs/vuetify/issues/299 The solution involved adding ...

Navigation problems resolved: The fixed-top class causes issues with navbar-right and obscures the logo

I have implemented a bootstrap Nav on this page To achieve a sticky top navigation, I used the fixed-top class. However, this caused the navbar to align left instead of right as intended. It also ended up covering my logo, which was originally aligned to ...

Creating a design utilizing HTML columns with a set height and the ability to scroll horizontally

For my android project, I have a requirement to display a view (WebView) that dynamically loads content. The content will consist of <div class="content-item"> tags that are added by JavaScript to <div class="content-holder"> after the page has ...

Tips for eliminating the flash of color upon website loading

Every time I visit my site at , there's a brief moment of grey before the site fully loads. Despite being a simple landing page, it seems to be taking longer than expected to load. I suspect this issue is caused by the combination of linear gradients ...

Why does tagging P trigger popups in the DIV when the DIV contains another DIV?

JSBIN HTML <p> <div>123</div> </p> CSS p div{ background:#f00; } Encountering an odd issue here. When I input the HTML structure as shown below: <p></p><div>123</div> The CSS code fails to produce ...

How can I use jQuery to vertically align an element?

Here's my website: Take a look here! I have a menu on the left column that I want to center. Take a look at the image below for a better understanding of what I'm trying to achieve. https://i.stack.imgur.com/ZzprT.png Here is the JavaScript c ...

What is the best way to dynamically change the main content based on the sidebar option chosen in a React application?

https://i.sstatic.net/fkIyh.png Currently, I am in the process of creating the layout for the page similar to the image provided. When a user selects option A from the sidebar, my goal is to display the corresponding content on the same page without navig ...

The HTML element <p> is not spilling over onto a new line

Having trouble with text overflow in a kanban board? I've tried various CSS styling options like overflow-wrap: break-word;, word-wrap: break-word;, and hyphens: auto;, but nothing seems to work. Here's a preview. I'm using Vue.js, but I do ...

CSS styling for the dropdown list in the content/option section

Can someone help me with a simple question I've been researching for hours without success? I'm trying to figure out how to style the content/option/popup of a dropdownlist on the right side of the 'open button' like in this screenshot ...

Retrieve a div element using two specific data attributes, while excluding certain other data attributes

Here are some examples of divs: <div id="1" data-effect-in="swing" data-effect-out="bounce"></div> <div id="2" data-effect-in="swing"></div> <div id="3" data-effect-out="swing"></div> <div id="4" data-effect-out data ...

Modify request parameters in real-time using JavaScript

i am seeking assistance with a link request: <a href=index.jsp></> also, i have various divs located elsewhere on the page that contain changing values based on user input or specific conditions: <input id="var1" /> <input id="var2" ...

Tips for changing the style ID depending on the variable value

Currently, I am exploring the possibility of switching CSS styles using jQuery through a simple if condition. Is there an easy method to switch styles based on IDs? In my case, I have two CSS blocks each with different IDs. Here is an example: <style ...

Adjust the ZIndex on a div through interactive clicking

For my new project, I'm exploring a concept inspired by Windows 7. The idea is that when you double click on an icon, a window will open. If you keep double clicking on the same icon, multiple windows should appear. The challenge I'm facing is im ...

Switching up the default font style within TinyMCE

After successfully changing the default font within the editor using the guidelines provided here, I have encountered a new issue. The original default font no longer appears in the font drop-down list. The previous default font was Verdana, and the new d ...

Guidance on creating a custom color selection tool for a specific task

Looking for assistance in converting a code snippet that uses <button> elements to select colors into a color picker. I am unsure how to retrieve the selected color and use it within a JavaScript function. Can someone provide guidance on this? Here i ...

creating a table displaying data in a horizontal format sourced from ajax/json transformation

I am currently working on a piece of code that retrieves a list of IDs from local storage and compares them to IDs in a JSON file. If there is a match, the corresponding data is displayed in a table. However, I am facing an issue with my table layout as i ...

Reveal Content once you give us a thumbs up on Facebook

Is there HTML/PHP code that can display specific content only after a user clicks the "Like" button on an external website, not an app? ...

An HTML table featuring rows and columns that can be adjusted in size separately from the content within each cell

Looking to create an HTML table where the columns and rows can be sized independently of the cell content? If a row or column isn't large enough to display all the content, it should simply overflow behind the cell. A solution was found that worked in ...

Issue with MaterializeCSS: Unable to add new options to select menu

I have a function that appends options to a dropdown based on the onchange event. The options are being appended successfully, but they are not visible when clicking the dropdown. $(document).ready(function(){ $('select').formSelect(); }) ...