I put together this fiddle
What I have is 2 divs and then one of them has 2 more divs in it. What I am trying to do it properly position label and span within second div
<div id="someotherdiv">
</div>
<div class="reportuserinfo">
<div class="leftdivinfo">
<label>Teacher:</label><span>Teacher Name</span><br/>
<label>District:</label><span>District Name</span><br/>
<label>School:</label><span>School Name</span>
</div>
<div class="rightdivinfo">
<label>Class:</label><span>Class Name</span><br/>
<label>Content:</label><span id="currcontent">Content</span><br/>
<label>Unit:</label><span id="currunit"></span>
</div>
</div>
If you look in fiddle right now label is floating to the right and so is span but they look strange, what I am attempting to accomplish is something like this on both sides of second div:
Teacher: Teacher Name
District: District Name
School: School Name
The way they look right now is
Teacher: Teacher Name
District: District Name
School: School Name
Thanks for your help.
I just added width:30%; in your css class ".reportuserinfo label" and it fixed the issue (fiddle). You need to define width in order to fix the text alignment issue. You can adjust space margins as per your requirements.
Here is how your code looks like to me now :-
.reportuserinfo label {
float: left;;
margin-right: 30px;
font-weight: bold;
text-align: right;
width:30%;
}
You should define a min-width style in your css apart from width:40% to ensure that the text you are trying to write does not scroll if browser width is decreased
.leftdivinfo {
float:left;
min-width: 200px;
width:40%;
background: yellow;
padding-left: 30px;
padding-right: 30px;
}
Have you tried using tables? You could enclose this information in a table, then right-align the text in the left column:
table {
border: none;
}
td:first-child {
text-align: right;
}
td {
padding: 5px;
}
<table>
<tr>
<td>Class Teacher:</td>
<td>John Smith</td>
</tr>
<tr>
<td>District:</td>
<td>Smithsville</td>
</tr>
<tr>
<td>School:</td>
<td>Smith's High School</td>
</tr>
</table>
Try setting width to auto and adding display:inline-block.
.leftdivinfo {
float:left;
width: auto;
background: yellow;
padding-left: 10px;
padding-right: 10px;
display:inline-block
}
Tables might be a better way to make this.
EDIT:
I see you want the colons aligned, add min-width:70px; to the label class.
Related
At my website guestspeaker.earth I have buttons down the right hand side that are clickable to jump to other pages. Each button is a table row, split into two columns, to help me organise and align the contents. The L/H column has an image with a link, the R/H column has a heading and a paragraph containing text, all surrounded by a link anchor. It works but it generates errors because of course you shouldn't have tags within a <a> tag. (There are also problems with using 'width' and 'align', I know.). I'm struggling to come up with an elegant alternative. I don't want to use Java. I'm open to alternatives that don't look exactly like what I've got, if it's not possible to mimic the current layout. Any help greatly appreciated, as I'm a beginner here.
<table class="rightpanel">
<tr class="rightpanellinkbox">
<td class="rightpanellinkbox" width="90"><a href="current-bookings.htm">
<img src="images/lecture-audience.jpg" alt="Current bookings" width="70" align="center"></a>
</td>
<td class="rightpanellinkbox"><a href="current-bookings.htm">
<h3>Current bookings</h3>
<p class="panel">Upcoming talks David is giving around the country</p></a>
</td>
</tr>
</table>
The CSS is:
table.rightpanel {
margin-left:auto;
margin-top: 10px;
text-align: center;
background-color:#000066;
margin-left: 10px;
margin-bottom:35px;
width: 85% }
td.rightpanellinkbox {
padding-top:2px;
border-bottom: 1px solid #0066CC }
tr.rightpanellinkbox { padding-top:2px; }
p.panel {
color: white;
margin-right: 10px;
margin-left: 10px;
text-align:left;
font-size: 75% }
My HTML:
<table style="width:100%;">
<tbody>
<tr style="cursor:pointer; border-bottom:1px solid #ACACAC; height:60px;">
<td style="text-align:right; vertical-align:middle; padding:10px 10px 10px 0px;">
<span style="color:#F87E20;">Copy</span>
<div style="display:inline; color:#ACACAC;"> | </div>
<span style="color:#F87E20;">Export</span>
<div style="display:inline; color:#ACACAC;"> | </div>
<span style="color:#F87E20;">Delete</span>
</td>
</tr>
</tbody>
</table>
The result:
This is all fine, and is working wonderfully. I want to make some QOL changes, though, and while looking into some of the changes I wanted to make, ran into something that is confusing me quite a bit.
The entire row is clickable, as well as the Copy, Export and Delete spans. This becomes a problem when I try to click on Export, but miss by 2 or 3 pixels, and instead navigate away from this area. I wanted to make the clickable area for the spans bigger, so I gave the a style property like so: padding:10px 0px 10px 0px;
The padding works as intended, enlarging the clickable area around the spans, making it easier to click on them. However, I was expecting the padding to also make the entire row taller, but instead it's as if the spans' padding is just flowing over the padding on the parent.
Here are some images to help explain the situation:
Parent:
And Child:
I don't understand why the child's padding is flowing outside it's container, and I don't want to go on in this direction without understanding what's going on. I was wondering if anyone could please help me understand what's happening here?
Your spans are inline elements. Top and bottom padding is ignored in case of inline elements.
By default, spans are inline, and divs are block. However, you can always override these with display: block; or display: inline;. Block elements (also inline-blocks) have full padding support.
See:
table {
width: 100%;
border-bottom: 1px solid #ACACAC;
}
tr {
cursor: pointer;
height: 60px;
}
td {
text-align: right;
vertical-align: middle;
padding: 10px 10px 10px 0px;
background-color: #e0c000;
}
span {
display: inline-block;
color: #F87E20;
background-color: #f0e000;
}
.padded {
padding: 10px 0 10px;
}
div {
display: inline;
color: #ACACAC;
}
<table>
<tbody>
<tr>
<td>
<span>Copy</span>
<div> | </div>
<span class="padded">Export</span>
<div> | </div>
<span>Delete</span>
</td>
</tr>
</tbody>
</table>
See also this article for more on this.
I have a table I have simplified for this question while preserving the problem.
It is a single row with three cells. The middle one contains an image and the first and last ones fill the remaining space and are textually supposed to be empty.
The height of the first and last cell adapts to that of the central (image) one as it is the highest, which does work -
but they become a tad bigger than they should visually creating a hole beneath the central cell.
HTML:
<article class="content" id="pc">
<table id="contentTable">
<tr>
<td class="pad" id="padL"></td>
<td id="cc">
<a href="#"><img src="http://38.media.tumblr.com/b9dc51057ece7352d07d40f4c59f0c65/tumblr_nb0xmfghdm1tin2h1o1_500.jpg" width="250"/>
</a>
</td>
<td class="pad" id="padR"></td>
</tr>
</table>
</article>
CSS:
#contentTable {
table-layout: fixed;
width: 700px;
}
#cc {
width: 500px;
}
.pad {
background-color: gray;
vertical-align: middle;
}
#padR {
border-top-right-radius: 2em;
border-bottom-right-radius: 2em;
}
#padL {
border-top-left-radius: 2em;
border-bottom-left-radius: 2em;
}
Here is a screen shot of the problem with the space highlighted in blue
And a CodePen if it helps.
Why does it occur and how can I remove it?
Since your image is inline, it obeys rules of baseline and has space at the bottom.
I had success by setting:
img {
display:block;
}
To get rid of other table spacing (not showing in your screen-shot), I suggest adding:
#contentTable {
border-collapse:collapse;
...
}
#contentTable tr td{
padding:0;
}
WORKING EXAMPLE
EDIT
Per your comment, here's an example using an embedded YouTube video (iframe). Iframes default to display:inline (replaced element), too.
iframe { display:block; }
WORKING EXAMPLE
Add a class to the cell containing the picture.
<td class="pad" id="cc">
I have a button inside of a dt element.
Everything looks fine, however I want to float the button to the right.
When I add the float on the button, now the dt is shorter causing it to look poor.
Adding overflow:auto fixes this, but now the text on the left is not vertically centered. vertical-align:center does not fix this nor does it help by hacking it with display:table-cell; Is there another way of accomplishing this that I am overlooking?
<dt style="overflow:auto">Title <button style="float:right">save</button></dt>
This is normally how I would do this.
My CSS:
dt {
display: block;
background: -webkit-linear-gradient(#bfbfbf 0%, #828282 100%);
background: linear-gradient(#bfbfbf 0%, #828282 100%);
padding: 10px 20px;
margin: 10px auto;
border-radius: 3px;
width: 400px;
}
dt:after {
content: "";
display: table;
float: none;
}
dt p {
display: inline-block;
}
dt button {
margin: 0;
padding-top: 3px;
padding-bottom: 3px;
float: right;
}
My HTML:
<dt>
<p>My Title</p>
<button>Save</button>
</dt>
Just remember that your text block "My Title" needs to be an inline-block and not a block
I hope that helps or at least points you in the right direction.
There are better approaches than simply floating the elements around. I would suggest either wrapping them with div elements, or creating a table, assuming of course that you plan on repeating the title-button structure. A sample may be something like:
<div style="width:50%;">Title</div>
<div style="width:50%;">
<button style="float:right">Button</button>
</div>
EDIT---
Try the quick-and-dirty table approach:
<body>
<table style="width:100%">
<tr><td>Title</td>
<td><button style="float:right">save</button></td></tr>
</table>
</body>
Also, what's with the dt element?
I think you just need to clear your floated element instead of using overflow:auto;
Example: http://jsfiddle.net/ChrisMBarr/UGWuc/
EDIT
Try this now: http://jsfiddle.net/ChrisMBarr/6HXek/5/
The button had some padding that the title did not have. This makes them have matching vertical padding.
I am writing a page for role-playing with some friends. When it comes to the character sheet we would like to have some tables with the statistics.
I would like to have inside every cell the name of the characteristic (strength, intelligence, etc.) and the number. Like this: http://jordi.dyndns-at-home.com:3000/characters/2
I would like to align the names to the left side of the cell and the numbers to the right side.
I have tried with <span style="text-align:right;"> and it will not work.
I have tried with <div style="text-align:right;"> and it does work but it "jumps a line", if I use display:inline it will not work.
It is possible to have both alignments on a <td> ?
BTW position:absolute; right:0 won't work. it will align to the end of the not the end of the
Use definition lists and be semantic.
HTML
<table><tr><td>
<dl>
<dt>Life:</dt>
<dd>15</dd>
</dl>
</td></tr></table>
CSS
dl {
width: 200px;
}
dl dt {
width: 160px;
float: left;
padding: 0;
margin: 0;
}
dl dd {
width: 40px;
float: left;
padding: 0;
margin: 0;
}
This way you can drop the whole table.
Here is an example:
<table>
<tr>
<td>
<span class='name'>name 1</span><span class='number'>100</span>
</td>
</tr>
</table>
With the following CSS:
.name{
width: 200px;
display: inline-block;
}
.number{
width: 200px;
display: inline-block;
text-align: right;
}
Hope you find this helpful. Here is a jsFiddle for you to mess with.
http://jsfiddle.net/K4fGq/
Bob
Although I agree with Oli's comment, I guess you can achieve it by using float:left and float:right.
If you put another table inside the TD with two TD's, one left and one right aligned, it will work and I will get down voted for such a suggestion.
Another mechanism is to use display: inline-block as suggested by #rcravens -- my personal choice.