margin-top aligned to the bottom of two elements

Within this straightforward CSS snippet, my goal is to align the bottom of h1 with the bottom of p, without having h1 display below p.

<div id="sqrs" style="width:200px; height:200px; border:1px solid BLACK;">
<p style="width:100px; height:100px; background-color:BLUE; float:right;">blue</p>
<h1 style="background-color:YELLOW; margin-top:100px;">yellow</h1>
</div>

The style of p cannot be altered and I do not have knowledge of the exact height of h1 (as it can vary between different heading elements).

I attempted using margin-top:100% instead of margin-top:100px, but this did not provide a solution either.

Thank you,

Answer №1

To view the demonstration, please visit http://jsfiddle.net/3sWEt/3/

If you want to position an element at the bottom of a container, you can set `position:relative` to the container and then use `position:absolute` with `bottom:0` on the desired element:

Here is the CSS code:

#sqrs{
    width:200px; height:200px; border:1px solid black;
    overflow:hidden;
}
#sqrs>*{
    height:100px;
}
#sqrs>p{
    width:100px; background-color:blue; float:right;
}
#sqrs>div{
    overflow:hidden;
    position:relative;
}
#sqrs>div>h1{
    background-color:yellow;
    position:absolute;
    bottom:0;
    width:100%;
}

And here is the HTML code:

<div id="sqrs">
    <p>blue</p>
    <div><h1>yellow</h1></div>
</div>

Answer №2

For a solution to the issue mentioned, please refer: http://jsfiddle.net/3sWEt/1/

#sqrs{
    width:200px; height:200px; border:1px solid black;
    line-height:100px;
    text-align:right;
}
#sqrs>*{
    text-align:left;
    line-height:normal;
}
#sqrs>p{
    width:100px; height:100px; background-color:blue; float:right;
}
#sqrs>h1{
    background-color:yellow;
    display:inline-block;
    vertical-align:bottom;
}

To maintain consistency, utilize the same properties such as line-height, display:inline-block, and vertical-align:bottom.

The main issue lies in the fact that <h1> does not expand its width to fill the remaining space within its parent element.

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

Utilize CSS to incorporate special characters as list markers

Is there a way to use CSS to make the list marker in an HTML list display as "►"? ...

Stop an animation while it is in the hover state

Currently experimenting with CSS3 Animations to create a Panoramic view using only CSS and no JavaScript. I have managed to create a somewhat working demo: http://www.example.com/images/panoramic/ The concept is simple: when the user hovers over the blac ...

There is excessive white space at the bottom of my Index page after the footer that needs to be removed

Recently began working on converting a PSD to HTML and I've reached a point where everything seems fine. However, I understand that my CSS programming may not be the best at the moment. With your help, I am confident that I can learn and improve gradu ...

I'm having trouble extracting data into my HTML file using the append function in Jquery. What could be causing this issue?

Hey there, I'm having some trouble extracting data from a URL to my HTML file using jQuery. Can anyone help out? I'm still new to this. Here's the code <html> <head> <title> My Program </title> <script src="ht ...

Bootstrap 3 Implementation of a Clear Navbar

I'm trying to create a transparent navbar using the code below: <div class="navbar navbar-default navbar-fixed-top" style="top:50px; background:transparent;"> <div class="navbar-inner"> <div class="container"> <ul cla ...

PHP is causing disruptions to HTML

Every time PHP code is present in my file, the page appears blank and upon checking the source code, there are no tags visible on the page. However, once I remove the PHP code, the form displays correctly without any issues. <?php $title = $_POST[&apos ...

What is the best way to eliminate the unwanted white space below the footer on a webpage

I'm working on a website and I've noticed that at the end of the footer, there is a large block of white space that shouldn't be there. I've tried adjusting the HTML and CSS code, but nothing seems to fix the issue. Strangely, other pag ...

Issue with JQuery causing maxlength input not to work

Is there a way to use JQuery to set the maxlength attribute for an input[type='text'] while users are typing, so that once the max value is reached, input will stop? This is the code I have been trying: $(document).ready(function(){ $(&apos ...

Tips for dynamically appending a string to a predefined variable in React

When I was working on creating a text input space using the input type area and utilizing the onChange utility, everything was running smoothly. Now, my task is to incorporate an emoji bar and insert a specific emoji into the input space upon clicking it. ...

Personalized design for the range input in React

I am working on a React component that includes an input range element. I have used some CSS to style it vertically and currently, it looks like this: https://i.stack.imgur.com/WmQP4.png My goal is to customize the appearance of the slider so that it res ...

Having issues with jQuery when trying to select only a single checkbox?

I have created a table with four rows and eight columns, each containing a checkbox. My goal is to allow only one checkbox to be checked per row. I am attempting to achieve this using jQuery. While it works in jsfiddle, I am experiencing difficulties in ge ...

Fill an HTML table with comma-separated values (CSV) information

After conducting some research on the internet, I attempted to modify a jQuery script in order to populate an HTML table with data from a CSV file. However, I encountered an issue as the table remained empty. Below, you can find the code that I used. Any a ...

The Jqueryui image link is not displaying the image despite no error being reported

Can anyone help me figure out what I'm missing? I'm currently working with ASP.NET MVC 5 and have downloaded the JqueryUI combined via Nuget package. Despite no error references for css/js files, the close button is still not showing in the ima ...

Retrieve information from a JSON script using C#

I need to extract data from a homepage, but the information I'm looking for is embedded in a JSON script. How can I retrieve this value? For example: <div id="contentWrap"> <div id="contentTextWrap"> <div id="contentText ...

Mastering jQuery UI: A beginner's guide to utilizing the date picker widget

This is my first venture into the world of HTML and JavaScript, so I am asking for your patience. I am trying to incorporate the Datepicker widget from jQuery UI, following the instructions on the Getting Started page. There seems to be an issue with lin ...

Transferring a unique Python object back and forth between Jinja2 and Flask

As a newcomer to Flask, Jinja2, and HTML templates, I am working on understanding how to transfer data between the controller and views. Currently, I have a calendar of events where each event is a hyperlink that uses url_for to navigate to a view with add ...

Should ng-binding be avoided in CSS selectors as a general rule?

Recently, I stumbled upon this code snippet in our codebase: li.ng-binding span { font-size: 13px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; } The selector above doesn't actually apply to one of the intended 'li& ...

Compel the browser to initiate a reflow by adjusting the CSS

I am currently in the process of building a responsive image slider without relying on jQuery, using CSS3 transitions. The setup is quite simple: there's a viewport with a UL inside, containing relatively positioned LIs that are left floated. Howeve ...

Div that scrolls only when children can be displayed without overflowing

I am seeking a solution to ensure that the children inside my scrollable div are only visible in their entirety. HTML <div id="container"> <div class="child"></div> <div class="child"></div> <div class= ...

Tired of the premium content on Medium.com. How can I conceal premium posts that are aimed at a specific class within a parent element?

I have noticed that all Premium posts contain an element with the class ="svgIcon-use" <svg class="svgIcon-use" width="15" height="15" viewBox="0 0 15 15" style=""><path d="M7.438 2.324c.034-.099.090 123 0l1.2 3.53a.29.29 0 0 0 .26.19h3.884c.11 0 ...