What is the best way to align a floated image to the left and text to the right inside a box?

I am currently attempting to design a box layout featuring an image on the left and text (including title, price, and description) on the right side. However, I am facing an issue where the text consistently appears outside of the designated box area. Can anyone identify what mistake I might be making in my approach?

.photo {
  width: 100%
}
.menu__item {
  width: 100%;
  border: 1px solid #c4c4c4;
  display: block;
}
.menu__item__photo {
  width: 40%;
  padding-right: 16px;
  display: block;
}
.menu__item__info__photo {
  width: 60%;
  display: block;
  float: right;
}
.menu__item__info__title {
  float: left;
  width: 78%;
}
.menu__item__info__price {
  float: right;
  width: 21%;
  text-align: right;
}
<div class="menu__item">
  <div class="menu__item__photo">
    <img src="http://placehold.it/350x150" class="photo">
  </div>
  <div class="menu__item__info__photo">

    <div class="menu__item__info__title">Product Title Here</div>
    <div class="menu__item__info__price">$9.99</div>

    <div class="menu__item__info__description">Description here..</div>

  </div>
</div>

For reference, you can view a fiddle of this code at: https://jsfiddle.net/pxanzefe/

Answer №1

You also have the option to float your left item:

  1. Simply float the .menu__item__photo item and ensure that the padding is included in the 40% width by adding box-sizing.

  2. Don't forget to use a clearfix method on your container.

.photo {
  width: 100%
}
.menu__item {
  width: 100%;
  border: 1px solid #c4c4c4;
  display: block;
}
.menu__item:after {
  content: "";
  display: table;
  clear: both;
}
.menu__item__photo {
  width: 40%;
  padding-right: 16px;
  display: block;
  float: left;
  box-sizing: border-box;
}
.menu__item__info__photo {
  width: 60%;
  display: block;
  float: right;
}
.menu__item__info__title {
  float: left;
  width: 78%;
}
.menu__item__info__price {
  float: right;
  width: 21%;
  text-align: right;
}
<div class="menu__item">
  <div class="menu__item__photo">
    <img src="http://placehold.it/350x150" class="photo">
  </div>
  <div class="menu__item__info__photo">
    <div class="menu__item__info__title">Product Title Here</div>
    <div class="menu__item__info__price">$9.99</div>
    <div class="menu__item__info__description">Description here..</div>
  </div>
</div>

Answer №2

If you simply want to display the text within the box, include overflow: auto; in the containing div:

.photo {
  width: 100%
}
.menu__item {
  width: 100%;
  border: 1px solid #c4c4c4;
  display: block;
  overflow:auto;
}
.menu__item__photo {
  width: 40%;
  padding-right: 16px;
  display: block;
}
.menu__item__info__photo {
  width: 60%;
  display: block;
  float: right;
}
.menu__item__info__title {
  float: left;
  width: 78%;
}
.menu__item__info__price {
  float: right;
  width: 21%;
  text-align: right;
}
<div class="menu__item">
  <div class="menu__item__photo">
    <img src="http://placehold.it/350x150" class="photo">
  </div>
  <div class="menu__item__info__photo">

    <div class="menu__item__info__title">Product Title Here</div>
    <div class="menu__item__info__price">$9.99</div>

    <div class="menu__item__info__description">Description here..</div>

  </div>
</div>

By floating elements, they are taken out of the normal document flow, and applying the overflow property brings back the desired behavior.

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

The expression '<xsl:value-of select="document(content)//title"/>' results in a null node response

Can someone assist me in retrieving the title of a basic HTML document in order to create a sitemap? I have been receiving an empty value every time I try. Upon inspecting the issue, it appears that the document(content) is returning document nodes. It s ...

Tips for adding additional columns to the final data output

When retrieving data from the database and binding it to a table, I found that I wanted to include additional columns in the displayed data. While I am getting the name value from the database, I also want to add a city column to the table on the UI. View ...

bootstrap class not detected and not visible in Firebug

I have been delving into a book about bootstrap in order to master it. Upon downloading the bootstrap files and integrating them into my test file, test.html, I encountered an issue where the "navbar-toggle" class was not recognized by my browser and did n ...

Centered alignment with floating divs of abbreviated length

My HTML file has a structure similar to the following: <div style="float:right"> A line of text, floating to the right </div> <div style="text-align:center"> And here, several lines of<br /> text which should be centered. </div& ...

The media does not need to be applied to the ".nav__btn" class

How can I hide an element with the ".nav__btn" class by setting its display to none when the screen width is at least 768px? I tried the following media code but it doesn't work. Media Code @media (min-width: 768px) { .nav__btn { display: ...

Align the items in the center of a Bootstrap navigation bar

I am looking to center the navigation menu items on my navbar. Currently, my navbar layout appears like this: https://i.sstatic.net/BqRwW.png Specifically, I want "Users", "Admin", and "Records" to be centered within the navbar. Additionally, I would lik ...

TinyMCE toolbar missing the "hr" option

I am encountering an issue while using TinyMCE as my editor. I have added the plugin as instructed, but I cannot find the "hr" button/option in the editor interface. If anyone has any insights or solutions to this problem, please share! This is how I am ...

How to create a heading within a Bootstrap list item?

Attempting to replicate this layout using Bootstrap 3 This is my current progress <ul class="list-group"> <li class="list-group-item> Cras justo odio </li> <li class="list-group-item> Dapibus ac facilisis in </l ...

Navigating URLs in various Android browsers: Tips and Tricks

I am on a quest to find a method to engage with various android browsers. My goal is to retrieve the URL of the website being viewed by the browser. I want my app to operate in the background and issue an alert when a designated URL is accessed, primarily ...

Resolving CSS Conflict in Panels with Legacy Bootstrap

I have recently implemented Panels from Bootstrap 3 with the older version of Twitter-Bootstrap. My challenge lies in adding the well class to the panel body, as it results in excessive padding. This was not an issue when using BS3, leading me to believe ...

utilizing the default placeholder image for videos and audios within the tags

In my web application, users can share music and videos that will be stored on a server used by the application. Some audio/video files come with posters or thumbnails attached to them. I want to display these posters along with the audio or video using HT ...

Guide on how to launch an Android app if it is already installed, or direct the user to the Play Store from a web

Looking to create an HTML page featuring a button with specific click event functionality requirements. If the corresponding app is already installed, it should open that app. If the app is not installed, clicking the button should redirect to the Google ...

When using Firefox, the -moz-transform:scale property allows you to scale the image without impacting its original dimensions

While resizing icons on a sprite sheet by scaling the image with CSS works fine in Chrome, Firefox seems to scale the graphic correctly but it still takes up the same space as the unscaled version. Even though I found -moz-focus-inner which removed some p ...

What is the best way to display a particular JavaScript variable in an HTML document?

Is there a way to display the value of a Javascript variable in an HTML form, such as showing it on the screen? Below is my JavaScript code: <script type="text/javascript"> var howLongIsThis = myPlayer.duration(); </script> The variable howL ...

Can you inquire about utilizing advanced search features when a user selects all options?

Can anyone help with advanced search queries when all options are selected by the user? My form consists of 2 drop-down menus and 1 submit button. ________________ ________________ body : |______All_______| color : |______All_ ...

Updating the design of mui stepper icon

Is there a way to change the color of this icon from https://i.sstatic.net/kr5lv.png I would like it to be this color: https://i.sstatic.net/dHh68.png I have tried using .Mui-disabled but it didn't work, not sure if it's .Mui-active or .Mui-com ...

I've encountered an issue when attempting to use innerHTML for DOM manipulation

I've been attempting to remove a specific list item <li> from the inner HTML by assigning them proper IDs. However, I'm encountering difficulties in deleting it. How can I delete these list items after clicking on the cross button? Feel fr ...

Scrollbar not displaying on IE when set to overflow: auto

Greetings, all! I am facing yet another design challenge with Internet Explorer. I have a DIV with Overflow:auto on my website that works perfectly fine on Chrome. However, when it comes to IE, the scroll bar doesn't show up and all the images inside ...

Perform an AJAX request to an encrypted URL with an unverified certificate

I'm experiencing an issue with my site that makes AJAX JSONP calls to a secured (SSL) web server. Everything works smoothly when using an unsecured (HTTP) web server, but once I switch to the SSL version, the call never returns. After checking with Fi ...

Is it allowed to modify <li> tag only when it contains a <strong> child element?

Is it possible to use SCSS to change the background color of a li element based on whether its child is a strong tag or not? li { margin-left: 3px; margin-bottom: 3px; border: solid transparent; background-color: #fff; border-top-left-radius: ...