HTML5 set image and text label in the center of web page - css

I need help with HTML / .css
I am trying to setup Image and Text in the center of Web Form Page. Also Image should be on the left of Text.
I've tried 2 solutions, but when I create table - it display on the left of screen and can't go middle.
A
<section class="main-page">
<div class="image-wrapper">
<asp:Image ID="Image2" Height="35px" runat="server" ImageUrl="~/Images/Origin/logo.jpg" />
</div>
<div class="content-wrapper">
<hgroup class="title">
<h1><%: Title %></h1>
</hgroup>
</div>
</section>
B
<table>
<tbody>
<tr>
<th><h1><%: Title %></h1></th>
<th><asp:Image ID="Image2" Height="35px" runat="server" ImageUrl="~/Images/Origin/logo.jpg" /></th>
</tr>
</tbody>
</table>
.css
.content-wrapper {
margin: 0 auto;
/*max-width: 960px;*/
}

You need to place the image/text in a container with text-align:center; defined in order for them to be centred, then the text should simply precede the image.
If for whatever reason the img element cant be placed following the text, you may need to float the elements to rearrange their ordering.
If the text is within a block level element, you will also need to set display:inline-block on it
As a basic demo:
HTML
<section>Text
<img src='https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRdw26b_dwa8moS-gRSFpaG3dwJTB6Nqtqxexmb7PscuD5gVP-P' />
</section>
CSS
section {
text-align:center;
vertical-align:Center;
}
Demo Fiddle of example A
HTML
<section class="main-page">
<div class="image-wrapper">
<img src='https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRdw26b_dwa8moS-gRSFpaG3dwJTB6Nqtqxexmb7PscuD5gVP-P' />
</div>
<div class="content-wrapper">
<hgroup class="title">
<h1>Title</h1>
</hgroup>
</div>
</section>
CSS
body{
text-align:center;
}
div, section{
display:inline-block;
}
div:first-child{
float:right;
}
div:last-child{
float:left;
}
Demo Fiddle of example B
HTML
<table>
<tbody>
<tr>
<th><h1>Title</h1></th>
<th><img src='https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRdw26b_dwa8moS-gRSFpaG3dwJTB6Nqtqxexmb7PscuD5gVP-P' /></th>
</tr>
</tbody>
</table>
CSS
body{
text-align:center;
}
table{
display:inline-block;
}

Related

css overflow scroll table cell with width:auto or width:100%

I would like to build flexible layout with scrollbars and dynamic widths.
I have table width,height 100% of window and 2(3) columns with auto widths ( widths depends on it's content ) and center column with remaining width. I can't specify exact width of column.
I would like to have Horizontal and Vertical Scrollbars if my content bigger than cell width and height;
Example 1
Example 2
Here is LiveDemo
<div style="width:1024px">
<table style="width:100%">
<tr>
<td>
<div>Column Menu with Dynamic Content and width</div>
</td>
<td style="width:100%;">
<div style="overflow:scroll;">Column Big Dynamic Content with ScrollBars</div>
</td>
</tr>
</table>
</div>
I can't use :
table-layout:fixed - because columns width depends on it's content
overflow:scroll with width,height specified
i need pure css solution
I've tried :
width:100%, max-width:100%, min-width:100% - not working
white-space:wrap works only if content is text, and there is only vertical scrollbar
Question is : How to make div with overflow:scroll or overflow:auto have width,height of it's parent container ( in my case it is TD )
p.s
BEST case would be if it was <TD style="overflow:auto"></TD> and width is auto.
Found second solution ( more prettier ), and it works!
Here on stackoverflow there are solutions saying add child div to td with height,width, and overflow scroll, but they all not working with dynamic widths of cells, so I slightly modified solution by adding to div position:absolute and set top,bottom,right,left to 0, and adding to parent container position:relative;
Here is Live Demo 1
<table border="1" width="100%" height="100%">
<tr>
<td> <!-- left column with autowidth -->
<div style="width:200px">Left Menu</div>
</td>
<td style="width:100%; position:relative;"> <!-- fill remaing width of columns -->
<div style="position:absolute; top:0; bottom:0; left:0; right:0; overflow:auto; ">
<div style="width:1000px; height:1000px; background:#f05555;">Calendar Mega BIG content</div>
</div>
</td>
<td> <!-- right column with autowidth -->
<div style="width:300px">Right Menu</div>
</td>
</tr>
</table>
If we go further and be more modern ( in life without tables ), it would be like this :
Here is Live Demo 2
<ul style="width:100%; height:100%; display:table;">
<li style="display:table-cell;"> <!-- left column with autowidth -->
<div style="width:200px">Left Menu</div>
</li>
<li style="display:table-cell; width:100%; position:relative;"> <!-- fill remaing width of columns -->
<div style="position:absolute; top:0; bottom:0; left:0; right:0; overflow:auto;">
<div style="width:1000px; height:1000px; background:#f05555;">Calendar Mega BIG content</div>
</div>
</li>
<li style="display:table-cell;"> <!-- right column with autowidth -->
<div style="width:300px">Right Menu</div>
</li>
</ul>
Found ( Ugly ) solution, but it works!
Basic idea, is to wrap scrollable content inside one more table with table-layout:fixed, leaving outer table with dynamic widths :)
Here is Live Demo
<table border="1" width="100%" height="100%">
<tr>
<td>
<div style="width:200px">Left Menu</div>
</td>
<td style="width: 100%;">
<table style="table-layout: fixed; width: 100%; height: 100%;">
<tr><td>
<div style="overflow:auto; width:100%; height:100%;">
<div style="width:1000px; height:1000px; background:#f0f0f0;">Calendar</div>
</div>
</td>
</tr>
</table>
</td>
<td>
<div style="width:300px">Right Menu</div>
</td>
</tr>
</table>
If you find better solution, without much markup tags, please welcome to post here...!
p.s I think i should try play with display:table or flex to achieve more elegant solution

Dividing page into sections by height, section not expanding to full specified height

Below is my code. I divided the whole page by height, 20%, 50%, 30%;
But for some reason the table in the SECOND SECTION is not getting the whole 50%. only maybe 10%. It is resided in a row-fluid. If I put overflow hidden, only the header will be visible.
css:
.body-wrapper{
height:100%;
}
.body-wrapper > fieldset div:nth-of-type(1){
height:20%;
}
.body-wrapper > fieldset div:nth-of-type(2){
height:40%;
}
.body-wrapper > fieldset div:nth-of-type(3){
height:30%;
}
html:
<div class = "main-Frame container-fluid"> <!-- mid section -->
<div class="well row-fluid" >
<div class="body-wrapper">
<fieldset>
<div> <!-- FIRST SECTION -->
<legend>User Profile</legend>
<div class="row-fluid">
<div class="col-md-2 text-right">User Name
</div>
<div class="col-md-4" >
<input style="width:90%;"type="text" placeholder="username"/>
</div>
<div class="col-md-2 text-right">User Email
</div>
<div class="col-md-3">kkk#gmail.com
</div>
<div class="col-md-1">
<input type="checkbox"/>Active
</div>
</div>
</div> <!-- END OF FIRST SECTION -->
<div> <!-- SECOND SECTION -->
<legend>Application Defaults</legend>
<div class = "container-fluid" style="height:100%;">
<div class="row-fluid" style="height:100%;">
<div class="col-md-10" style="height:100%;">
<div class="table-responsive user-table">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>App</th>
<th>Type</th>
<th>Setting</th>
<th>check</th>
</tr>
</thead>
<tbody>
<tr>
<td>first</td>
<td>first</td>
<td>first</td>
<td>first</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-2">
</div>
</div>
</div>
</div> <!-- END of SECOND section -->
<div> <!-- LAST section -->
<legend>ChangePassword</legend>
</div> <!-- END of footer section -->
</fieldset>
</div>
That problem here is that your CSS selectors are too broad - the selector .body-wrapper > fieldset div:nth-of-type(1) matches both the intended target (the first <div> directly under the fieldset, and the more deeply nested <div class="table-responsive user-table">. (On all preceding elements that also match, you've used inline styles to force their heights to 100%, so they don't exhibit the same problem behaviour.)
To address this issue, simply narrow down the range of elements matched by your selectors by increasing their specificity - for example, target only immediate child <div> elements under the fieldset, rather than all descendant ones:
.body-wrapper {
height: 100%;
}
.body-wrapper > fieldset > div:nth-of-type(1) {
height: 20%;
}
.body-wrapper > fieldset > div:nth-of-type(2) {
height: 40%;
}
.body-wrapper > fieldset > div:nth-of-type(3) {
height: 30%;
}
Hope this helps! Let me know if you have any questions.

SharePoint "Chrome Type" issue in publishing pages

Although my issue is related to SharePoint, I think it's also a html/css issue, so I post the question here. If you are familiar with SharePoint, you can follow the steps to reproduce the issue (or you can just skip this part, it's ok):
Create a publishing page, and the Page Layout is "(Welcome Page) Blank Web Part page"
Add two Web Parts in "Top Left" and "Top Right".
Set the "Chrome Type" to "None/Border Only" in the first Web Part (The Top Left one)
And so will see the issue in non-IE browsers (they should be in the same line):
To make it simple, I create a demo :
<html>
<head>
<style type="text/css">
.ms-table {
display: table;
}
.ms-fullWidth {
width: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.tableCol-75 {
display: table-cell;
min-width: 74.9%;
}
.cell-margin {
margin: 5px 0px 5px 0px;
}
.tableCol-50 {
display: table-cell;
min-width: 49.9%;
}
</style>
</head>
<body>
<div class="ms-table ms-fullWidth">
<div class="tableCol-75">
<div class="ms-table ms-fullWidth">
<div class="cell-margin tableCol-50">
<div class="ms-fullWidth">
<div class="ms-fullWidth ">
<div>
<div width="100%">
<div wpid="ctl00_ctl40_g_0b541c21_2ea1_46e2_bc3e_7c10ff98186d">
<table id="myWP1">
<tbody>
<tr>
<td>
<p>hello</p>
<p>hello</p>
<p>hello</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell-margin tableCol-50">
<div class="ms-fullWidth">
<div class="ms-fullWidth ">
<div>
<div>
<span><h2 style="text-align:justify;"><nobr><span>Web Part Title</span>
<span></span>
</nobr>
</h2>
</span>
</div>
<div width="100%">
<div wpid="ctl00_ctl40_g_0b541c21_2ea1_46e2_bc3e_7c10ff98186d">
<table id="myWP2">
<tbody>
<tr>
<td>
<p>hello</p>
<p>hello</p>
<p>hello</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
And my Web Part also has this issue in all browsers. I am not familiar with css, so I really don't know how to analysis the issue. But I found something interesting:
If I add a span tag (or other) before "myWP", then there is no issue. (demo)
If I delete the table ("myWp") and use a div instead, no issue again. (demo)
Currently I choose the first way to fix it, but I don't know why. Is it a right way? What is the reason of this issue? Thanks in advance!

HTML Div Float Will not center

I am trying to make it so two uneven columns are close together, but I'm not sure where I went wrong while I was writing the code.
When these are entered, the columns span the width of the page, where I would like this to be about 720px total, with the main column being 520px and the secondary column being 200px.
I've tried about 30 different things, changing nearly all the variables, and adding random "align=/:center"'s around all the divs.
I'm not quite sure, and I could really use some help.
I've been working on this the last 5 hours of work, and this needs to be done, I cant quite get it :(
Thank you so much!
<style>
.table { border:0px solid black; padding:10px; max-width:723px; overflow:hidden; }
.left { float:left; width:75%; }
.right { float:right; width:25%; }
</style>
</head>
<body bgcolor="#ffffff">
<center>
<table style="display: inline-table;" border="0" cellpadding="0" cellspacing="0" width="723">
<tr>
<td colspan="3"><img name="gala_r1_c1" src="/images/gala_r1_c1.jpg" width="720" height="42" border="0" alt=""></td>
</tr>
<tr>
<td colspan="3"><img name="gala_r2_c1" src="/images/gala_r2_c1.jpg" width="720" height="283" border="0" alt=""></td>
</tr>
</table>
</center>
<div class="table">
<div class="left">
<h1>From the President</h1>
<p></p>
<p>Dear Friends,</p>
blah
</div>
<div class="right">
<h1>From Students</h1>
-"<b>derp"<br>
</b>
</div>
</div>
</center>
{embed="embeds/global_footer"}
</body>
</html>
left .table { border:0px solid black;
padding:10px;
max-width:723px;
overflow:hidden;
margin:0 auto; }
So, add margin:0 auto; to the table class.
Update:
<style type="text/css">
<style>
Remove one of them... :)
Instead this:
<div class="right"> <div class="right" padding-right="100px"> <h1>From Students</h1> -"<b>derp</b> </div> </div>
try with this:
<div class="right"> <div style='padding-left:15px'> <h1>From Students</h1> -"<b>derp</b> ....</div> </div>
There are 3 changes you will need to make:
1) MOST IMPORTANT - you need a doctype! Otherwise this pushes the browser into quirks mode and into otherwise no man's land. Always include this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2) Remove the dangling </center> tag at the bottom.
3) Add margin: 0 auto; as the gentleman before me posted. This the css equivalent to the html <center> tag.
Final results:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Float center</title>
<style>
.table { border:0px solid black; padding:10px; max-width:723px; overflow:hidden; margin: 0 auto;}
.left { float:left; width:75%; }
.right { float:right; width:25%; }
</style>
</head>
<body bgcolor="#ffffff">
<center>
<table style="display: inline-table;" border="0" cellpadding="0" cellspacing="0" width="723">
<tr>
<td colspan="3"><img name="gala_r1_c1" src="/images/gala_r1_c1.jpg" width="720" height="42" border="0"
alt=""></td>
</tr>
<tr>
<td colspan="3"><img name="gala_r2_c1" src="/images/gala_r2_c1.jpg" width="720" height="283" border="0"
alt=""></td>
</tr>
</table>
</center>
<div class="table">
<div class="left">
<h1>From the President</h1>
<p></p>
<p>Dear Friends,</p>
blah
</div>
<div class="right">
<h1>From Students</h1>
-"<b>derp"<br>
</b>
</div>
</div>
{embed="embeds/global_footer"}
</body>
</html>
Snapshot:

CSS Layout - Dynamic width DIV

I have a pretty common layout issue that I have traditionally used a table to solve, but would like some advice on getting it done with CSS. I have 3 images that makeup a 'container'. The left and right images are usually just shown using tags, and the center image is displayed as a 'background-image" with my content over it, so that the content appears to be in the container. I'm sure you've seen/used this a million times:
<table width="100" cellpadding="0"><tr>
<td width="50"><img src="myleftimage" /></td>
<td style="background: url('mymiddleimage');">Content goes here...</td>
<td width="50"><img src="myrightimage" /></td>
</tr></table>
The nice thing about this is that the width of the table is always the width of the browser (or parent) and the middle column where the content is dynamically sizes to take up the remaining space between the left/right images.
What I want to is recreate this using CSS, with as little hard coded info as possible. So something like this:
<div style="float:left; width:100%">
<div style="width: 50px;float:left;"><img src="myleftimage" /></div>
<div style="background: url('mymiddleimage');float:left;width:???">Content goes here...</div>
<div style="width: 50px;float:left;"><img src="myrightimage" /></div>
</div>
This works great accept for the middle div -how do I set the width? Right now I can hard-code it to be, say, 92%, etc. But want I want is for it to auto-fill the space. Can it be done using only CSS?
try
<div style="width:100%;">
<div style="width:50px; float: left;"><img src="myleftimage" /></div>
<div style="width:50px; float: right;"><img src="myrightimage" /></div>
<div style="display:block; margin-left:auto; margin-right: auto;">Content Goes Here</div>
</div>
or
<div style="width:100%; border:2px solid #dadada;">
<div style="width:50px; float: left;"><img src="myleftimage" /></div>
<div style="width:50px; float: right;"><img src="myrightimage" /></div>
<div style="display:block; margin-left:auto; margin-right: auto;">Content Goes Here</div>
<div style="clear:both"></div>
</div>
This will do what you want. Fixed sides with 50px-width, and the content fills the remaining area.
<div style="width:100%;">
<div style="width: 50px; float: left;">Left Side</div>
<div style="width: 50px; float: right;">Right Side</div>
<div style="margin-left: 50px; margin-right: 50px;">Content Goes Here</div>
</div>
Or, if you know the width of the two "side" images and don't want to deal with floats:
<div class="container">
<div class="left-panel"><img src="myleftimage" /></div>
<div class="center-panel">Content goes here...</div>
<div class="right-panel"><img src="myrightimage" /></div>
</div>
CSS:
.container {
position:relative;
padding-left:50px;
padding-right:50px;
}
.container .left-panel {
width: 50px;
position:absolute;
left:0px;
top:0px;
}
.container .right-panel {
width: 50px;
position:absolute;
right:0px;
top:0px;
}
.container .center-panel {
background: url('mymiddleimage');
}
Notes:
Position:relative on the parent div is used to make absolutely positioned children position themselves relative to that node.
making a dynamycal width with mobile devices support
http://www.codeography.com/2011/06/14/dynamic-fixed-width-layout-with-css.html

Resources