I am giving a width to a div with display table cell property but it is not working .
My code is
<header class="header layers">
<div class="wrap">
<h1 id="title"><?php echo $blogtitle;?></h1>
</div>
</header>
And the css is
.layers {
position: absolute;
display: table;
top: 0;
left: 0;
overflow: hidden;
box-sizing: border-box;
table-layout: fixed;
}
.wrap {
height: 100%;
margin-right: auto;
margin-left: auto;
overflow: hidden;
display: table-cell;
vertical-align: middle;
width: 1040px;
}
.header {
height: 80px;
width: 100%;
}
#title {
font-size: 30px;
color: black;
font-family: 'Roboto', sans-serif;
}
But i don't know why it is not working this time i have used it many time with no problem . When i see the width of the wrap class in inspect element it is setting it to 100% with no error .
Please help . Appreciate any anwser and comment .
The fiddle link is this https://jsfiddle.net/yashag/zhsxLhzj/
As requested in the comments, here is an example of using display: flex (flexbox) to vertically-align text (as you were trying to do with display: table-cell). You can also specify width and height of a flexbox easily, like a normal block element.
As you can see, the display:flex element correctly vertically-centers the text, and can be set to a 1040px width correctly.
* {
box-sizing: border-box;
}
html, body {
height: 100%;
padding: 0;
margin: 0;
}
div#container {
display: flex;
align-items: center;
height: 100%;
width: 1040px;
}
div#centered {
height: 20px;
width: 100%;
text-align: center;
}
<div id="container">
<div id="centered">Vertically-centered using <code>display: flex</code>.</div>
</div>
Related
I'm attempting to insert a logo image using css on a element with background-image.
However, I couldn't get the a:before box to respect a's padding.
The first example in the snippet below is using width, height and display: block but nothing get shown at all.
So, I tried with position: absolute in second example. The logo is shown but it's not respecting a's padding.
How do I make it so the logo fit inside the padding of a?
Current
Expected
What I want to avoid doing
Due to responsive design requirement, I'd like the logo's size to change based on the a's element size. Therefore, below are some things I'd like to avoid.
Using fixed values to fit .logo:before inside a's padding.
Amending a styles
*, ::before, ::after {
box-sizing: border-box;
}
body { margin: 0; }
.container, .container > p, .container > .logo {
display: flex;
}
.container {
align-items: center;
justify-content: space-between;
margin-left: 2rem;
margin-right: 2rem;
}
.container > p, .container > .logo {
flex-basis: auto;
flex-grow: 1;
align-items: center;
}
.logo {
position: relative;
padding-top: .3125rem;
padding-bottom: .3125rem;
color: transparent !important;
}
.logo:before {
content: '';
background: url('https://via.placeholder.com/150x100/FF0000/000000') no-repeat;
background-size: contain;
width: 100%;
height: 100%;
display: block;
}
.logo.absolute:before {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
<div class="container">
<a class="logo">Logo</a>
<p>Navigation links</p>
</div>
<div class="container">
<a class="logo absolute">Logo</a>
<p>Navigation links</p>
</div>
Try to change the value of top and bottom property of your logo to .3125rem;
.logo.absolute:before {
position: absolute;
left: 0;
right: 0;
top: .3125rem;
bottom: .3125rem;
}
I removed the padding for logo and added min-height: 28px; to your background image. Looking forward to further question.
*, ::before, ::after {
box-sizing: border-box;
}
body { margin: 0; }
.container, .container > p, .container > .logo {
display: flex;
}
.container {
align-items: center;
justify-content: space-between;
margin-left: 2rem;
margin-right: 2rem;
}
.container > p, .container > .logo {
flex-basis: auto;
flex-grow: 1;
align-items: center;
}
.logo {
position: relative;
/*padding-top: .3125rem;
padding-bottom: .3125rem;*/
color: transparent !important;
}
.logo:before {
content: '';
background: url('https://via.placeholder.com/150x100/FF0000/000000') no-repeat;
background-size: contain;
width: 100%;
height: 100%;
display: block;
min-height: 28px;
}
<div class="container">
<a class="logo">Logo</a>
<p>Navigation links</p>
</div>
<div class="container">
<a class="logo absolute">Logo</a>
<p>Navigation links</p>
</div>
Since .logo:before's content is an empty string, nothing will ever be displayed unless height is explicitly defined with a fixed value.
content: ' ' can fix the problem but this is just a patch rather than a root fix.
The root cause is due to align-items: center in .container which will align the content in the middle vertically with its minimum height required. A combination with empty content caused .logo:before element to not show anything at all.
The current desired behavior is wanting .logo's height to match the navigation links' height, there's no need to use align-items: center here and normal should do fine.
The position: absolute method will always ignore padding.
*, ::before, ::after {
box-sizing: border-box;
}
body { margin: 0; }
.container, .container > p, .container > .logo {
display: flex;
}
.container {
align-items: normal;
justify-content: space-between;
margin-left: 2rem;
margin-right: 2rem;
background-color: gray;
}
.container > p, .container > .logo {
flex-basis: auto;
flex-grow: 1;
align-items: center;
}
.logo {
position: relative;
padding-top: .3125rem;
padding-bottom: .3125rem;
color: transparent !important;
}
.logo:before {
content: '';
background: url('https://via.placeholder.com/150x100/FF0000/000000') no-repeat;
background-size: contain;
width: 100%;
height: 100%;
}
<div class="container">
<a class="logo">Logo</a>
<p>Navigation links</p>
</div>
I have to get my content div vertical centered in my header div, and i also dont need space around the content div because i did have it working but it would take up width 100% everytime. Here is my code
#header {
position: absolute;
width: 100%;
height: 15%;
background-color: white;
z-index: 10;
overflow: hidden;
display: table;
}
#content {
display: table-cell;
overflow: auto;
vertical-align: middle;
padding: 0px;
margin: 0px;
float: left;
}
h1 {
display: inline-block;
margin: 0px;
}
<div id="header">
<div id="content">
<h1>Jari Rengeling</h1>
</div>
</div>
Not really sure if this is what you are trying to achieve:
https://jsfiddle.net/pf8oujj6/
Use
display: inline-block;
use display:flex with align-items:center on #header .
is this what you are looking for ?
#header
{
position: absolute;
width: 100%;
height: 30%;
background-color: green;
z-index: 10;
overflow: hidden;
display: flex;
align-items:center;
}
#content
{
overflow: auto;
vertical-align: middle;
padding: 0px;
margin: 0px;
float: left;
}
h1
{
display: inline-block;
margin: 0px;
}
<div id="header">
<div id="content">
<h1>Jari Rengeling</h1>
</div>
</div><!-- #header -->
Remove float left in the content.
https://jsfiddle.net/4tc95oy0/
#content
{
display: table-cell;
overflow: auto;
vertical-align: middle;
padding: 0px;
margin: 0px;
}
Float transform youre element in display block override the previous declaration of display table-cell.
I have a responsive design with a header image which is placed in a container. The image has width:100%; and height:auto; so it grows as you enlarge the viewport. I don't want to exceed a certain height so the container has a max-height. The image still grows but now the bottom part is cut off now because it aligns to the top of the container.
I would like the image to stay vertically centered in it's container so that parts of the image are cut off at the top and at the bottom. The outcome should look like this:
The header images are uploaded by users so they might have different heights therefore I cannot work with specific pixel-values. Is there a CSS-solution for this or do I have to use JavaScript?
Here is the code:
.wrapper {
width: 90%;
max-width: 600px;
margin: 1em auto;
background-color: #E9ADAD;
}
.container {
text-align: center;
height: auto;
line-height: 200px;
max-height: 200px;
overflow: hidden;
}
img {
width: 100%;
height: auto !important;
vertical-align: middle;
}
<div class="wrapper">
<div class="container">
<img src="http://placehold.it/600x300/C00000/FFFFFF&text=Image+vertically+centered">
</div>
</div>
And I prepared a fiddle.
You can use absolute positioning for your image , negative top/bottom values and margin:auto; to verticaly center the image in the container :
.wrapper {
width: 90%;
max-width: 600px;
margin: 1em auto;
background-color: #E9ADAD;
max-height: 200px;
}
.container {
position:relative;
padding-bottom:40%;
overflow: hidden;
}
img {
position:absolute;
top:-50%; bottom:-50%;
margin:auto;
width: 100%;
height: auto;
}
<div class="wrapper">
<div class="container">
<img src="http://placehold.it/600x300/C00000/FFFFFF&text=Image+vertically+centered">
</div>
</div>
Not so long ago there was only a javascript way to do this but now we have some css rules: object-fit and object-position
They work just like the background-size rules cover and contain:
.container img{
width: 100%;
height: auto;
}
#supports(object-fit: cover){
.container img{
height: 100%;
object-fit: cover;
object-position: center center;
}
}
The problem with this approach is that is very new and doesn't work on ie or Edge yet.
Pen here: http://codepen.io/vandervals/pen/MwKKrm
EDIT: Please, see that you need to declare the width and the height of the image, or it won't work.
.wrapper {
width: 90%;
max-width: 600px;
margin: 1em auto;
}
.container {
height: 200px;
overflow: hidden;
}
.imgWrapper {
position: relative;
width: 200%;
height: 200%;
top: -50%;
left: -50%;
}
img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
height: auto;
width: 50%;
}
<div class="wrapper">
<div class="container">
<div class="imgWrapper"><img src="http://placehold.it/600x300"></div>
</div>
</div>
http://jsfiddle.net/ghygpw8t/5/
inspired by: https://css-tricks.com/perfect-full-page-background-image/
Try like this: Demo
If image size is small it will be arranged in vertical middle and if its big, it will fit in box.
CSS:
.wrapper {
width: 90%;
max-width: 600px;
margin: 1em auto;
}
.container {
text-align: center;
line-height: 200px;
overflow: hidden;
background-color:#ccc;
vertical-align:middle;
height: 200px;
border:2px solid green;
display: flex;
width: 100%;
align-items: center;
justify-content: center;
}
img {
width: 100%;
max-height: 196px;
border:2px solid red;
vertical-align: middle;
line-height: 196px;
}
Hope this is what you want!
On the element you want centered.
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
on its parent.
.parent { transform-style: preserve-3d; }
Use a polyfill to render cross browser styles.
I'm now trying another strange and not working thing: the vertical auto alignment of a child div.
I would like the content to be vertically centered within the panel, because the panel have a height in % that fits the window size, it's really important for me to have a strict alignment.
All right, here's my code: JSFiddle
HTML
<div id="panel">
<div id="content">
</div>
</div>
CSS
html, body
{
height: 100%;
background-color: #273034;
margin: 0;
}
#panel
{
height: 100%;
width: 380px;
margin: auto;
background-color: rgba(255,255,255,0.3);
}
#content
{
height: 100px;
width: auto;
background-color: rgba(117,169,56,0.9);
}
Why a so simple thing doesn't work?
Hope someone could help me, I've tried these solutions: margin : auto not working vertically? but it actually didn't make the trick
Here is a simple Solution for vertical aligning, using Pure CSS without fixing any top-margin, top-padding. so its totally responcive.
See this Working Fiddle
HTML: (Same)
<div id="panel">
<div id="content">
</div>
</div>
CSS:
html, body
{
height: 100%;
background-color: #273034;
margin: 0;
}
#panel
{
height: 100%;
width: 380px;
margin: 0 auto;
background-color: rgba(255,255,255,0.3);
}
/*this is new*/
#panel:before
{
content: '';
height: 100%;
margin-left: -5px;
vertical-align: middle;
display: inline-block;
}
#content
{
vertical-align: middle; /*this is new*/
display: inline-block; /*this is new*/
height: 100px;
width: 100%; /*this is new*/
background-color: rgba(117,169,56,0.9);
}
I'm trying to vertically and horizontally center some content overlaying an image slide (flexslider). There were some similar questions to this one, but I couldn't find a satisfactory solution that applied directly to my specific problem. Because of the limitations of FlexSlider, I cannot use position: absolute; on the img tag in my implementation.
I almost have workaround below working. The only problem is I cannot get the width & height declarations to work on inner-wrapper div with the display: table-cell property.
Is this standard behavior, or am I missing something with my code? If it's standard behavior, what's the best solution to my problem?
HTML
<ul>
<li>
<img src="#">
<div class="outer-wrapper">
<div class="inner-wrapper">
<h1>My Title</h1>
<h5>Subtitle</h5>
</div>
</div>
</li>
</ul>
CSS
html, body {
margin: 0; padding: 0;
width: 100%; height: 100%;
}
ul {
background: #CCC;
height: 100%;
width: 100%;
list-style-position: outside;
margin: 0; padding: 0;
}
li {
width: 100%;
display: table;
}
img {
width: 100%;
height: 410px;
}
.outer-wrapper {
position: absolute;
width: 100%;
height: 100%;
top: 0;
margin: 0; padding: 0;
}
.inner-wrapper {
display: table-cell;
vertical-align: middle;
text-align: center;
width: 100%;
height: 100%;
}
Note: the centered content will be more than 1 element, so I can't use the line-height trick.
jsFiddle.
Putting display:table; inside .outer-wrapper seemed to work...
JSFiddle Link
EDIT: Two Wrappers Using Display Table Cell
I would comment on your answer but i have too little rep :( anyways...
Going off your answer, seems like all you need to do is add display:table; inside .outer-wrapper (Dejavu?), and you can get rid of table-wrapper whole-heartedly.
JSFiddle
But yeah, the position:absolute lets you place the div over the img, I read too quickly and thought that you couldn't use position:absolute at all, but seems like you figured it out already. Props!
I'm not going to post the source code, after all its 99% timshutes's work, so please refer to his answer, or just use my jsfiddle link
Update: One Wrapper Using Flexbox
It's been a while, and all the cool kids are using flexbox:
<div style="display: flex; flex-direction: column; justify-content: center; align-items: center;">
stuff to be centered
</div>
Full JSFiddle Solution
Browser Support (source): IE 11+, FireFox 42+, Chrome 46+, Safari 8+, iOS 8.4+ (-webkit- prefix), Android 4.1+ (-webkit- prefix)
CSS Tricks: a Guide to Flexbox
How to Center in CSS: input how you want your content to be centered, and it outputs how to do it in html and css. The future is here!
I figured this one out. I know this will help someone someday.
How to Vertically & Horizontally Center a Div Over a Relatively Positioned Image
The key was a 3rd wrapper. I would vote up any answer that uses less wrappers.
HTML
<div class="wrapper">
<img src="my-slide.jpg">
<div class="outer-wrapper">
<div class="table-wrapper">
<div class="table-cell-wrapper">
<h1>My Title</h1>
<p>Subtitle</p>
</div>
</div>
</div>
</div>
CSS
html, body {
margin: 0; padding: 0;
width: 100%; height: 100%;
}
ul {
width: 100%;
height: 100%;
list-style-position: outside;
margin: 0; padding: 0;
}
li {
width: 100%;
display: table;
}
img {
width: 100%;
height: 100%;
}
.outer-wrapper {
width: 100%;
height: 100%;
position: absolute;
top: 0;
margin: 0; padding: 0;
}
.table-wrapper {
width: 100%;
height: 100%;
display: table;
vertical-align: middle;
text-align: center;
}
.table-cell-wrapper {
width: 100%;
height: 100%;
display: table-cell;
vertical-align: middle;
text-align: center;
}
You can see the working jsFiddle here.
I discovered that the higher the value of 'width' is, the smaller the box width is made and vice versa. I found this out because I had the same problem earlier. So:
.inner-wrapper {
width: 1%;
}
solves the problem.
Welcome to 2017 these days will using vW and vH do the trick
html, body {
margin: 0; padding: 0;
width: 100%; height: 100%;
}
ul {
background: #CCC;
height: 100%;
width: 100%;
list-style-position: outside;
margin: 0; padding: 0;
}
li {
width: 100%;
display: table;
}
img {
width: 100%;
height: 410px;
}
.outer-wrapper {
position: absolute;
width: 100%;
height: 100%;
top: 0;
margin: 0; padding: 0;
}
.inner-wrapper {
display: table-cell;
vertical-align: middle;
text-align: center;
width: 100vw; /* only change is here "%" to "vw" ! */
height: 100vh; /* only change is here "%" to "vh" ! */
}
<ul>
<li>
<img src="#">
<div class="outer-wrapper">
<div class="inner-wrapper">
<h1>My Title</h1>
<h5>Subtitle</h5>
</div>
</div>
</li>
</ul>
Your 100% means 100% of the viewport, you can fix that using the vw unit besides the % unit at the width. The problem is that 100vw is related to the viewport, besides % is related to parent tag. Do like that:
.table-cell-wrapper {
width: 100vw;
height: 100%;
display: table-cell;
vertical-align: middle;
text-align: center;
}
How about this? (jsFiddle link)
CSS
ul {
background: #CCC;
height: 1000%;
width: 100%;
list-style-position: outside;
margin: 0; padding: 0;
position: absolute;
}
li {
background-color: #EBEBEB;
border-bottom: 1px solid #CCCCCC;
border-right: 1px solid #CCCCCC;
display: table;
height: 180px;
overflow: hidden;
width: 200px;
}
.divone{
display: table-cell;
margin: 0 auto;
text-align: center;
vertical-align: middle;
width: 100%;
}
img {
width: 100%;
height: 410px;
}
.wrapper {
position: absolute;
}
Just add min-height:100% and min-width:100% and it will work. I had the same problem. You don't need a 3th wrapper