CSS: Vertically aligning inline-block element in a line of text - css

I have an inline-block element put inside a line of text:
.icon element has vertical-align: middle;, which results in this picture:
As you can see, the icon is not aligned with the middle of the text, it is a little bit lower, whilst the text alignment looks OK. The .button-content has line-height equal to the height of the parent. I tried to wrap the text elements around the icon:
And got this result:
The coin went up a little relatively to the text, whereas the whole line went down a pixel or two.
What is the proper way to align an inline-block element inside a lign of text? And what are these text chunks and how do they behave? Do they have display:inline; or something, because I can't see their properties in the DevTools?

Vertical-align doesn't work like you would think it would. It's used in HTML tables, but doesn't work in divs. It's been a pain for a while. Luckily, nowadays you can achieve this easily with flexbox.
To achieve this, wrap your two bits of copy in individual span elements, so your structure looks like:
<div class="button-content">
<span>buy for</span>
<div class="icon"></div>
<span>1000</span>
</div>
Then your css should look like this:
.button-content{
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
width: 130px;
}
Or if you can't support flexbox, your .button-content can be set to display: table-cell; and the vertical-align: middle; should work.
I strongly recommend flexbox.

Unless your span elements are styled, the result will be the same with or without them.
With vertical-align: middle; position: relative; top: -1px; you can get some nice results.
.icon {
display: inline-block;
width: 10px;
height: 10px;
background: blue
}
.top {
vertical-align: top;
}
.bottom {
vertical-align: bottom;
}
.middle {
vertical-align: middle;
}
.moveup {
position: relative;
top: -1px;
}
<div class="button">
<span>Buy for</span>
<div class="icon"></div>
<span>1000</span>
</div>
<div class="button">
Buy for
<div class="icon"></div>
1000
</div>
<hr>
<div class="button">
Buy for
<div class="icon top"></div>
1000 vertical-align: top;
</div>
<div class="button">
Buy for
<div class="icon bottom"></div>
1000 vertical-align: bottom;
</div>
<div class="button">
Buy for
<div class="icon middle"></div>
1000 vertical-align: middle;
</div>
<hr>
<div class="button">
Buy for
<div class="icon middle moveup"></div>
1000 vertical-align: middle; top: -1px;
</div>

Simple answer
I have not seen a simple answer yet, so I'll just post mine:
.icon_tpye-gold {
vertical-align: -5px; /* << or another value to center the inline element vertically */
}
A suggestion: be consistent with class names (so icon_type-gold is clearer when named icon_type_gold or icon-type-gold, this looks less sloppy)

The icon actually IS aligned vertically, but relating to the complete line-height, including the space below the baseline reserved for the descenders of characters like y, g, p etc . (also the y in your button Text)
You can try to add position: relative; and bottom: 3px; (try different values) to that inline-block to move it up.

Related

Align scroll down arrow to the bottom center of a full-screen div in WPBakery Visual Composer

I have a series of full-screen divs in Visual Composer and I want an arrow at the bottom of each one indicating to users they should scroll for more content. I tried absolute positioning on the divs containing the icon with no luck. All I've done is move the icon a few pixels to th
<section class="l-section wpb_row height_full valign_center width_full with_img" id="home">
<div class="l-section-img loaded" data-img-width="1920" data-img-height="809">
</div>
<div class="l-section-h i-cf">
<div class="g-cols vc_row type_default valign_top">
<div class="vc_col-sm-12 wpb_column vc_column_container">
<div class="vc_column-inner">
<div class="wpb_wrapper">
<div class="w-image align_center" id="mainlogo">
<div class="w-image-h"><img src="logo.png" class="attachment-full size-full">
</div>
</div>
<div class="ult-just-icon-wrapper">
<div class="align-icon" style="text-align:center;">
<a class="aio-tooltip" href="#whatis">
<div class="aio-icon none " style="display:inline-block;">
<i class="Defaults-chevron-down"></i>
</div>
</a>
</div></div></div></div></div></div></div>
</section>
Existing CSS:
.aio-icon.none {
display: inline-block;
}
.aio-tooltip {
display: inline-block;
width: auto;
max-width: 100%;
}
.vc_column-inner {
display: flex;
flex-direction: column;
flex-grow: 1;
flex-shrink: 0;
}
.wpb_column {
position: relative;
}
.vc_column_container {
display: flex;
flex-direction: column;
}
.vc_row {
position: relative;
}
.l-section-h {
position: relative;
margin: 0 auto;
width: 100%;
}
The icon itself is the Defaults-chevron-down.
Do you have an idea how to position that icon properly?
I also struggled a little with this. But there is a rather quick and dirty fix for this:
Just put another row below the full height row. Place your icon there and give this element a top margin of i.e. -200px.
For some strange reason the rather logical approach to put the icon in the full height row itself and to position it absolute to the bottom is not properly supported by the source generated from WPB.
I had this issue this week. The way I resolved it was added the icon in that row/section (in my case a single image element with a custom link to a .svg) and added a class to it.
The CSS for the class was then:
position:absolute;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
text-align: center;
margin-top:-30px;
(I added a negative margin top as I noticed the icon was cutting of a little on my Google Pixel phone with the fixed bottom bar so that pulled it up a little.)

How can a vertical align a div with a background color inside a div so the background fills up?

Updated question:
I have a bunch of DIV, they are part of a menusystem and are displayed using display:inline-box. Each div contains text. I want to have different background-color on the different DIVs and that the background fills up the whole height of the div and I also want the text to be vertically aligned along all the div. The fiddle below shows that the background color is only used around the text.
Old text:
I've spent hours on this. I found the vertical alignment quite easy (for example here: How to vertically align div inside another div without display:table-cell) but cannot figure out how i i can fill the whole div with the background color.
My example code is on fiddle: http://jsfiddle.net/joche/s7beksLt/
<div class="DivParent">
<div class="DivWhichNeedToBeVerticallyAligned bg1">
one line
</div>
<div class="DivWhichNeedToBeVerticallyAligned bg2">
<p>one line</p>
</div>
<div class="DivWhichNeedToBeVerticallyAligned bg3">
<p>one line</p>
<p>two line</p>
</div>
<div class="DivHelper"></div>
</div>
css:
.DivParent {
height: 100px;
border: 1px solid lime;
white-space: nowrap;
background-color:#deadad;
}
.DivWhichNeedToBeVerticallyAligned {
display: inline-block;
vertical-align: middle;
white-space: normal;
}
.bg1 {
background-color:#ffaaff;
}
.bg2 {
background-color:#ffffff;
}
.bg3 {
background-color:#ffffa9;
height:100%
}
.DivHelper {
display: inline-block;
vertical-align: middle;
height:100%;
}
I looked at your JSfiddle, and based on your code and question it's a little misleading. Especially since the code at the fiddle is not the code you posted in your question.
So you are trying to fill each div "cell" with a different background color? If so, those "cells" are of the .DivParent class. The internal divs (which you have labeled .bg1, .bg2, .bg3) are simply composed of the text itself - these divs only extend to the boundaries of the text they include (plus any margins, padding, etc.) The .DivParent is actually the entire "cell". See this image to see what I mean: http://i.imgur.com/67y3iWV.png
So all you need to do is apply the classes .bg1, .bg2, etc. to the parent classes. Here is my fiddle with each "cell" a different background color: http://jsfiddle.net/Arkatect/8vmp0124/
Notice in the HTML that the separate bg classes are on the parents, not the divs that just have the text:
<div class="DivParent bg2">
<div class="DivWhichNeedToBeVerticallyAligned">
<p>Two</p>
<p>Lines</p>
</div><div class="DivHelper"></div>
</div>
I hope this is what you were looking for.
Take a look at this one i made for you without .table-cell :http://jsfiddle.net/csdtesting/sos5sxkj/
.DivParent {
height: 100px;
border: 1px solid lime;
white-space: nowrap;
background: gray;
text-align: center;
position: relative;
}
.DivWhichNeedToBeVerticallyAligned {
position: absolute;
top: 50%;
left: 50%;
width: 100px;
height: 100px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background: red;
}
.DivHelper {
display: inline-block;
vertical-align: middle;
height: 100%;
}
<div class="DivParent">
<div class="DivWhichNeedToBeVerticallyAligned">one line</div>
<div class="DivHelper"></div>
</div>
<div class="DivParent">
<div class="DivWhichNeedToBeVerticallyAligned">
<p>Two</p>
<p>Lines</p>
</div>
<div class="DivHelper"></div>
</div>

How to center three span3

I'm using Twitter Bootstrap and I'm trying to create a grid with centered rows of three span3 divs.
I have tried to achieve this by wrapping the three span3s in a centered span9, but that doesn't seem to work.
My problem is that the divs don't center correctly and as I'm new to HTML and CSS I would really need some help. Thanks!
HTML
<div class="row">
<div class="span9 center">
<div class="span3">
<p>THUMBNAIL1</p>
</div>
<div class="span3">
<p>THUMBNAIL2</p>
</div>
<div class="span3">
<p>THUMBNAIL3</p>
</div>
</div>
</div>
etc.
CSS:
.center {
float: none;
margin-left: auto;
margin-right: auto;
align: center;
}
Instead of using:
align: center;
Use:
text-align: center;
align is not a css rule though I guess that you got it from the syntax of align as an attribute on the tag
You could also put them inside and element with position relative and then inside have them with:
position: absolute;
left: 50%;
transform: (0, 50%);
You can also use this:
.span3 {
display: inline-block;
clear: both;
}
.span9 {
text-align: center;
}
Fiddle Demo

How do I right align div elements?

The body of my html document consists of 3 elements, a button, a form, and a canvas. I want the button and the form to be right aligned and the canvas to stay left aligned. The problem is when I try to align the first two elements, they no longer follow each other and instead are next to each other horizontally?, heres the code I have so far, I want the form to follow directly after the button on the right with no space in between.
#cTask {
background-color: lightgreen;
}
#button {
position: relative;
float: right;
}
#addEventForm {
position: relative;
float: right;
border: 2px solid #003B62;
font-family: verdana;
background-color: #B5CFE0;
padding-left: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript" src="timeline.js"></script>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" />
</head>
<body bgcolor="000" TEXT="FFFFFF">
<div id="button">
<button onclick="showForm()" type="button" id="cTask">
Create Task
</button>
</div>
<div id="addEventForm">
<form>
<p><label>Customer name: <input></label></p>
<p><label>Telephone: <input type=tel></label></p>
<p><label>E-mail address: <input type=email></label></p>
</form>
</div>
<div>
<canvas id="myBoard" width="600" height="600" style="background:lightgray;">
<p>Your browser doesn't support canvas.</p>
</canvas>
</div>
</body>
</html>
Floats are okay, but problematic with IE 6 & 7.
I'd prefer using the following on the inner div:
margin-left: auto;
margin-right: 0;
See the IE Double Margin Bug for clarification on why.
You can make a div that contains both the form & the button, then make the div float to the right by setting float: right;.
Old answers. An update: use flexbox, pretty much works in all browsers now.
<div style="display: flex; justify-content: flex-end">
<div>I'm on the right</div>
</div>
And you can get even fancier, simply:
<div style="display: flex; justify-content: space-around">
<div>Left</div>
<div>Right</div>
</div>
And fancier:
<div style="display: flex; justify-content: space-around">
<div>Left</div>
<div>Middle</div>
<div>Right</div>
</div>
You can use flexbox with flex-grow to push the last element to the right.
<div style="display: flex;">
<div style="flex-grow: 1;">Left</div>
<div>Right</div>
</div>
Note that while this answer is not wrong, it is very outdated methodology written in 2015
Other answers for this question are not so good since float:right can go outside of a parent div (overflow: hidden for parent sometimes might help) and margin-left: auto, margin-right: 0 for me didn't work in complex nested divs (I didn't investigate why).
I've figured out that for certain elements text-align: right works, assuming this works when the element and parent are both inline or inline-block.
Note: the text-align CSS property describes how inline content like text is aligned in its parent block element. text-align does not control the alignment of block elements itself, only their inline content.
An example:
<div style="display: block; width: 80%; min-width: 400px; background-color: #caa;">
<div style="display: block; width: 100%">
I'm parent
</div>
<div style="display: inline-block; text-align: right; width: 100%">
Caption for parent
</div>
</div>
Here's a JS Fiddle.
If you have multiple divs that you want aligned side by side at the right end of the parent div, set text-align: right; on the parent div.
Do you mean like this? http://jsfiddle.net/6PyrK/1
You can add the attributes of float:right and clear:both; to the form and button
Maybe just:
margin: auto 0 auto auto;
Simple answer is here:
<div style="text-align: right;">
anything:
<select id="locality-dropdown" name="locality" class="cls" style="width: 200px; height: 28px; overflow:auto;">
</select>
</div>
Sometimes float: left leads to design problems, for that cases you can use display flex like this:
.right {
display: flex;
justify-content: flex-end;
margin-left: auto;
margin-right: 0;
}
<div>
<div class="right">Right</div>
</div>
If you are using bootstrap, then:
<div class="pull-right"></div>
One way could be setting a parent div for those elements that need to be pulled right and do the rest like the way shown in the the example below to have them right-aligned:
.parent-div {
display: flex;
float: right;
}
/*Below: child-div styling is not needed for this purpose! this is just for demonstration:*/
.child-div {
text-align: center;
background-color: powderblue;
margin: auto 10px;
height: 100px;
width: 50px;
}
<div class="">CANVAS div </div>
<div class="parent-div">
<div class="child-div">child 1</div>
<div class="child-div">child 2</div>
<div class="child-div">...</div>
<div class="child-div">child n</div>
</div>
If you don't have to support IE9 and below you can use flexbox to solve this: codepen
There's also a few bugs with IE10 and 11 (flexbox support), but they are not present in this example
You can vertically align the <button> and the <form> by wrapping them in a container with flex-direction: column. The source order of the elements will be the order in which they're displayed from top to bottom so I reordered them.
You can then horizontally align the form & button container with the canvas by wrapping them in a container with flex-direction: row. Again the source order of the elements will be the order in which they're displayed from left to right so I reordered them.
Also, this would require that you remove all position and float style rules from the code linked in the question.
Here's a trimmed down version of the HTML in the codepen linked above.
<div id="mainContainer">
<div>
<canvas></canvas>
</div>
<div id="formContainer">
<div id="addEventForm">
<form></form>
</div>
<div id="button">
<button></button>
</div>
</div>
</div>
And here is the relevant CSS
#mainContainer {
display: flex;
flex-direction: row;
}
#formContainer {
display: flex;
flex-direction: column;
}
display: flex;
justify-content: space-between;
hasnt been mentioned. if there are 2 elements (even if one is an empty div) it will place one on the left and one on the right.
<div style="display: flex; justify-content: space-between;">
<div id="emptyDiv"></div>
<div>I'm on the right</div>
</div>
You can simply use padding-left:60% (for ex) to align your content to right and simultaneously wrap the content in responsive container (I required navbar in my case)
to ensure it works in all examples.
You can do it easy by just add this css:
(Works in IE11)
<div>
<!-- Subtract with the amount of your element width -->
<span style="margin-left: calc(100vw - 50px)">Right</span>
</div>
I know this is an old post but couldn't you just use <div id=xyz align="right"> for right.
You can just replace right with left, center and justify.
Worked on my site:)

css - oversized div within small div

I need to make a menu that looks like this:
The upper entries need to have a right margin of (lets say) 20px.
Problem arises, when I add the sub-menus, especially like the red one with the «large Menu-Entry». The top menu needs to stay in place and all the sub-menus need to be centered under that top menu. But either the top-entry is enlarged (which makes the green part shift to the right) or the sub-entries aren't positioned at the center of the top-entry...
As the menu-entries are dynamic, I can't predict how wide they are and thus I can't apply any math.
Also - the sub-entries are only visible, if the user is on the according page (means - the green part only shows «Menu1» if the user is on the red page)
I «could» use some javascript to do it after the page loaded, but I'm trying to avoid that.
I tried all sorts of stuff, including negative margins and whatnot - but nothing seems to work... Any ideas?
[edit]
some html here - tried to fumble around like crazy with no results (except the one from Brad, but that one doesn't work with IE)
<div class="center">
<div class="menu-container">
<div class="menu-title">Title 1</div>
<div class="menu-items">
Testomat<br />
Yo, this is a long text
</div>
</div>
<div class="menu-container">
<div class="menu-title">Title 1</div>
<div class="menu-items">
Testomat<br />
Yo, this is a long text
</div>
</div>
</div>
CSS:
.menu-container{
width: 100px;
float: left;
}
.menu-items, .menu-title{
text-align: center;
}
If you don't care about IE: Have you tried using display:table-cell?
You could try something like:
<div class="menu-container">
<div class="menu-title">
Menu1
</div>
<div class="menu-items">
<div class="menu-item">large menu item</div>
<div class="menu-item">sub</div>
<div class="menu-item">sub</div>
</div>
</div>
With CSS:
.menu-container {
display : table;
width: 100px;
}
.menu-title, .menu-items {
display : table-cell;
width: 100%;
text-align: center;
}
Naturally, the content within the table-cells will wrap to 100px.
My first approach uses different html mark-up to your own, but gives the visual effect you you're looking for with, perhaps, a slight increase in semantics:
html:
<dl>
<dt>Title One</dt>
<dd>Testomat</dd>
<dd>Yo, this is a long text</dd>
</dl>
<dl>
<dt>Title Two</dt>
<dd>Testomat</dd>
<dd>Yo, this is a long text</dd>
</dl>
css:
dl {
width: 100px;
float: left;
position: relative;
text-align: center;
color: #0f0;
}
dl:nth-child(odd) {
color: #f00;
}
Demo of the above at JS Fiddle.
Edited, to add the following:
On looking at your posted mark-up, and applying the css:
.menu-container {
width: 100px;
float: left;
text-align: center;
overflow: hidden;
color: #0f0;
}
.menu-container:nth-child(odd) {
color: #f00;
}
JS Fiddle demo
I'm not sure why you're experiencing difficulties. Admittedly, at the moment, I'm only able to test on Chrome and IE 8 (Win XP), but the above seems to work. Am I missing something important in your problem description?

Resources