Is there a way for me to align my div with my header on the same line?

HTML:

<h2> RAN shares false news story about Hershey's Oil Commitment</h2>   
<div class="date">  
<div class="number">27</div>
<div class="month">Oct</div>  </div> 
<p>The Rainforest Action Network circulated a fabricated news report regarding Hershey's Oil   
Commitment. The report claimed that Hershey would eliminate palm oil from all their products, and 
several news outlets were fooled by it, including Hershey's local newspaper. In addition to this, members of RAN crashed Hershey's Halloween celebration with a unique orangutan! 
<a href="hershey.html">Curious for more details?</a> </p>

CSS:

.date {
  width:50px;
  height:50px;
  background-color:orange;
  border-radius:1em;
  margin-left:75%;
}

.number {
  color:white;
  text-align:center;
}

.month {
  text-align:center;
  background-color:#E68A00;
  color:white;
}

h2 {
  font-size:1.2em;
  color:#007e7e;
}

I am attempting to align the date and news article title on the same line. Here is the current layout:

Answer №1

Combine the date and title in a single container and align them to the left with a clearfix to prevent sizing problems.

Alternatively, position them relatively by specifying left/top values.

Answer №2

Check out this simple fix: within your CSS

h2{
    float: left;
}
.date{
    float:right;
}
p {
    clear:both;
}

By floating the h2, it disrupts its normal flow. When you then float the .date element, it also gets pulled out of its natural HTML flow, causing it to be positioned next to the h2. The clear: both; property prevents the paragraph text from overlapping and filling any remaining space, ensuring that everything remains in its original position.

Answer №3

To achieve the desired layout, apply float:left to the h2 element and float:right to the .date class. You can see an example implementation here: http://jsfiddle.net/ABCD5/3/

<h2> Breaking news: Hershey's oil commitment exposed as fake! </h2><div class="date">
<div class="number">27</div>
<div class="month">Oct</div>  </div> <br><br>
<p>A fabricated report by the Rainforest Action Network claimed that Hershey would be eliminating palm oil from all products. Several news outlets were fooled, including a local newspaper. Additionally, RAN members crashed Hershey's Halloween event with an orangutan!
<a href="hershey.html">Curious for more details?</a> </p>




.date {
float:right;
width:50px;
height:50px;
background-color:orange;
border-radius:1em;
}

.number {
color:white;
text-align:center; }

.month {
text-align:center;
background-color:#E68A00;
color:white; }

h2 {
    float:left;
font-size:1.2em;
color:#007e7e; }

p {
clear:both;
}

http://jsfiddle.net/ABCD5/3/

Answer №4

h2 {
font-size:1em;
color:#003366; 
float:left;
}

Implement a left float to the header - this will result in the following div floating to the left on the same line as the h2.

You could alternatively apply a right float to the div, but this will position the div at the far right within its container.

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

When verifying the source, the empty() function does not eliminate any HTML content

I am facing an issue with an AJAX call that retrieves data from a PHP file. I am attempting to remove existing HTML content and replace it with the data fetched from the PHP file. To clear the content, I am using the following: $('#due-tasks-content& ...

Top strategies for avoiding element tampering

What is the best solution for handling element manipulation, such as on Chrome? I have a button that can be hidden or disabled. By using Chrome's elements, it is possible to change it from hidden/disabled to visible/enabled, triggering my click functi ...

Changing Ionic Column Widths Based on Content Length

In my dynamic ionic 2 grid system, I am facing a layout issue. <div> <ion-row> <ion-col > ; <ion-card-header class="card-header"> {{hunt.title}} </ion-card-header> </ion-col> <ion-col *ngIf="!hunt. ...

Activate vertical scrolling in JavaScript when an image is clicked

I currently have a collection of button images with different hover effects specified like this: <img src="home.PNG" onmouseover="this.src='homemo.PNG'" onmouseout="this.src='home.PNG'" /> My goal is to make them scroll down to ...

I am interested in creating a text box that has the ability to gather user input and connect it to a search

I have been attempting to develop a text box that can collect answers and link them with the search URL. However, I am encountering issues where even though I can input text into the textbox, it is not being recognized by the script. The error message sh ...

Why is this specific element showing as undefined in the form during editing, but appearing correctly in both the debugger and console.log?

I've encountered an error that keeps popping up: "Cannot read property 'textContent' of undefined at HTMLDocument". This error specifically occurs when dogName, dogBreed, and dogSex are set within the 'click' listener. It's st ...

The Bootstrap navigation menu is functioning properly when opening, but has an issue when attempting

Creating a mirrored version of an HTML file using NuxtJS, I've included the following code snippet in the Navbar component for my NuxtJS project. <template> <section id="navigation-menu" class="menu menu3 cid-sLhoPz7Ycg" ...

Determining the pageY value of a div element with overflow-y styling

Currently, I have implemented a script that tracks the mouse's position upon hover. My goal is to integrate this script within a div that has overflow-y: scroll The script currently utilizes pageY which identifies the position relative to the windo ...

Trouble with downloading a file from an HTML hyperlink

When I attempt to download a file from my file folder using the absolute path <a href='N:\myName\test.xlsx'>download</a>, the file opens directly instead of downloading. However, if I use the relative path <a href=&apos ...

Placing a div underneath a different element on a webpage

I am struggling with the placement of a div on my webpage. I have tried various methods to position it beneath two other elements without success, despite following advice from online resources. Here is the code I have been working with: ...

Hovering into Transition Time

My article card has a transition on the top attribute of the info div, which is supposed to be smooth and last for 0.3 seconds. However, the description suddenly appears during this transition. I'm trying to figure out why this is happening and how to ...

Adjust the border hue of the MUI disabled outline input

I am currently struggling to locate the exact definition of this border color. After inspecting the dom, I cannot seem to find any border style within the input component or its pseudo elements... My main goal is to slightly lighten the color of the input ...

The <h> tag gains height on its own accord

Here is my original code: <h4 class="orange2" ><a href="<?php echo base_url();?>v/<?php echo $this->uri->segment(3);?>/<?php echo $v['uniq'];?>"><?php echo trim(strip_tags($v['video_name']));?> ...

SQL Delete rows from Table

I am dealing with an issue where a table displaying notices has a clear option next to each row, but when the clear button is clicked nothing happens. The setup involves a button that triggers a clear function. What could be causing this problem? <bu ...

JavaScript combined with a dynamic menu, a customized current link style using CSS, and a site built on PHP

Here's my current website setup: My website is modular and uses dynamic inclusion. The header is a crucial part of the main page, which is responsible for displaying content from specific links on the site. External links to CSS and js files are incl ...

When interacting with elements with the "user-select: none" property, WkWebView still allows text selection

When using an iOS iPad app with a single fullscreen WKWebView, an issue arises when long-pressing on an area with user-select:none - the first text in the next available area without user-select:none gets selected instead of being ignored. Is there a way t ...

The functionality of the jQuery program is compromised when used on the Google Chrome browser

I just recently started diving into the world of Jquery, attempting to learn it from scratch. However, I have encountered an issue while trying to make my Jquery program run smoothly: Let's take a look at the CSS first: p { opacity: 0; } Now on ...

Aggregate the data entered into input fields on one page and display them on a separate page

Can you help me with this task? - I have 2 pages set up (page 1 has input fields & page 2 is where the entered data should be displayed) - I want to retrieve all the text input from the first field and insert it into the "content" tag on the second page. ...

"Gone in a Scroll: Div vanishes as page moves

Fiddle: http://jsfiddle.net/ud6hejm6/ I was tasked with creating a website for a video game tournament. By opening the fiddle link provided, you can preview the page layout. A central div is featured in the design: <div class="middle teko" id="mezzo"& ...

Arranging data into columns using HTML and CSS

I'm currently working on a CSS solution to organize various elements like buttons and text in a tabular column format. My main challenge lies in controlling the layout effectively, especially since CSS columns are not compatible with IE9 and earlier v ...