How can I get my elements to align properly? - html

I need help with css in my angular app.
Here's my plunker:
https://plnkr.co/edit/5GvpLHcGq93yfpqHAJwy?p=preview
But here's the main code snippets as well:
import {Component} from '#angular/core'
#Component({
selector: 'app-child-component',
template: `<div class="header">
<div style="text-align:left;">
<div class="headerText"> Foo
<div style="float:right;">
<span class="headerText">Language</span>
<span>
<select style="margin-right:10px;margin-top:4px;">
<option *ngFor="let language of languages">{{language}}</option>
</select>
</span>
</div>
</div>
<div class="headerText"> Bar
<div style="text-align:-webkit-right; padding-right:10px;">
<span class="headerText">
user: admin
</span>
</div>
</div>
</div>
<div style="float:right;">
<div>
<button class="signOutButton">Sign Out</button>
</div>
</div>
</div>
`,
styles: [`.signOutButton {
background:#5dacb4 !important;
width:100px;
height:30px;
font-size:14px;
margin:5px;
float:right;
}
.header {
background:#3f3f3f;
}
.headerText {
color:#ffffff;
font-size:16px;
margin-bottom:0px;
margin-left:10px;
}`]
})
export class ChildComponent {}
The issue I'm having is I need user:admin to be on the same line as Bar
and the sign out button to align all the way right after the language and user and vertically aligned centered on the right.
How can I achieve that?

Something a bit like this?
.signOutButton {
background:#5dacb4 !important;
width:100px;
height:30px;
font-size:14px;
position:absolute;
right:10px;
top:50%;
transform:translateY(-50%);
}
.header {
background:#3f3f3f;
color:#ffffff;
font-size:16px;
position:relative;
padding:10px;
}
.left {
float:left;
}
.right {
float:right;
}
.line {
width:calc(100% - 110px);
}
.line::after { /* CLEARFIX */
content: "";
clear: both;
display: table;
}
<div class="header">
<div class="line">
<div class="left">
Foo
</div>
<div class="right">
<select>
<option>English</option>
</select>
</div>
</div>
<div class="line">
<div class="left">
Bar
</div>
<div class="right">
user:admin
</div>
</div>
<button class="signOutButton">Sign Out</button>
</div>
Explaination
Essentially, with a design like this, the approach that I took was to break it up into two parts: the two "Foo" / "Bar" lines, and the Sign Out button.
Each line needed to have something aligned to it's left, and something aligned to it's right. A normal line of text can't do this, but it is the perfect scenario in which to use float!
Unfortunately, floating elements don't take up any space, and since these lines are entirely comprised of floating elements, this means that they themselves don't take up any space. Thankfully, this problem was solved long ago, with a solution commonly known as the clearfix hack. This is what we use here.
For the Sign Out button, a little absolute positioning is all that we need to pop it in the right place (remember that absolute positioned elements inside elements positioned with relative or absolute will be positioned relative to that element, not the entire page). However, the lines will overlap with the button. Not good.
Therefore, we simply need to make the lines slightly shorter. How much shorter? I'd say 110px, as the button is 100px and it'll look nicer if we leave 10px of space between the end of the lines and the button, rather than having them pushed up against each other. We can use calc() to make these lines exactly 110px shorter, by simply having this value subtracted off of 100%.

Related

DIv inside my Bootstrap div will not right align (but text does)

I am trying to align two divs, each of which are inside Bootstrap column divs. I want to align the one in the first column to the right, and the one in the second column to the left. The divs inside are positioned relative, so that content inside them can align absolutely. But even removing that positioning didn't fix my issue. I can't figure it out, because it works on teh plain text i place inside the columns/divs, just as a test, but not on the divs.
Here is my html:
<div class="container">
<div class="row">
<div class="leftFeature col-md-6">TESTTEXT<br>
<div class="blockFeature">
[types field='square-feature-image' size='full'][/types]
<div class="blockFeatureOverlay"></div>
<div class="blockFeatureText">
<h2>[types field='front-page-feature-tagline'][/types]</h2>
<h3>[types field='tag-line'][/types]</h3>
</div>
</div>
</div>
<div class="rightFeature col-md-6">TESTTEXT<br>
<div class="blockFeature">
[types field='square-feature-image' size='full'][/types]
<div class="blockFeatureOverlay"></div>
<div class="blockFeatureText">
<h2>[types field='front-page-feature-tagline'][/types]</h2>
<h3>[types field='tag-line'][/types]</h3>
</div>
</div>
</div>
</div>
</div>
Here is my css:
.leftFeature {
background-color:#F9D069;
text-align:center!important;
}
.rightFeature {
background-color:#B6DEFF;
text-align:center!important;
}
.col-sm-6 {padding:0;}
.blockFeature {
width:80%;
background-color:#7F9FA1;
background-size:100%;
line-height:0;
position:relative; /*removing this rule did not fix my issue */
}
.blockFeatureOverlay {
position:absolute;
width:100%;
height:100%;
top:0;
left:0;
background-color:rgba(0, 0, 0, 0.25);
/*background-color:rgb(158,84,6, 0.25);*/
}
.blockFeature a {
text-decoration:none;
}
.blockFeature img {
width:100%;
height:auto;
}
.blockFeatureText {
position:absolute;
bottom:20px;
left:20px;
}
.blockFeatureText h2 {
font-size:2.4em;
font-weight:700;
color:#fff;
text-transform:uppercase;
margin-bottom:10px!important;
line-height:100%!important;
text-align:left;
}
.blockFeatureText h3 {
font-size:1.2em;
font-weight:300;
color:#fff;
text-transform:uppercase;
margin-bottom:0!important;
line-height:100%!important;
text-align:left;
}
And this is a representation of what i am getting: http://imgur.com/a/LekqV
but this is what i want: http://imgur.com/a/ER1kB
It's probably (hopefully) something really obvious. But after 24 hours of messing around, I cannot seem to fix it. I have tried adding text-right or pull-right class on the left hand div, but it either does something different than I need, or doesn't work at all.
thank you for anyone who can maybe help me out?
EDIT: Here are two fiddles i created
forked from a fiddle where someone seemed to have already created the bootstrap environment: https://jsfiddle.net/Katrina_B/w4dt8qq7/
one with no bootstrap, in case the above is not correct, and someone knows better how to add bootstrap to it: https://jsfiddle.net/Katrina_B/c4kxa06s/
I am completely new to Bootstrap, and rarely use Jfiddle, so apologies if i have done this incorrectly. In any case, in neither example, are the divs doing what i expect or wish them to do.
simple you should use bootstrap pull-left and pull-right class to align your divs
<div class="leftFeature col-md-6 pull-left">TESTTEXT<br> .... </div>
<div class="rightFeature col-md-6 pull-right">TESTTEXT<br>....</div>
.leftFeature .blockFeature {
float: right;
}
This style may keep your complete div in right
Below the div with the class col-md-6 you can use bootstrap again. Try adding a div with class="col-md-10 col-md-offset-2" for the left side, and on the right side class="col-md-10"
<div class="container">
<div class="row">
<div class="leftFeature col-md-6">TESTTEXT<br>
<div class="col-md-10 col-md-offset-2>
<div class="blockFeature">
[types field='square-feature-image' size='full'][/types]
<div class="blockFeatureOverlay"></div>
<div class="blockFeatureText">
<h2>[types field='front-page-feature-tagline'][/types]</h2>
<h3>[types field='tag-line'][/types]</h3>
</div>
</div>
</div>
</div>
<div class="rightFeature col-md-6">TESTTEXT<br>
<div class="col-md-10">
<div class="blockFeature">
[types field='square-feature-image' size='full'][/types]
<div class="blockFeatureOverlay"></div>
<div class="blockFeatureText">
<h2>[types field='front-page-feature-tagline'][/types]</h2>
<h3>[types field='tag-line'][/types]</h3>
</div>
</div>
</div>
</div>
</div>
</div>
You should use bootsrap predefined pull-left and pull-right class to positioning div left and right. This could work for you.
Or you can use css float: left and float: right property.
Example:
.leftFeature {float: left;} and .rightFeature {float: right}

Html <div class="row"> not centering

First I'd like to say that I know very little about coding.
In this website I made http://academiadae.com, I added two small divs at each side, so I could get a div class="6u" centered.
<div class="row">
<div class="3u"></div>
<div class="6u"><img src="images/logo.png" /></div>
<div class="3u"></div>
</div>
Can you help me to get it centered without the need for the other divs?
I tried making different elements =center in the CSS, but it didn't work.
Thanks.
First of all, your are using as class 6u which will not be selected. A CSS name must begin with an underscore (_), a hyphen (-), or a letter(a–z) to use it as an CSS selector. You can check this page for any reference.
Second if you want to have the a single div centered you could apply this:
<div class="row">
<div class="item6u">
test
</div>
</div>
Where there is only one div with a class name that starts with a letter.
For you CSS you need to set the width of the div and like #Sprazer told you need to set the margin:
.row{
background-color:yellow;
}
.item6u{
background-color:red;
width:50%; //changed to 50% percentage as wawa suggested
margin:0 auto;
text-align:center;
}
See code here: JSFIDDLE.
So, you currently have something like: HTML
<div class="row">
<div class="3u">
</div>
<div class="6u">
<img src="images/logo.png">
</div>
<div class="3u">
</div>
</div>
and CSS:
div.6u{
width: 50%;
clear: none;
float:left;
margin-left: 0;
}
You need to change this to HTML:
<div class="row>
<div class="6u">
...contents of the div here...
</div>
</div>
and CSS (note: do remove float:left, otherwise it will not work):
div.6u{
width:50%;
clear:none;
margin-left:auto;
margin-right:auto;
}

z-index wont work with positions set

I have two divs. One is a bar with background transparency - this contains a menu. The other contains an image. I want the image to be behind the first div. I can't get the z-index to do this:
<div id="headerwrapper">
<div class="mastheadimage"><img src="....." /></div>
...more code
</div>
css
#headerwrapper {
position:relative;
z-index:10;
background-color:rgba(59, 59, 59, 0.3);
}
.mastheadimage {
position:absolute;
z-index:-1;
}
I have tried various different numbers for the -1
That's because mastheadimage is nested within headerwrapper.
You need to split them up to achieve what you are looking for, perhaps wrapping the two in an overall parent.
<div class="wrapper">
<div id="headerwrapper">...more code</div>
<div class="mastheadimage"><img src="....." /></div>
</div>
CSS:
.wrapper {
position:relative;
}

what is the use of this div tag with only clear rule [duplicate]

This question already has answers here:
What is the use of style="clear:both"?
(3 answers)
Closed 8 years ago.
I was noticing that <div style="clear:both;"></div> had been frequently used in a website between div areas. Given the fact that no other rules such as width and height has been specified for this, what is the effect of this type of usage? an example of the site code follows below
<div id="content">
<div id="middle-cont"></div>
<div id="bot-r">
<div style="clear:both;"></div>
<div class="hwd-module latest-audio"></div>
<div style="clear:both;"></div>
</div>
</div>
<style>
#middle-cont {
padding: 18px 0px;
margin-top: 20px;
margin-right: -40px;
margin-left: -40px;
}
#bot-r, #bot-c, #bot-l {
width: 32%;
height: auto;
float: right;
padding: 5px;
}
</style>
Clear:both
is used to clear any (or for that matter, all preceding) floats.
It basically means "No floating elements allowed on either the left or the right side".
Let us try to understand this with a demonstration :
You can see a couple of examples below:
No clear -> http://jsfiddle.net/0xthns3k/
The html and css are as follows :
HTML :
<div class="left">
</div>
<div class="right">
</div>
<!-- No clear -->
<div class="Green"></div>
CSS :
div {
display:inline-block;
width: 150px;
height:150px;
}
.left {
background-color:Orange;
float:left;
}
.right {
background-color:Red;
float:right;
}
.Green {
background-color:Green;
}
.yellow {
background-color:yellow;
width:30px;
}
This is the image of the generated HTML.
If you see here, the green colored box is placed somewhat in the center of the two floated elements. Well, actually since there are floated elements the new "non-floated" element is actually placed adjacent to the leftmost floated element. Hence, you see the green colored element just adjacent to the leftmost floated element.
Now, if you were to have another element(s) floated left, this would automatically fit between the Orange and the Green elements.
See this below :
http://jsfiddle.net/0xthns3k/1/
Also, the position of this 'new' left floated element wouldn't be that important too with respect to the said HTML.
Placed below green element
<div class="left">
</div>
<div class="right">
</div>
<!-- No clear -->
<div class="Green"></div>
<div class="left yellow">
</div>
Placed after right floated element.
<div class="left">
</div>
<div class="right">
</div>
<div class="left yellow">
</div>
<!-- No clear -->
<div class="Green"></div>
Placed after left floated element
<div class="left">
</div>
<div class="left yellow">
</div>
<div class="right">
</div>
<!-- No clear -->
<div class="Green"></div>
All the above HTML code would generated the same HTML as shown in the image above.
With clear -> http://jsfiddle.net/bk3p160d/
The HTML is only slightly modified here :
HTML
<div class="left">
</div>
<div class="right">
</div>
<div class="clearAll"></div>
<div class="Green"></div>
and one additional CSS class :
CSS
.clearAll {
clear:both;
}
If you see here, the green colored element is positioned below the line containing the aforementioned floats. This is because "clear: both" tells the HTML rendering engine
"No floating elements allowed on either the left or the right side". Hence, it cannot place this element on the same line as it would violate the defination. This causes the engine to place it on a new line. On the line the preceding float properties are essentially nullified. Hence, clear:both is used to effectively clear any preceding floats.
See here for further information : http://www.w3schools.com/cssref/pr_class_clear.asp
Hope this helps!!!

Create thee divs in one line and 4th div in bottom. CSS/HTML

I want to create html page the next vision. Location of divs 1,2 and 3 in one line was done, but with 4th div I have some troubles and can't make it.
You really should post your code to see whats wrong with it.. But i made the example for you.
Here you go, you could use float.
Html Code:
<div id="wrapper">
<div id="first">
</div>
<div id="second">
</div>
<div id="third">
</div>
<div id="fourth">
</div>
</div>
Css Code:
#wrapper{width:300px; margin:0;}
#first { height:300px; width:100px; background:black; float:left;}
#second{ height:250px; width:100px; background:red;float:left;}
#third{ height:250px; width:100px; background:green;float:left;}
#fourth{ height:50px; width:200px; background:blue;float:left;}
Working DEMO
Here's an example that uses non-fixed heights and widths. The key is wrapping the subsections in divs as well and styling accordingly. div is short for division after all.
<div class="left">
1
</div>
<div class="right">
<div class="second-third-wrapper">
<div class="second">
2
</div>
<div class="third">
3
</div>
</div>
<div class="fourth">
4
</div>
</div>
http://jsfiddle.net/Pb5NX/2/
The divs then use percentage height and widths to size them properly. These percentages take up a percentage of the parent element (the <body>, which then inherits from the <html>), so the parents height needs to be set as well.
body, html {
width: 100%;
height: 100%;
}
.left {
background-color: blue;
width: 50%;
height: 100%;
float: left;
}
If you want them a fixed size, you can just set a specific height and width style on the specific elements and the percentages will do the rest.