Aligning 3 divs side by side in a navigation bar - html

I am trying to align 3 divs side by side in a navigation bar. I've spent the past 5 hours trying to figure this out and I know it's something super simple that I can't just wrap my head around.
This is where I am at right now.
If I float the align-right div the tags Join & Support stack ontop of each other.
<div id="sticky-nav">
<div class="container">
<div class="box">
<div class="align-left">
Home Listings
</div>
<div class="align-center">
<form action="/action_page.php">
<input type="text" placeholder="Search..">
<button type="submit">
<i class="fa fa-search"></i>
</button>
</form>
</div>
<div class="align-right">
Join Support
</div>
</div>
</div>
</div>
#sticky-nav {
overflow: hidden;
background-color: #7889D6;
position: fixed;
top: 0;
width: 100%;
}
#sticky-nav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}
#sticky-nav input[type=text] {
padding: 6px;
margin-top: 8px;
font-size: 17px;
border: none;
max-width: 300px;
width: 100%;
}
#sticky-nav button {
padding: 6px 10px;
padding-top: 1px; margin-top : 8px;
margin-right: 16px;
background: #ddd;
font-size: 17px;
border: none;
cursor: pointer;
margin-top: 8px;
}
#sticky-nav a:hover {
background-color: #ddd;
color: black;
}
#sticky-nav a.active {
background-color: #4CAF50;
color: white;
}
.container {
display: table;
width: 100%;
}
.box {
display: table-row;
}
.align-left {
width: 33%;
text-align: justify;
display: table-cell;
padding: 10px;
}
.align-center {
width: 33%;
text-align: justify;
display: table-cell;
padding: 10px;
}
.align-right {
width: 33%;
display: table-cell;
padding: 10px;
text-align: right;
}
EDIT: This is the layout I am trying to achieve,

I see that you're using display: table to achieve this effect. I highly recommend reading up more first before continuing with your work or project. Some layout concepts you have to know are grid and flex. In your case, we can use the flexbox concept to solve your problem.
flex basically is a method that can distribute space between items more easily. In your case, you can get what you're trying to achieve by using flex-grow and flex-basis. flex-basis defines how, initially, long/tall an item inside a flex container should be. flex-grow defines how an item inside a flex container can expand (grow) in width/height depending on the remaining space of the container.
In your case, we can simply set the flex container's width (your wrapping div) to 100%. To distribute space evenly between the items, we can set all the items' initial widths to 0. Then, distribute the remaining space of the container (which is still 100%) evenly using flex-grow to 1 for each flexbox item. However, this will make all the items similar in width. To make the center div wider, you can set the flex-grow to 2. This will make it so that the left div, center div, and right div have 25%, 50%, and 25% of the container's remaining space in width respectively. I really recommend reading further about flex to understand what I mean. After reading about flex in the above link, try visiting this and this to learn more about flex-basis and flex-grow.
Here's a working solution using flex. Again, I recommend reading more about flex so that you can use flex better.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: Helvetica;
}
body,
html {
width: 100%;
height: 100%;
}
#wrapper {
display: flex;
width: 100%;
}
#wrapper * {
padding: 30px;
text-align: center;
color: white;
}
.left-align,
.right-align {
flex-basis: 0;
flex-grow: 1;
}
.center-align {
flex-basis: 0;
flex-grow: 2;
}
.left-align {
background: #121212;
}
.center-align {
background: #232323;
}
.right-align {
background: #454545;
}
<div id="wrapper">
<div class="left-align">Some content</div>
<div class="center-align">Some content</div>
<div class="right-align">Some content</div>
</div>

I have created a simple example for your layout.
You can achieve it using flex box i.e
.box{
display: flex;
width:100%;
justify-content:space-between;
}
Here is the link: https://codesandbox.io/s/optimistic-euclid-xmv6h
Hope it answer your question.

Related

Anchor tag issue with width

I'm having some issues with styling the top bar for a website. I want all of the anchor tags to be equally distributed throughout the entirety of the screen. There are four anchor tags, so I thought that by making the width of each tag 25%, they would each take up a quarter of the block. They should theoretically all be in one line, but the very last one gets moved down. I have no clue what's causing this to happen and hope I'll be able to get some help. Thank you!
html code:
<div class="navbar">
Home
Lessons
Contact
Login
</div>
CSS code:
* {
margin-left:0;
margin-right:0;
margin-bottom:0;
margin-top:0;
}
html, body {
height:100%;
width:100%;
}
.navbar {
background-color: #555;
overflow: auto;
}
.navbar a {
float: left;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
width: 25%; /* Four equal-width links. If you have two links, use 50%, and 33.33% for three links, etc.. */
text-align: center; /* If you want the text to be centered */
}
It is the padding defined in .navbar a that causes the offset. You can add a box-sizing: border-box to the rule to avoid this effect (content will shrink instead of container expanding to fit the padding). Or, better i think, use flexbox :
.navbar {
background-color: #555;
overflow: auto;
display: flex
}
.navbar a {
flex: 1;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
text-align: center; /* If you want the text to be centered */
}
The display: flex will initialize flexbox on navbar container and the flex: 1 in anchor will tell browser to give equal width to all elements.
A quick fiddle to explain (case 1 & 2 can be easily fixed with box-sizing) :
.container {
width: 200px;
border: 1px solid #000;
}
.item-no-flex {
width: 25%;
padding: 3px;
border: 1px dashed #000;
float: left;
}
.item-ib {
width: 25%;
padding: 3px;
border: 1px dashed #000;
display: inline-block
}
.container-flex {
width: 200px;
display: flex;
border: 1px solid #000
}
.item-flex {
width: 25%;
padding: 3px;
flex: 1;
border: 1px dashed #000;
}
<p>Float</p>
<div class="container">
<div class="item-no-flex">1</div>
<div class="item-no-flex">2</div>
<div class="item-no-flex">3</div>
<div class="item-no-flex">4</div>
</div>
<p style="clear:both">Inline-block</p>
<div class="container">
<div class="item-ib">1</div>
<div class="item-ib">2</div>
<div class="item-ib">3</div>
<div class="item-ib">4</div>
</div>
<p style="clear:both">Flexbox</p>
<div class="container-flex">
<div class="item-flex">1</div>
<div class="item-flex">2</div>
<div class="item-flex">3</div>
<div class="item-flex">4</div>
</div>
Try simplifying your code and using flexbox.
HTML:
<div class="navbar">
Home
Lessons
Contact
Login
</div>
CSS:
.navbar {
display: flex;
justify-content: space-around;
}
This should distribute the links equally across the page with space around each link, regardless of screen width.

How do I style a chat box in CSS?

I have a chat box that looks like this:
My main issue is that all messages are a fixed width, when I want the width to auto-resize by the amount of text. The elements are all spans within the chat div, with a display of block (inline or inline-block will render side-by-side) and changed side margins to make messages 'stick' to one side. Here is my css
.psentMessage {
text-align: right !important;
background-color: #F0F0F0;
margin-right: 0 !important;
border-top-right-radius: 0 !important;
}
.pmessage {
text-align: left !important;
background-color: white;
margin-left: 0 !important;
border-top-left-radius: 0 !important;
}
.pmessage, .psentMessage {
display: block !important;
padding: 2vh 1vw;
max-width: 85%;
margin-top: 1vh;
margin-bottom: 1vh;
border-style: hidden;
border-radius: 10px;
}
How can I make the divs auto-resize but still have them in this layout?
This can be solved by a flexbox layout with flex-direction: column;.
When you add align-items: flex-start;, the width of each message is adapted to the content. Add align-self: flex-end; to the messages you want to align to the right side.
Simplified example: https://codepen.io/anon/pen/bMobqM
HTML:
<div>
<p class="other">Hello</p>
<p class="other">This is the project managment app</p>
<p>gwkki</p>
<p class="other">hello</p>
<p class="other">hi</p>
<p>Hello</p>
<p class="other">online</p>
</div>
CSS:
div {
background: #ccc;
padding: 20px;
width: 150px;
display: flex;
flex-direction:column;
align-items: flex-start;
}
p {
background: #fff;
padding: 10px;
}
.other {
align-self: flex-end;
background: #ddd;
}
More information about flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

How width in percentage is getting assigned to div?

I have main div and inside that i have 3 divs and each have individual width in percentage. So i got divs with 20%, 80% and 100% respectively.
Working with them so far i realised that if I have given then width of 200% all together then it divide browser width in two equal parts and first half is considered 100% and other one 100%. Then rest of percentage distribution happens. I am new to CSS but want to make this clear. Is my understanding correct and that's why my code is working correctly? Is there any disadvantage of doing so?
.main {
font-size: larger;
font-weight: bold;
}
.column1 {
position: relative;
line-height: 30px;
width: 20%;
text-align: left;
}
.column2 {
width: 80%;
}
.column3 {
position: relative;
width: 100%;
float: right;
text-align: right;
}
.clear-both {
clear: both;
}
.row {
border-width: 2px;
border-bottom-color: #f4f4f4;
border-style: none;
position: relative;
line-height: 30px;
border-bottom-style: solid;
margin-top: 0.2%;
font-weight: normal;
font-size: 12px;
display: flex;
}
<div class="row main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
if you use flexbox, it makes it clearer for you instead of using floats mixed with position,
body {
margin: 0
}
.main {
font-size: larger;
font-weight: bold;
display: flex;
flex-wrap: wrap;
}
.column1 {
flex: 0 20%;
background: red
}
.column2 {
flex: 0 80%;
background: green
}
.column3 {
flex: 0 100%;
background: yellow
}
<div class="main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
EDIT OP's comment
I want these 3 columns to be in one row. Working with my code too but
total width of main div is 200%. is there any possibility of breaking
of ui if i do it in this way
You can do this way:
body {
margin: 0
}
.main {
font-size: larger;
font-weight: bold;
display: flex
}
.column1,
.column3 {
flex: 0 20%;
background: yellow
}
.column2 {
flex: 1;
background: green
}
<div class="main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
You are doing wrong.
Keep following things in mind.
1- Parent/ main div should have appropriate width in percent or pixels. if you don't assign a width then it will take width automatically depending on contents of width.
2- If you are assigning width in percent to child divs (column1, column2 etc) then total of child divs should not exceed 100%. If it exceeds then you will not get desired/appropriate results.
3- You need to use floats or display:inline-block to place all child divs/elements in one row.
.main {
font-size: larger;
font-weight: bold;
width: 100%;
max-width: 1140px;
margin: 0 auto;
}
.column1 {
position: relative;
line-height: 30px;
width: 20%;
text-align: left;
float: left;
}
.column2 {
width: 60%;
float: left;
}
.column3 {
position: relative;
width: 20%;
text-align: right;
float: left;
}
<div class="row main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
If you are using padding or borders then you may need to use
box-sizing: border-box.
You can use bootstrap grid system.
Grid systems are used for creating page layouts through a series of
rows and columns that house your content.
So you can easily manage your layout depending on which device you are using :
.col-lg* classes refer to large sized devices.
.col-md* classes refer to medium sized devices .
.col-xs* classes refer to small sized devices.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-md-2 col-xs-2 bg-warning">CODE</div>
<div class="col-md-10 col-xs-10 bg-primary">NAME</div>
<div class="col-md-12 col-xs-12 bg-success">TOTAL</div>
</div>

Centering a Div in a Div (As well as sizing a div based on size of content)?

Okay, so, I honestly don't know what I'm doing wrong. So let me just show you:
This is what I'm trying to accomplish (made in PS):
http://puu.sh/ryXC9/7d82671ee0.png
What my results are so far:
http://puu.sh/ryXST/02c9722a53.png (Obviously not successful, and as a side note, the orange box is just a placeholder, I'll fill out the form later).
The problem I'm having is first: Trying to have the width of "social-content" to be just the width and height of the held contents. Of course "main-social" is just the width of screen and height of contents. If I can accomplish the width thing with "social-content" then I'll be able to center the div with "Margin: 0 auto" but alas, I cannot figure out my dilemma. This is my relavent markup( "Follow us" bar is excluded, as its irrelevant):
.fa-facebook {
color: white;
overflow: hidden;
}
.fa-twitter {
color: white;
padding: 0.2em;
}
#main-social {
height: 8em;
}
#social-content {
height: 100%;
width: 70%;
margin: 0 auto;
margin-top: 1.4em;
}
#facebook {
float: left;
display: inline-block;
}
#facebook a {
padding: 0.2em 0.4em 0.2em 0.4em;
background-color: #3b5998;
height: 100%;
text-decoration: none;
}
#facebook a:hover {
color: white;
}
#twitter {
float: left;
display: inline-block;
}
#twitter a {
background-color: #10bbe6;
text-decoration: none;
}
#twitter a:hover {
color: white;
}
#emailForm {
float:left;
display: inline-block;
width: 25em;
margin: 0 auto;
background-color: orange;
height: 7em;
}
<script src="https://use.fontawesome.com/d7993286b8.js"></script>
<div id="main-social">
<div id="social-content">
<div id="facebook">
</div>
<div id="emailForm">
</div>
<div id="twitter">
<a href="twitter.com" class="fa fa-twitter fa-5x" ></a>
</div>
</div>
</div>
Also, the problem also is, It needs to be responsive, the entire site is responsive, and since I'm still new to the responsive scene, I may have not taken the best approaches to it. (Tips not needed, but greatly appreciated :) )
To get #social-content's width to be that of its content, use display: inline-block without a width defined.
If #main-social is simply a container, you can use flexbo to center #social-content within it. Add the following to #main-social:
#main-social {
display: flex;
justify-content: center; // Centers horizontally
align-items: center; // If you need to center vertically, as well
}

Align and center two divs and two headers

I have 2 divs and 2 H4 headers which need to be in a line. Tried to align with text-align and float left but it doesn't work.
From my understanding, side by side alignment can be achieved by using float for the elements but it is not happening in my case. Also unable to center them. At present trying to use margin left with a 30% which I believe is not a proper solution. The images below shows how it looks currently and how I am trying to make it look.
HTML
<div class="k-legend-title">
<div class="k-stat-title-color-box" style="background-color: #3DA1ED;"></div>
<h4 class="">Driver 1</h4>
<div class="k-stat-title-color-box" style="background-color: #652D91;"></div>
<h4 class="">Driver 2</h4>
CSS
.k-legend-title{
color: #C3CF01;
}
.k-stat-title-color-box {
width: 10px;
height: 10px;
border-radius: 25px;
background: #ccc;
float: left;
margin-top: 6px;
margin-right:5px;
margin-left: 30%;
}
Current Layout
Trying to get this layout. Center and in 1 line
Make them inline-level, don't use floats. Then you can align them horizontally through text-align on their container, and align them vertically through vertical-align on themselves.
.k-legend-title {
color: #C3CF01;
text-align: center;
}
.k-stat-title-color-box, h4 {
display: inline-block;
vertical-align: middle;
margin: 5px 0;
}
.k-stat-title-color-box {
width: 10px;
height: 10px;
border-radius: 25px;
background: #ccc;
}
<div class="k-legend-title">
<div class="k-stat-title-color-box" style="background-color: #3DA1ED;"></div>
<h4>Driver 1</h4>
<div class="k-stat-title-color-box" style="background-color: #652D91;"></div>
<h4>Driver 2</h4>
</div>
By default h1-h6 elements has display: block, you should use display: inline-block in this situation.
h4{
display: inline-block;
}
h4:first-of-type{
margin-right: 15px;
}
JSfiddle here
Try using display:inline-block with parent using text-align:center:
.k-legend-title {
text-align:center;
color: #C3CF01;
}
.k-stat-title-color-box {
width: 10px;
height: 10px;
border-radius: 25px;
background: #ccc;
display:inline-block;
margin-top: 6px;
}
h4 {
display:inline-block;
}
.k-legend-title h4:first-of-type {
margin-right: 10px;
}
Fiddle: https://jsfiddle.net/kaarccq4/