I can't get this to center. I've tried adding aligns everywhere, and every different combination of tags. I know the HTML isn't great... but I'm just trying to get something functional right now - not worrying about best practices.
<table class='navbar' width='100%'>
<tr>
<td class='navbar'>
<a href='#' class='cta cta-big cta-red'><center><span class='icon-go'>No</span></a></center>
</td>
<td class='navbar'>
<center><a href='#' class='cta cta-big cta-yellow'><span class='icon-download'>Maybe</span></a></center>
</td>
<td class='navbar'>
<center><a href='#' class='cta cta-big cta-green'><span class='icon-check'>Yes</span></a>
</td>
</tr>
</table>
Any help would be appreciated! Thanks!
These answers show there are different kinds of "centering" in CSS, let's call these two text centering and block centering. The 'text-align' property governs where lines of text flush up, right, left, center, justified (right and left with extra padding between the words to make them flush up). This is what usually is thought of as "centering", and what you can see as icons in WYSIWYG text editing.
When you do layout you often have another centering task, you want one box to be in the center of another box. By the CSS way of thinking this is the box model (almost everything is a box to CSS), and you can reformulate "center this box inside that box" into "make sure that the left margin and the right margin of the inner box are the same". For instance if the outer box is 200 pixels wide, and the inner box is 120 pixels wide, you want the left margin = right margin, or in other words each margin will have to be 40 pixels. (40+120+40 = 200).
Hard-coding this is a lot of calculation, and it will break if either box changes size. Fortunately we have the margin: auto value which does it for us (it does "OK, this box is 120 pixels wide, we have room for 200 pixels, then I'll split the extra 80 in half and give it to each margin"), and it will keep working even when the layout (and box sizes) change.
Finally we have vertical alignment. CSS doesn't do that satisfactory yet, but there is a trick. See http://www.w3.org/Style/Examples/007/center for more on this.
Oh, and the HTML 'center' element mixed those two above models, but it didn't do it very well. I would advise you to stay away from the center tag (besides it is a good idea to keep style apart from markup).
Get rid of the <center> tags:
<table class='navbar' width='100%'>
<tr>
<td class='navbar'>
<a href='#' class='cta cta-big cta-red'><span class='icon-go'>No</span></a>
</td>
<td class='navbar'>
<a href='#' class='cta cta-big cta-yellow'><span class='icon-download'>Maybe</span></a>
</td>
<td class='navbar'>
<a href='#' class='cta cta-big cta-green'><span class='icon-check'>Yes</span></a>
</td>
</tr>
</table>
And use CSS to do the centering:
table {
text-align:center;
}
Demo: http://jsfiddle.net/w7rWx/
"Not worrying about best practices" is often a difficult way to "get something functional", especially cross-browser. It's in your best interest to at least make an effort, consider adopting a different approach towards HTML/CSS.
Try this css.
.center {
float: none;
margin: 0 auto;
padding: 0;
position: relative;
}
margin: 0 auto; does the trick of centering. BTW, mentioning the width attribute also will make sure that the right and left margins depending on the window size.
In the first <td>, you have your HTML organized like so:
<a><center><span></span></a></center>
You're closing the tags in the wrong order; from the other two lines, it looks like you meant to do:
<center><a><span></span></a></center>
And in the third <td>, you didn't close the <center> at all.
Here's a fiddle with those changes made, and it looks like it works.
As already said: get rid of the center tag…
As for code Wesley Murch already gave you a very good answer though it will apply to all your table. I you want your style to apply only to a few elements try this:
.center{
text-align:center;
}
And apply it to "td" element by writing:
<td class='navbar center'>
<a href='#' class='cta cta-big cta-green'><span class='icon-check'>Yes</span></a>
</td>
Related
If I use CSS line-height property on a span element to adjust for particular lines I get the extra space before the line in questions but cannot reset to regular line spacing with a follow up CSS class. In table shown below see bottom right cell. Before "Tokyo:" I want extra 'space-before' and after "Tokyo:" I want regular line spacing to return.
td span.hard_line_break {
line-height: 2em;
}
td span.soft_line_break {
line-height: 1em;
}
<table>
<tr class="table_top row_color_A">
<td class="bold_type td">Encroachments onto Council-Controlled Land</td>
<td class="td">Councils, both urban and rural, must address encroachments on reserves, roads and lanes.</td>
<td class="td">Brisbane:<br />Friday 23 May<br />9am—12:30pm<br />
<span class="hard_line_break">Tokyo:</span>
<span class="soft_line_break"><br />Tuesday 20 May</span>
</td>
</tr>
</table>
Entire table HTML/CSS to see and interact with:
http://jsfiddle.net/wideEyedPupil/3LeS6/1/
Line-height feels like it's the wrong property but can't get padding/margins to work on span elements inside a table cell. What is the proper way to do this please?
EDIT:
Okay I have it working using suggestion of div tags.
Demo
You can use:
.hard_line_break {
display: block;
margin-top: 1em;
}
And get rid of the <br> before and after it.
Demo
Do u mean something like this?
DEMO
As u can see, maybe adding a css could help you
UPDATE:
DEMO 2
Just put two <br /><br /> tags before Tokyo and get rid of those <span> tags. It's the easiest solution I can think of.
I've got a set of tabular data that I'm presenting in a standard HTML table tag. The design calls for a one pixel separating line between each row as well as having all cells in a given row be vertically centered against each other. The various rows and cells can have an arbitrary height and need to grow based on the content inside of them.
The tricky piece here comes from the fact that the one pixel separating line is supposed to stop 15px from either side of the edge of the table. My initial inclination to solve this is to merely add a border-bottom to either my <tr> or <td> tags, however, I couldn't for the life of me figure out any sort of way to not get the line to go all the way across. If I put <div> tags inside of the <td> tags, I can then apply the border to there, but then my border bottom is right up against the bottom of the content rather than at the bottom of the row.
The only thing I've been able to make work feels really "dirty" to me.
<table>
<tr>
<td style="width: 15px;"> </td>
<td style="border-bottom: 1px solid gray;">My table data</td>
.... more cells here as needed
<td style="width: 15px;"> </td>
</tr>
</table>
This works because I add those two spacer cells on either end of the row. I've even gone pretty far down some non-table solutions, but I always run into issues trying to get the content to grow correctly (i.e. no absolute positioning in the various rows) and/or getting things to vertically center and wrap like they are supposed to.
Anybody have any better ideas? If it helps, I'm only supporting "newer" browsers, e.g. IE9+/etc, including mobile.
You can try adding a new row between each pair or rows with colspan="n" where n is the total number of collumns, and inside that new row you put a div with the desired specs (I would say 1px height, for instance).
HTML, add this between your rows:
<tr class="test_tr" >
<td colspan="3"><div class="test_div"></div></td>
</tr>
CSS:
.test_tr
{
height: 1px;
}
.test_div {
height: 1px;
width: 80%;
background-color: red;
margin: 0 auto;
}
Here is the Fiddle: http://jsfiddle.net/Y8eWR/
the following html will only work in IE, but not in firefox. Can anyone see why? Thanks
<a href="../homepage.aspx" id="aHomepage">
<td id="myHeader_td0_a" class="Tab_White" width="61" align="center" valign="middle">Home</td>
</a>
Put the <a> inside of your table cell.
<td id="myHeader_td0_a" class="Tab_White" width="61" align="center" valign="middle">Home</td>
If you want the entire cell to be a link use CSS to make the <a> fill the cell or use JavaScript to make the whole cell respond to an onclick event.
It doesn't work in FF because that is bad markup. Don't ever wrap td's with anchors. Browsers will try to interpret your html as best as it can. You are lucky that IE decided to play nice this time. Instead do this:
<td id="myHeader_td0_a" class="Tab_White" width="61" align="center" valign="middle">
Home
</td>
To make the anchor the size of the <TD> use some css like this:
td a{
display:inline-block;
height: 100%;
width: 100%;
}
Anchors are inline elements, which do not have height. To make it respect height make it display: inline-block so it stays inline but respects height and width like a block level element.
Edit:
One way of passing that depth variable would be to set a property on the anchor and getting that with the click event:
<a href="../homepage.aspx" id="aHomepage" depth='100'>Home</a>
Then you can use js to get the value (roughly this way as I don't know how your events are set up):
$("a").click(function(){
this.getAttribute("depth");
});
Sorry I don't have the VB code to add it to the anchor.
I'm to implement a fullscreen layout for a Web app according to custom specs. I've got most of it under control but there's one part I have some trouble with.
To economize on space in an otherwise already crowded GUI, a "Log out" button should go into the title row rather than elsewhere. The title row, of course, contains a title. The button should appear in its default dimensions for the given browser/opsys combination at the top right, with a little padding. The title should be centered in the remaining space in that row. Here's a picture:
+====================+=======+
| ACME Widgets | [Btn] |
+====================+=======+
I don't know how wide the button will be, nor should I need to. The layout should scale smoothly on a range of devices and resolutions, from about 200 px width to 2000:
+==================================================+=======+
| ACME Widgets | [Btn] |
+==================================================+=======+
...with the title continuing to be centered in its area, which again will always be (total available width - width required for the button). The page may end up being used in a JavaScript-less environment, so dynamic size calculation is not an option. Nor (before you ask) is talking the customer out of his design.
Can anyone please suggest HTML (and, if required, CSS) to achieve this layout?
Update More constraints/explanation (sorry): This app could be viewed by people with poor vision, who like to use their zoom button (Ctrl-+) to blow up font sizes. Therefore, I'd like to go with as few assumptions about things like text sizes as possible. Obviously, on a tiny display with big zoom I would eventually not have enough space for the unpadded title and button; but until then I'd like to stay flexible.
I have two possible solutions. I will admit, they seem like these are simply modifications to some answers already given but should hopefully address the comments you've left so far.
CSS approach:
Lets say you determine that a nice width for your button is 5em. This of course scales with the browser's text zoom to always be, well, 5em.
Then perhaps you could float this to the right, and put a margin-right on your title of 5em.
#buttonContainer {
float:right;
display:inline;
width:5em;
text-align:right;
}
#titleContainer {
text-align:center;
margin-right:5em;
border:1px solid blue;
}
<div id="buttonContainer">
<input id="btnLogOut" type="button" value="Log Out" />
</div>
<div id="titleContainer">
<h1 style="text-align:center;" id="title">ACME Widgets</h1>
</div>
This approach may not be picture-perfect, but you can tweak the em unit and arrive at a nice solution hopefully.
Table-approach:
Another approach is a modification of the table-based approach given by borayeris. I have modified this to not make any assumptions about the width of the button...
<table border="0" width="100%">
<tr>
<td width="99%" align="center">ACME Widgets</td>
<td width="1%" align="right">button</td>
</tr>
</table>
Good luck!
You can use a floating div.
<div style="float:right">[Btn]</div>
<h1 style="text-align:center;">ACME Widgets</h1>
Edit: second attempt, using a displayed-but-invisible div with the same button as content to center the title in the remaining space (aka doing math in css :)
<div style="float:right">[Btn]</div>
<h1 style="text-align:center;">ACME Widgets<div style="visibility:hidden">[Btn]</div></h1>
If table is acceptaable use that
<table border="0" width="100%">
<tr>
<td align="center">ACME Widgets</td>
<td width="60">button</td>
</tr>
</table>
Might not be the most elegant solution but something like this should work. This is based off Adrian's solution
CSS
h1 {position: relative; left: 0; right: 100px; text-align: center}
.logout {float: right; width: 100px}
HTML
<div class="logout">Log me out</div>
<h1>ACME widgets</h1>
What is the best and easiest way to vertically center text that's next to an image in html? Needs to be browser version/type agnostic. A pure html/CSS solution.
This might get you started.
I always fall back on this solution. Not too hack-ish and gets the job done.
EDIT: I should point out that you might achieve the effect you want with the following code (forgive the inline styles; they should be in a separate sheet). It seems that the default alignment on an image (baseline) will cause the text to align to the baseline; setting that to middle gets things to render nicely, at least in FireFox 3.
<div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg" style="vertical-align: middle;" width="100px"/>
<span style="vertical-align: middle;">Here is some text.</span>
</div>
Does "pure HTML/CSS" exclude the use of tables? Because they will easily do what you want:
<table>
<tr>
<td valign="top"><img src="myImage.jpg" alt="" /></td>
<td valign="middle">This is my text!</td>
</tr>
</table>
Flame me all you like, but that works (and works in old, janky browsers).
There are to ways:
Use the attribute of the image tag align="absmiddle"
or locate the image and the text in a container DIV or TD in a table and use
style="vertical-align:middle"
That's a fun one. If you know ahead of time the height of the container of the text, you can use line-height equal to that height, and it should center the text vertically.
I recommend using tables with <td valign="middle"> (as inkedmn mentioned, it works in all browsers), but if you're wrapping with a link, here's how to do it (works in ugly old browsers too, like Opera 9):
<a href="/" style="display: block; vertical-align: middle;">
<img src="/logo.png" style="vertical-align: middle;"/>
<span style="vertical-align: middle;">Sample text</span>
</a>
There are a couple of options:
You can use line-height and make sure it is tall as the containing element
Use display: table-cell and vertical align: middle
My preferred option would be the first one, if it's a short space, or the latter otherwise.
Since most of the answers to this question are between 2009 and 2014 (except for a comment in 2018), there should be an update to this.
I found a solution to the wrap-text problem brought up by Spongman on Jun 11 '14 at 23:20. He has an example here: jsfiddle.net/vPpD4
If you add the following in the CSS under the div tag in the jsfiddle.net/vPpD4 example, you get the desired wrap-text effect that I think Spongman was asking about. I don't know how far back this is applicable, but this works in all of the current (as of Dec 2020/Jan 2021) browsers available for Windows computers. Note: I have not tested this on the Apple Safari browser. I have also not tested this on any mobile devices.
div img {
float: left;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
I also added a border around the image, just so that the reader will understand where the edge of the image is and why the text wraps as it does. The resulting example looks is here: http://jsfiddle.net/tqg7hLzk/
One basic way that comes to mind would be to put the item into a table and have two cells, one with the text, the other with the image, and use
style="valign:center"
with the tags.