I'm really new to CSS/programming in general and am trying to build a website logo for a friend as practice. He wants his name justified across the entire header and responsive to the page, and I came up with something that ALMOST works perfectly. Here's what I've got:
<style>
body {
background: black;
}
.container {
overflow: hidden;
line-height: 1em;
color: white;
}
.header {
border: 1px solid white;
width:30%;
margin: 20px 0 0 0;
padding: 5px;
display:flex;
flex-direction: column;
line-height: 0em;
align-items: center;
}
.header h1{
flex-direction: row;
display:flex;
flex-grow: 1;
flex-shrink: 1;
flex-basis: auto;
align-self: center;
width: 100%;
}
.header h1 p {
text-align: center;
/* background: gray;
*/
flex-grow: 1;
flex-shrink: 1;
flex-basis: auto;
width: 100%;
font-size: 20px;
}
</style>
<div class="container">
<div class="header">
<h1><p>I</p><p>S</p><p>T</p><p>H</p><p>I</p><p>S</p></h1>
<h1><p>T</p><p>O</p><p>T</p><p>A</p><p>L</p><p>L</p><p>Y</p></h1>
<h1><p>I</p><p>N</p><p>S</p><p>A</p><p>N</p><p>E</p><p>?</p></h1>
</div>
</div>
So it looks okay, at the moment, and I'm comfortable incorporating it into the rest of the site CSS. The problem I'm having is twofold:
A) I cannot seem to adjust the flexbox properties to eliminate/control all of that empty space. The width is fine but the box is too high, and I'm unsure which combination of properties I need to achieve fine-grained control of the header's height.
B) It has 20 p's in a row at the end. This seems inefficient? I'd like to keep it pure CSS for the moment, so this might be unavoidable, but is there a better way to do this by incorporating another language?
Apologies for any rookie mistakes/StackOverflow faux pas. Infinite apologies if this has already been answered clearly elsewhere.
Related
I have a container, in my case the body and html tags are the containers. And then I have 3 divs in them and I want the last one to fill the remaining vertical space available while still having a margin.
The third div is generated dynamically so I can't predict what height it's gonna need. The problem is, if it grows too much and a scrollbar is required, the bottom-margin it used to have goes away too. If a scrollbar is NOT required and doesn't appear, the margin is still there and everything looks like I want it to.
I tried to draw what I meant as best as I could in the above image. The 1st case is what I want to happen all the time, regardless of whether there's a scrollbar or not. The 2nd picture is what actually happens, the blue div loses its bottom margin, despite having it set.
Here's my CSS for the html and body tags (they contain the 3 divs, including the blue one):
html,
body
{
width: 100%;
height:100%;
margin: 0px;
padding: 0px;
display: flex;
flex-direction:column;
align-items: center;
background: #494d5f ;
}
and here's my code for the 3rd div, the blue one:
.bottomDiv
{
display:flex;
flex-direction:column;
background: #a0d2eb ;
align-items: center;
width: 97%;
margin-bottom:1.5%;
flex: 1 1 auto;
padding-top:1%;
padding-bottom:1%;
}
Maybe I didn't clarify well enough but the 3rd div in my case, the blue one grows just like it should, fully obeying its margins UNTIL a scrollbar appears and is needed. No matter the amount of growth it has to do, it does it perfectly while respecting its margin. But if it has to grow "out of bounds" of the page so to say, as in, a scrollbar is needed to display all the webpage then its margin is simply gone. IF there is NO scrollbar, everything looks perfect.
Just change this line: body { height: 100%; } to: body { min-height: 100vh; }.
With that line the body will have a height of at least the screen height (100vh) but allows i proper overflow as it is allowed to eb alrger then the screen. As such the margins wont get removed.
html,
body {
width: 100%;
min-height: 100vh;
margin: 0px;
padding: 0px;
display: flex;
flex-direction: column;
align-items: center;
background: #494d5f;
}
.topDiv,
.midDiv {
width: 97%;
height: 50px;
}
.topDiv {
margin-top: 1.5%;
background-color: red;
}
.midDiv {
background-color: blue;
}
.bottomDiv {
display: flex;
flex-direction: column;
background: #a0d2eb;
align-items: center;
width: 97%;
margin-bottom: 1.5%;
flex: 1 1 auto;
padding-top: 1%;
padding-bottom: 1%;
}
#height:checked + label::after {
content: "";
display: block;
height: 150vh;
}
<div class="topDiv"></div>
<div class="midDiv"></div>
<div class="bottomDiv">
<input type="checkbox" id="height" name="height">
<label for="height">checkmark me to extend box height</label>
</div>
I've put an example, so you may try this approach:
body,
html {
margin: 0;
padding: 0;
}
#page {
display: flex;
flex-direction: column;
height: 100vh;
outline: 1px solid purple;
}
#header {
height: 30px;
outline: 1px solid red;
}
#middle {
flex-grow: 1;
/* height: 100vh; */
/* flex-direction: column; */
outline: 1px solid green;
}
#footer {
outline: 1px solid blue;
}
<div id="page">
<div id="header">...</div>
<div id="middle">...</div>
<div id="footer">...</div>
</div>
Try to remove #footer and see that #middle fills the entire screen till the end.
In addition if you need any margins so you have to modify the container like this: #page { ..., margin: 10px; height: calc(100vh - 20px); }
I'd like to know how to shrink a row of images so that they all fit within a div with an unspecified height. The images should never scale up beyond their native height, and they must maintain their aspect ratio. Also, I'd like the height of the containing div to be limited to the native height of the tallest image. The image tags have no height or width attributes.
Here's a fiddle with what I have so far. I approached this using flexbox and object-fit: scale-down. The row of images in question are gray and are in the div with the green background. They currently do not scale at all, but they are at least centered vertically and horizontally how I'd like them to be. Here are before and after images of the effect I'd like to achieve (sorry for switching the green background to yellow in the images). Additional details below the code snippet, but that about sums up the basic question.
body {
font-family: arial;
font-size: 26px;
text-align: center;
color: black;
background-color: white;
}
.smallhint {
font-size: 16px;
color: #8c8c8c;
}
img {
padding: 0;
margin: 0;
font-size: 0;
display: block;
object-fit: scale-down;
min-height: 0;
}
.flex-column {
display: flex;
flex-direction: column;
padding: 0px;
margin: 0px;
height: 90vh;
flex-grow: 0;
min-width: 0;
min-height: 0;
}
.flex-row {
display: flex;
flex-direction: row;
flex: 0 1.5 auto;
justify-content: center;
align-items: center;
background-color: green;
}
.context {
display: flex;
flex-direction: column;
max-height: 100%;
background-color: blue;
}
.primary {
position: relative;
z-index: 0;
right: 0;
bottom: 0;
padding: 0;
font-size: 0;
min-height: 0;
align-items: end;
background-color: orange;
}
.primary img {
margin-left: auto;
margin-right: auto;
border-style: solid;
border-width: 3px;
border-color: black;
height: calc(100% - 2*3px);
}
.mask {
position: absolute;
z-index: 1;
top: 0;
right: 0;
width: 100%;
height: 100%;
font-size: 0;
}
.nonimage {
padding-top: 5px;
display: inline;
background-color: pink;
}
<div class="flex-column">
<div class="primary">
<img src="https://via.placeholder.com/200">
<div class="mask">
<img src="https://via.placeholder.com/200/FF000">
</div>
</div>
<div class="flex-row">
<div class="context">
<img src="https://via.placeholder.com/75x150">
</div>
<div class="context">
<img src="https://via.placeholder.com/150x75">
</div>
</div>
<div class="nonimage">
<div class="smallhint">Some Text<br>Other Text</div>
</div>
</div>
I'm working on a (fixed-height) interface styled with CSS and will likely be asking a series of questions. I'm not great at CSS, so I'm open to approaches that are very different from my failed attempt!
At the top is a single centered image ("primary image"), below that are two other images ("secondary images") in a row, and below that is some text. Eventually, I'd like both sets of images to be responsive to changes in the height and width of the browser. However, I'd like to preferentially scale down the secondary images more than the primary image when the browser is too short to contain everything at native dimensions. For this, it seemed like flexbox containers with various flex-grow values would work here; it seems to work with the primary image somewhat, but the secondary images refuse to scale.
Also, I'm aware that, even if my current approach worked, the object-fit: scale-down strategy would leave behind some unwanted "padding" that will result in visual space between the secondary images. I have a feeling a very different approach may be required to get the effect that I want in the end, since I want the images to sit adjacent to each other without extra space around them. Furthermore, there also seems to be an issue with the container itself when the browser becomes very thin, since a scrollbar appears but it should always be 90vh.
Thank you all for the input!
Add a min-height: 0; rule for .flex-row. I guess that means it was pretty close to working when I asked the question.
This solution retains the issue I mention in my question about the additional "padding" created around images when object-fit: scale-down (or cover) is used. So, that means I'll be asking another question about that topic!
After spending all day long trying to found how to make a nice tumblr-like grid for my website, I'm posting here to find help.
here's the page: http://alexis.gargaloni.free.fr/main.html
In order to access to my project there's a grid of images displayed. At the moment it looks OK, but now that I need to add something new, it starts to look really bad. screenshot
As you can see, there's a white gap. I've tried many things and there's every time a gap (even when it's not supposed to be there).
Here's an example of what I want to achieve: http://alexgargaloni.tumblr.com
here's my HTML code (included filter):
<div id="myBtnContainer">
<button class="btn active" onclick="filterSelection('all')"> TOUT</button>
<button class="btn" onclick="filterSelection('imprime')"> IMPRIMÉ</button>
<button class="btn" onclick="filterSelection('digital')"> NUMERIQUE</button>
<button class="btn" onclick="filterSelection('picture')"> PHOTO</button>
</div>
<div class="pic"> <img class="filterDiv picture" src="images/ego5-4.jpg" alt="ego graphique" style="width:40% "> <img class="filterDiv imprime" src="images/ihp1_1.jpg" alt="affiches pour l'institut henri poincaré" style="width:25%"> <img class="filterDiv imprime" src="images/jpogat17.png" alt="portes ouvertes lycée du gué à tresmes 2017" style="width:25%"><img class="filterDiv imprime" src="images/detailbook1.jpg" style="width:35%"> <img class="expav filterDiv digital" src="images/expavstatic.png" alt="expérience de la durée" style="width:35%" ></div>
and CSS:
html {
position: relative;
min-height: 100%;
}
body {
background: white;
font-size: 20px;
font-family: Helvetica, Arial, sans-serif;
line-height: 25px;
margin: 0 0 900px; /* bottom = footer height */
padding: 25px;
}
.img{
margin: 20px;
}
/* FILTER */
.container {
overflow: hidden;
}
.filterDiv {
float:left;
/*color: #ffffff;*/
width: 100px;
line-height: 100px;
text-align: center;
margin: 13px;
display:none; /* Hidden by default */
}
.show {
display: block;
}
.pic
{
max-width: 100%;
}
I'm new to HTML and CSS, and I know that my code is not a 100% clean and it could be way more simplified, but I'm working on it.
Excuse my English, I'm French
Thank for your help!
EDIT #ben_fluleck
Thank you. There’s a problem with “height: 100%”, because it modifies the aspect ratio of my pic. If I change height and width with max-width and max-height, the white gap is back. I also need to keep “display: none” on. filterDiv to make the filter function work (something with javascript). And also I’m having a problem with filter now, it still works, but pics are not getting how they’re supposed to (before:picture after:picture, it's like elements filtered create a white space instead of disappearing). I’ve tried to do something with the tumblr html, but it didn’t seem to work. Simple things are super tricky to do… I really need something that trick the size itself like tumblr theme, because when I’ll ad new things on my website, I feel like it’s going to be a mess again.
Yes, my footer is not really well implemented, I’ve checked online a way to make it because it’s really tricky, and how I did was the only way I was able to make it work. Thank a lot for you help! We can see the footer later, for the moment I really need to focus on this grid
Sorry, the picture in my Css above should be pic since you have named them you can constrain the pics giving them a width property. I would remove the inline styling for width and use percentages in the Css.
I have constructed a fiddle you could expand upon but it should give you a better idea of the layout.
.container {
display: flex;
justify-content: center;
align-items: center;
}
.box {
flex: 1 1 auto;
color: white;
font-size: 50px;
text-align: center;
padding: 10px;
}
/* Colours for each box */
.box1 {
background: #1abc9c;
}
.box2 {
background: #3498db;
}
.box3 {
background: #9b59b6;
}
https://jsfiddle.net/sLjuLjoc/1/
Try these CSS Layouts they will get your pics in order
Flexbox or Grid
set a viewport height in the body and work of that
body{
background: white;
font-size: 20px;
font-family: Helvetica, Arial, sans-serif;
line-height: 25px;
/* margin: 0 0 900px; */
padding: 25px;
height: 100vh;
display: flex;
flex-direction: column;
}
.pic {
display: grid;
grid-template-columns: repeat(4,1fr);
grid-gap: 10px;
}
.filterDiv {
float: left;
width: 100%;
line-height: 100px;
text-align: center;
height: 100%; /*optional if you want them to be all the same same */
}
.show {
/* display: block; */
}
This question is almost the same as one of my previous questions, but it's specifically focused on IE11, as the answer of the previous question worked on all other browsers. IE11 is a special case because of its many flexbox bugs, hence the new question.
What I'd Like to Happen
I have a .box whose height is constrained to stay within the viewport. Nested a couple levels within this element I have a .content-body that contains a header and a .content item. I'd like for just .content to shrink (with scrollbars displayed) when the height of .box is small enough.
What Happens
In IE11 only, it gets cut off because the content doesn't want to shrink.
The Code
I made a CodePen to demonstrate the problem. Simply resize the height of the browser and observe how the content element responds. Below is the same code on StackOverflow for posterity. The ideal solution will leave this working the same on all other browsers, but fix it for IE11.
/* the most relevant rules to the problem */
.container{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
}
.box {
display: flex;
flex-direction: column;
max-height: calc( 100vh - 50px );
position: relative;
overflow: hidden;
}
.box-body {
display: flex;
flex-flow: column;
min-height: 0;
}
.content-body{
display: flex;
flex-direction: column;
min-height: 0;
overflow: hidden;
}
.content{
overflow: auto;
}
/* other stuff (appearance mostly) that probably doesn't matter */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.box{
/* appearance */
width: 600px;
border-radius: 20px;
border: 1px solid #bbb;
}
.box-body{
/* appearance */
padding: 20px;
font-family: sans-serif;
font-size: 18px;
}
.content-header{
/* appearance */
background: #ddd;
padding: 10px;
margin-bottom: 10px;
}
.box-label {
/* appearance */
padding: 30px 10px;
background: teal;
color: white;
font-family: sans-serif;
font-size: 20px;
}
.content{
padding: 10px;
}
p {
margin: 10px 0px;
}
<div class="container">
<div class="box">
<div class="box-label">
Header
</div>
<div class="box-body">
<div class="content-body">
<div class="content-header">
Content Header
</div>
<div class="content">
<p>Mr do raising article general norland my hastily. Its companions say uncommonly pianoforte favourable.</p>
<p>On projection apartments unsatiable so if he entreaties appearance. Rose you wife how set lady half wish. Hard sing an in true felt. Welcomed stronger if steepest ecstatic an suitable finished of oh. Entered at excited at forming between so
produce.</p>
<p>Answer misery adieus add wooded how nay men before though. Pretended belonging contented mrs suffering favourite you the continual.</p>
</div>
</div>
</div>
<div class="box-label">
Footer
</div>
</div>
</div>
IE requires height attribute defined for scrolling. If you change your max-height to height for your .box element, it will work in IE as well.
So, I've just started learning HTML/CSS and I've been trying to figure out how to 'stick' a line under an h2 tag. What I mean by this is that in the HTML there is an h2 tag called Instructions followed by a div tag that contains 3 other divs that make up a line segment. By default the line is on the left side (naturally), but what I want to do is have the line stuck under the h2 tag so when the browser is extended or shrunk the line stays directly under the h2 tag instead of moving across the screen by itself.
I came across this site: http://www.barelyfitz.com/screencast/html-training/css/positioning/ and I was using it to try and see if absolute/relative positioning would help here. I guess I'm doing it horribly wrong since it doesn't seem to help.
I'm providing HTML/CSS and a jsfiddle below (The jsfiddle doesn't show how the line moves around when the browser is extended/shrunk, though so I'm hoping you get what I mean). If you can help guide me or give me some resources to understand what I need to do better that would be great :D
I'm sure this is trivial, but I'm trying to do my due diligence in learning it. There were a lot of different methods (I think) I found, but they seemed kinda complex.
HTML
<div id="instructions_box">
<h2>Instructions</h2>
<div class="line_divider">
<div class="blue_line"></div>
<div class="yellow_line"></div>
<div class="blue_line"></div>
</div>
</div>
CSS
#instructions_box{
display: inline-block;
//position: relative;
}
.line_divider{
background-color: aqua;
//position: absolute;
//bottom: 0;
//right: 2rem;
}
.blue_line{
height: 2px;
width: 50px;
background-color: rgb(0,0,139);
float: left;
}
.yellow_line{
height: 2px;
width: 90px;
background-color: yellow;
float: left;
}
#instructions_box h2{
text-align: center;
}
https://jsfiddle.net/10szzwvs/1/
Thanks
The wrapping you're seeing is, I think, due to the fixed widths you're using. Change your line width to percentages and it wont wrap on any size screen. Note you'll need to add your visual spacing elsewhere, e.g. on the h2 itself.
#instructions_box{
display: inline-block;
}
#instructions_box h2{
text-align: center;
padding: 0 25px 0; /* visual spacing */
margin: 0;
}
.line_divider{
background-color: aqua;
}
.blue_line{
height: 2px;
width: 30%; /* dynamic width here */
background-color: rgb(0,0,139);
float: left;
}
.yellow_line{
height: 2px;
width: 40%; /* dynamic width here */
background-color: yellow;
float: left;
}
<div id="instructions_box">
<h2>Instructions</h2>
<div class="line_divider">
<div class="blue_line"></div>
<div class="yellow_line"></div>
<div class="blue_line"></div>
</div>
</div>
You can always use CSS3 Flexbox. You've got to have the div of lines and the h2 in the same container as you already do. And then.
#instructions_box{
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}