Mini-navbar with left, middle and right in HTML and CSS - html

I want to make a very simple navbar with HTML and CSS (so simple I prefer to do it without Bootstrap), made of just three short texts, situated on the leftmost, center, and rightmost part of one single line.
My idea is that I cut the line in two halves, put the left & middle part in the first half, and the rightmost part in the second half. So I tried the following :
.div_left {
float: left;
position: absolute;
}
.div_right {
float: right;
text-align: right;
position: absolute;
}
.container_for_mininavbar {
width: 100%;
position: absolute;
}
.mininavbar_left_half {
width: 50%;
float: left;
position: absolute;
}
.mininavbar_right_half {
width: 50%;
float: right;
position: absolute;
}
<div class="container_for_mininavbar">
<div class="mininavbar_left_half">
<div class="div_left">Left Text</div>
<div class="div_right">Center Text</div>
</div>
<div class="mininavbar_right_half">
<div class="div_right">Right Text</div>
</div>
</div>
But that doesn't work, all the texts are on top of each other.
What is the correct way to do this?

Just remove position absolute.
I'll suggest to use flexbox to do this and don't use float anymore
.div_left {
float: left;
}
.div_right {
float: right;
text-align: right;
}
.container_for_mininavbar {
width: 100%;
}
.mininavbar_left_half {
width: 50%;
float: left;
}
.mininavbar_right_half {
width: 50%;
float: right;
}
<div class="container_for_mininavbar">
<div class="mininavbar_left_half">
<div class="div_left">Left Text</div>
<div class="div_right">Center Text</div>
</div>
<div class="mininavbar_right_half">
<div class="div_right">Right Text</div>
</div>
</div>
And this is a little example with flexbox
.container_for_mininavbar {
width: 100%;
border: 1px;
display: flex;
}
.container_for_mininavbar div {
flex: 0 1 33.33%;
border: 1px solid #000;
text-align: center;
}
<div class="container_for_mininavbar">
<div>Left Text</div>
<div>Center Text</div>
<div>Right Text</div>
</div>

So, you want some links to the left, some to the center and the rest to the right?
The easiest and most effective way (by me) is to use Flexbox.
So, you need a container div, named "navigation" (or however you want) which contains another 2 divs, one for the left side, and one for the right side.
Now, assign to the navigation div, the following:
display: flex; /* is going to display the div flex */
justify-content: space-between; /* this is where magic happens, it will push the items from the nav div, which are the other 2 divs to the left and right side*/
flex-flow: row nowrap;
The first property is for it to be displayed in a row, you can set it to column too, and the nowrap is not going to let the content to deform in some sort of way, if you set that to wrap, of course, it will wrap under, but I suggest letting that nowrap, but I don't think flex-flow is 100% neccesary in this situation
Now, the flexbox works for the other 2 divs as well, maybe you want the links in the left-side div to be "justify-content: space-between;" or space-evenly, or center, space-around, etc.
I recommend you to learn Flexbox, it's very useful and simple to use.
I hope this answer will help you. :)
And to center the links in each div, use align-items: center; , it will center the links on the Y scale. (which is top-bottom)
EDIT: If you want center links too, it's the same thing, just make another div between the left-side div and the right div. And the justify-content: space-betweeen; it's going to have the same effect. And if you don't link how it scales, you can always use the margins in the div.

Related

Justify content does not seem to affect my flexbox children. Is it because of my height/width properties for one of my elements?

What I have set up is a flexbox that contains 3 children: the previous button, a div that contains the slideshow, and the next button. I have the flexbox set to justify-content: center, but the arrows stick to the far sides of the flexbox instead of right next to the slide div on either side. There is a gap between each arrow and the slide that won't go away unless I make the browser window narrower so the flexbox doesn't have any extra room. Setting justify-content to center should be keeping them all together.
I'm having the same issue with the slide container as well. It is also a flexbox and contains 3 children: a title, the image, and numbers at the bottom. When I shrink the window, the slide image shrinks to fit, but the title and numbers both stay stuck to the top and bottom of the flexbox instead of clustered in the middle with the image.
I would like this to be dynamic so I won't have to adjust the CSS as much with media queries for a number of different breaking points.
Things I have tried:
setting the margins to 0px for all children
setting the padding to 0px for all children
putting the buttons inside the slideshow container instead of the
current parent and setting the slideshow container to flex
every other option for justify-content (center, flex-start, flex-end, etc); none of them seemed to have any effect on the children
removing the width attribute from my slideshow container and setting it to auto (this made the slides disappear)
removing the height attribute from my slide container (this did fix the issue, but it also caused the slide to extend past the boundaries of its container)
I am also using some javascript that probably doesn't need to be included, but it sets each new slide's display to block and each previous slide's display to none. In order for this slideshow to work, I am under the impression that the slides need to their position set to absolute, and their parent container set to relative.
Here is a fiddle with the exact code I am using. I initially tried to simplify it to ask this question by removing container1 and the description on the left, but then I couldn't replicate the problem I was having. I am wondering if it has something to do with the height or width attributes of one of elements.
https://jsfiddle.net/kj7wzr5a/
.container1 {
width: 85%;
height: 650px;
display: flex;
justify-content: space-around;
align-items: center;
margin: 0 auto;
text-align: center;
}
.left {
width: 45%;
text-align: center;
margin-left: 30px;
margin-right: 20px;
}
.descrip1 {
margin-top: 20px;
}
.right {
height: 100%;
width: 45%;
display: flex;
justify-content: center;
align-items: center;
}
.flex {
height: 100%;
display: flex;
flex-direction: column;
align-content: center;
}
.slideshow-container {
height: 100%;
width: 360px;
margin-top: 25px;
position: relative;
}
.slide {
height: 90%;
width: 100%;
display: none;
position: absolute;
}
.slide:first-child {display: block}
.slide {
-webkit-transition: 0.5s ease;
transition: 0.5s ease;
}
.slide img {
height: 100%;
width: 100%;
margin: 0px;
object-fit: contain;
}
/* Next & previous buttons */
.buttons {
height: auto;
}
<div class="container1 bodytext">
<div class="left">
<div class="descrip1">
<p>Blah blah text</p>
</div>
</div>
<div class="right">
<div class="buttons">
<a class="slider_nav prev"><</a>
</div>
<div class="slideshow-container">
<div class="slide">
<div class="flex">
<div class="text">New Character</div>
<img src="1.png" style="width:100%">
<div class="numbertext">1 / 10</div>
</div>
</div>
<div class="slide">
<div class="flex">
<div class="text">New Character Stats</div>
<img src="2.png" style="width:100%">
</div>
</div>
</div>
<div class="buttons">
<a class="slider_nav next">></a>
</div>
</div>
</div>
I am fairly new to coding and haven't had any formal training.

Align two colums (the second of them, with two items) to the base line

I'm trying to recreate something like the screenshot attached. Please, note the block with "another text" is aligned with the left column. Each grey box will be an image. Also take into consideration that the text can be multiple lines.
This is what I have so far: https://jsfiddle.net/chux/qgbw0moz/51/
The problem is that, when the first colum have more lines than the second one, the bottom of the boxes will be aligned, not the bottom of the images.
How can align them?
.container {
width: 100%;
}
img {
display: block;
margin: 0;
max-width: 100%
}
.row {
display: flex;
align-items: strech;
}
.row>.col {
flex-grow: 1;
}
.row-col {
background: grey;
flex-direction: column;
justify-content: space-between;
height: 100%;
display: flex;
}
.first-col {
background: red;
}
.second-col {
background: green;
}
.third-col {
background: pink;
}
<div class="container row">
<div class="col first-col">
<img src="https://placeimg.com/450/480/animals">
</div>
<div class="col">
<div class="row-col">
<div class="col second-col">
<img src="https://placeimg.com/400/180/animals">
</div>
<div class="col third-col">
<img src="https://placeimg.com/400/180/animals">
</div>
</div>
</div>
</div>
So, here goes, nice and simple - just give a fixed height to the text shown under each photo. You will have to decide about this height depending on your 'average text height' and use overflow:hidden or use the highest value possible.
.subtitle {
line-height:20px;
height:40px;
overflow:hidden;
}
Here is a working jsfiddle:
https://jsfiddle.net/scooterlord/7wqmf1Lr/
edit: here is another way to fake it. For the first column instead of using an img, just use a div element with a background image. Safely assuming that the second column might have variable height, expand the div with the background image on first column to consume all of the first column's height and then add a second row for either single or multi-text beneath. This might be a bit tricky for responsive -eg change the background image div's height to absolute value or add multiple layers of the same text to hide/show on specific resolutions (or use JS instead), but is certainly doable and a direct solution to your problem.
If you like I can help you with the implementation of the responsiveness as well - although out of the scope of this question.
Replaced img tag with div and background-image:
<div class="img" style="background-image:url(https://placeimg.com/400/480/animals)">
</div>
Added css:
.img {
display: block;
width: 100%;
height: 100%;
background-size: cover;
background-position: center center;
}
Working fiddle.
https://jsfiddle.net/scooterlord/7wqmf1Lr/13/

Image mosaic with html/css fill the space

I'm trying to make an "image mosaic" that consists mostly of images of the same size, and some of them the double height.
They all should align neatly like this:
enter image description here
But still like this:
enter image description here
I know is more "easy" do this with divs and "float:left" but i have to do with list.
Someone had i idea how can i fix this?!
Use flexbox. You don't HAVE to use list? Unless this is a homework assignment in which case he's making you do something that's not best practice
.mosaic-container {
display: flex;
justify-content: space-between;
width: 330px;
padding: 10px;
background-color: blue;
border: 3px solid black;
}
.left-col, .mid-col, .right-col {
display: flex;
flex-direction: column;
flex: 0 0 100px;
}
.left-col .mosaic-tile:first-of-type, .right-col .mosaic-tile:first-of-type {
margin-bottom: 10px;
}
.mid-col .mosaic-tile {
height: 100%;
}
.mosaic-tile {
width: 100%;
height: 100px;
background-color: #fff;
}
<div class="mosaic-container">
<div class="left-col">
<div class="mosaic-tile">1.1</div>
<div class="mosaic-tile">2.1</div>
</div>
<div class="mid-col">
<div class="mosaic-tile">1.2</div>
</div>
<div class="right-col">
<div class="mosaic-tile">1.3</div>
<div class="mosaic-tile">2.2</div>
</div>
</div>
I believe you will be able to implement this in a simpler and more future proof way by using CSS Grids https://learncssgrid.com/ Float based layouts are generally discouraged, similar to table based layouts.
If you are set on float based, I would create a left column div, middle column div, and right column div all float: left. Then I would place the two images in the left, tall in the middle, and last two in the right column div.

How to vertically & horizontally center children elements in a fluid layout set by media queries?

I'm trying to achieve a certain fluid layout where the content of each DIVs are centered vertically and horizontally. But, my middle row (A, B, C) keeps on having vertical and/or horizontal alignment issues.
The goal is to have it work like this:
Note: If there's a way I can have the option to set the Mobile layout's "C" area fluid as well (without having to change the HTML, just the CSS, so that I can test which option works best), that'd be a bonus!
Here's a snippet of the HTML:
<div class="page">
<div class="col col-top">top</div>
<div class="col col-mid">
<div class="col col-left">
<div class="centerBox"><div class='debugBox'></div></div>
</div>
<div class="col col-center">
<div class="centerBox"><div class='debugBox'></div></div>
</div>
<div class="col col-right">
<div class="centerBox"><div class='debugBox'></div></div>
</div>
</div>
<div class="col col-bottom">bottom</div>
</div>
I'm not sure if the "wrapper" DIVs with the "centerBox" class is really necessary (they're set as display: table-cell while each col class are set to display: table to behave like tables, but this causes issues to place those areas with position: absolute and % values for their left / right / top / bottom properties.
For instance, if the "C" area is set to display: table, this happens:
And if I change the "C" area to display: block;, then it fills that full center area, but...
... the horizontal and vertical alignment breaks inside of it.
Would using "Ghost" DIV elements (as discussed in this css-tricks article, "Centering in the Unknown" by Chris Coyier ) be any better to get the correct alignment?
Ok, this solution works without a framework, pure CSS using flexbox. As long as the layout is horizontal, C has a fixed width. When it is mobile, C takes up the whole width and has a variable height.
header,
footer {
padding: 10px;
background-color: lightblue;
}
main {
display: flex;
flex-direction: row;
}
main > div {
padding: 10px;
background-color: tomato;
flex-grow: 1;
min-height: 40px;
display: flex;
align-items: center;
}
main > div:nth-child(2) {
background-color: olive;
}
.fixed {
width: 400px;
}
#media (max-width: 768px) {
main {
flex-direction: column;
}
.fixed {
width: auto;
}
}
<header>Top</header>
<main>
<div>A</div>
<div class="fixed">C</div>
<div>B</div>
</main>
<footer>Bottom</footer>
Here is a pen (drag the border to see the mobile layout):
Codepen
Here are the styles for the code you have provided. The one thing to keep in mind is your middle column, being a fixed width, is what helps with the calc() function. 50% of HALF the width of the middle container. This will not work in IE 8 or less, so you'll have to write a JS solution if you care about those browsers.
.page {
width: 100%;
position: relative;
}
.col-top {
background: #0f0;
width: 100%;
height: 50px;
}
.page .col-mid {
width: 100%;
display: table;
}
.page .col-mid .col {
width: calc(50% - 250px);;
height: 100px;
background: #f00;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.page .col-mid .col-center {
width: 500px;
background: #00f;
}
.debugBox {
display: inline-block;
width: 20px;
height: 20px;
background: #000;
vertical-align: middle;
}
.col-bottom {
clear: both;
height: 50px;
background: #0f0;
}
and a working example here:
https://jsfiddle.net/g45pwedd/
And you don't need some of the container elements, as you stated.
UPDATE
Sorry, forgot to add for responsive. I wasn't sure if you still needed vertical align for responsive or not. This solution removes vertical align, as I doubt it's needed on a mobile display anyways:
#media (max-width: 768px) {
.page .col-mid .col {
display: block;
width: 100%;
}
}
https://jsfiddle.net/g45pwedd/2/
In bootstrap 4
to center the childs horizontally, use bootstrap-4 class
justify-content-center
to center the childs vertically, use bootstrap-4 class
align-items-center
but remember don't forget to use d-flex class with these
it's a bootstrap-4 utility class, like so
<div class="d-flex justify-content-center align-items-center" style="height:100px;">
<span class="bg-primary">MIDDLE</span>
</div>
Note: make sure to add bootstrap-4 utilities if this code does not work

CSS - Make divs align horizontally

I have a container div with a fixed width and height, with overflow: hidden.
I want a horizontal row of float: left divs within this container. Divs which are floated left will naturally push onto the 'line' below after they read the right bound of their parent. This will happen even if the height of the parent should not allow this. This is how this looks:
How I would like it to look:
![Right][2] - removed image shack image that had been replaced by an advert
Note: the effect I want can be achieved by using inline elements & white-space: no-wrap (that is how I did it in the image shown). This, however, is no good to me (for reasons too lengthy to explain here), as the child divs need to be floated block level elements.
You may put an inner div in the container that is enough wide to hold all the floated divs.
#container {
background-color: red;
overflow: hidden;
width: 200px;
}
#inner {
overflow: hidden;
width: 2000px;
}
.child {
float: left;
background-color: blue;
width: 50px;
height: 50px;
}
<div id="container">
<div id="inner">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</div>
style="overflow:hidden" for parent div and style="float: left" for all the child divs are important to make the divs align horizontally for old browsers like IE7 and below.
For modern browsers, you can use style="display: table-cell" for all the child divs and it would render horizontally properly.
You can now use css flexbox to align divs horizontally and vertically if you need to. general formula goes like this
parent-div {
display: flex;
flex-wrap: wrap;
/* for horizontal aligning of child divs */
justify-content: center;
/* for vertical aligning */
align-items: center;
}
child-div {
width: /* yoursize for each div */
;
}
This seems close to what you want:
#foo {
background: red;
max-height: 100px;
overflow-y: hidden;
}
.bar {
background: blue;
width: 100px;
height: 100px;
float: left;
margin: 1em;
}
<div id="foo">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
you can use the clip property:
#container {
position: absolute;
clip: rect(0px,200px,100px,0px);
overflow: hidden;
background: red;
}
note the position: absolute and overflow: hidden needed in order to get clip to work.
Float: left, display: inline-block will both fail to align the elements horizontally if they exceed the width of the container.
It's important to note that the container should not wrap if the elements MUST display horizontally:
white-space: nowrap
Float them left. In Chrome, at least, you don't need to have a wrapper, id="container", in LucaM's example.