Nested flex box with image has extra height [duplicate] - html

This question already has answers here:
Mystery white space underneath image tag [duplicate]
(3 answers)
Closed 6 years ago.
I'm working on a navigation bar for a page using only HTML and CSS. It should look like this, with the logo setting the height of the containing div, then with the text vertically centered in other divs next to it.
What it should look like:
I've tried to do this by setting up some nested flex boxes. The idea is that:
The container (nav-holder) stretches to fit the tallest content
The second container (nav-item) should all be as tall as its parent
The third container (nav-cont) should be as tall as its own content, and should be vertically centered inside nav-item
Instead, I'm ending up with an odd extra bit of space at the bottom of my logo inside nav-cont, and I can't work out where it's coming from.
It looks like this:
What it really looks like:
HTML code:
<div id="header">
<div id="nav-holder">
<div class="nav-item">
<div class="nav-cont"><img src="images/placeholder-logo.jpg"/></div>
</div>
<div class="nav-item">
<div class="nav-cont">Listings</div>
</div>
<div class="nav-item">
<div class="nav-cont">Services</div>
</div>
<div class="nav-item">
<div class="nav-cont">About</div>
</div>
<div class="nav-item">
<div class="nav-cont">Blog</div>
</div>
<div class="nav-item">
<div class="nav-cont">Contact</div>
</div>
</div>
</div>
CSS Code:
#nav-holder {
background-color: blue;
display: flex;
}
.nav-item {
background-color: yellow;
display: flex;
align-items: center;
margin-right: 0.5em;
}
.nav-cont {
background-color: green;
}
Attempted fixes:
Checked to see if there was a margin or padding set for images or for divs in general.
Looked for information in the most similar solved problem on StackOverflow ("CSS flexbox vertically/horizontally center image WITHOUT explicitely defining parent height")
Went through a couple of flexbox tutorials to see if there were any similar issues described, including "Solved by Flexbox" on GitHub and "Dive into Flexbox" by Greg Smith

I have took at look at your code and changed some CSS to try to get the idea of what you want.
#nav-holder {
background-color: blue;
display: flex;
}
.nav-item {
background-color: yellow;
display: flex;
align-items: center;
margin-right: 0.5em;
padding: 10px;
}
.nav-cont {
background-color: green;
width: 70px;
text-align: center;
}
Here is a Jsfiddle:
https://jsfiddle.net/r3msjtnL/
You had to change the nav-cont div to a pixel and float the text in the center, so it is not unbalanced. Also, I added a padding to make space around your nav.items.
Update:
If you do not want your buttons being fixed to a specific pixel, attempt changing the width to a percentage (%) instead !
If this helped vote up !

Related

Common methods for horizontal centering not working in CSS [duplicate]

This question already has answers here:
Can't scroll to top of flex item that is overflowing container
(12 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 years ago.
I've been trying to do the simple and mundane task of centering divs in CSS with no success.
Here's the code snippet:
* {
box-sizing: border-box;
}
body {
margin: 0;
}
#content {
}
.list-item {
position: relative;
display: inline-block;
border: solid thin #444;
}
.list-item .scene {
width: 200px;
height: 200px;
}
.list-item .description {
width: 200px;
margin-top: 0.5em;
}
.storyboard-row {
display: flex;
/* Method 1 */
/*justify-content: center;*/
/* Method 2 */
/*margin-left:auto;*/
/*margin-right:auto;*/
}
<div id="content">
<div class="storyboard-row">
<div class="list-item">
<div class="description">Scene 1</div>
<div class="scene"></div>
</div><div class="list-item">
<div class="description">Scene 2</div>
<div class="scene"></div>
</div><div class="list-item">
<div class="description">Scene 3</div>
<div class="scene"></div>
</div>
</div>
</div>
What I'm trying to center: The div with class="storyboard-row" in relation to the div with id="content"; and the div with id="content" in relation to its parent (the <body>).
What I tried: In the snippet you will find "Method 1" and "Method 2" which are my attempts at centering stuff around. The first method, using justify-content: center;, works but on downsizing the window, the leftmost squares will be pushed outside off the screen. The second method simply does nothing. Bootstrap can be used.
What I need to continue to happen: Currently the div with class="storyboard-row" has display: flex; which I used so that when downsizing the window, a scrollbar appears instead of pushing down a number of squares (which happens with block). In the snippet, only one row is shown, but the idea is to have multiple (each bellow the former).
EDIT: Thanks to #TemaniAfif the centering problem was fixed. However, because the div with id="content" now has display: flex, when rows are small enough in relation to the screen, they appear on the same line. An updated snipped can be found here: https://jsfiddle.net/hdnz34g8/
If I remove the display: flex from it, the rows appear as intended, line-wise, but they're no longer centered.

How to set a DIV block with dynamic size with header DIV block above it at center of the page horizontally and vertically? [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 3 years ago.
I have a DIV block where there are a title and an image into the him. The image is setted by mouse click, so I don't know the size of image and DIV block. It's a structure:
.view-image .header { text-align: center; border: 1px solid black; }
.view-image .image { max-width: 100%; }
.view-image {
position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: fit-content;
height: fit-content;
max-width: 100%;
text-align: center;
}
<div class = "view-image">
<div class = "header">View of image</div>
<div class = "image"><img src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png" /></div>
</div>
I need that view-image block will take place in the centre of page horizontally and vertically. And, it's very important, the width of header and image blocks must be same.
Later I saw that Firefox doesn't support height: fit-content, in the IE and Edge also width: fit-content doesn't work.
How to make this? Of course, I can set a view-image sizes by JavaScript after image selecting, but if is it possible, it will be better do it without JS.
P.S. I want to write why my question isn't a duplicate in my opinion. The main complexity is the width of header block. If I use the flexbox as you suggest, the width of header will be count by width of the its inner text, and header width doesn't equal to image width. But if I use the fit-content as in my code, the header width and the image width are idential. It's very important for me, because in my site the header has a borders and background color, and if these blocks have different width, it won't be good. Maybe I wrote my question incorrect, then I'm sorry and I have edited my question. I have changed the title and added the border for header block in my code
Added: I found the solution by changing the structure to this:
<div class = "view-image">
<div class = "image">
<div class = "header">View of image</div>
<img src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png" />
</div>
</div>
I'm very sorry for incorrect question
Here is an example using flexbox:
.view-image {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
<div class="view-image">
<div class="header">View of image</div>
<div class="image">
<img src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png"/>
</div>
</div>

How to center a form on the screen (between navigation bar and footer) [duplicate]

This question already has answers here:
Vertical Align Center in Bootstrap 4 [duplicate]
(20 answers)
Closed 4 years ago.
First of all, this isn't a duplicated question. I checked all previous posted questions similar to this type of issue and I didn't find any solution for this specific scenario.
I'm using Bootstrap 4 and here is my app URL: https://loving-shaw-e78a46.netlify.com/auth/login
(I share an URL because I don't know how to post the CSS and HTML of a React app, code is huge and Stack Overflow doesn't let me to post it because of the size limit).
So what I would like to do is:
Make <main class="container"> fill all the height between the navigation bar and footer.
Center vertically <div class="form-page">.
Here's an image:
I used flexbox
I needed to overwrite some of your values
#app > div {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: space-between;
}
/* overwrite */
body {
margin-bottom: 0;
}
footer.cmTXSD {
position: relative;
}
Note
the margin-bottom overwrite 'deletes' your margin-bottom: 313px; 313px is like a magic number you did not calculate, but measured by experiment. This is a strong code smell in CSS. If you need to use magic numbers in CSS, try to think it over.
You can do this with flexbox:
First you have to surround everything within a div that looks like this:
<div class="d-flex flex-column" style="height: 100vh">
// header
// content
// footer
</div>
The 100vh stands for view height. Now to make sure your content is the size of the page add flex: 1. That's the answer on your first question.
And to center it add justify-content-center and align-items-center.
So the end result should look like:
<div class="d-flex flex-column" style="height: 100vh">
// header
<div class="justify-content-center align-items-center" style="flex: 1">
// content
</div>
// footer
</div>
This also means you don't have to force your footer down with position absolute or fixed.
You just need to add few css code to make the form look in the center of the container.
Please find below code and add to your css file.
.container {height: calc(100vh - 383px);position: relative;}
.container .form-page {position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
background: #fff;
height: 500px;
width: 500px;
padding: 15px 30px;}
Thank You.

How to make a div section responsive in a vertical website

I am creating a vertical website that has several different sections.
I want to make each section responsive to the content it has, but it seems like it's not responsive right now. Those two texts on the first row below the navbar is supposed to be in two different lines because it is written like:
<div id="firstRow">
<a id="about" class="smooth"></a>
<div class="intro">
<div>Welcome to my website</div>
<div>Scroll down to know more about us</div>
</div>
</div>
and I tried to use flex to make the first div responsive
div#firstRow {
padding: 100px;
display: flex;
}
How can I make this work?
I think you should put the display: flex property to your .intro div and also add a flex-direction of row to put it on the same line:
.intro {
display: flex;
flex-direction: row;
}
Example on jsFiddle.
do it something like this
.intro > div {
float:left;
clear: both;
display:block;
}

Creating a linked title inside a linked div

I'd like to create a full-width header div that links to the top of the page and inside this div, there is the 'page title' that links to the home page.
Doing so, doesn't seem to work: https://jsfiddle.net/9wscc5yy/
<a href="www.example.com">
<div id="header" style="width:100%; background-color: #fff">
<a href="www.google.com">www.google.com
</a>
</div>
</a>
So I tried to create three divs next to each other with the middle div containing the 'page title' and the remaining two divs floating left and right. The result: https://jsfiddle.net/vef0tt07/
<div id="header">
<a href="www.example.com">
<div style="float: left; width: 40%; background-color:#fff">
</div>
</a>
<a href="www.example.com">
<div style="float: right; width: 40%; background-color:#fff">
</div>
</a>
<a href="www.google.com">
<div style="overflow:hidden; text-align: center;">
<strong>Title</strong>
</div>
</a>
</div>
The new issue is that I don't know how to let the side divs change width so that they always reach to the text of the 'page title'.
Is there a better way to create a linked title inside a linked div?
Thanks in advance for your time to help me.
Try this:
HTML
<header>
<h1>
link to top of page
</h1>
<h2>
link to home page
</h2>
</header>
CSS
header {
position: relative;
background-color: red;
height: 50px;
}
h1 {
margin: 0;
}
h1 > a {
display: block;
color: red;
}
h2 {
margin: 0;
background-color: yellow;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
DEMO
The background section (red) links to one place. The title in the middle (yellow) links to another place.
With absolute positioning, the h2 is set to remain perfectly centered in the header.
Update (based on comments)
In order to make the header fully responsive, with no artificial heights, and all links equal height regardless of content size, use flexbox.
It's actually very simple and requires minimal code.
HTML
<header>
</header>
CSS
header { display: flex; } /* establish flex container */
header > a { flex: 1; } /* make all flex items equal width */
DEMO
To learn more about flexbox visit:
Using CSS flexible boxes ~ MDN
A Complete Guide to Flexbox ~ CSS-Tricks
Note that flexbox is supported by all major browsers, except IE 8 & 9. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add all the prefixes you need, post your CSS in the left panel here: Autoprefixer.