I have following code snippet
body {
margin: 0 auto;
min-width: 480px;
padding: 0;
width: 800px;
}
#media (max-width: 840px) {
body {width: 95%; }
}
div.top-header-cont {
display: flex;
}
header {
display: inline-block;
width: auto;
}
div .theme-select-cont {
display: inline-block;
flex-grow: 100;
}
div .theme-select-table-wrapper {
display: table;
height: 100%;
width: 100%;
}
div .theme-select-table-cell-wrapper {
display: table-cell;
vertical-align: middle;
}
<div class="top-header-cont">
<header>
<h1>TODO</h1>
</header>
<div class="theme-select-cont">
<div class="theme-select-table-wrapper">
<div class="theme-select-table-cell-wrapper"><select>
<option value="base16-dark">base16-dark</option>
</select></div>
</div>
</div>
</div>
It looks like this in browser
I suspect that I can do the same easier, I mean place select element centered vertically relative to h1 element and on right side of page. I don't know how to do it without table wrappers. Or maybe another way, without flexed div elements. Any way my variant looks too mach complex for such easy task like place two element at top of page. What you can suggest to solve this problem? I have seen some solutions using absolute div positioning, but it is not appropriate in my case.
You can use position absolute and transform: translateY(-50%) to vertically center if the 2 elements are bound by the same wrapper with a relative position.
Just make the header position relative, put the select element in there right after the h1 and give it a class to target.
<header>
<h1>TODO</h1>
<select class="select-box">
<option value="base16-dark">base16-dark</option>
</select>
</header>
header{
position:relative;
}
.select-box{
position:absolute;
top:50%;
right:2rem;
transform:translateY(-50%);
}
You may find it easier to balance it the way you have it now. Remember that absolute items don't really know of the surrounding elements so they can overlap if you don't handle your break points correctly.
You can also do this with grid, but until grid is fully supported I'll refrain from providing examples.
Related
I have two questions. Essentially, I want to display an image in its original size and place it in the middle of the screen(by middle I mean in the "horizontal" middle not the center of the screen), and then place a div with texts right below this image. This is the code I use:
<div class="figure">;
<img src="...">;
</div>';
<div class="text">
text here
</div>
This is the css:
.figure{
position:absolute;
margin:0 auto;
left:0;
right:0;
bottom:10px;
top:30px;
}
.figure img{
margin-left: auto;
margin-right: auto;
display: block;
height:100%;
width:100%;
}
I have two questions. The first one is when the image height is theoritcally longer than the screen height, there is no vertical scrollbar, the image just got resized to fit the screen. The second question is how can I place the text below the image without knowing the size of the image? I tried figcaption but it doesn't work.
To make it perfect center, you might need min-height: 100vh; and min-width: 100vw; then use display flex to center it. Otherwise, you might not center it vertically.
Also, move your text block inside one div with the img.
by default, the img will not resize unless you specify it.
By default div has display block so it will take the whole row, with text-align: center; it will just center your text.
.figure {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
min-width: 100vw;
}
.centered {
text-align: center;
}
html,
body {
padding: 0;
margin: 0;
}
<div class="figure">
<div>
<img src="http://placehold.it/950x450">
<div class="centered">
text here
</div>
</div>
</div>
First of all I I assume this does what you have in mind.
html
<img src="https://static.tumblr.com/07f1e3ffdfdd03631d00e7792ea3fa93/mja6mxp/AUUna8jev/tumblr_static_tumblr_static_88o50pnpc3k04gw0ck888o80o_640.jpg"/>
<div class="text">Isn't that pretty!</div>
CSS
body {
color: #eee;
background-image: url("chrome://global/skin/media/imagedoc-darknoise.png");
}
img {
margin: 0 auto;
display: block;
}
.text {
margin: 0 auto;
display: table;
}
Your first question about why the image is resized when it's bigger than the page. I have to point out that in your CSS you set the image to be the 100% the of the possible width and height and by default, the images will be stretched to fit the element.
To answer your second question, because your "figure" div containing the img position is absolute it ignores the position of other elements. Change position to another type such as "position: relative" and it will position its self with other elements in mind.
I'm not the most confident in html and css skills but I hope my two cents at least helps you press on forward.
I would like to make an anchor that is in a lower z-index clickable, here's a fiddle:
http://jsfiddle.net/mjooh3gv/
Setting the z-index only for the anchor (that's different from the partent div) does not work.
.underlay a {
position: absolute;
z-index: 5000;
}
What's the point in layering the two DIVs on top of each other in the first place?
If you want to position elements at the outer edges of a container, you can use several methods:
postion: relative on the container and postition: absolute on the children, or
float: left and float: right on the children (with clearing the container), or
using display: inline-block and width: 50% on the children, or
a layout <table> with two columns (Yeah, I know. Sue me.), or
using display: table-row on the container and display: table-cell on the children, or
display: flex, see https://css-tricks.com/snippets/css/a-guide-to-flexbox/ and http://caniuse.com/#feat=flexbox
The latter would work like this:
.container {
border: 5px inset red;
width: 300px;
padding: 5px;
display: flex;
}
.overlay {
margin-right: auto;
}
.underlay {
margin-left: auto;
}
<div class="container">
<div class="overlay">
Link 1
</div>
<div class="underlay">
Link 2
</div>
</div>
While creating a HTML layout, I noticed some strange positioning issue I was unable to solve.
Take the following HTML:
<div class="outer-wrap">
<div class="header">
I am a Header
</div>
<div class="element">
Hello world
</div>
And combine with this CSS code:
#import "https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.css";
html, body { height: 100%; }
.outer-wrap {
width: 100%;
height: 100%;
display: table;
background: grey;
}
.element {
display: table-cell;
vertical-align: middle;
background: blue;
}
.header {
position: absolute;
background: red;
}
Fiddle
As you can see, I've set the wrapper to display: table, which enables me to vertically center any child element with setting display: table-cell and vertical-align: middle.
Now when I try to add a header, strange things start to happen.
First, I have to declare position: absolute on the header, otherwise the header horizontally pushes away .element. I don't know why this happens, but I understand why this fix works: Because position: absolute takes things 'out of the flow'.
But if I take a look at the Fiddle, you'll notice a small gap on the left side which exposes the grey background color defined on .outer-wrap:
What is causing this gap & how to fix this?
Why do I have to use absolute positioning on the header to make it expand to the full container width?
The key reason causing that is you're not defining the table-cell div and would not be 100% wide and you see its shifting towards right seeing the gray border color which is the background of outer-wrap div. So, you need to define the width:100%; when you use display:table-cell; to make it display correctly.
Changed css:
.outer-wrap {
width: 100%;
height: 100%;
display: table;
background: grey;
}
.element {
display: table-cell;
vertical-align: middle;
background: blue;
width: 100%;/*explicitly define width to be 100%*/
}
.header {
position: absolute;
background: red;
z-index: 1;/*to make it display in front*/
}
Fixed fiddle
This question has been asked several time on stackoverflow, however I was wondering if someone -perhaps you- doesn't have a unique solution to my problem.
I currently have an parent div that is of varying height and width whose contents are also of varying width and height. To vertically align the child div I have styled it's parent as display: table; and it as display: table-cell; and nested yet another div, as seen below:
<!-- css styling -->
<style type="text/css">
.div-table {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.div-cell {
display: table-cell;
padding: 10px;
vertical-align: middle;
}
.div-alignedcontents {
margin-left: auto;
margin-right: auto;
text-align: center;
}
</style>
<!-- html -->
<div class="div-table">
<div class="div-cell">
<div class="div-alignedcontents">
<p>Some content that has a varying height and width!</p>
</div>
</div>
</div>
The problem is that the overflow:hidden property doesn't seem to work on tables, and table-layout: fixed property doesn't constrain vertical proportions/height. One solution would be to nest all the above html in yet another div and style that div with overflow:hidden, however I thought I might pick your brains for any suggestions first.
Thank you in advance for any help.
One solution could be defining padding for .div-cell in percentage and subtracting the same padding from the width of your div-table. Something like this:
.div-table {
width: 95%
height: 95%
}
.div-cell {
padding: 2.5%
}
Fiddle
I am practising with making a webpage responsive for mobile screen resolutions. I succeeded with the width. How do I manage this with height? My wrappers continues to stay on top instead of vertical align.
I've seen a lot of questions about this problem, but couldn't find a solution specified on my css yet. Hope someone can help me out.
HTML
<body>
<div class="wrapper">
<div class="header">
</div>
<div class="content">
<div id="topleftbox"></div>
<div id="toprightbox"></div>
<div id="bottomleftbox"></div>
<div id="bottomrightbox"></div>
</div>
</div>
CSS
body {
}
.wrapper {
margin: 0 auto;
width: 100%;
height: 100%;
}
.header {
min-width: 50%;
height: 20px;
}
.content {
min-width: 500px;
width: 40%;
height: auto;
margin: 0 auto;
}
#topleftbox {
background: url(..);
background-repeat: no-repeat;
width: 229px;
height: 228px;
float: left;
}
#toprightbox {
background: url(...);
background-repeat: no-repeat;
width: 229px;
height: 228px;
float: right;
}
etc.
To use display:table-cell; you need to simulate the full table structure. Luckily you won't have to add any extra markup since you can style against the html and body tags:
html{display:table;width:100%;height:100%;}
body{display:table-row;}
.wrapper{
display:table-cell;
vertical-align:middle;
text-align:center;
}
Note: this vertically centers .wrapper's content, not the div itself.
You could use display:table to render your DIVs like tables and table cells.
More here: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
Unlike with centering horizontally there never has been a CSS way to center vertically. The only way is to use javascript to grab the "window.innerHeight" then add the height of the HTML element (wrapper) to it and divide the total in half. And set that as the new top position.
Also your "wrapper" has a height of 100% this should mean it fills the entire screen height. It will not center if it is the same height as the screen. Also it has a width of 100%, I doubt the "margin: 0 auto" is doing anything.
Vertical align within CSS has a few options:
1. Via table-cell. (This does work in IE8+)
{
display:table-cell;
vertical-align:middle;
}
2. line-height = height of element (If you have more than one line of text this will look funky I imagine)
{
height:10em;
line-height:10em;
}
There are more options you can find, but I believe display: table should be your best bet nowadays.
http://www.zann-marketing.com/.../vertically-centering-text-using-css.html
https://stackoverflow.com/questions/2939914/