The <div> positioned at the highest point within its container

Looking to position a div at the top within its parent div, while leaving space below it unused. The default behavior appears to be the opposite, with the inner div dropping down to the bottom of its container and creating empty space above it.

It seems like a simple task, but I'm struggling to find a solution through Google searches like "div float top" or "div gravity".

Here is the HTML code:

<div class="bonus">
  <div class="bonusbookmakerlogo">
    <a rel="nofollow" href="http://..." target="_blank"><img src="/img/box.png" alt="blah" title="blah"/></a>
  </div>
  <div class="bonustext">
    <span>Bonus description.</span>
  </div>
  <div class="bonusdivider"></div>
</div>

And the corresponding CSS:

.bonus {
  font-size: 90%;
  text-align: justify;
  margin: 1em 2em;
}

.bonusdivider {
  margin: 1em 0 1em 0;
  border: none;
  height: 1px;
  color: #999999; 
  background-color: #999999; 
}

.bonusbookmakerlogo {
  display: inline-block;
  width: 20%;
}

.bonustext {
  display: inline-block;
  width: 70%;
}

The layout looks good overall, but the logo div (containing the img tag) is stuck to the bottom of its container rather than the top. How can I make it stay at the top?

Appreciate any help in advance.

Answer №1

Here is a small tweak using the float property instead of inline-block. It appears to be functioning properly:

<div class="special">
  <div class="speciallogo">
    <a rel="nofollow" href="http://..." target="_blank"><img src="/img/box.png" alt="example" title="example"/></a>
  </div>
  <div class="specialtext">
    <span>Special offer description.</span>
  </div>
  <div class="specialdivider"></div>
</div>

Here's the corresponding CSS:

.special {
  font-size: 90%;
  text-align: justify;
  margin: 1em 2em;
  height: 100px;
  border: 10px solid red; /* for testing */
}

.specialdivider {
  margin: 1em 0 1em 0;
  border: none;
  height: 1px;
  color: #999999; 
  background-color: #999999; 
  clear: both;
}

.speciallogo {
  float: left;
  width: 20%;
}

.specialtext {
  float: left;
  width: 70%;
}

Answer №2

After reviewing the answer provided by @Marius George, which I believe is a solid solution, I came across an alternative approach that may be of interest:

.bonusbookmakerlogo {
  display: inline-block;
  width: 20%;
  vertical-align: top;
}

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

Why aren't the JavaScript stars shining once I've selected one?

I recently set up a star rating feature on my website by following the tutorial "Creating an Ajaxified Star Rating System in Rails 3". Although I managed to get everything working on the index page, I noticed that the stars do not remain lit up after bein ...

Exploring the art of JSON interpretation within Highcharts

Below is an example snippet that utilizes static data: Highcharts.chart("container", { title: { text: "Highcharts pie chart" }, xAxis: { categories: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Ju ...

You are only able to click the button once per day

I am working on a button that contains numeric values and updates a total number displayed on the page when clicked. I would like this button to only be clickable once per day, so that users cannot click it multiple times within a 24 hour period. Below i ...

Using jQuery to create dynamic CSS effects directly on the drop-down menu button

Seeking your kind assistance once again... I am attempting to design a navigation bar that displays the dropdown section upon hovering over the buttons AND shows a bottom border on the button that was last hovered over My code functions properly when I ...

An error has been thrown stating that the function startTimer is not defined, despite the fact that it is clearly defined

const startBtn = document.querySelector('.startBtn'); const pauseBtn = document.querySelector('.pauseBtn'); const ResetBtn = document.querySelector('.resetBtn'); const time = document.querySelector('.time'); let sec ...

Add styles to IMG elements only if there are no existing styles already applied

Feeling a bit embarrassed asking such a basic question, but I seem to be struggling with it. I want to use CSS to style all IMG elements on my page that do not have any other class applied to them. For instance, I want all images to have a red border by ...

Alternating row colors using CSS zebra striping after parsing XML with jQuery

After successfully parsing XML data into a table, I encountered an issue with applying zebra stripe styling to the additional rows created through jQuery. Despite my efforts to troubleshoot the problem in my code, I remain perplexed. Below is a snippet of ...

Modify the color of the chosen option in the dropdown menu without affecting the rest of the options

I am facing an issue with dynamic drop down lists in my HTML content. The requirement is to change the color of the selected option to dark red if the option is "success". I have implemented a jQuery function for this purpose. HTML: <select class="rea ...

Adjusting the options in the subsequent dropdown menu based on the selection made in the initial dropdown menu

I am currently working on select boxes where values are dynamically added through an array. For example, if I have 4 values in the first select box, I only want to display 3 values in the second select box, excluding the value that has already been selecte ...

CSS3 circular gradient effect

I am trying to figure out if it is feasible. The photoshop team created an image that originally had a gradient fading from 1 to 5 colors. Now, they have added a white circle on top of it. How can I achieve this effect? The provided examples lack specific ...

Tips for managing accordion events using a button press

I found a script online that I want to modify. Specifically, I am looking to create a button that will control the accordion event by closing the current div and opening the next one. <button> Click me to open next</button> inside each div in ...

What are the best practices for incorporating keywords into HTML code for SEO purposes?

Even though this topic is borderline related to programming questions, I believe it's still relevant here. After all, only those of us who are actually coding for a website would be considering this aspect. Lately, I've been delving deeper into ...

Is there a way to extract all the content from a webpage's body using BeautifulSoup?

Looking to extract text from a medical document webpage for a Natural Language Processing project using BeautifulSoup, but encountering challenges. The specific website in question is located here: The goal is to capture the entire text body by utilizing ...

REST, hypertext, and clients other than web browsers

I'm struggling to understand how a REST API can be both hypertext driven, as explained in this article, and still be machine readable. Let's say I create an API where one endpoint lists the contents of a collection. GET /api/companies/ When the ...

How can I prevent a browser from allowing users to select an image tag?

I'm dealing with an issue on my webpage where I have an image that is inadvertently picking up mouse clicks, causing the browser to perform drag and drop actions or highlight the image when clicked. I really need to use the mousedown event for somethi ...

The property 'innerHTML' cannot be assigned to null, as stated in Vue

I am dealing with two components that allow for editing JSON objects. Additionally, I have a button that toggles between these two components. Below is the HTML code snippet: <v-btn @click="switchConfigClicked">Switch config</v-btn> <h4 cla ...

What is the best way to vertically center a column of images on mobile devices in a responsive manner?

Currently, I am developing an application that showcases the 9 newest photos with a specific tag. To ensure consistency in image sizes, I have set each photo to be 240px wide and 200px tall. My query now is how can I vertically center these images so they ...

Getting the IDs of selected nodes all the way up to the root node in jsTree

How can I retrieve the IDs of all parent nodes from a selected node to the root node in jsTree? If node C is selected, how can I obtain the IDs of all its parent nodes? Tree Structure: B C +C1 +c2 The following code snippet only returns the imme ...

When a div element is clicked, it becomes the selected element and borders are displayed around it

Within a div on my wordpress site, the logo is prominently displayed. Feel free to check it out here: Strange issue - whenever I click on the logo, a large rectangular box suddenly appears. I've experimented with the outline and user-select properti ...

Ways to track activities involving a specific input element

Currently, I'm dealing with an input field within an ASPX page utilizing minified JavaScript. Unfortunately, I do not have access to the C# source code for this particular page as it is not publicly available. However, I can still make changes to the ...