How to position a div at the top of the div above it using CSS?

I'm having trouble aligning a div to the top of the previous div.

Despite using vertical-align:top;, the alignment does not seem to work as expected.

To demonstrate this problem, I have prepared a FIDDLE

Here is the code for the div that needs to be aligned to the top:

<div style="display:inline-block; background:#EFEFEF; border-radius:4px; margin:5px; padding:10px;">
<p style="font-size:12px; font-weight:bold; padding:5px;vertical-align: top;">Draw Days</p>

<label for="one">
<input type="radio" id="one" name="duration" value="1" />
Tue
</label>

<label for="two">
<input type="radio" id="two" name="duration" value="1" />
Fri
</label>

<label for="two">
<input type="radio" id="two" name="duration" value="1" />
Tue &amp; Fri
</label>
</div>

If anyone has advice on how to solve this issue, I would greatly appreciate it.

Thank you in advance.

Answer №1

To ensure proper alignment, add vertical-align: top to both divs as they are inline blocks within the text flow. This will align them with the top of the line they are on. Additionally, removing the 5px top margin of the second div is necessary for visual alignment.

Check out the updated fiddle here:

https://jsfiddle.net/9pzuhnfy/1/

Answer №2

To align these divs side by side, you can apply the CSS property float:left. Additionally, insert another div after these ones with the CSS property clear:both.

https://jsfiddle.net/b9r7yut3/8/

If you prefer not to use float properties, consider setting your divs as inline-block elements. This is because vertical-align only affects inline and inline-block elements.

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

Warning: The DataTables alert has been triggered for table ID DimStatus. It is indicating that an unknown parameter, 'Code', has been requested for row 0 and column 0 during the initialization

https://i.stack.imgur.com/yEpSp.pngI am encountering an error while attempting to display my data in a datatable. The structure of my table is as follows: [Table("DimStatus", Schema = "dmg")] public class PolicyState { [Key] ...

I successfully created a Chinese writing practice deck in Anki, which is functional on the desktop version but unfortunately not compatible with Ankidroid

While AnkiDesktop is functioning properly, there seems to be an issue with character encoding in Ankidroid. Despite trying numerous solutions, the problem persists. The character length displays correctly in Anki Desktop, but not in Ankidroid. For example ...

How to toggle the visibility of checkboxes in JavaScript based on their checked status

I am facing an issue with some checkboxes as I am unable to filter them based on whether they are checked or not. The concept is simple. When I click on "All", all the checkboxes should be displayed. However, when I click on "Selected", only the selected ...

Progress bar display not available

I have recently developed a JavaScript quiz application and included a progress bar feature. While it works flawlessly offline on my laptop, I encountered an issue when uploading the files to Codepen.io - the progress bar fails to display. I would appreci ...

CSS:Tips for removing borders with border-radius in CSS

I have a div that has been styled with a left-hand border, and it's causing me some trouble. You can view the styling here on Jsfiddle. Is there a way to remove just the left hand side border without affecting the rest of the design or creating an un ...

Issue when attempting to animate an SVG point using translateX transformation

I am attempting to create a basic animation using the translate X property on a section of my svg when hovering over the element. Here is the code I have so far: <html> <style> .big-dot:hover { transform: translateX(20px); animat ...

Expanding an item to fill the entire width within a Bootstrap navigation bar

Here is the layout of the navigation bar in my current web application project: [ Brand [button] [button] [----- input field -----] ----- empty space ----- [button] ] This is a simplified version of the code I am working with: <div class="container-f ...

Div that stays in place exclusively for mobile devices

At the top, there are two divisions and on a cellphone device, the second one is meant to become fixed after the user scrolls past it. To see an example of what I am trying to achieve, check out this link: https://jsfiddle.net/livibetter/HV9HM/ Specifica ...

Divergent font sizes displayed in Ubuntu Chrome and Firefox with Bootstrap 4

I recently started working on a website using Ubuntu. However, I noticed that when I view it in Firefox, the layout appears smaller compared to Chrome. Here are screenshots for reference: https://i.sstatic.net/YnMFL.jpg https://i.sstatic.net/Jespf.jpg B ...

CSS for collapsible sidebar with dynamic width

Imagine having a collapsible fixed-width sidebar like this: HTML <div class="container"> <div class="sidebar" id="sidebar"> SIDEBAR </div> <div class="content"> lorem bla bla <button onclick="document.getEleme ...

How can you transfer data from a jQuery function to a designated div element?

I'm struggling to transfer data from a function to a specific div, but I can't seem to make it work. I'm in the process of creating a gallery viewer and all I want is to pass the counter variable, which I use to display images, and the total ...

Transforming Codeigniter Forms into Custom HTML

I need help converting the following CodeIgniter code into HTML. Can someone assist me with this? <?php echo form_open('contact/create_member'); echo form_input('name', set_value('name', ' Name')); echo form_in ...

Unable to close bootstrap error alert by clicking on the X button

I am having an issue with my error div that is supposed to display errors: <div *ngIf="isScreenError" class="alert alert-danger alert-dismissible"> <button class="close" type="button" data-dismiss="alert" (click)='closeAlert()'>×& ...

What could be causing the issue with this jQuery selector for the parent form element?

I created a form that connects a button with an attribute, but it's not hiding the parent("form"). However, when I try this, it works. $($("[editFormContent]").attr("editFormContent")).click(function(e) { e.preventDe ...

Fundamental CSS Selector

I'm encountering an issue with CSS selectors while using Foundation 5: I am having trouble selecting the desired element, which seems strange to me. The specific problem I am facing is related to changing the background-color of the "Right Button Act ...

Is it possible to adjust the default zoom level of my website by utilizing HTML or CSS?

After creating my website with a 50% zoom setting, I've encountered an issue where it appears too zoomed in for users whose browser default is set at 100%. How can I fix this problem? I attempted to use *{zoom:50%} in CSS to set the default zoom leve ...

Enhance your MUI treeview by incorporating stylish connecting borders

I've been trying to add borders that connect to the nodes in the mui treeview, but I'm having difficulty with not having a vertical border when it's the last leaf node. It currently looks like this: See border example here. However, it sh ...

Exciting effects activated by hovering on a button

I'm attempting to create an animated effect by having a rectangle expand over the button when hovered by the user. However, I am unsure how to achieve this and have hit a roadblock. Here's what I want: There's a button. There's a rect ...

"Owlcarousel Slider: A beautiful way to showcase

Currently, I am working on a project that involves implementing a slider. Since I lack expertise in JavaScript, I opted to use a plugin called owlcarousel. The challenge I'm encountering relates to the sizing of the container for the items. I'm ...

What could be causing the shift from querySelector to querySelectorAll to result in an undefined outcome?

When I switched from using querySelector to querySelectorAll, I encountered an issue where it returned undefined. I have double-checked the spelling of the class names and they are correct. This led me to believe that there might be something else overlook ...