Implementing an image viewer using CSS - css

The following code (jsfiddle) implements an image viewer in CSS
<!DOCTYPE html>
<head>
<title>Trains, Planes, Automobiles, and Boats</title>
<meta charset="UTF-8">
<style type="text/css" media="screen">
html, body { background:#ddd; margin:0; padding:0; height:100%; }
#foo { position:absolute; left:5%; width:60%; top:5%; height:80%; background:#dcc; }
#bar { position:absolute; left:70%; width:25%; top:5%; height:80%; background:#cbd; vertical-align: middle; }
.fullwidth { width: 100%; vertical-align: middle; }
.vcenter {
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="foo">
<div class="vcenter">
<img class="fullwidth" src="https://openclipart.org/image/800px/svg_to_png/19623/philrich123-A380.png" />
</div>
</div>
<div id="bar">
<img class="fullwidth" src="https://openclipart.org/image/800px/svg_to_png/4703/ryanlerch-Steam-Train-Engine.png" />
<img class="fullwidth" src="https://openclipart.org/image/800px/svg_to_png/19623/philrich123-A380.png" />
<img class="fullwidth" src="http://openclipart.org/image/800px/svg_to_png/74557/rally-car.png" />
<img class="fullwidth" src="https://openclipart.org/image/800px/svg_to_png/196201/Model-T-Ford.png&disposition=attachment" />
<img class="fullwidth" src="https://openclipart.org/image/800px/svg_to_png/24418/Jarno-Boat-1.png&disposition=attachment" />
</div>
</body>
How do I fix the following two bugs?
The bar side does not scroll.
The image in the foo side is not centered. The intention is to make all the image visible in the left side while filling its container in one dimension (vertical/horizontal) and being scaled in the other (horizontal/vertical) to remain at 1:1 ratio.

I'm not sure I understood your question, but if you only want to make your right div scrollable then add this to your css
#bar{
overflow: scroll;
}
Example
To answer your second question (vertically centering the left image), add these rules to your vcenter class
position: relative;
top: 50%;
transform: translateY(-50%);
Example 2

Related

How to use CSS to vertically center a form in a div

video.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="headerBar">
<div class="searchForm">
<form id="search" action="" method="get" target="_blank">
<input id="vb_yt_search-term" name="search_query" type="text" maxlength="128" />
<select name="search_type">
<option value="sAll">All</option>
</select>
<input type="submit" value="Search" />
</form>
</div>
</div>
</body>
</html>
style.css
.headerBar {
background-color: #f1f1f1;
width: 100%;
height: 44px;
border: 0px;
padding:0px;
margin:0px;
position:fixed;
top:0;
left:0;
}
.searchForm {
text-align: center;
border: 0px;
width: 100%;
vertical-align: middle;
position: relative;
}
Code:
Here is the code for you to try.
Question 1:
How can I vertically center the form (TextBox, ComboBox, and Button) in the headerBar using CSS? I want to stick with <div> instead of <table> if at all possible. I want it to be centered dynamically, so that if the page is re-sized, it will re-center... basically it can't be hard coded in with px.
Question 2:
How can I add more elements, once again using the <div> tags so that the items will not be centered, but approximately 1/4 and 3/4 respectively. To give you an example, the top bar in the image is YouTube.com. The Second, is my code. I would like to place objects where the YouTube logo is.
Question 3: (bonus)
This is not important, only an extra. In the picture shown above, is it possible to get the exact search bar and search button onto my form, but allow it to have what ever functionality I want?
Question #1:
Remove height from div.headerBar, this way it will size up with the inputs.
If you want a height, then don't use height. Use line-height:
div.headerBar { line-height: 44px; }
Question #2:
<div class="wrap">
<div class="left"></div>
<div class="mid"></div>
<div class="right"></div>
<div class="clear" />
</div>
.wrap { width: 100%; }
.left { float: left; width: 25%; }
.mid { float:left; width: 50%; }
.right { float:left; width: 24%; }
.clear { clear: both; }
Check fiddle: http://jsfiddle.net/WHXnW/2/
Updated fiddle: http://jsfiddle.net/WHXnW/4/
Now this is closer to what you want. Just play around with the styles to get it just the way you want.

CSS Alignment, image and text

I want to put one image left, one image right and text in the middle.
I keep thinking I am close then one or another runs away! I would appreciate some help and guidance.
Code for page and CSS below
VB page
<div id="HeaderTitleWrapper">
<h2><img src="images/Header/logo.jpg" alt="Writting Icon" />
<div id="HeaderTel">
GATEWAY<img src="images/Header/pic2.jpg" alt="Writing Icon" />
</div> <!-- HeaderTel -->
</h2>
</div> <!-- HeaderTitleWrapper -->
CSS
/* Header Text Title */
#HeaderTitleWrapper {
width: 1000px ;
margin-left: auto ;
margin-right: auto ;
}
#HeaderTitleContent {
padding: 0px;
height: auto;
position: relative;
left: 0px;
top:10px;
}
#HeaderTel
{
position:absolute;
right:150px;
top:auto;
}
Using text-align:justify and pseudo , you can spray inline-boxes from left to right.
<header>
<img/>
<span>text</span>
<img/
<header>
header {
line-height:0;
text-align:justify;
}
header span {
display:inline-block;
vertical-align:middle;
line-height:1.2em;
}
header:after {
content:'';
display:inline-block;
width:99%;
vertical-align:top;
}
similar exemple : http://codepen.io/gcyrillus/pen/dlvCp
<div
id="firstImage">
<img
src="1.jpg"
alt="Writting Icon"
/>
</div>
<span
id="Text">
GATEWAY
</span>
<div
id="secondImage">
<img
src="2.jpg"
alt="Writing Icon"
/>
</div>
<!-- HeaderTel -->
</div>

How to center vertically a floating div inside an inline-block?

I'm trying to center vertically a div inside an inline-block,
I used this inline-block to get automatically size of child in order to center my div.
The problem is my children div are floating... in order to constrain it to the left/right position.
Here is how the HTML look like :
<span class="block_container">
<div class="block_text"> <!-- float:right -->
<h1>TITLE</h1>
<p>lorem ipsum</p>
</div>
<div class="block_image"> <!-- float:left -->
<img src="test.png"></img>
</div>
</span>
However, I can't figure out this problem : http://jsfiddle.net/kl94/nH2sd/
Edit:
Here is what I want :
Here is what I tried :
http://jsfiddle.net/kl94/nH2sd/
To get the actual vertical alignment working the way you want it to work as per your attached screenshot, you have to change a few things.
1. Adding a display:table-row; to the parent block.
2. Removing all floats and replacing it with display:table-cell;
This will enforce the exact characteristic of vertical-alignment to co-exist and work the way you want it to work as per the attached screenshot.
Here is the WORKING DEMO
The HTML:
<span class="block_container">
<div class="block_image">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/64/Gnu_meditate_levitate.png"></img>
</div>
<div class="block_text">
<div class="bgColor">
<h1>TITLE</h1>
<p>I should be align vertically but the problem is i don't know my left neightbor height...</p>
<div>
</div>
</span>
The CSS:
.block_text {
/*background: red;*/
/*float: right;*/
width: 60%;
display:table-cell;
vertical-align:middle;
}
.block_image {
background: yellow;
/*float: left;*/
width: 40%;
display:table-cell;
}
.block_image img {
width: 100%;
max-width: 300px;
height:auto;
}
.block_container {
background:teal;
/*display:inline-block;*/
display:table-row;
}
.bgColor{background:red;}
Hope this helps.
You could try something like this: http://codepen.io/pageaffairs/pen/LlEvs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.block_text {
background: red;
display:inline-block;
vertical-align: middle;
}
img {
width: 40%;
max-width: 300px;
vertical-align:middle;
background: yellow;
}
.block_container {
background:teal;
display: inline-block;
}
</style>
</head>
<body>
<div class="block_container">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/64/Gnu_meditate_levitate.png"><div class="block_text">
<h1>TITLE</h1>
<p>I should be align vertically but the problem is i don't know my left neightbor height...</p>
</div>
</div>
</body>
</html>
You can try to add this:
margin-top: 13%; at your .block_text selector in CSS.

CSS div with margin moving everything

Below is my code for a simple page. I'm trying to have (A) a banner on the top which consists of a logo, a header to its right and then a "sign in/register" link, (B) below all this then I will have the main text of the site.
I would like a large gap between the main text and banner at the top. So I divide the page up with divs. But when I apply a "margin-top" to #main to keep the banner at a certain distance, EVERYTHING, that is, the main text and everything in my banner all move down the page. Same thing happens if I apply a "margin-bottom" to the header element.
I'm kind of new to CSS and HTML but I though I had the hang of it until this. I've scratched my head for ages about this but I can't seem to understand positioning here at all!
<!DOCTYPE html>
<html lang="en">
<head>
<link href="style.css" rel="stylesheet" type="text/css"/>
<meta charset="utf-8"/>
<title>My Page</title>
</head>
<body>
<header id="masthead" role="banner">
<img src="jep.jpeg" alt="My Page">
<h2>Welcome!</h2>
<p>Sign in Register</p>
</header>
<div id="main" role="main">
<!--main text here -->
</div>
</body>
</html>
Here is the CSS code:
#masthead {
position: relative;
}
#masthead img {
position: absolute;
}
#masthead h2 {
position: absolute;
left: 150px;
}
#masthead p {
position: absolute;
right: 10px;
}
#main {
margin-top: 40px;
}
The problem is that all of the absolute positioning removes the elements from the document flow. That means your header has a height of 0px, but everything is still positioned relative to it.
Just give your masthead a height.
JSFiddle
You just need to wrap your elements in their own containers so you can position them a little bit better. You will probably want to define some heights in this also. Including a height on #masthead
Assuming you need a responsive design:
<header id="masthead" role="banner">
<section class="logo">
<img src="jep.jpeg" alt="My Page">
</section>
<section class="title">
<h2>Journal of Electronic Publishing</h2>
</section>
<section class="sign-in">
Sign in Register
</section>
</header>
.logo {
width: 30%;
float: left;
margin-right: 5%;
box-sizing: border-box;
}
.title {
width: 30%;
float: left;
margin-right: 5%;
box-sizing: border-box;
}
.sign-in {
width: 30%;
float: left;
margin-right: 0;
box-sizing: border-box;
}
Note that the total 100% is assuming you include the margins in that calculation. So, 30+30 = 60 + 5 + 5 = 70 + 30 = 100%
Edit: Now that I can see your CSS, your specific issue is the use of position:absolute;. Removing these should get you along the correct path.
I suggest using a table layout. Using 1-row tables for styling is a bit frowned upon by some, but this seems to work:
HTML:
<body>
<header id="masthead" role="banner">
<table>
<tr>
<td><img src="jep.jpeg" alt="My Page"></td>
<td><h2>Welcome!</h2></td>
<td><p>Sign in Register</p></td>
</tr>
</table>
</header>
<div id="main" role="main">
<p>Testing</p>
</div>
</body>
</html>
and CSS:
#masthead {
width: 100%;
}
#masthead table {
width: 100%;
}
#main {
margin-top: 40px;
}
EDIT: Using divs.
This is a bit messy, but it works. It's been a while since I've used div for positioning like this.
HTML:
<body>
<header>
<div class="col">
<div class="content">
<img src="jep.jpeg" alt="My Page">
</div>
<div class="content">
<h2>Welcome!</h2>
</div>
<div class="content">
<p>Sign in Register</p>
</div>
</div>
</header>
<div id="main" role="main">
Testing
</div>
</body>
</html>
CSS:
header {
width: 100%;
}
.col {
height: 100px;
position: relative;
}
.content {
float: left;
width: 33.3%;
}
#main {
margin-top: 50px;
}

alignment issues in float divs

I have a problem with aligning images in div which is floated to left.
in the code below how to align "logo.jpg" to center vertically and horizentally?
Thank you.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" />
<title>layout</title>
<style type="text/css">
body,div,ul,li,dl,dt,dd,ol,p,h1,h2,h3,h4,h5,h6,form {font-size:11px; margin:0;padding:0;line-height: 150%;}
ul,ol,dl {list-style:none}
img {border:0;vertical-align:top;}
ul {list-style:none; padding:0; margin:0;}
#_layout {margin:0 auto; width:980px;}
#_top_cntr {width:980px; height:150px; background:#fff; margin-bottom:10px;}
/* top_cntr */
#_brand {height:110px; background:url(images/top_bg.jpg);}
#_left_logo {width:280px; height:110px; float:left; padding-left:10px;}
#_right_logo {width:680px; height:110px; float:right; margin-left:10px;}
</style>
</head>
<body>
<div id="_layout">
<div id="_top_cntr">
<div id="_brand">
<div id="_left_logo">
<img src="images/logo.jpg"/>
</div>
<div id="_right_logo">
</div>
</div>
</div>
</div>
</body>
</html>
you have to use vertical-align: middle but as it works only on element with display: table-cell you need to add it also, like in this example (I have modified the style of #_left_logo).
You can take a look at this article, there are a couple of other methods how you can vertically center the content of your div
I think this would typically be done through CSS.
<div id="_brand">
<a id="_left_logo" class="img-replacement"> <!-- best practice to make the logo a link -->
Now you can even throw text Here as a fall back when the image doesn't work
</a>
<div id="_right_logo">
</div>
</div>
And then:
.img-replacement {
display: block;
text-indent: -9999px;
}
#_left_logo {
width:280px;
height:110px;
float:left;
padding-left:10px;
/* and then: */
background: url("/images/logo.png") center center no-repeat;
}
If you know the height of the height of the image, you can do it with position: absolute. For example, image size 200x60.
#_left_logo {width:280px; height:110px; float:left; padding-left:10px; position: relative;}
#_left_logo img {width:200px; height:60px; position: absolute; top: 50%; left: 50%; margin-top: -100px; margin-left: -30px; }

Resources