The texts baseline of inline-block is not aligning at the bottom.
How do you align the radical at the bottom of inline-block instead of text bottom (baseline)?
Currently i have this
The goal is this
Current Code
https://jsfiddle.net/x9ugahdb/1/
.parent {
height: 200px;
}
.radical {
border: 1px solid #000;
display: inline-block;
vertical-align: bottom;
}
.radicalData {
height: 200px;
}
<div class="parent">
<span class="radical">√</span>
<span class="radical">hello</span>
<span class="radicalData"></span>
</div>
You could treat the square root mark √ as content via an ::after selector. From there use position: absolute along with top: 6px to layer it over the bordered span.
HTML:
<div class="parent">
<span class="radical2"></span>
<span class="radical">hello</span>
<span class="radicalData"></span>
</div>
CSS:
body {
font-size:28.8px;
font-family:"Symbola";
vertical-align:bottom;
}
.parent{
height:200px;
}
.radical {
border:1px solid #000;
display:inline-block;
vertical-align:bottom;
}
.radical2 {
border:1px solid #000;
display:inline-block;
height: 33px;
position: relative;
vertical-align:bottom;
width: 20px;
}
.radical2::after {
content: "√";
height: 33px;
position: absolute;
margin-bottom: -10px;
top: 7px;
z-index: 1;
}
.radicalData {
height:200px;
}
Working Fiddle here.
Add overflow:hidden to the second inline-block to move its baseline to the bottom then keep the default alignment:
.parent {
height: 200px;
}
.radical {
display: inline-block;
}
.radicalData {
height: 200px;
}
<div class="parent">
<span class="radical">√</span>
<span class="radical" style="overflow:hidden;">hello</span>
<span class="radicalData"></span>
</div>
You can also adjust the line-height like this:
.parent {
height: 200px;
}
.radical {
display: inline-block;
border:1px solid;
vertical-align:bottom
}
.radicalData {
height: 200px;
}
<div class="parent">
<span class="radical" style="line-height:12px;">√</span>
<span class="radical">hello</span>
<span class="radicalData"></span>
</div>
Related
I'm trying to float two elements of different height, with the shorter one being middle centered.
If I use inline-block instead of float the vertical centering works correctly, but the 'middle' div doesn't stretch to fit.
float example: http://jsfiddle.net/jonofan/r3pejgud/3/
inline-block: http://jsfiddle.net/jonofan/87kwpuxa/
Also interested to hear if people think should be going about this layout a different way entirely.
EDIT: I don't see this to be a duplicate of this question because my current code doesn't use table display. It just so happens that 'use table display' is the best answer in this case.
.header {
width: 600px;
border: 1px solid black;
display: inline-block;
}
.header img {
width: 50px;
float: left;
}
.middle {
position: relative;
top: 50%;
transform: translateY(-50%);
border: 1px solid gray;
height: 20px;
overflow: hidden;
}
.middle .itemheading {
position: absolute;
bottom: 0;
left: 0;
font-size: 1.8em;
}
.middle .itemdate {
position: absolute;
bottom: 0;
right: 0;
}
<div class='header'>
<img src='http://i.imgur.com/J2HToiP.jpg' />
<div class='middle'>
<span class='itemheading'>Heading Text</span>
<span class='itemdate'>Wednesday 01 July 2015</span>
</div>
</div>
Not perfect but you don't have to resort to absolute positioning. Use display: table-cell; instead.
Not sure how the border for .middle is supposed to work.
<div class='header'>
<div class="img-wrap">
<img src='http://i.imgur.com/J2HToiP.jpg' />
</div>
<div class='middle'>
<span class='itemheading'>Heading Text</span>
<span class='itemdate'>Wednesday 01 July 2015</span>
</div>
</div>
.header {
width: 600px;
border: 1px solid black;
}
.header img {
width: 50px;
}
.header .img-wrap {
display: table-cell;
vertical-align: middle;
}
.header .middle {
width: 100%;
display: table-cell;
vertical-align: middle;
border: 1px solid gray;
}
.itemdate {
float: right;
}
http://jsfiddle.net/87kwpuxa/2/
I'm trying to put two spans in a div, one on each side, and both aligned to the bottom of the div, without using absolute positioning (since that ignores padding, etc and I always feel bad after resorting to it). The text in the right span is taller than in the left span. If I use vertical-align to position them, it doesn't take affect if they are both floated, however without them both being floated, they will not be horiziontally aligned properly. I don't have any guarantees on which of the two spans will have more text in it.
http://jsfiddle.net/gsvfn07f/
<div class="outer">
<div class="inner">
<span class="left">left</span>
<span class="right">right</span>
<div class="clearfix"></div>
</div>
</div>
.outer {
width: 40%;
height: 200px;
border: 1px solid red;
}
.inner {
border: 1px solid green;
padding: 5px;
}
.left {
float: left;
display: inline-block;
vertical-align: baseline;
}
.right {
float: right;
font-size: 2em;
}
.clearfix {
clear: both;
}
You can use line-height to get margin from top for your text.
This code seems to do what you want:
.outer {
width: 40%;
height: 200px;
border: 1px solid red;
}
.inner {
height: 35px;
width: 100%;
border: 1px solid green;
}
.left {
line-height: 48px;
float: left;
width: 50%;
}
.right {
display: block;
float: right;
width: 50%;
text-align: right;
font-size: 2em;
}
<div class="outer">
<div class="inner">
<span class="left">left</span>
<span class="right">right</span>
</div>
</div>
How can I center text 'One' vertically with respect to the parent box '#container' using only CSS 2.1 in the following code without using table ? Text '2.1' and text '2.2' also need to be centered vertically in the green boxes.
<div id="container">
<div id="col-1">
One
</div>
<div id="col-2">
<span>2.1</span>
<span>2.2</span>
</div>
</div>
<style>
#container {
position: absolute;
height: 100px;
border: 1px solid crimson;
vertical-align: text-bottom;
}
#col-1, #col-2 {
display: inline-block;
height: 100%;
}
#col-2 span {
display: block;
height: 48px;
line-height: 48px;
border: 1px solid green;
}
</style>
http://jsfiddle.net/wa3r7d6j/
Just change in css:
#col-1, #col-2 {
display: inline-block;
vertical-align:middle;
}
remove : height:100%; from #col-1, #col-2
Check Fiddle here.
Here is a fiddle of the below:
.filterDivActual, #filterSeparator {
display: inline-block;
}
.filterDivActual {
border: 2px solid grey;
width: 15%;
height: 50px;
line-height: 50px;
color: grey;
position: relative;
}
#filterSeparator {
height: 50px;
width: 5px;
background-color: black;
}
<div id='filterDiv'>
<div class='filterDivActual'>Top</div>
<div class='filterDivActual'>New</div>
<div id='filterSeparator'></div>
<div class='filterDivActual'>Today</div>
<div class='filterDivActual'>Yesterday</div>
<div class='filterDivActual'>None</div>
</div>
What I want is for the #filterSeparator to be aligned with the other divs.
For some reason, all the other divs are below the #filterSeparator.
If I put text inside #filterSeparator, then it works.
Is there a way for me to get it to work without placing any text inside #filterSeparator?
fiddle
For inline / inline-block elements, use the vertical-align property:
.filterDivActual, #filterSeperator {
display: inline-block;
vertical-align: middle ; /* or some other value: */
}
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
I don't know why it does this but you can fix it by using float:left; instead of display:inline-block
Putting content in the empty <div> will fix it.
<div id='filterSeperator'> </div>
#filterSeparator:before {
content: ".";
visibility: hidden;
}
.filterDivActual, #filterSeparator {
display: inline-block;
}
.filterDivActual {
border: 2px solid grey;
width: 15%;
height: 50px;
color: grey;
position: relative;
}
#filterSeparator {
height: 50px;
width: 5px;
background-color: black;
}
#filterSeparator:before {
content: ".";
visibility: hidden;
}
<div id='filterDiv'>
<div class='filterDivActual'>Top</div>
<div class='filterDivActual'>New</div>
<div id='filterSeparator'></div>
<div class='filterDivActual'>Today</div>
<div class='filterDivActual'>Yesterday</div>
<div class='filterDivActual'>None</div>
</div>
I want to set three header div's 25% 50% 25% Horizontally, shown in image, have laid them three div's in header div with corresponding css, but the div's are placed vertically shown here.
Have checked with previous answers which I couldn't get a lead, give me a directions please! Thanks
HTML code:
<div id="header">
<div id="header-left" class="div-border">
<h6 align="center">Image holder</h6>
</div>
<div id="header-middle" class="div-border">
<h1 align="center">Dashboard</h1>
</div>
<div id="header-right" class="div-border">
<h6>
<span >
<span style="line-height: 24px;" id="border-around">
<b>Profile | Help | Admin </b>
</span>
</span>
</h6>
</div>
</div>
CSS code:
#header {
background-color:#fecb00;
color:white;
text-align:center;
padding:5px;
}
#header-left {
width: 25%;
}
#header-middle {
width: 50%;
}
#header-right {
width: 25%;
}
.div-border {
border: 2px solid silver;
}
#border-around {
border: 1px solid #000;
}
You have to set box-sizing: border-box to include borders to width calculation, set display: inline-box to display elements inline, vertical-align: middle and eliminate white spacing between divs.
.div-border {
box-sizing: border-box;
display: inline-block;
vertical-align: middle;
}
Fiddle: http://jsfiddle.net/khbv2mac/1/
Your elements are displayed as block elements by default, which means that they will take an entire 'line' to themselves unless floated. You can either float your elements, or display them inline-block.
The accumulated width of your three elements is wider than 100% of the width of the page, because their borders are not included in the width you specify. You can fix this by changing box-sizing.
Note: I have commented out the white-space between your inline-block elements. Because they are displayed inline, space between them will be acknowledged.
body{margin:0}
#header {
background-color:#fecb00;
color:white;
text-align:center;
}
#header-left {
width: 25%;
}
#header-middle {
width: 50%;
}
#header-right {
width: 25%;
}
.div-border {
/* Style changes here */
display:inline-block;
box-sizing:border-box;
border: 2px solid silver;
}
#border-around {
border: 1px solid #000;
}
<div id="header">
<div id="header-left" class="div-border">
<h6 align="center">Image holder</h6>
</div><!--
--><div id="header-middle" class="div-border">
<h1 align="center">Dashboard</h1>
</div><!--
--><div id="header-right" class="div-border">
<h6><span><span style="line-height: 24px;" id="border-around"><b>Profile | Help | Admin </b></span></span></h6>
</div>
</div>
JSFiddle
used to floating element as like this
*{ // add this line
box-sizing:border-box; // add this line
} // add this line
#header {
background-color:#fecb00;
color:white;
text-align:center;
padding:5px;
overflow:hidden; // add this line
}
#header-left {
width: 25%;
float:left; // add this line
}
#header-middle {
width: 50%;
float:left; // add this line
}
#header-right {
width: 25%;
float:left; // add this line
}
.div-border {
border: 2px solid silver;
}
Demo
you can try it using css order property .
code:
<html>
<head>
<style>
#header {
background-color:#fecb00;
color:white;
text-align:center;
padding:5px;
width:100%;
display: -webkit-flex; /* Safari */
display: flex;
}
#header-left {
width: 25%;
-webkit-order:1;
order:1;
}
#header-middle {
width: 49%;
-webkit-order:2;
order:2;
}
#header-right {
width: 25%;
-webkit-order:3;
order:3;
}
.div-border {
border: 2px solid silver;
}
#border-around {
border: 1px solid #000;
}
</style>
</head>
<body>
<div id="header">
<div id="header-left" class="div-border">
<h6 align="center">Image holder</h6>
</div>
<div id="header-middle" class="div-border">
<h1 align="center">Dashboard</h1>
</div>
<div id="header-right" class="div-border">
<h6>
<span >
<span style="line-height: 24px;" id="border-around">
<b>Profile | Help | Admin </b>
</span>
</span>
</h6>
</div>
</div>
</body>
</html>
fiddle : http://jsfiddle.net/invincibleJai/sbuveeLw/