What is the best way to resize a div to fit its content if 'clear:both' and 'float:left' are not working as desired?

I've been struggling to find a solution for an issue with my HTML. I have three 'divs' that need to display different information in their own line, enclosed in a parent element with a centered border. Despite trying solutions like inline-block and float, I can't seem to get the width of the parent to adjust correctly while keeping the content centered both vertically and horizontally.

Here is the HTML code snippet:


<div class="service-options">
    <div class="from-group">
        <h1 class="animal-title">testing inline-block</h1>
        <div class="list-services">
            <div class='column-service'>
                <div class="service-name">
                    online
                </div>
                <div class="service-description">
                    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
                </div>
                <div class="service-price">
                    100<span class="pricecurrency">€</span>
                </div>
            </div>
            <div class='column-service'>
                <div class="service-name">
                    store
                </div>
                <div class="service-description">
                    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
                </div>
                <div class="service-price">
                    200<span class="pricecurrency">€</span>
                </div>
            </div>
            <div class='column-service'>
                <div class="service-name">
                    postal service
                </div>
                <div class="service-description">
                    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
                </div>
                <div class="service-price">
                    150<span class="pricecurrency">€</span>
                </div>
            </div>
    </div>
</div>

And here is the corresponding CSS:

.animal-title {
  padding: 45px 0 45px 60px;
  color: #666B74;
  font-family: 'RalewayRegular';
  font-weight: bold;
  font-size: 36px;
}
.column-service {
  border: 3px solid #D53865;
 }

.list-services {
  display: flex;
  justify-content: space-around;
  padding: 45px 0 0 60px;
}

.service-name {
  clear: both;
  float: left;
  
  font-size: 20px;
  color: #D53865;
  font-weight: bold;
  font-family: 'RalewayRegular';
}

.service-description {
  clear: both;
  float: left;
  
  font-size: 16px;
  color: #666b74;
  font-family: 'RalewayRegular';
  width: 50%;
}

.service-price {
  clear: both;
  float: left;
  font-size: 22px;
  color: #D53865;
  font-weight: bold;
}

.price-currency {
  font-weight: normal;
}


If you want to see the current behavior in action, check out the Codepen link below:

https://codepen.io/CharlieJS/pen/GRZqbGB

I'd appreciate any help or guidance you can offer. Thank you for your time and assistance.

Answer №1

By setting the width of .service-description to 50%, you are essentially restricting the container's ability to shrink proportionally. Instead of using percentages to define the width, consider setting the width on the container itself for more flexible sizing options. Alternatively, you can add margins to the column services to control the spacing of the content.

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 border-radius property in CSS selectively rounds only one corner of the modal div window

There seems to be an issue with the border-radius property on modal divs in my project. When I apply the property, only one or two corners of the div round while the other corners remain unchanged. For a visual reference, please check out this image linke ...

Bring div button on top of the contenteditable field

I am working on an Angular app for a client and need to implement a clickable button at the bottom right of a contenteditable element, similar to the image shown below : https://i.sstatic.net/J6XdW.png The challenge is that the content needs to be scroll ...

What's the best way to add margin or padding to an SVG image within a v-app-bar

Seeking assistance with a seemingly simple question that can help enhance my understanding as I dive into using vue/vuetify for a new front end project. I am starting fresh and utilizing v-app-bar. Below is a snippet from my app.vue file: <template> ...

Error Occurs in Vue Cli: Module '../package.json' not Found After Installation using npm

While I may not be an expert in Vuejs or Vuecli, I somehow manage to make things work. Recently, when revisiting a project I had previously worked on using Vuecli3 and webpack, I encountered the following error in development mode: $ vue-cli-service ser ...

A child component in Vue.js unexpectedly starts updating when the parent component renders and uses object literals as props

I'm currently facing an issue with my Vue components where object literals are passed as props like: <child :prop1="{ foo: 'bar' }"></child> After rerendering the parent component, the prop prop1 changes and triggers an update ...

Having trouble getting the header div to span the entire screen

I'm currently working on a project where I have set the header div to 100% width and the main container to also be 100% width. However, I am facing an issue where the right and left sides of the header are not filling the entire space. I managed to fi ...

I'm having trouble getting my HTML button to redirect to a PHP link

I'm attempting to call a PHP script from my HTML file. Here's the snippet of my HTML code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://w ...

What is the comparable javascript code for the <script src="something1.json"></script> tag?

Within my HTML code, I currently have the following line to read a JSON file: <script src="something1.json"></script> Inside this JSON file, there is a structure like so: var varName = { .... } This method of definition naturally sets up ...

What could be causing the error.status value to not be displayed on my Embedded Javascript (EJS) page?

Currently utilizing ejs, node.js & express along with other packages I'm facing an issue where the error.status and error.stack values are not appearing on the rendered page. I suspect that the error variable may be undefined or inaccessible within t ...

When using Ruby on Rails, the image_tag method generates two <img> tags within my HTML code

I recently started learning Ruby on Rails and encountered an issue with resizing images using CSS. After replacing an image field with the Ruby image_tag, I noticed extra blank space below the image. Upon inspecting the element, I found two tags for the s ...

Safari does not support font-face rendering, unlike Chrome

I am in the process of creating a typewriter-style font page using a local font. I have used @font-face to import it, and while it displays correctly on Google Chrome, it does not render properly in Safari. You can view the web page here: (you will need ...

Flexbox or Bootstrap 4 Grid: Which is the Better Choice?

My form is structured as follows: <div class="row"> <div class="col"> <textarea name="comment" class='form-control' placeholder='Type new comment here..'></textarea> </div> <div clas ...

Transforming CSS with React Router Links

Why is my CSS being significantly altered by the React Router Link? Is there a way to prevent the Link from affecting the styling? Without Link: https://i.sstatic.net/ZoVEq.jpg With Link: https://i.sstatic.net/84qRS.jpg This is the code for the ...

Tips for personalizing the error message displayed on Webpack's overlay

Is there a way to personalize the error overlay output message in order to hide any references to loaders, as shown in this image: https://i.sstatic.net/ESFVc.png Any suggestions on how to remove the line similar to the one above from the overlay output? ...

Stop opening when v-btn is clicked in v-expansion-panel headers

function handleClick (event) { event.preventDefault() console.log('bar') }, <v-expansion-panel> <v-expansion-panel-content> <div slot="header"> <v-btn icon flat @click="handleClick($event)"><v-icon>ad ...

Arranging mat-checkboxes in a vertical stack inside a mat-grid-tile component of a mat-grid-list

I'm looking to create a grid list with 2 columns, each containing 2 checkboxes stacked vertically. While I found this helpful question, it still involves a lot of nested divs. Is there a cleaner way to achieve this layout? Here's how I currentl ...

Exploring creative methods for incorporating images in checkboxes with CSS or potentially JavaScript

Although it may seem like a basic question, I have never encountered this particular task before. My designer is requesting something similar to this design for checkboxes (positioned on the left side with grey for checked boxes and white for unchecked). ...

How do I align the Close button to the right in a Bootstrap 5 toast message?

To create a Bootstrap 5 toast, I use the following code: <div id="liveToast" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true"> <div class="toast-header"> ...

Unlock the full potential of Laravel and Vue.js with these advanced techniques

I am currently working with vue.js 2.0 and Laravel 5.4. I am interested in finding a more efficient way to pass data from Controllers to views without losing the value or being overwritten by the v-model. When using Vue.js, the value in data is defined li ...

What measures can be taken to guarantee that a user is only able to delete designated records?

When it comes to writing SQL commands directly, it's simple to specify which records to delete using identifiers like ID. However, when dealing with webpages that contain a list of links pointing to different records (with the ID embedded as a query ...