Stuck on responsible flex boxes - html

got a problem with wrapping a flexbox, when the width is too small to display both. Here is a fullscreen screen: Shows a fullscreen-site
So here is what happens if reduce the browser width: reduced width
Now, if I reduce the width again, it looks like that: 1 mm more reduced than before
But of course, it should be among themselves.
body {
background-color: aquamarine;
}
#Wrapper,
body,
html,
main,
nav,
header,
div,
class,
footer {
margin: 0;
padding: 0;
}
main {}
header h1 {
position: relative;
width: 50%;
top: 8vh;
left: 25%;
text-align: center;
margin: 0;
padding: 0;
font-family: 'Bree Serif', serif;
color: deeppink;
}
.Container {
position: absolute;
top: 25%;
left: 30vw;
display: flex;
height: 50%;
width: 40%;
flex-flow: row wrap;
justify-content: space-between;
}
.item {
width: 16em;
background-color: lightslategrey;
}
<link href="https://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet">
<div id="Wrapper">
<header>
<nav>
</nav>
<h1>This is a heading and it's extra long to test stuff.</h1>
</header>
<main>
<div class="Container">
<div class="item">
</div>
<div class="item">
</div>
</div>
</main>
</div>

I see a few issues on your CSS that forces this behavior on your boxes item.
The main issue is that you limit your container to a width of 40% and then when you resize your window, there's no space for the inner boxes to be displayed correctly.
I'd give a full width: 100% to the container, left: 0 and justify-content: center.
Then I'd give margins-left and right to the inner items.
So your CSS to something like this:
.Container {
position: absolute;
top: 25%;
left: 0;
display: flex;
height: 50%;
width: 100%;
flex-flow: row wrap;
justify-content: center;
}
.item {
width: 16em;
background-color: lightslategrey;
margin: 0 30px;
}
This solves partially the issue as your inner boxes are still overlaped if you reduce your window size.
To solve it for real, you'd probably need a mediaquery to make the inner boxes smaller when the window is resized.
It could be something like this:
#media all and (max-width: 650px) {
.item {
width: 8em;
}
}
Here's a codepen so you can see the results: https://codepen.io/annabranco/pen/VEjXOL

Related

How to position text and images in a responsive way, with HTML and CSS

Im trying to position this image and tittle with text on my website.
How can I make it responsive so that the scale doesnt change on different devices and everything stays at the same position.
Im new at this, so im probably making a lot of mistakes.
div.content {
width: 100vw;
display: flex;
}
div.column1 {
width: 15%;
background-color: #F7F7F7;
overflow: hidden;
height: 100vh;
}
div.column2 {
width: 70%;
overflow: hidden;
}
.lobby {
width: 45%;
height: 45%;
padding-top: 5.6rem;
padding-left: 1rem;
}
.title {
padding-top: 4.7rem;
display: inline-block;
float: right;
width: 50%;
font-size: 1.8rem;
}
.descr {
display: inline-block;
width: 30vw;
float: right;
font-size: 1rem;
padding-top: 0.4rem;
}
<div class="content">
<div class="column1">
</div>
<div class="column2">
<div class="title">Installations
<p class="descr"> We are a gym based in Carballo, A Coruña, counting with an installation </p>
</div>
<img class="lobby" src="img/lobby.jpg" alt="photo of the lobby of the gym" />
</div>
</div>
<div class="column1">
</div>
As you can see im trying to position the image and the text on the middle row of my website, since it is divided into 3 columns.
Anything I can do to improve the code and to make it more responsive.
Thanks!
You don't need 3 columns. My approach starts defining the wrapper layout (the container), then working on the content layout (2 columns nested on the wrapper).
Here is my way of doing it:
HTML
<div class="main-container container">
<div class="inner-container content">
<div class="column-left column column-1">
<img class="lobby" src="https://upload.wikimedia.org/wikipedia/en/c/cb/Placeholder_logo.png" alt="photo of the lobby of the gym">
</div>
<div class="column-right column column-2">
<h1 class="title">Installations</h1>
<p class="descr">We are a gym based in Carballo, A Coruña, counting with an installation </p>
</div>
</div>
</div>
CSS
<style>
html,
body {
padding: 0px;
margin: 0px;
}
/* set the layout */
.main-container {
width: 100%;
max-width: 100vw;
min-height: 100vh;
box-sizing: border-box;
padding-left: 15%;
padding-right: 15%;
position: relative;
}
/* this is a pseudo element, it renders the grey background on the left */
.main-container:before {
content: "";
display: block;
height: 100%;
background-color: #F7F7F7;
/* same width of padding-left as it covers only the left side */
width: 15%;
position: absolute;
z-index: 1000;
top: 0;
left: 0;
}
.inner-container {
width: 100%;
display: flex;
flex-wrap: wrap;
padding: 2rem 1rem 0;
box-sizing: border-box;
}
.column {
padding: 3.6rem 0 1rem;
box-sizing: border-box;
/* Column width - Change this with media Queryes */
width: 50%;
flex-basis: 50%;
}
/* page elements*/
.title {
font-size: 1.8rem;
margin-top: 0px;
}
.descr {
font-size: 1rem;
padding-left: 3rem;
}
.lobby {
width: 45%;
height: auto;
}
/* responsive media query */
/* decide the breakpoint to start having 1 column */
#media screen and (max-width: 767px) {
.column {
width: 100% !important;
flex-basis: 100%;
}
}
</style>
Here is a working Codepen: https://codepen.io/Davevvave/pen/BaPZRbb
Consider the #Sfili_81 advice, first study and learn about #media_queries (https://www.w3schools.com/css/css3_mediaqueries_ex.asp), avoid float, and
(I suggest) try to understand the natural behavior of HTML rendering flux, the semantic meaning of HTML tags and empower all with CSS.

Footer hides content of the div above it

Here is the link to codesandbox where I am trying to add a footer but the content above it gets hidden. How can I fix this? What is the mistake I am doing?
The footer component is commented in the app.js file. Scroll to the bottom and see the content and if you uncomment the footer, it will hide the content.
https://codesandbox.io/s/rk4kl
The main problem is using the position property in the wrong place.
the position property is good especially when you're considering boxes.
in your styles.css
.App {
font-family: sans-serif;
height: 100%;
position: relative;
}
.footer {
position: absolute;
min-height: 2rem;
width: 100%;
text-align: center;
bottom: 0;
}
.flex {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
}
change it into
.App {
font-family: sans-serif;
min-height: 100vh;
}
.footer {
min-height: 2vh;
width: 100%;
text-align: center;
}
.flex {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
min-height: 90vh;
}
Explanation:
The main idea is that for keeping the footer always have to split the viewport into separate ratios. So that they can always stay with that ratio irrespective of the content.
here the div with className App have a min-height of 100vh, which means when we split the viewport into 100 pieces div with App occupies a minimum of 100 slices. Now inside App, we have the flex and footer. We give flex a minimum of 90 pieces and a footer of a minimum of 2. so the footer will always keep in the bottom now.
here is the updated result
I think you are searching for some "Sticky Footer" Solutions, if your Content is not long enough, the footer should be at the end of the page.
You could give a margin-top - footer height.
html, body {
height: 100%;
margin: 0;
}
.content {
background-color: blue;
min-height: 100%;
}
.content-inside {
padding: 20px;
padding-bottom: 50px;
}
.footer {
background-color: yellow;
height: 50px;
margin-top: -50px;
}
<body>
<div class="content">
<div class="content-inside">
content
</div>
</div>
<footer class="footer">Here comes the Footer</footer>
</body>
Just change position: absolute; to position: relative;
.footer {
position: relative;
min-height: 2rem;
width: 100%;
text-align: center;
bottom: 0;
}
Use from position static instead absolute for .footer.
When you use position: absolute for an element, it may be placed over other elements. because:
Absolute positioned elements are removed from the normal flow and can overlap elements.
From: https://www.w3schools.com/css/css_positioning.asp
Just remove position "absolute" or add position "relative" to it.
.footer {
position: relative;
min-height: 2rem;
width: 100%;
text-align: center;
bottom: 0;
}

Web: Perfectly center content in middle, but have a div on top that doesn't push content down

Just learning web dev with react, so apologies if this is a basic question, but there are so many ways to do things with CSS and I'm lost.
Here is my HTML:
<div class="main">
<div class="top-app-bar">
Top App Bar
</div>
<div class="center-content">
Perfectly centered
</div>
</div>
Here is my css
.main {
background-color: #212121;
}
.top-app-bar {
height: 20vmin;
margin: 2vmin;
}
.center-content {
text-align: center;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
The css is mainly copied from create react app starter project
https://jsfiddle.net/2wz7dahq/
As you can see in the jsfiddle, having my content pushes my content down. I do not want my main content to be pushed down, I want it to be perfectly centered so if the screen is resized into any dimension it has no scroll bars or anything. This page should be a perfect single page app with no scrolling.
Use absolute positioning for the .center-content so that the .top-app-bar or any other elements/contents would not influence its position. As for the scrollbar, this appears because of the additional height generated by the margins. So the equation for your document height becomes: 100vh + 8px + 2vmin where 8px is the body margin coming from the user agent stylesheet & 2vmin is the margin coming from user defined CSS for the element .top-app-bar.
For that you could do any of the following:
Get rid of the margins
Apply overflow:hidden CSS property to prevent the scrollbar view
or Refactor min-height to factor in the margins, e.g., min-height: calc(100vh - XXX)
In this implementation, you need to use a higher z-index for the interactive elements (for example, .top-app-bar) if the .center-content is going to take up the entire viewport (primarily mentioning this because currently it is taking up the entire viewport in your code example) because technically, it is currently at the top of the Stacking Context. Alternatively, control .center-content's height & width to not take up the entire viewport.
html,
body {
margin: 0;
overflow: hidden;
}
.main {
background-color: #212121;
min-height: 100vh;
color: silver;
}
.top-app-bar {
height: 20vmin;
margin: 2vmin;
position: relative;
z-index: 1;
}
.center-content {
text-align: center;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
<div class="main">
<div class="top-app-bar">
Top App Bar
</div>
<p>
Lorem Ipsum
</p>
<div class="center-content">
Perfectly centered
</div>
</div>
React Component Below (since this is tagged as reactjs):
const App = () => {
return (
<div className="main">
<div className="top-app-bar">
Top App Bar
</div>
<p>
Lorem Ipsum
</p>
<div className="center-content">
Perfectly centered
</div>
</div>
);
}
ReactDOM.render(<App/>, document.getElementById("root"));
html,
body {
margin: 0;
overflow: hidden;
}
.main {
background-color: #212121;
min-height: 100vh;
color: silver;
}
.top-app-bar {
height: 20vmin;
margin: 2vmin;
position: relative;
z-index: 1;
}
.center-content {
text-align: center;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Footer is at the bottom of one html page but in the middle of another

I am trying to set a footer at the bottom of the page for each HTML page.
On my main index page, it sets at the bottom because I have several sections on the page, which includes a block of text and a video sitting side by side above the footer.
On another HTML page I just have my navigation bar and the footer styled. The main tag has no content. The footer on this page is half way up the screen.
I've looked through different solutions and have tried bottom: 0 as well as position: fixed and position: absolute, but absolute just causes the footer on my main page to cover up the horizontal line between the text/video section and the footer, and the very bottom part of the video and text. Fixed causes it to sit over all the content and at the bottom of the page does the same as absolute.
.about {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
width: 50%;
font-size: 1.5em;
float: left;
height: 50vh;
margin-top: 3em;
left: 0;
margin-bottom: 3em;
}
.videos {
width: 50%;
height: 50vh;
float: left;
margin-top: 3em;
right: 0;
margin-bottom: 3em;
}
.frontPageVideo {
width: 95%;
height: 100%;
}
footer {
clear: both;
background-color: rgb(0, 0, 0);
height: 20vh;
margin-top: 5em;
}
How can I set the footer at the bottom of every page?
Give your body a min-height: 100vh css rule to ensure your page is never shorter than the viewport.
You might also consider using flex to establish the header/main/footer layout.
body {
margin: 0;
}
.main {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.main > * {
flex: 1 1 auto;
background: bisque;
}
.main header,
.main footer {
flex: 0 0 30px;
background: tomato;
}
<div class="main">
<header>Header</header>
<div>Content</div>
<footer>Footer</footer>
</div>
In addition to the answer by Ray Hatfield, I would also add it to the html element. Here is what I always include in my websites:
html, body{
width: 100vw;
height: 100vh;
padding: 0;
margin: 0;
}

:before to respect parent's padding

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>