Cannot center img horizontally - html

I'm trying to center image horizontally in this sample http://jsfiddle.net/3k3CC/1545/ , but I'm unsuccessfull. It only works when container is wider than image, but what I need is for the image to be centered even if the container is thinner than the image itself.
Sample CSS:
#artiststhumbnail {
width:100px;
height:308px;
overflow:hidden;
border-color:#DADADA;
border-style:solid;
border-width:thin;
background-color:pink;
}
#artiststhumbnail:hover {left:50px }
#genre {
font-size:12px;
font-weight:bold;
color:#2A2A2A
}
#artiststhumbnail a img {
display : block;
margin : auto;
}
Sample HTML:
<div id="artiststhumbnail">
<a href="#">
<!--image here-->
<img src="http://goo.gl/QrKCc" height="100%" alt="artist" border="1" />
</a>
</div>

You should add text-align: center; to #thumbnailwrapper and set display: inline-block; to #artiststhumbnail.
DEMO
EDIT:
I'm sorry I don't unsderstand your question at the beggining. Use this simple CSS:
#artiststhumbnail a img {
position: relative;
left: 50%;
transform: translateX(-50%);
}
DEMO2

you can add to the #artiststhumbnail this code,
width: 100%;
padding: 15px;
http://codepen.io/jrey/pen/waXgBO updated code :)
try this CSS.. comment your css and try this please...
#artiststhumbnail {
width:100px;
height:308px;
overflow:hidden;
border-color:#DADADA;
border-style:solid;
border-width:thin;
background-color:pink;
width: 100%;
padding: 15px;
display: inline-flex;
flex-flow: row wrap;
justify-content: center;
}
#artiststhumbnail:hover {left:50px }
#genre {
font-size:12px;
font-weight:bold;
color:#2A2A2A
}
#artiststhumbnail a {
display: flex;
justify-content: center;
}
#artiststhumbnail a img {
display : block;
margin : 15px;
width: 200px;
height: auto;
}

DEMO
CSS
#thumbnailwrapper {
color:#2A2A2A;
margin-right:5px;
border-radius:0.2em;
margin-bottom:5px;
background-color:#E9F7FE;
padding:5px;
border-color:#DADADA;
border-style:solid;
border-width:thin;
font-size:15px;
/* The following rules make this a flexbox container */
display: flex;
display: -webkit-flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
#thumbnailwrapper:hover {
box-shadow:0 0 5px 5px #DDDDDD
}
#artiststhumbnail {
width:100px;
height:308px;
overflow:visible; // Changed from hidden to visible
border-color:#DADADA;
border-style:solid;
border-width:thin;
background-color:pink;
}
#artiststhumbnail:hover {
/*left:50px*/
}
/* No position: fixed. absolute, or relative; left: 50px has no effect on a static element */
#genre {
font-size:12px;
font-weight:bold;
color:#2A2A2A
}
#artiststhumbnail a img {
/* display: block;
margin: auto; */
}
/* Removed margin and block display to demonstrate that flexbox container and it's children elements are not easily influenced by other element properties and attributes */
HTML
<div id="thumbnailwrapper">
<div id="artiststhumbnail"> <a href="#">
<!--image here-->
<img src="http://goo.gl/QrKCc" height="100%" alt="artist" border="1" />
</a>
</div>
</div>

you could try your image width to 100%
#artiststhumbnail a img {
display : block;
margin : auto;
width:100%;
}

Not sure exactly what you mean by center but here it goes. If you want the image itself to center, use background-size: cover and background-position: center to accomplish this.
<div id="thumbnailwrapper">
<div id="artiststhumbnail">
<a href="#">
<!--image here-->
<div></div>
</a>
</div>
</div>
#artiststhumbnail a div {
display : block;
margin : auto;
height: 100%;
width: 100px;
background-image: url('http://goo.gl/QrKCc');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
JSFiddle
EDIT: sorry, had the wrong jsfiddle link attached

Related

Position h1 with border in center of picture

I am making a banner with Bootstrap 3, and the end result should look like this, and have a height off 350px;
A demoversion off the code I have until now can be seen here.
I am not sure how I can make the h1 and p tag align in the middle? At the moment they are at the top, and I cannot figure out how to position them.
When I look on < 768px the picture is way to big. Is that because of the cover in the header tag? How can I make that picture get smaller on < 768px?
header {
height:100vh;
background-image:url('https://img2.picload.org/image/daapipii/header-2.jpg');
background-size:cover;
background-position:center center;
background-attachment:fixed;
}
#media (max-width:767px) {
header {
/*height:100%;*/
background-attachment:inherit;
height:100vh;
}
}
header h1 {
/*background-color:rgba(255,255,255,0.36);*/
padding:10px;
text-align:center;
border:5px solid rgba(108,111,119,0.19);
/*box-shadow:0px 0px 1px #333;*/
}
header h1 {
font-family:'Aladin';
font-size:71px;
color:#fff;
text-shadow:1px 1px 1px #333;
}
#media (max-width:767px) {
header h1 {
font-size:41px;
}
}
header p {
text-align:center;
padding-top:20px;
font-size:26px;
font-weight:600;
font-family:'Source Sans Pro';
/*letter-spacing:2px;*/
color:#f5f5f5;
text-shadow:1px 1px 1px #111;
padding-bottom:20px;
}
#media (max-width:767px) {
header p {
font-size:17px;
}
}
header .row.no-space p {
text-align:right;
}
/* Social Media Position */
header .row.no-space {
margin-top:initial;
bottom:0px;
position:fixed;
right:20px;
}
header .fa {
color:#fff;
text-shadow:1px 1px 1px #333;
padding:7px;
}
<header>
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1 text-bg">
<h1>Welcome on free feelings</h1></div>
<div class="col-md-12">
<p>Animated header with scroll engine </p>
</div>
</div>
<div class="row hidden-xs no-space">
<div class="col-md-12">
<p>
<i class="fa fa-instagram"></i>
<i class="fa fa-facebook-official"></i>
<i class="fa fa-pinterest-square"></i>
</p>
</div>
</div>
</div>
</header>
To vertically center the content on the page, I would use CSS flexbox on your <header> element like this:
header {
display: flex;
align-items: center;
}
Regarding the size of the image, would it be acceptable to align the text to the top of the page on mobile?
If so, then I would suggest removing the height: 100vh and flexbox from the header on mobile. Instead you need a responsive image in relation to the screen width but since you are using a background image you can do this:
header {
background-image: url(...);
background-size: cover;
height: 0; /* Use padding-bottom to define height */
padding-bottom: 62%; /*Aspect ratio of image (h/w*100)*/
}
Add this at the bottom of your CSS:
body {
margin: 0
}
h1 {
position: absolute;
}
.text-bg {
display: flex;
justify-content: center;
}
.wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Doing this old school you can use the display: table approach (which is not what is recomended these days):
header {
height:100vh;
background-image:url('https://img2.picload.org/image/daapipii/header-2.jpg');
background-size:cover;
background-position:center center;
background-attachment:fixed;
display: table;
width: 100%;
}
.container {
display: table-cell;
vertical-align: middle;
}
The current recommended way usually is with Flex which is cleaner and nicer. If you want to keep all as is then you just change your header CSS to:
header {
height:100vh;
background-image:url('https://img2.picload.org/image/daapipii/header-2.jpg');
background-size:cover;
background-position:center center;
background-attachment:fixed;
display: flex;
align-items: center;
}

Header with Left, Right and Centered elements

I am having trouble creating a header with left, right and centered elements - I keep trying but i am unable to get the elements positioned in that specific layout with the title being centered at all times
Can anybody help? I feel that I am close but i can't get it to work!
CODE HERE: https://jsfiddle.net/4a25nqb4/
HTML:
<div class="container">
<div class="top">
<div id="social">
<span class="fa fa-facebook"></span>
<span class="fa fa-instagram"></span>
<span class="fa fa-youtube"></span>
</div>
<p id="website">www.AlmostFreeFurniture.com</p>
<h1 id="title">Almost Free Furniture</h1>
</div>
</div>
CSS:
* {
margin:0 auto;
padding:0px;
}
.top {
height: 40px;
background-color:black;
color:red;
margin-top:4px solid orange;
margin-top:10px;
display: table;
width: 100%;
text-align:center;
}
#social, #website, #title {
display:inline-block;
vertical-align: top;
}
#social {
float:right;
}
#website {
float:left;
}
#title {
text-align: center;
}
The floats effect the amount of available area to center in. Because the item on the left is larger then the right, the center between them is closer to the right. If you'll position the left and right items absolutely, the centering mechanism will ignore them (fiddle):
#social {
position: absolute;
right: 0;
}
#website {
position: absolute;
left: 0;
}
I would use display: flex instead of display: table, and then you can push the #social over to the right with order:
* {
margin:0 auto;
padding:0px;
}
.top {
height: 40px;
background-color:black;
color:red;
margin-top:4px solid orange;
margin-top:10px;
display: -webkit-flex;
display: flex;
width: 100%;
text-align:center;
}
#social, #website, #title {
-webkit-flex: 1;
flex: 1;
vertical-align: top;
}
#website { overflow-x: hidden; }
#social {
text-align: right;
-webkit-order: 1;
order: 1;
}
#title {
text-align: center;
}
https://jsfiddle.net/4a25nqb4/
To give #title more room, you can either give it a flex: 2 or something like flex-basis: 50% or thereabouts.
Set width of this elements to 33.33%
#social, #website, #title {
display:inline-block;
vertical-align: top;
width: 33.33%;
}
Next give text-align for elements left, center and right.

inline 2 spans inside div

html
<div id="container">
<div>
<span>Visit website</span>
<span>View project</span>
</div>
</div>
css
#container {
width: 100%;
padding:0;
background-color: green;
}
div { padding: 0 20px; width: 0px; background:red;overflow:visible; text-align: center;}
span {
background:#222;
color:#fff;
display:inline-block;
margin:10px 10px 0 0;
padding:5px 10px
}
Demo: http://jsfiddle.net/cePe3/445/
How to make the 2 span to be inline with each other in the middle of the container DIV!
Note: code structure must be as its.
thank you
You can use a more modern solution: flexbox
Add display: flex; justify-content: center; to #container and to #container div. It's magic.
Working fiddle: http://jsfiddle.net/cePe3/448/
You have to set your div width to auto
div { padding: 0 20px; width: auto; background:red;overflow:visible; text-align: center;}
https://jsfiddle.net/cePe3/446/
you have to set div width for inline span
div
{ padding: 0 20px;
width: 350px;
background:red;
overflow:visible;
text-align: center;
}
DEMO
make the position absolute for the spans, then add jquery to center the spans, even on resizing of the window:
$( window ).resize(function() {
shuffle();
});
function shuffle() {
$('span').each(function(){
$(this).css('left',($('#container').width()-$(this).width()) / 2);
});
$('#container').find('div').css('height',$('span:first').height()*6);
}
shuffle();
#container {
width: 100%;
padding:0;
background-color: green;
}
div { padding: 0 20px; width: 0px; background:red;overflow:visible; text-align: center;}
span {
position: absolute;
background:#222;
color:#fff;
display:inline-block;
margin:10px 10px 0 0;
padding:5px 10px
}
span:nth-child(2) {
top: 60px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div>
<span>Visit website</span>
<span>View project</span>
</div>
</div>

Centering a specific sized div within a div

I have tried the normal techniques for horizontally centering a div within a div. I must still be doing something wrong. Check out the following jsFiddle and help me center the depiction of a tank under the "Inner Level" heading. For convenience I have added a border to all the div tags.
jsFiddle Source
HTML:
<body>
<a href="#">
<div class="div-inline-block" style="text-align:left;width:150px;">
<h2>TEST</h2>
<p>A description</p>
</div>
<div class="div-inline-block"><p><b>Last Update:</b><br />???</p></div>
<div class="div-inline-block"><p><b>Flow Rate:</b><br />??? gpm</p></div>
<div class="div-inline-block"><p><b>Inner Level:</b><br />???</p><div class="tank-level-outer"><div class="tank-level-inner"></div></div></div>
<div class="div-inline-block"><p><b>Outer Level:</b><br />???</p></div>
<div class="div-inline-block"><p><b>Battery Voltage:</b><br />???</p></div>
<div class="div-inline-block"><p><b>Rainfall:</b><br />???</p></div>
<div class="div-inline-block" style="text-align:left;width:100%;"><p><b>Notifications:</b><br />???<br />???</p></div>
</a>
</body>
CSS:
a{
text-decoration:none;
}
div{
border:1px solid red;
}
.div-inline-block {
text-align: center;
height: 100%;
display: inline-block;
vertical-align: top;
margin-left:15px;
width: 100px;
}
.tank-level-outer {
border:1px solid black;
width:25px;
height:32px;
display:table-cell;
vertical-align:bottom;
margin:0 auto;
}
.tank-level-inner {
background-color:dodgerblue;
width:25px;
height:25px;
}
add this
.div-inline-block {
text-align: center;
height: 100%;
display: inline-block;
vertical-align: top;
margin-left:15px;
width: 100px;
margin:0 auto;
}
.tank-level-outer {
border: 1px solid black;
width: 25px;
height: 32px;
margin: 0 auto;
padding: 0;
}
.tank-level-inner {
background-color:dodgerblue;
width:25px;
height:25px;
margin-top:7px;
display: table;
vertical-align: bottom;
}
Updated Fiddle
http://jsfiddle.net/czgoxafr/22/
Add this:
.tank-level-outer {
display: table;
margin: 0 auto;
}
change the css of div .tank-level-outer and add margin zero auto , remove display table-cell
.tank-level-outer {
border: 1px solid #000;
width: 25px;
height: 32px;
margin: 0px auto;
vertical-align: bottom;}
This works:
.tank-level-outer {
width:25px;
height:32px;
display:block; // not necessary since this class is applied to a div
vertical-align:bottom;
margin:0 auto;
}
try this:
//css
.firstDiv{
padding:25% 25% 25% 25%
height:400px;
width:400px;
background-color:red;
}
.secondDiv{
height:200px;
width:200px;
background-color:pink;
}
//html
<div class="firstDiv">
<div class="secondDiv">
</div>
</div>

Not able to align the elements in-line

I am trying to make a footer for my website.
I want 2 divs and one list to be inline in this footer. But that is not happening. I used the css the CSS Property display:inline-block. What happens is that the divs get aligned but the list element just shifts a bit down.
Here is the jsFiddle Link: http://jsfiddle.net/tw2Wp/2/.
If you see the JSfiddle, you will that the three divs with class footerContents are not aligned in-line.
Could someone please explain me why this is happening? Is it right thing to use inline-block for this thing or is there some better way(I'm sure that there is)?
Please add vertical-align to .footerContents:
.footerContents {
display: inline-block;
width: 200px;
height: 200px;
padding: 5px;
margin-top: 30px;
margin-left: 50px;
margin-bottom: 5px;
background-color: red;
vertical-align: top; /* <<< */
}
Demo
Would you use the css3 flexbox module, like this:
HTML
<div id="footer">
<div class="footerContents">
...
</div>
<ul class="footerContents">
...
</ul>
<div class="footerContents">
...
</div>
</div>
<div>Copyright © </div>
CSS
#footer {
height:auto;
width:100%;
background-color:#666;
background-image:url(footer_shade.png);
background-repeat:no-repeat;
background-position:50% 0;
border-top:1px solid #FFF;
color:#fff;
font-family:'Bree Serif',serif;
font-size:16px;
line-height:20px;
display:-moz-box;
display:-webkit-box;
display:-ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: justify;
-moz-box-pack: justify;
-ms-flex-pack: justify;
-webkit-justify-content: space-between;
justify-content: space-between;
}
.footerContents {
width:200px;
height: 200px;
padding:5px;
margin-top: 30px;
margin: 20px;
margin-bottom: 5px;
background-color: red;
-moz-flex-box:1;
-webkit-flex-box:1;
-ms-flex:1 1 200px;
-webkit-flex: 1 1 200px;
flex:1 1 200px;
}
Please view the demo. and some about the flexbox, if you want to know,please will click here and here.
A solution would be to float the elements rather than using inline-block, see working example here: http://jsfiddle.net/tw2Wp/2/
Note that I also added an extra div with the class clear which will clear the floats. By doing this your background will appear again. Because of the floated elements the parent div doesn't have a clue how large it should be (so it's height 0).
.clear{
clear:both;
}
You can do like this:
HTML
<div id="footer">
<div class="footerContents">
...
</div>
<ul class="footerContents">
...
</ul>
<div class="footerContents">
...
</div>
</div>
<div>Copyright © </div>
CSS
#footer {
height:auto;
width:100%;
background-color:#666;
background-image:url(footer_shade.png);
background-repeat:no-repeat;
background-position:50% 0;
border-top:1px solid #FFF;
color:#fff;
font-family:'Bree Serif',serif;
font-size:16px;
line-height:20px;
**white-space: nowrap;**
}
.footerContents {
display:inline-block;
width:200px;
height: 200px;
padding:5px;
margin-top: 30px;
margin-left: 50px;
margin-bottom: 5px;
background-color: red;
**vertical-align: top;
white-space: normal;**
}
please view the new demo.