I have this situation in which I have a header with on the left a logo and on the right some user info (like an avatar). Important to understand is that the width of the logo and the user element are not the same!
So, in the middle of the header I have a title, something like this:
<header>
<img class="logo" src="http://file.png">
<div class="user"> </div>
<h1>This is the title</h1>
</header>
I have create a DEMO
Although this header works great if you resize the area (with ellipses), however, the title is not in the middle of the screen/viewport.
So the question is, considering the behaviour when resizing, is it possible to center the title based on the viewport width ?
UPDATE: Based on the work done by Hassan Ahmad (see below) I've updated the DEMO. This is exactly what I need, but as mentioned already, the media query makes this solution a bit hacky. Any suggestions how to achieve the same without a media query ?
How about this css:
header {
position: relative;
height: 50px;
background-color: lightgrey;
text-align: center;
}
.logo {
position: absolute; top: 0; left: 0;
height: 50px;
width: 300px;
}
.user {
position: absolute; top: 0; right: 0;
width: 50px;
height: 50px;
background-color: red;
}
edit:
h1 {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-right: 50px;
}
#media only screen and (max-width: 820px){
.logo { position: static; float: left; }
}
Related
I'm having trouble getting a h2 header to appaer within an absolutely positioned element. I've ran through the list of possible errors, but still can't seem to find the answer. The h2 element, after inspecting the screen, has a height of 0 for some reason.
Here's a codepen link
HTML:
<div class="gallery">
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<a class='call-action' href='#'>
<h2>Shop Now.</h2>
</a>
</div>
CSS:
.gallery {
position: absolute;
z-index: 2;
width: 100%;
display: flex;
flex-wrap: wrap;
font-size: 0;
img {
display: block;
width: 25%;
height: 50%;
#media only screen and (max-width: 500px) {
width: 50%;
height: 100%;
}
}
}
$call-action-width: 150px;
$call-action-height: 50px;
.call-action {
width: $call-action-width;
height: $call-action-height;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 0;
background-color: white;
opacity: 0.5;
border-radius: 5%;
h2 {
position: relative;
color: black;
font-size: 2em;
}
}
Sorry if the answer is obvious - I've just returned to practicing coding and I'm extremely rough.
Thanks for your help!
The problem lies in your font-size. You can't have an em font within a position: absolute <div>, as em is relative to the parent element.
Simply swapping to a fixed-size font (such as px) fixes the problem. I've created a new pen showcasing this here.
Unfortunately this means that your font can't be responsive. If you want responsive font, you'll either have to use a few media queries, or restructure your HTML so that the <h2> element has a position: relative parent (so you can use em).
Hope this helps! :)
I'd like to had a specific design to a webpage i'm designing.
The main wrapper contains a succession of <div class='section'> and <div class='section-header'>. The section-header should display the section's title over an image.
exemple:
<div id="tag" class="section-header">
<h1>Title</h1>
<img src="assets/img/some_image.jpg">
</div>
So far my css is:
.section-header
{
width: 100%;
height: 192px;
overflow: hidden;
}
.section-header > *
{
width: 100%;
line-height: 192px;
margin: 0;
}
.section-header > h1
{
position: absolute;
z-index: 10000;
text-align: center;
}
.section-header > img
{
filter: opacity(50%);
}
however i'd like to add some relative movement between the background image and the section-header. I basically wanted to fixe the image to the screen with position: fixed; and let the overflow: none; do the job.
However it appears that as soon as I add position: fixed; top: 0; to .section-header > img, the overflow isn't hidden anymore and the image is visible regardless of the position of the header it's nested in.
How can I solve that ?
Edit:
devel code is visible here. I'd basically have the image behind each section's title not to scrool with the page, and just to have the section's header reveal it as you scrool
If I understand the effect you want, you may need to use the img as background; try this:
body{
background: #f3f3f3;
height:800px;
}
div {
text-align:center;
color:white;
width:80%;
margin:150px auto;
line-height:200px;
background:url('http://lorempixel.com/600/600') no-repeat center;
background-attachment: fixed;
}
div h1 {
font-size:3em;
}
<div>
<h1>Mytitle</h1>
</div>
Jsfiddle Demo
Overflow is ignored for position:absolute children in parent that is relatively positioned.
One way of fixing the problem is giving .section-header a position:absolute or position:fixed too. (whichever is most useful for you).
like this :
.section-header
{
width: 100%;
height: 192px;
overflow: hidden;
position: absolute;
}
see this jsfiddle
I'm currently working on the following website:
http://hmdesign.alpheca.uberspace.de/
As you can see, I already managed to create a list of div.project-item elements. Due to the use of inline-blocks, it also reduces the number of columns when you resize the window. What I want to accomplish now is, when you resize the window, that the elements scale up/down in a certain range (between min-width and max-width) until it reaches the maximum/minimum and that it THEN removes/creates a column. The problem now is that there is a huge empty gap after removing a column. It would be much smarter to still show lets say 3 smaller columns in that situation instead of 2 big ones.
I already tried to use a flexbox which didn't really help and also to use block elements instead of inline-block and float them to the left. Then the resizing works but I also want the whole thing to be centered (like now), which I didn't found a way yet to do with floated elements.
Relevant code below:
HTML:
<div class="content-wrapper">
<div class="project-list">
<div class="project-item">
<a href="#">
<img class="project-image" src="res/img/Placeholder.jpg">
<div class="project-overlay">
<div class="project-desc">
<span class="project-title"></span>
<span class="project-text"></span>
</div>
</div>
</a>
</div>
<div class="project-item"...
CSS:
/* Wrapper */
div.nav-wrapper, div.content-wrapper {
max-width: 1280px;
padding: 0 25px;
position: relative;
margin: 0 auto;
height: 100%;
}
/* Portfolio Projektliste */
div.project-list {
padding-top: 150px;
max-width: 1300px;
margin: 0 10px;
text-align: center;
font-size: 0;
}
/* Projekt Item */
div.project-item {
display: inline-block;
position: relative;
width: 400px;
height: auto;
margin: 10px;
overflow: hidden;
}
div.project-item:after {
padding-top: 56.25%;
/* 16:9 ratio */
display: block;
content: '';
}
img.project-image {
position: absolute;
left: 50%;
top: 50%;
transform: translateY(-50%) translateX(-50%);
-webkit-transform: translateY(-50%) translateX(-50%);
-moz-transform: translateY(-50%) translateX(-50%);
max-width: 100%;
}
div.project-overlay {
position: absolute;
width: 100%;
height: 100%;
z-index: 10;
background-color: black;
opacity: 0;
}
You can set the width of div.project-item in % instead of a fixed width (px)
e.g
div.project-item{
width: 30%;
}
So when you resize the window it will adjust.
And if in some point you want to show only two, you can do it using medias
e.g
#media (max-width: 920px){
div.project-item{
width: 45%;
}
}
I agree with Yandy.
Try changing your div.project-item width to like 40% (this is for when it has two divs showing). Like so:
div.project-item {
display: inline-block;
position: relative;
width: 40%;
height: auto;
margin: 10px;
overflow: hidden;
}
Then add this code:
#media only screen and (min-width: 1347px) {
div.project-item {
width:31%
}
}
#media only screen and (max-width: 926px) {
div.project-item {
width:75%
}
}
All of the width percentages can be picked to your choosing for your project.
The one with the 926px is for the single div, and the 1347px is for the triple div.
I have this page with several images. On the left is a bigger image that covers 32% of the width of the page and on the right there is a 6 image grid that covers the other 68% of thw width.
I have the images resize as the page width increases or decreases and that works beautifully.
At the bottom I have a navigation menu. The problem I have is with the navigation menu.
When resizing the page the images correctly respond but that leaves a white space between the menu and the images. I want to fill that gap. I tried to lett the menu fill in that blank space but couldn't get that figured out. then I suddenly realised that that would look terrible on phone's and such as the images would become terribly small and the menu would end up on over half of the page.
So my question is: what is a good way to fix my problem? Do I need to take a whole different approach?
EDIT: I'm familiar with the concept of responsive and know there are frameworks, but the frameworks i know of only offer a Grid layout and this site has to cover the entire screen on every resolution (per client request) So if you know how to do that with something like Bootstrap let me know.
If this is not possible at all, also please let me know so I can tell the client.
Heres a quick Fiddle (Image resolutions are accurate resolutions)
HTML
<div class="content">
<div class="left">
<div id="big-block">
<img id="red-tiger" src="img/tijger2.png" alt="Tijger"/>
</div>
</div>
<div class="right">
<img class="block" id="red-cat" src="img/plaatje1.png" alt="Rode Kat"/>
<img class="block" id="wild-dog" src="img/plaatje2.png" alt="Wilde Hond"/>
<img class="block" id="red-panda" src="img/plaatje3.png" alt="Rode Panda"/>
<img class="block" id="white-tiger" src="img/plaatje4.png" alt="Siverische Tijger"/>
<img class="block" id="puppys" src="img/plaatje5.png" alt="Puppy's"/>
<img class="block" id="grey-cat" src="img/plaatje6.png" alt="Grijze Kat"/>
</div>
</div>
<div class="footer">
<ul class="menu">
<li>HOME</li>
<li>EIGEN ONTWERP</li>
<li>GALLERIJ</li>
<li>WEBSHOP</li>
<li>CONTACT</li>
</ul>
</div>
CSS
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: 0;
width: 100%;
height: 100%;
}
img {
width: 100%;
}
.content {
width: 100%;
padding-bottom: 45%;
}
/* block width */
.left, .right {
float: left;
}
.left {
width: 32%;
}
.right {
width: 68%;
}
/* smaller blocks */
.block {
width: 33.33%;
float: left;
}
/* footer */
.footer {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
padding-bottom: 4.6%;
background-color: black;
}
/* menu */
.footer > ul {
list-style-type: none;
margin: 0;
}
.footer > ul > li {
width: 20%;
float: left;
line-height: 30px;
text-align: center;
}
.footer > ul > li > a {
font-family:'Fjalla One', sans-serif;
color: white;
text-decoration: none;
}
.footer > ul > li > a:hover {
color: orange;
}
As we've discussed, using a responsive framework is the way to go. I would suggest bootstrap.
problem is all the frameworks I know of offer Grid layout only and
this site should be screen wide on any screen.
Again, as I said in my comment, thats not actually right. Bootstrap has a class container-fluid that will make the container span the full width. see - http://getbootstrap.com/css/#grid-example-fluid.
With regards to the sticky footer, this is possible too, see - http://getbootstrap.com/examples/sticky-footer/
You have to use responsive layouts for it.. You have to use frameworks to achieve it easily... I suggest Bootstrap...
I have tried changing your fiddle. The images fill the gap but they do not look good. Here is the link http://jsfiddle.net/0an15t5y/2/
Changed a few css.
.content {
width: 100%;
height: 80%;
}
/* block width */
.left, .right {
float: left;
}
.left {
width: 32%;
}
.right {
width: 68%;
height: 100%;
}
/* smaller blocks */
.block {
width: 33.33%;
float: left;
height: 50%;
}
/* footer */
.footer {
position: absolute;
left: 0;
height: 20%;
width: 100%;
background-color: black;
}
I have a problem with CSS that's only visible in FireFox (cur.ver. 31).
I am trying to make a responsive layout, with a row of images (with links), that are centered, and having the same height and scale with the viewport width. My approach is to create a container with a fixed aspect ratio, and place the images inside (each image inside a separate <a> tag), center them, and scale their heights to the container height. It's working great, except in FireFox.
To achieve this I applied a display: inline-block; height: 100% to <a> tag and height: 100%; width: auto to <img> tags. For some (unknown) reason FF is not calculating the width of the <a> tag correctly (when it contains described above <img> tag) and it collapses horizontally. The result is, that all <a> with 0 width are placed very close to each other (separated only by white spaces), and the images overlap each other. I get the same result with display: block; float: left; on <a> tags.
The CSS
.container-ratio {
width: 100%;
height: 0;
position: relative;
padding-bottom: 10%;
background: #ddd;
}
.container-inner {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: #ddf;
text-align: center;
}
.block {
display: inline-block;
height: 100%;
background: #f00;
}
.block img {
height: 100%;
width: auto;
display: block;
}
The HTML
<div class="container-ratio">
<div class="container-inner">
<a class="block">
<img src="http://placehold.it/100x80/42bdc2/FFFFFF&text=No1">
</a>
<a class="block">
<img src="http://placehold.it/150x80/242bdc/FFFFFF&text=No2">
</a>
<a class="block">
<img src="http://placehold.it/200x80/c242bd/FFFFFF&text=No3">
</a>
</div>
</div>
I think this is what your trying to do. Demo
You had no width on .block and auto on .block img.
It needs to be percentages.
.container-ratio {
width: 100%;
height: 0;
position: relative;
padding-bottom: 10%;
background: #ddd;
}
.container-inner {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: #ddf;
text-align: center;
}
.block {
display: inline-block;
width:20%;
height: 100%;
background: #f00;
}
.block img {
height: 100%;
width:100%;
display: block;
}
It's been nearly two years since this question was asked, and Firefox still exhibits this behavior.
So, for anyone in the same situation, here's a solution (only tested on Chrome 49.0 and Firefox 45.0.1).
Edit:
Originally, I used inline wrapper divs and two instances of the images, one of which was not displayed and only served as a dummy. It appears this is not necessary, as can be seen here.
All in all, it seems you can't use inline-block that way in Firefox, but all you need to do to get what you want is leave the anchors and images as inline elements. As long as the anchor's parent is a block-level element other than inline-block, and its height is specified, then you'll get the intended result.
If, for some reason, inline-block is really needed, I don't see how to work around this problem.
Note:
Beware of the "font-size: 0;" on the .block class, used to remove spaces between the images. Without this, images are seperated by whitespaces that behave like links. If the images need some space between them, adding some right or left margin as in the fiddle would be a solution.
Also, though the .block class name is no longer appropriate, I left it to stay consistent with the OP.
The modified CSS
.container-ratio {
width: 100%;
height: 0;
position: relative;
padding-bottom: 10%;
background: #ddd;
}
.container-inner {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: #ddf;
text-align: center;
}
.block {
font-size: 0;
}
.block img {
height: 100%;
margin-right: 1%;
}