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

Is there a way to arrange my bootstrap navigation tabs vertically on my mobile device?

I have a group of five bootstrap navigation tabs that are evenly spaced and visually appealing on desktop screens. However, when viewed on a mobile device, the text within the tabs becomes cramped together. How can I make the tabs stack vertically on mobil ...

Incorporate an ASP table within an ASP table to enhance data organization and presentation

Is it possible to create a dynamic ASP.NET table structure similar to the one demonstrated below by nesting tables inside table cells? <table> <tr> <td>[Image]</td> <td> <table> <tr> ...

Is Javascript the best choice for managing multiple instances of similar HTML code?

I am currently facing the challenge of dealing with a lengthy HTML page consisting of around 300 lines of code. The majority of the code involves repetitive forms, each identical except for the ID (which varies by number). Is it considered appropriate or ...

Enhancing HTML table interactivity: Updating class of cells upon hover

While attempting to update the class on hover, I noticed that the class changes from 1 to hovering cells However, I want the class to change from 1 to currently hovered cells If moving the mouse from 1 to 10 and then to 2, the currently hovered cells wil ...

Can you explain the guidelines for overlapping CSS media queries?

When it comes to spacing out media queries accurately to prevent overlap, how should we approach it? Let's examine the following code as an example: @media (max-width: 20em) { /* styles for narrow viewport */ } @media (min-width: 20em) and (max ...

Distinguishing Features of HTML, Canvas, and WebGL

Is there a way to draw images in WebGL with the same quality as HTML, even when downscaled? I've noticed that scaling down images in WebGL results in poor quality compared to HTML. After reading about 'Handling High DPI (Retina) displays in WebGL ...

Margins and borders with a negative impact

I have a collection of images that I am trying to stack using negative margins. However, because stacking them without defined boundaries can lead to visual confusion, I thought about adding borders around each image. Surprisingly, while the images are sta ...

Is there a way to achieve horizontal alignment for the Twitter and Facebook buttons on my page?

By utilizing the html code provided, I successfully incorporated Twitter and Facebook "Like" buttons into my website. Initially, they were stacked vertically, which resulted in excessive use of vertical space. To optimize space usage, I decided to align th ...

How can I retrieve the index of a v-for loop within a function in Vue.js HTML?

How can I splice the array from the fields in HTML Vue JS if the status is true? I also need to pass the index value to a function. Is this possible? The error I am encountering is: Uncaught ReferenceError: index is not defined at Object.success (80 ...

Initial button click fails to trigger onclick event

After researching and reading various articles on the issue, I have tried several solutions but nothing seems to work. The problem occurs when the button is clicked for the first time - nothing happens. It is only after clicking the button a second time th ...

What is the best way to access dropdown sub-menu options from a complex multi-level navigation bar

Struggling to figure out how to open my dropdown sub-menu using CSS. Hoping to make it so the user can open it by hovering over the corresponding tag. I attempted to use #lablinksDD to trigger the opening of #ddSbMenu when hovered over #menuDD with #labLin ...

Navigation Menu Spanning Across Full Width of All Screens

Hello! I am currently working on designing a responsive menu using HTML5 and CSS. My approach involves creating a navigation menu for desktop computers and implementing a checkbox with a label to reveal the responsive menu when needed. Here is the code s ...

How can the backgroundImage css in react admin <Login /> be replaced using Material-UI?

I have been refering to the following resources: Learn about customizing login page background in React-Admin: Explore themes customization in Material-UI: https://material-ui.com/customization/components/ Instead of using a preset background, I want to ...

Photo captured by camera is not stored in photo gallery

I am currently working on a basic image upload form that allows users to take photos using their phone camera and upload them. However, I have noticed that the pictures taken this way are not being saved to the gallery. Is there something missing in the H ...

Designate the preferred location for requesting the desktop site

Is there a way to specify the location of the 'Request desktop site' option? Here is the code I am currently using: var detector = new MobileDetect(window.navigator.userAgent); if(detector.mobile() != null || detector.phone() != null || det ...

Disable body scrolling on mobile devices in native browsers

When using the native browser on Samsung Galaxy S5 / S6, the following CSS code: body { overflow: hidden; } does not successfully prevent the body from scrolling. Is there a way to work around this issue? Edit: As mentioned below, one workaround is t ...

Ways to isolate and limit the application of CSS in React and Gatsby to specific pages instead of having it affect the global scope

At the moment, I'm working on integrating a keen-slider library(https://www.npmjs.com/package/keen-slider). To install it, we have to include import 'keen-slider/keen-slider.min.css'. This essentially adds the CSS globally to our project. Ho ...

Blurring the background creates an "inset shadow" problem when transitioning

Problem: Strangely, when using Backdrop-filter: blur(Xpx); during a transition as shown below, unexpected "inset Shadows" mysteriously appear until the end of the transition. https://i.stack.imgur.com/uij7F.gif Illustration of the Issue Using jsfiddle Sp ...

AngularJS Bootstrap CSS implementation for Hand Cursor Grab

Is there a way to ensure the cursor is always a hand / grab for sortable containers in AngularJS & Bootstrap? What specific HTML modification would achieve this change? <div ui-sortable="sortableOptions" ng-model="responses" class="container-f ...

Can CSS be used to generate a grid or error image in Internet Explorer?

Can an image like this be created using CSS in Internet Explorer? I attempted to use filter: progid:DXImageTransform.Microsoft.gradient, but it was unsuccessful. ...