Center background for inline block - css

Given HTML:
<div id="title">Text</div>
<div class="circlecontainer">
<div class="circle">Hello</div>
</div>
And CSS:
div {
display: inline-block;
}
.circlecontainer {
background: #E0E;
text-align:center;
line-height:200px;
width:200px;
height:200px;
}
.circle {
width:100px;
height:100px;
border-radius: 50px;
font-size:20px;
color:#fff;
background:#000
}
.circle:hover {
width:200px;
height:200px;
border-radius: 100px;
}
JSFiddle: http://jsfiddle.net/S329D/4/
I would like the black circle to be vertically centered when not hovered over.
But when I set vertical-align:middle, the text looks screwed up:
http://jsfiddle.net/S329D/5/
Why does this happen?

It's because the line-height is still 200px. Try this:
div {
display: inline-block;
}
.circlecontainer {
background: #E0E;
text-align:center;
line-height:200px;
width:200px;
height:200px;
}
.circle {
line-height: 100px;
position: relative;
vertical-align: middle;
width:100px;
height:100px;
border-radius: 50px;
font-size:20px;
color:#fff;
background:#000
}
.circle:hover {
width:200px;
line-height: 200px;
height:200px;
border-radius: 100px;
}
http://jsfiddle.net/S329D/7/

If you remove the border-radius the box is not centered, so the circle is not centered too
You need to work with the margin-top. I've edited your fiddle

Simply add a span tag inside the circle div element with style "vertical-align: middle;" and remove the "vertical-align: middle;" of the circle class.
<div class="circle">
<span style=" vertical-align: middle;">Hello</span>
</div>
The problem is because of the height of the circle class change also affect the text element. With the span, it does not.

Related

Centering a div both horizontally and vertically at all times on a responsive parent

I have a header that I would like to keep centered inside a parent image div both horizontally and vertically at all times when the parent div does not have a fixed width and height but instead has a responsive width and height using Twitter Bootstrap 3.1's responsive columns.
HTML:
<div id ="firstholder" class= " col-sm-4 col-md-4 col-lg-4">
<a href="home.html" title="Home" class="imglink">
<div class="item1"><h1 class="slickfont1" >Home</h1>
</div><img src="/images/slide2.JPG" alt="City Lights Image" class="img-responsive" >
</a>
</div>
#firstholder {
display:inline-block;
float:left;
margin:0;
padding:0;
height:auto;
position:relative;
}
a.imglink {
background: #fff;
display: inline-block;
width:100%;
height:auto;
position:relative;
}
.item1 {
height:150px;
width: 150px;
margin: 0 auto;
position:absolute;
z-index:100;
vertical-align:middle;
}
.slickfont1 {
z-index:10;
color: #fff;
position:absolute;
font-family: 'Bowlby One SC', cursive;
font-size: 37px;
font-weight:lighter;
position: absolute;
text-shadow:0 0 0 transparent, -2px 2px 2px #1542bd;
}
Thanks :)
You can use this nice method by Chris Coyier at CSS Tricks, he uses percentages and CSS3's transform:translate to achieve what you need. Centering Percentage Width/Height Elements
Now as you're using Bootstrap, so you're to tweak it for yourself.
The code from Chris Coyier:
.center {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 40%;
height: 50%;
}
Better to go to CSSTricks (using above link) and study the whole Post (you'll also find the reason of using it and why its better than other methods of centring).
I hope this helped you.
I am not sure if I understood you right, but let's start from here:
CSS:
div.center {
display: table-cell;
width: 200px;
height: 200px;
vertical-align: middle;
text-align: center;
border: 1px solid #000;
background-color: #ccc;
}
div.inner {
margin: 0 auto;
}
HTML:
<div class="center">
<div class="inner">
<div class="title">
Title Here
</div>
</div>
</div>
Is this what you are trying to do? assuming the gray background is an image? SAMPLE HERE

How to apply margin to child div without affecting parent div?

I am trying to apply margin to specific child div 2*"#child2"* but it applies margin to parent div too. Problem is margin collapse.
<div id="parent">
<div id="child1" class="child">hello1</div>
<div id="child2" class="child">hello1</div>
<div id="child3" class="child">hello1</div>
</div>
CSS
#parent{
overflow:auto;
padding-top: -1px;
margin-top: 1px;
}
.child{
margin:0 30px;
display:inline-block;
background-color: #5395ce;
padding: 5px;
}
#parent{
background-color: #000;
}
#child2{
margin-top: 15px;
}
Here is the code: http://jsbin.com/nibaw/5/edit?html,css,output
Define your your .child class vertical-align:top;
.child{
vertical-align:top;
}
Get rid of
#child2 {
margin-top: 15px;
}
which is adding 15px top margin.

How can I center data inside parent div?

I have a div that has text and images inside, it's 190px x 190px and I would like to have everything vertically centered.
I've tried looking on SO and Google and can't seem to find just a simple answer to do so.
What's the simplest way of doing this?
Fiddle
HTML:
<div class="block">
<h2>TITLE</h2><br/>
<img src="...." width="190px"/>
<p>Hello world</p>
</div>
CSS:
.block {
position:relative;
width:190px;
height:190px;
background-color:FFF;
margin:10px;
float:left;
left:-10px;
text-align: center;
padding:10px;
border:solid 1px #CCC;
font-size:small;
}
.block p {
text-align: left;
}
Hi you can use this two properties:
.block {
display:table-cell;
vertical-align:middle;
}
and remove the float:left. Review this demo http://jsfiddle.net/kGt54/17/ and ask any question.
Edit
If you want to keep the float:left you need to make an external container who float and have the margin :
.blockC {
float:left;
margin:10px;
}
New Demo http://jsfiddle.net/kGt54/29/
.block {
position:relative;
background-color:FFF;
margin:10px;
float:left;
left:-10px;
text-align: center;
padding:10px;
border:solid 1px #CCC;
font-size:small;
}
.block p {
text-align: center;
}
just remove the width:190px; and height:190px; in the .block{} and in .blick p{} just change the text-align : left; to text-align : center; i hope thats what you want to achieve.. happy coding.

Line from left side of screen to end of centered div

I want to make a 1 px line from the left side of the screen to the end of a centered div.
The div is centered with margin: auto;.
This image shows how it should look:
Here's an example using calc:
.box{
width:200px;
height:200px;
border:1px solid blue;
margin:0 auto;
}
.line{
border: 1px solid red;
width: calc(((100% - 200px)/2) + 200px);
}
JSFiddle
Browser support
How about this solution? no extra markup needed, cross browser and does not depend on the width of the element
#content {
width:400px;
height: 200px;
margin:auto;
position: relative;
}
#content:before{
content: '';
height: 1px;
background: red;
position: absolute;
top: -5px;
right: 0;
width: 999%; /*a large number*/
}
Demo fiddle
here is another solution and it is cross browser http://jsfiddle.net/9qrSy/3
<div class="inner"></div>
<div class="wrapp"></div>
css
body {
padding:8px;
}
div.wrapp {
width:300px;
height:300px;
border:2px solid green;
margin:auto;
position:relative;
}
div.wrapp:before {
content:'';
position:absolute;
width:100%;
height:1px;
right:0;
top:-6px;
background:blue;
z-index:1;
}
.inner {
width:50%;
float:left;
position:absolute;
height:1px;
left:0;
top:12px;
background:blue;
}
I am not sure if this works in all browsers, but I believe hr takes up all the space you provide it with. Therefore you can give it a large negative left-margin and put it inside the centered div. Instead of a hr-element, you could use an empty div too, which might or might not be easier to use. You can set the border-top style of that div to a wider range of border-types (dotted for example).
<div id="content">
<hr id="bar" />
<div id="realcontent">
Something here
</div>
</div>
With CSS:
#content {
width: 400px;
margin: auto;
color: white;
}
#bar {
margin-left: -1000px;
margin-bottom: 5px;
background: blue;
}
#realcontent {
background-color: #000000;
}

How to position absolutely inside float:left?

Why adding of left rule changes behavior so drastically? Is it possible to position relative to default position?
http://jsfiddle.net/suzancioc/drDn3/6/
HTML:
<div class='level0'>
<div class='level1'>
Hello
</div>
<div class='level1'>
Hello
<div id='inner2'>inner2</div>
</div>
<div class='level1'>
Hello
<div id='inner3'>inner3</div>
</div>
</div>
CSS:
.level0 {
height:40px;
width: 500px;
background:red;
}
.level1 {
float:left;
margin:2px;
border-style: solid;
background: cyan;
}
#inner1 {
position: absolute;
background: green;
}
#inner2 {
position: absolute;
background: green;
left:0px;
}
#inner3 {
position: absolute;
background: green;
}
In order to position absolute something you need to assign that div(in your case) to a relative positioned parent
.level1 {
float:left;
margin:2px;
border-style: solid;
background: cyan;
position:relative;
}
Adding position:relative makes .level1 a sort of coordinate system for all elements inside of it.
Take a look at this JSFIDDLE

Resources