Insert an image beneath a DIV element and adjust its horizontal alignment to match the width of the DIV

Can someone help me place the picture under the green container and align it horizontally based on the width of the green container? Any suggestions on how to achieve this would be greatly appreciated. Thank you!

Check out the demo at http://jsfiddle.net/29cz81du/

HTML:

<div id="carte-des-soins">
  <ul>
    <li><span class="menu-items"><a href="#" data-region="soins-1">Visage</a></span>

    </li>
    <li><span class="menu-items"><a href="#" data-region="soins-2">Epilation</a></span>

    </li>
    <li><span class="menu-items"><a href="#" data-region="soins-3">Mains</a></span>

    </li>
    <li><span class="menu-items"><a href="#" data-region="soins-4">Pieds</a></span>

    </li>
    <li><span class="menu-items"><a href="#" data-region="soins-5">Corps</a></span>

    </li>
  </ul>
</div>
<div id="container-des-soins">
  <div id="soins-1" class="textzone">
    <div class="desc-box clearfix">
      <p class="desc-box-text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <img class="type-de-soins" src="http://lorempixel.com/output/city-q-g-267-204-7.jpg"> 
    </div>
    <div class="carte clearfix">
      <div class="desc-soins">
        <p><strong>PHYT'S : soin visage Equilibre&nbsp;</strong>
          <br>60 min.&nbsp;</p>
        <p>Type de peaux : toute peau à équilibrer (grasse, mixte ou sèche). Soin du visage traditionnel, personnalisé selon le type de peau, qui vous fera retrouver une peau saine et un teint éclatant.&nbsp;</p>
        <p><strong>PHYT'S : soin visage Pureté active</strong>

          <br>60 min.&nbsp;</p>
        <p>Type de peaux : peaux grasses ou mixtes comédogènes. Soin spécifique visant à assainir le teint, éliminer les comédons et aider à réguler les excès de sébum. Ce soin s’effectue, de préférence par cure de 4 soins avec l’application au préalable
          d’une crème spécifique pendant quelques jours.&nbsp;</p>
        <p><strong>PHYT'S : soin visage Aqua Phyt's
</strong>
          <br>75 min.&nbsp;</p>
        <p>Soin très hydratant à l'Acide Hyaluronique d'origine végétale&nbsp;</p>
        <p><strong>PHYT'S : soin visage Capyl
</strong>
          <br>60 min.&nbsp;</p>
        <p>Type de peaux : peaux sèches à rougeurs diffuses, sensibles, fines. Soin calmant et décongestionnant, spécialement conçu pour les peaux sensibles ou à rougeurs diffuses.&nbsp;</p>
      </div>
    </div>
  </div>

CSS:

/* CARTE DES SOINS */
#carte-des-soins {

  text-align: center;
  margin-top: 40px;
}
#carte-des-soins ul {
  list-style: none;
        padding:0;

}
#carte-des-soins li {
display: inline-block;
    margin: 0 10px;
    background: red;

}

#carte-des-soins li a {
    color: #fff;
    text-decoration:none;
    text-align:center;
    padding: 20px;
    background: #474032;
    transition: background-color 0.5s ease;
}



#container-des-soins {
  margin-right: auto;
  margin-left: auto;
  width: 90%;
}
#soins-1,
#soins-2,
#soins-3,
#soins-4,
#soins-5 {
  position: relative;
  float: left;
      margin-top: 30px;

  margin-bottom: 30px;
  padding: 5px 20px 20px 20px;
  color: #333;
  font-weight: 400;
  line-height: 1px;
}

#soins-2,
#soins-3,
#soins-4,
#soins-5 {
  display: none;
}
#soins-2 p {
  margin-bottom: 5px;
}
#soins-1 p {
  margin-bottom: 5px;
  padding-right: 10px;
}
#soins-1 a {
  color: #3A7CDB;
  text-decoration: none;
}
#soins-1 a:hover {
  text-decoration: underline;
}
.carte p {
  margin-bottom: 1em;
}
.carte {
  float: right;
  width: 55%;
  background-color: red;
  padding-top: 25px;
}
.desc-box {
  position: relative;
  z-index: 1;
  width: 40%;
  float: left;
  background-color: green;
}
.desc-box-text {
  float: left;
  padding-top: 25px;
  padding-right: 25px;
  padding-bottom: 25px;
  padding-left: 25px;
  font-weight: 400;
  line-height: 1.38;
  color: rgb(0, 0, 0);
}
.desc-soins {
  float: left;
  font-weight: 400;
  line-height: 1.38;
  color: rgb(0, 0, 0);
}
.type-de-soins {
  display: block;
  position: relative;
margin: 0 auto;  
    z-index: 4;
  width: 179px;
  height: auto;
  overflow: hidden;
}
.desc-soins strong {
  font-style: normal;
  font-weight: 700;
}


/* END CARTE DES SOINS */

Answer №1

When making changes to the HTML of the green box, you should go from:

<div class="desc-box clearfix">
  <p class="desc-box-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <img class="type-de-soins" src="http://lorempixel.com/output/city-q-g-267-204-7.jpg"> 
</div>

To this:

<div class="desc-box clearfix">
  <div class="green">
  <p class="desc-box-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <img class="type-de-soins" src="http://lorempixel.com/output/city-q-g-267-204-7.jpg">
  </div>
  <img class="type-de-soins" src="http://lorempixel.com/output/city-q-g-267-204-7.jpg" style="width:204px;display:block;margin:0 auto;"> 
</div>

To achieve this, encapsulate the content in your column to highlight the desired area with green. I have included inline styling for the image to ensure centering using width, block display, and margin as auto.

Next, update the CSS for .desc-box as follows for the intended outcome:

.desc-box {
  position: relative;
  z-index: 1;
  width: 40%;
  float: left;
}
.desc-box>.green {
  background-color: green;
}

You can view the result here

Answer №2

If you're struggling to center your image, consider placing it in a separate DIV container and then organizing all of the divs, including the image div, within a table. You can then add align-content='center' to your CSS for the table rows. Hopefully, this suggestion will help with your layout.

Alternatively, a more effective solution could be to group both the red div and the image div within another div. This way, you can easily apply the necessary CSS styling to achieve the desired centering effect.

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

Determining the exact location of elements within a static div box

I'm facing an issue with navigating between pages on my website. I have a list of pages and links to each page, and I am using the jQuery animate() function along with offset().top value to move to the desired page. However, after the first click, the ...

Converting HTML into an object using $compile

I am currently working on calling an HTML template and want to bind the data with Angular. I successfully retrieve the data to bind and the HTML, but when I try to compile it, it returns all the HTML binded as an object. How can I convert it back to plain ...

Div not displaying images when clip-path CSS is used

I am facing an issue with the z-index of an image when using clip-path on a div. Can anyone help me solve this problem and easily adjust the z-index of the image? Thank you in advance. Below is my code snippet: .mymap { background-image: url("https:/ ...

Django auto-fill using the primary key on the page

Is there a way to automatically select the id or (pk) of an employee's page when adding documents to their profile through a form? Any suggestions for a solution would be greatly appreciated! view.py def createDocument(request): forms = documentF ...

How to Resize Images in Internet Explorer and Firefox Using CSS

Having an issue with CSS formatting on Internet Explorer and Firefox. I am trying to create a navigation bar with several images displayed in a column, using a background image. I positioned the background image on the page and then created a div inside it ...

Utilizing Skeleton to create a cropped text button on an iPhone

I am currently using Skeleton for my simple CSS needs. While it works fine on desktop, I have noticed that on an iPhone, the text in my button gets cropped as shown in the picture. https://i.stack.imgur.com/EYo2P.png Below is the HTML code I'm using ...

Extracting Data from Input Box Using Query

In my HTML setup, I have five <textarea> tags with IDs text1,text2,text3,text4, and one additional <textarea> tag with ID output. I want to query based on the values of text1,text2,text3,text4 (referred to as field1, field2, field3, field4). F ...

The slideUp function is not functioning as expected

I am trying to implement a slideUp effect on my webpage. Here is the code within my <body> tag: <script> $('#slide_up').click(function(){ $('p.text_study').slideUp('slow', function() { $ ...

Assign a CSS class to a specific option within a SelectField in a WTForms form

Could someone explain the process of assigning a CSS class to the choices values? I am looking to customize the background of each choice with a small image. How can this be done using wtforms and CSS? class RegisterForm(Form): username = TextField( ...

Converting radio button responses into an array using Express.js

After setting up a series of radio button groups to collect answers to questions, I am trying to find a way to store the data in an array format. Here is an example of the format: What Is your favourite Colour Red | Blue This is how it can be marked up: ...

What is the best way to apply a conditional class to multiple elements with jQuery?

Hey there, I'm new here so please forgive any mistakes I might make or if I break any etiquette rules! I'm trying to create some cool buttons for radio inputs on a form using field yields. The idea is to have them start off in grayscale and then ...

What is the method for customizing the default dropdown color of a bootstrap 4 selectpicker?

In my form, there are single and multiple dropdown selects. To keep things simple, I use the selectpicker class for both types. However, I want to customize the default grey color for Bootstrap 4 Dropdown with the selectpicker class to a clear color. Which ...

Bootstrap is having trouble with aligning flex items and preventing them from wrapping to a new row

I am working on a code implementation that dynamically displays flex items, with each item taking up half of the display width. So, after two items, I need to start a new row. Is there a way to add a line break after every two items displayed on the page ...

Create a responsive layout of inline-block elements that adjusts to resizing

I am currently focusing on developing the website found at: The progress so far includes successfully creating a list of div.project-item elements that adjust the number of columns based on window resizing due to the use of inline-blocks. My next goal is ...

Switch classes while navigating within a div

My website has a sticky sidebar with a list of cars and their corresponding categories in a table: <ul class = "cars"> <li class=""><a href="javascript:void(0)" class="model" data-id="1"> BMW </a></li> ...... ...

When the form is submitted, any blank inputs and their corresponding hidden fields will be disabled

I created a form that has multiple input fields, and users have the option to enter values or leave them blank. Each input field is accompanied by a hidden input field which contains a specific id unique to the corresponding visible input field. To disable ...

Guidelines for attaching successful data to an element using URL action including parameter

How can I utilize an AJAX call to retrieve a list of properties and then append one of the list items as a hyperlink with a post method to an Action controller in the MVC framework? success: function (data) { debugger; for (v ...

My goal is to dynamically assign a class to an iframe element by identifying a specific class contained within the html of the iframe

I need assistance with adding a class to an iframe only if the body tag has a specific class name. Here is my attempt at writing some code: <iframe class='iframeContent' src='' width='100%' height='' frameborder= ...

I need to extract information from the source code of a different website - how can I do this

Is it possible to extract content from the source code of another website and display it on our own site? For example, imagine a scenario where a site contains the following source code: <table ...> <tr> <td class ...

Using jQuery to append a new item to a JSON file

I have a straightforward JSON file containing a few pieces of data. Here is an example: { "id": 1, "name": "Porsche", "type": "911", "price": "135000" } My task is to create an HTML form with inputs for name, type, and price. When the user s ...