Placing an image in the middle of a div - html

I want to place a 100px x 100px image in the middle of a 210px width div. Please help. 1em=10px
#sidebox > .centered {
text-align: center;
margin-left: em;
height: 10em;
width: 10em;
margin-top: 10em;
position: absolute;
background-position: center center;
background-size: cover;
border: 0.5em solid;
border-radius: 100%;
border-color: #2c3e50;
background-image: url();
}
<div id="sidebox">
<p>
<div class="centered"></div>
</p>

You can align your image <img> center of outer div by below property's
margin:0 auto; OR this only works for aligning text or image text-align:center;

Might you just need to centre align 100x100px image in 210px width wrapper.
You can centre align inline element using text-align:center;
HTML
<div id="sidebox">
<img width="100px" height="100px" src="https://lh5.googleusercontent.com/-y2IopJORVh4/AAAAAAAAAAI/AAAAAAAAAD0/ePzQLLN6hKM/photo.jpg"/>
</div>
CSS
#sidebox{
width: 210px;
text-align:center;
background: #eee;
}
JSFIDDLE DEMO

The most important point, mentioned by others, is that text-align:center must apply to the to the box the image is being centered in, not the image itself.
You have other problems:
position:absolute will shrink the effective image width to zero for centering purposes, so the left edge will be centered.
You are using a <div> like an <img>, but it won't center like an <img> unless it is styled with display:inline-block.
Before & After:
Before:
#sidebox {
font-size:10px; /*ADDED so 1em=10px*/
width: 210px; /*ADDED*/
border: 2px dashed red; /*ADDED*/
}
#sidebox > .centered {
text-align: center;
margin-left: em;
height: 10em;
width: 10em;
margin-top: 10em;
position: absolute;
background-position: center center;
background-size: cover;
border: 0.5em solid;
border-radius: 100%;
border-color: #2c3e50;
background-image: url(https://lh5.googleusercontent.com/-y2IopJORVh4/AAAAAAAAAAI/AAAAAAAAAD0/ePzQLLN6hKM/photo.jpg);
}
<div id="sidebox">
<p>
<div class="centered"></div>
</p>
***TEXT ADDED***
</div>
After:
#sidebox {
font-size:10px;
width: 210px;
border: 2px dashed red;
text-align: center; /*ADDED*/
}
#sidebox > .centered {
display:inline-block; /*ADDED*/
/*text-align: center; REMOVED*/
margin-left: em;
height: 10em;
width: 10em;
margin-top: 10em;
/*position: absolute; REMOVED*/
background-position: center center;
background-size: cover;
border: 0.5em solid;
border-radius: 100%;
border-color: #2c3e50;
background-image: url(https://lh5.googleusercontent.com/-y2IopJORVh4/AAAAAAAAAAI/AAAAAAAAAD0/ePzQLLN6hKM/photo.jpg);
}

Related

Is there a way to make a two-sided inner shadow in a div?

Is there any possible way to make a two-sided inner shadow by shadow-box in css like the image below?
You can use a background image to fill the div:
#custom{
min-width: 200px;
min-height:200px;
width : 100%;
background-image : url("http://i.stack.imgur.com/y6HMs.png");
background-repeat : no-repeat;
background-size: contain;
}
<div id="custom">
</div>
And make sure to give it background-size: contain; so the image can fit all the div.
EDIT:
This is a snippet using a border in the div, so you can see that the image is filling all the space.
#custom {
min-width: 200px;
min-height: 200px;
width: 100%;
background-image: url("http://i.stack.imgur.com/y6HMs.png");
background-repeat: no-repeat;
background-size: contain;
border: 1px solid black;
}
<div id="custom">
</div>
You can use pseudo elements for both shadows:
div {
box-shadow: inset 0 0 2em -.5em gray;
line-height: 3em;
text-align: center;
position: relative;
overflow: hidden;
}
div:before,
div:after {
content: '';
display: inline-block;
width: 40%;
height: 100%;
position: absolute;
left: 5%;
top: -100%;
border-radius: 50%;
box-shadow: 0 0 2em #aaa;
}
div:after {
left: auto;
right: 5%;
}
div[contenteditable] {
margin-top: 3em;
display: inline-block;
outline: none;
}
<div>hello world</div>
<div contenteditable>TYPE HERE...and watch the shadows</div>
These shadows have a responsive behavior. Their size expands when the div width expands.

Center text vertically: inline-block div next to text

Link to fiddle: https://jsfiddle.net/ernhep2a/2/
I am trying to center the image and text in the middle of the div. When I do so the text is no longer centered vertically, it drops down. How can one achieve so that the text is centered vertically?
html:
<div>
<div class="secureHome-sessions-top-status0">
<div class="secureHome-sessions-top-status-img0"></div>
Status
</div>
</div>
<div>
<div class="secureHome-sessions-top-status">
<div class="secureHome-sessions-top-status-img"></div>
Status <!-- <-- this is not centered vertically anymore -->
</div>
</div>
css:
div.secureHome-sessions-top-status0 {
float:left;
width: 10%;
margin-left:2%;
height: 20px;
background:lightblue;
}
div.secureHome-sessions-top-status-img0 {
float:left;
width: 20px;
height: 20px;
background: red;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
div.secureHome-sessions-top-status {
float:left;
width: 10%;
margin-left:2%;
height: 20px;
background:lightblue;
text-align:center;
}
div.secureHome-sessions-top-status-img {
display:inline-block;
width: 20px;
height: 20px;
background: red;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
Try adding this CSS to your non-floating thumbs up div:
vertical-align: middle;
When you set the second div to "inline-block" that placed the div in your text string instead of beside it. Since the div is taller than the text, it's stretching the line height and the text just falls in place afterwards. The vertical-align:middle will prevent that by centering your div against the rest of the string.
When you use inline-block, then you make a wrapper which will behave like span and text would always start from the bottom. to fix this issue please use vertical-align:middle in class div.secureHome-sessions-top-status-img issue will get resolve.
But I would recommend you to use all the text and images in separate blocks. It will help you in maintaining the content.
<div>
<div class="secureHome-sessions-top-status0">
<div class="secureHome-sessions-top-status-img0"></div>
<div>this is center</div>
</div>
</div>
<div>
<div class="secureHome-sessions-top-status">
<div class="secureHome-sessions-top-status-img"></div>
<div>this isn't center</div>
</div>
</div>
CSS
<style>
div.secureHome-sessions-top-status0 {
float: left;
width: 20%;
margin-left: 2%;
height: 20px;
background: lightblue;
}
div.secureHome-sessions-top-status-img0 {
float: left;
width: 20px;
height: 20px;
background: red;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
vertical-align: middle;
}
div.secureHome-sessions-top-status {
float: left;
width: 20%;
margin-left: 2%;
height: 20px;
background: lightblue;
text-align: center;
}
div.secureHome-sessions-top-status-img {
display: inline-block;
width: 20px;
height: 20px;
background: red;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
vertical-align: middle;
}
</style>
In img0 you're using float. Float elements are not in the page page normal "flow". So when you use float the image is not interfering to the text.
you could try vertical-align but that probably will dislocate the image a bit.
It will be better if you wrap the image and the text in a separated div and just center that div then you can make the float with no problem.
div.secureHome-sessions-top-status0 {
float:left;
width: 10%;
margin-left:2%;
height: 20px;
background:lightblue;
}
div.secureHome-sessions-top-status-img0 {
float:left;
width: 20px;
height: 20px;
background: red;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
.in-center {
display: inline-block;
}
div.secureHome-sessions-top-status {
float:left;
width: 30%;
margin-left:2%;
height: 20px;
background:lightblue;
text-align:center;
}
div.secureHome-sessions-top-status-img {
float: left;
width: 20px;
height: 20px;
background: red;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
<div>
<div class="secureHome-sessions-top-status0">
<div class="secureHome-sessions-top-status-img0"></div>
Status
</div>
</div>
<div>
<div class="secureHome-sessions-top-status">
<div class="in-center">
<div class="secureHome-sessions-top-status-img"></div>
Status
</div>
</div>
</div>
http://jsfiddle.net/8hmwojk5/
This is probably better suited as a comment as it does not answer the question within the constraints presented, but I am not yet able to leave comments; nonetheless, I hope it is helpful.
If you are able to use Flexbox, this sort of layout becomes trivial to implement.
.container {
display: flex;
flex-flow: row nowrap;
align-items: center;
justify-content: center;
background: lightblue;
margin: 0.5rem;
padding: 0 0.5rem;
width: 20%;
}
.icon {
flex: 0 0 20px;
height: 20px;
background: red;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
.text {
padding: 0 0.5rem;
}
<div class="container">
<div class="icon"></div>
<div class="text">This is centered.</div>
</div>

How will I write text in the center of the square

Here is my link http://jsfiddle.net/sanand29/7fh2em4f/
<div class="link1">
<div>
the css part corresponding to it
.link1
{
display: block;
margin-top: 15%;
width: 78%;
margin-left: 26%;
}
a.square1
{
width: 50%;
height: 50%;
border: 1px solid red;
padding: 9%;
margin-top: 0%;
background-color: #FAFAFA;
opacity: 0.5;
}
How will I put any text in the center of the square keeping it responsive.
Try this
<div class="link1">
<div class="square1">Text to be centred</div>
</div>
And the css
div.link1 {position: relative; width: 200px; height: 200px; border: 1px solid red;}
div.square1 {height: 20px; text-align: center; margin: auto; position: absolute; top: 0px; left:0px; bottom:0px; right:0px;}
This will keep the text in the middle of the box. In fact it keeps the div with class square1 centred - the text may overflow this div, depending on the size of font you choose. If you set the height of the div to the height of your font you can't go wrong.

Vertically center child container

I am having a parent and child container, where I need to align the child container to middle(both horizontal and vertical).
HTML:
<section class="travelPlace">
<div>
</div>
</section>
CSS:
.travelPlace {
width: 50%;
height: 100%;
float: left;
}
.travelPlace > div {
width: 53.4375%;
margin: 0 auto;
background: url('../images/globe.png') center no-repeat;
height: 344px;
background-size: contain;
}
.travelPlace {width: 100px; height: 250px; position:relative;background:black;}
.travelPlace > div {position: absolute;
right: 0;
top: 0;
bottom: 0;
margin: auto;
color: #fff;
background: red;
height: 19px;
text-align: center;
display: block;
width: 100%;}
Hope it was helpful.Rajesh
Maybe you can do something wih this code, I made the background yellow and the content red to make it easier to see.
.travelPlace {width: 50%; height: 800px; background: yellow;}
.travelPlace > .something {width: 53.4375%; background: red; height: 344px; margin-top: -172px;}
.travelPlace > .somepusher{
display: inline-block;
height: 50%;
width: 100%;
}
<div class="travelPlace">
<div class="somepusher"></div>
<div class="something">
</div>
</div>
Basicly what I did:
Add a new div which is 50% the height of the main Div, then add a negative margin to the top of your inner div which is 50% the height of the inner div (172px)
You could use display:table-cell for your parent item and display: inline-block to your child item. Here is a snippet...
.travelPlace {
display: table-cell;
width: 50%;
height: 100%;
vertical-align: middle;
text-align: center;
background: black;
padding: 50px 0px 50px 0px;
}
.place {
display: inline-block;
width: 300px;
height: 300px;
text-align: left;
background: #CCC;
}
<section class="travelPlace">
<div class="place">
</div>
</section>

CSS Center (vertical on line-height and horizontal on page) Div's

I'm attempting to make a header with the div's centered with a logo in the middle. With the Logo div hanging over, which I've successfully done. I can not however figure out how to place the logo div in the middle of the link divs for the life of me.
Any help would be appreciated, google searching hasn't gotten me any luck, but I'm not sure I know what to search for. I'm not good at making things look pretty normally :(
Page Layout:
Header (overlay)
Content (z-index: -1)
Image of site without a float declared on .headerLogo
Image of site with float: left declared on .headerLogo
html:
<div class='header'>
<div class='headerLink'>Home</div>
<div class='headerLink'>Contact</div>
<div class='headerLogo'></div>
<div class='headerLink'>Menu</div>
<div class='headerLink'>Connect</div>
</div>
<div class='content'>
</div>
css:
*{
margin: 0;
}
body,html{
background-color: #000;
color: #FFF;
text-align: center;
font-family: calibri;
height: 100%;
overflow:auto;
}
.header{
top:0;
height: 100px;
line-height: 100px;
width: 100%;
display: inline-block;
border-bottom: lime 10px solid;
margin:0;
position: static;
background-color: #000;
font-size: 24px;
}
.headerLink{
display: inline-block;
float: center;
margin-left: 60px;
margin-right: 60px;
margin-top: 0px;
margin-bottom: 0px;
}
.headerLogo{
display: inline;
float: left;
position: static;
background-image: url('/images/logo.jpg');
background-image: no-repeat;
background-position: left top;
background-size: 100%;
margin-left: auto;
margin-right: auto;
background-color: lime;
color: lime;
width: 200px;
height: 200px;
line-height: 100px;
}
.content{
z-index: -1;
float: left;
text-align: center;
min-height: 100%;
width: 100%;
margin-top: -110px;
margin-bottom: -50px;
background-color: #333343;
}
You've got a couple ways to go about this. I think the easiest way is to use a fake placeholder to make the horizontal space between menu items in the middle, then have the logo be absolutely positioned on top.
HTML:
<div class='header'>
<div class='headerLink'>Home</div>
<div class='headerLink'>Contact</div>
<div class='headerLogo'></div>
<div class='headerLink headerLogoFake'></div>
<div class='headerLink'>Menu</div>
<div class='headerLink'>Connect</div>
</div>
<div class='content'>
</div>
CSS:
.headerLogo{
display: inline;
float: left;
position: static;
background-image: url('/images/logo.jpg');
background-image: no-repeat;
background-position: left top;
background-size: 100%;
background-color: lime;
color: lime;
width: 200px;
height: 200px;
line-height: 100px;
position:absolute;
top:0;
left:50%;
margin-left:-100px;
}
.headerLogoFake {
width:200px;
}
As you probably know, if you just leave it as you have, without the float, it'll be in the middle, but it'll push the green bottom border down. This will place a fake empty thing in the middle, but at the same height as the menu items, so won't push it down. It'll add the logo in on top.
Working Fiddle
.headerLogo{
position:absolute;
display: inline;
float: left;
left 50%
position: static;
background-image: url('/images/logo.jpg');
background-image: no-repeat;
background-position: left top;
background-size: 100%;
margin-left: -50px;
margin-right: auto;
background-color: lime;
color: lime;
width: 100px;
height: 100px;
line-height: 100px;
}
Your mark-up provided did not produce the result you were looking for, so I applied the bare minimal styling to accomplish what you are asking for.
Most importantly float:center; does not exist.
If you give elements a property of display:inline-block;, then you can use text-align:center; around the container to center all of the elements.
.header {
text-align:center;
}
.headerLink {
display:inline-block;
padding:10px;
}
.headerLogo {
display:inline-block;
height:100px; width:100px;
background:red;
margin:0px 10px;
}
Then, to line them up with your logo, vertically, give them a vertical-align:top property and set the margin to near half the width of the logo
http://jsfiddle.net/5V29a/1/ - UPDATED DEMO