Styling with CSS: Making a div's height fit within another div, including padding

Looking at the image, there seems to be a div with a width of 100% and no defined height. Instead, the height adjusts based on the content, with top and bottom padding in percentages. The issue arises when trying to place navigation buttons on the right side that need to be perfectly aligned vertically in the middle. I've shared a Fiddle showcasing my attempt, but the alignment isn't consistent across all scenarios. What would be the best approach to achieve optimal alignment?

HTML

<div class="title">Title
    <ul id="bullets">
      <li></li>
      <li></li>
    <ul>
</div>

CSS

.title {
    width:100%;
    background:#365F95;
    background-size: contain;
    font-size:130%;
    color:#FFF;
    padding:5% 0;
    text-align:center;
    position:relative;
}
.title ul {
    position:absolute;
    right: 0;
    top: 0%;
}
.title ul li {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 10px;
    background: #FFF;
    box-shadow: inset 0 1px 3px black, 0 0 1px 1px #202020;
    margin: 0 2px;
}

http://jsfiddle.net/HJLEe/2/

Answer №1

check out this snippet demonstrating how to vertically center a list

position: absolute;
top: 50%;
margin-top: - half-height;

Answer №2

While not the most optimal solution, it does get the job done.

HTML:

<div class="title">Title
    <ul id="bullets">
        <li></li>
        <li></li>
    <ul>
</div>

CSS:

.title {
    width:100%;
    background:#365F95;
    background-size; cover;
    font-size:130%;
    color:#FFF;
    padding:5% 0;
    text-align:center;

}
.title ul {
position:relative;
float: right;
margin-top: 0px;
}
.title ul li {
    display: inline-block;
    width: 10px;
height: 10px;
    border-radius: 10px;
    background: #FFF;
    box-shadow: inset 0 1px 3px black, 0 0 1px 1px #202020;
    margin: 0 2px;
}

JSFIDDLE

Answer №4

give this a shot CSS

.header ul {
position: fixed;
right: 10px;
top: 5%;
padding-top: inherit;
margin: 0;
}

Answer №5

If you want to add some space using a percentage margin, here's an example for you...

.header ul {
    position: relative;
    left: 0;
    bottom: 0%;
    vertical-align: middle;
    margin-bottom: 10%;
}

Answer №6

One alternative is to utilize vertical-align:middle along with a pseudo element to maintain the height.

See it in action here: http://jsfiddle.net/HJLEe/13/

Additional code that should be included:

.title ul {
    position:absolute;
    right: 0;
    top: 0;
    bottom: 0;
}

/* No changes made to LI styles */

.title ul:before {
    content: '';
    display: inline-block;
    height: 100%; /* ensures line height matches container height */
    vertical-align: middle; /* vertically aligns center with text's middle, including bullets */
}

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

Problem with HTML relative paths when linking script sources

Having trouble with a website I constructed using Angular. The problem lies in the references to javascript files in index.html. The issue is that the paths in the HTML are relative to the file, but the browser is searching for the files in the root direct ...

Ways to configure an Express.js server file with the router path set as the root directory

I have a directory structure set up like this root private js css index.html app.js privateRouter.js ... Within my index.html, I am referencing the js and css files using relative paths: <link rel="stylesheet" href="css/index.css" ...

Has anyone figured out the issue with Uplodify? It suddenly ceased to function

For some time now, I've been using uplodify without any issues. Unfortunately, as of yesterday, it suddenly stopped working. I'm at a loss as to what might have caused this sudden change. Could someone please offer me some assistance? I've ...

Utilizing spans to adjust the formatting of text within list items

When it comes to shaping text inside li elements, I've encountered a few issues. One of the problems is that the float-right divs aren't floating to the top right corner but instead leaving a space at the top. Additionally, the text isn't &a ...

Troubleshooting resizing images in React using Material UI's useStyles

When attempting to resize an image using React, I am encountering a problem where adjusting the height and width of the tag with useStyles does not reduce the size of the image but instead moves it around on the page. Here is the code snippet: import { ma ...

The way images appear can vary between desktop and mobile devices

I am in the process of creating a photography website with a simple goal of displaying images down the page one after another. However, I am encountering issues with uniform display across various desktop and mobile platforms. The site appears perfect on i ...

Adjusting diagram size based on screen width using Bootstrap and jQuery

I recently created a diagram that adjusts its size according to the screen width. However, when I implemented this code on my page, I encountered an issue where the final circle or glyph would drop to the next line instead of staying on the same line as in ...

What could be causing the extra space at the top of my div element?

My goal is to accomplish the following: However, there appears to be an unnecessary gap above the Social icons div. You can view the live page at This is my current markup:- <header> <img src="images/home-layout_02.jpg" alt="Salem Al Hajri Logo ...

How can I customize the color of Chrome's default date and time picker to a different shade of blue?

Is there a way to customize the color of the blue buttons, text, and date indicator in the native datetime picker for Chrome using CSS? https://i.stack.imgur.com/eZSaE.png ...

Instructions for changing the background of a specific area to gray

Could use some assistance in changing the red area to grey, tried numerous solutions but can't figure it out. Any help would be highly appreciated! Looking to change the background to a light grey excluding the top bar and navigation. Image Current ...

Modify the background color based on the length of the input in Vue

Can you change the background color of the initial input field to green if the value of the Fullname input field is greater than 3 characters? See below for the code: <div id="app"> <input type="text" v-model="fullname" placeholder="Enter Full ...

Is there a way to automatically redirect a webpage based on the URL that is entered

Looking for a solution where a page checks a typed link upon loading, such as: www.test.com/myphp.php=10 If it detects a specific number, it should redirect to another link. For example: Upon finding www.test.com/myphp.php=10, it redirects to www. ...

How come my CheerpJ web page displays the loading screen but fails to function properly?

I have recently started using CheerpJ and went through the getting started tutorial. Despite following all the steps, I encountered an issue where my webpage only displayed the CheerpJ loading screen (the rotating circle) without running my app. Can anyo ...

Implement a sequence of animations within a div using jQuery

My goal is to create an animation effect for each div element within a row when the user scrolls down to that specific point on the page. The layout consists of three <div> elements in the same row. The structure of the HTML code is as shown below: ...

Can someone assist me with positioning an HTML child element to appear behind a parent element using CSS?

I need help displaying an HTML element behind a fixed header in my webpage. I want the div element (which is a child of the header) to appear beneath the header like a dropdown menu. However, I've tried adjusting the z-index and position properties wi ...

Text reveal animation in Framer Motion is not functioning as expected

I'm having trouble locating the issue in my code, and the text reveal feature isn't working. Interestingly, a simple animation works fine, indicating that there's no problem with the import or library: import { motion } from ...

Angular is unable to iterate through a collection of ElementRef instances

My list of ElementRef contains all my inputs, but adding listeners to them seems to indicate that textInputs is empty even though it's not. @ViewChildren('text_input') textInputs!: QueryList<ElementRef>; ngAfterViewInit(): void { ...

What is the best way to hide the background of an extension's HTML?

Currently, I am working on developing a Chrome extension for a university project. However, I am facing challenges in making the background or body of the extension's HTML completely transparent to achieve a cleaner interface. The issue specifically l ...

Arrange divs in a stack fashion

Currently, I have a table-styled three-column set of divs. I would like these columns to stack vertically on mobile devices. I think this can be achieved using the float method, but the titles and descriptions for each column are in separate rows (divs), ...

Jquery Parallax causing scrolling problems across different browsers

Could you please take a look at the following link in both Chrome and Firefox? I am encountering some unusual issues. Primary Concern The scrolling function works smoothly in Firefox, but it seems to be malfunctioning in Chrome. Secondary Problem Whe ...