I am trying to make a fancy header. Here is the JSFiddle for you to see how it looks like before I explain my issue.
This header is great in medium/big screen width, but the problem comes at smaller screen width, where the text does not fit in that width and gets overlapped by the right div.
Here is the html/css(same as JSFIddle). Below I will explain what I want to achieve
html:
<div class="header">
<div class="left"></div>
<div class="text">
<span>TITLE</span>
</div>
<div class="right"></div>
</div>
css:
div.header{
height:30px;
font-size:30px;
width:100%;
margin-top:100px;
display: block;
margin-bottom:30px;
}
div.header .left{
background-color: #D82B38;
width:10%;
height:30px;
display: inline-block;
float:left;
border-radius:0 15px 0 0;
}
div.header .text{
width:20%;
display: inline-block;
height:30px;
float:left;
}
div.header .right{
background-color: #D82B38;
width:70%;
height:30px;
display: inline-block;
float:right;
border-radius: 0 0 0 15px;
}
div.header .text span{
font-size:30px;
color: #D82B38;
position: relative;
top:-5px;
text-align:center;
display: block;
font-weight:500;
letter-spacing:1px;
}
I would like to give some left and right paddings to div.text, and make it's width depend on the size of the text, and then div.left and div.right takes their width depending on the remaining width of the screen(always being the left one a lot smaller than the right one), instead of always having the same percentage. I know I could play with media queries to change that percentage in smaller screens, but I know media queries does not work well in internet explorer and I prefer to avoid them.
I tried several things using CSS to get this, but I can't get it to work. Please tell me if my question is quite unclear and I will try to clarify what I mean.
Thanks in advance!
EIDT: I do not want to use javascript for this, I know there must be some way to achieve it using purely CSS
You can use Flexbox
CSS
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
align-items: center;
justify-content: center;
}
.flex-item {
-webkit-flex: 1 auto;
flex: 1 auto;
vertical-align: middle;
}
.flex-item-fixed {
-webkit-flex: 0 70px;
flex: 0 70px;
text-align: center;
}
.flex-item-fixed1 {
-webkit-flex: 0 10%;
flex: 0 10%;
}
.left {
background-color: #D82B38;
display: block;
border-radius: 0 15px 0 0;
height: 30px;
}
.right {
background-color: #D82B38;
height: 30px;
display: inline-block;
float: right;
border-radius: 0 0 0 15px;
}
DEMO HERE
Flexible widths (percentages) are the key to responsive web design, but in some cases, this is not the way to go. In this case in particular, you want to keep the right side of the header responsive meanwhile the left side, including the text, is static. That avoids any flickering and movement, as well as excess #media queries. You can keep the responsiveness by employing the calc function.
Here is what I did. I gave the left side and the text a 350px width shared between the two. (100px and 250px). 350px is well within the width of any smartphone. And I told the right side to make it 100% - 350px. That makes the right side respect the left side and adjust accordingly.
SCSS/LESS - If you were using LESS or SCSS/SASS you will have a real winner here, because you can store these two fixed widths in variables and then if you ever needed to change these values, LESS or SCSS/SASS would adjust everything for you.
Here is your new CSS:
div.header{
height:30px;
font-size:30px;
width:100%;
margin-top:100px;
display: block;
margin-bottom:30px;
}
div.header .left{
background-color: #D82B38;
width:150px;
height:30px;
display: inline-block;
float:left;
border-radius:0 15px 0 0;
}
div.header .text{
width:200px;
display: inline-block;
height:30px;
float:left;
}
div.header .right{
background-color: #D82B38;
width: calc(100% - 350px);
height:30px;
display: inline-block;
float:right;
border-radius: 0 0 0 15px;
}
div.header .text span{
font-size:30px;
color: #D82B38;
position: relative;
top:-5px;
text-align:center;
display: block;
font-weight:500;
letter-spacing:1px;
}
And here is how it looks like in a DEMO
EDIT
As an alternative option, you can distribute the widths and use media queries to re-adjust. This is something you will be able to use in older browsers. To center the text accordingly, you have 2 options, 1. is depicted in my DEMO after I edited it. using position left and transform. And if that gives you problems with older browsers, option 2 is to wrap the text is another div and center align its content. See the DEMO again for the alternative option
This one works from IE8 but uses flex if it's supported by the browser.
div.header{
display: table;
height:30px;
font-size:30px;
width:100%;
margin-top:100px;
margin-bottom:30px;
}
div.header .left{
display: table-cell;
background-color: #D82B38;
width:10%;
height:30px;
border-radius:0 15px 0 0;
}
div.header .text{
display: table-cell;
width:20%;
height:30px;
}
div.header .right{
display: table-cell;
background-color: #D82B38;
width:70%;
height:30px;
border-radius: 0 0 0 15px;
}
div.header .text span{
font-size:30px;
color: #D82B38;
position: relative;
top:-5px;
text-align:center;
display: block;
font-weight:500;
letter-spacing:1px;
}
#supports (display: flex) {
div.header{
display: flex;
}
div.header .left{
display: block;
}
div.header .text{
display: block;
min-width: 90px;
}
div.header .right{
display: block;
}
}
<div class="header">
<div class="left"></div>
<div class="text">
<span>TITLE</span>
</div>
<div class="right"></div>
</div>
Related
I have a span which has to aligned bottom and left-most to the content of it's container. Span text-node and container text-node font-size may differ.
Whatever it's span should always align to its container text-node bottom and to left-most. I tried using float left to the span node. It aligns to the left most but not to it's bottom. Removing float to the span, Aligns bottom but not left most. Sorry if I have not explained you better.
Refer the image attached for more clarification
Also here is the code which I tried:
.flexCtn{
display:flex;
align-items:center;
justify-content:flex-end;
height:50px;
width:300px;
border:1px solid #dfdfdf;
background:#fff;
}
.w100{
font-size:30px;
width:100%;
text-align:right
}
span{
font-size:14px;
float:left;
display:inline-block;
}
<div class="flexCtn">
<div class="w100">
<span>check</span>
the alignment
</div>
</div>
P.S I don't want any modification to the DOM. I have specific reason for this DOM structure which is going ti be vague if i'm going to explain you guys. Also don't want absolute position to be applied for the span. Thanks in advance
You have a flexbox container - so why not make w100 also a flexbox and align vertically using align-items: center - see demo below:
.flexCtn {
display: flex;
align-items: center;
justify-content: flex-end;
height: 50px;
width: 300px;
border: 1px solid #dfdfdf;
background: #fff;
}
.w100 {
font-size: 30px;
width: 100%;
text-align: right;
display: flex;
justify-content: space-between;
align-items: center;
}
span {
font-size: 14px;
float: left;
display: inline-block;
}
<div class="flexCtn">
<div class="w100">
<span>check</span>
the alignment
</div>
</div>
.flexCtn{
display:flex;
align-items:center;
justify-content:flex-end;
height:50px;
width:300px;
border:1px solid #dfdfdf;
background:#fff;
}
.w100{
font-size:30px;
width:100%;
text-align:right
}
span{
font-size:14px;
float:left;
display:inline-block;
padding : 16px 0 0 0; /* specify top position */
}
<div class="flexCtn">
<div class="w100">
<span>check</span>
the alignment
</div>
</div>
I have used flex property
https://plnkr.co/edit/flxsEcpSBe8sr2w3wPFc?p=preview
div{
font-size:14px;
display:flex;
align-items:flex-end;
flex:1;
}
I want divs to go from left to right but also to be evenly distributed in the content of the page with width: 100%;
Can this be done in CSS without using any JS or Display:Flex which actually allows you to do it with flex-direction .... but its not compatible with IE8 and IE9!
#container {
width: 100%;
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
}
#container:after {
content: '';
width: 100%;
display: inline-block;
font-size: 0;
line-height: 0
}
div {
width: 27%;
display: inline-block;
zoom: 1;
position: relative;
background-color:lightblue;
text-align:center;
color:red;
height:100px;
border:1px solid black;
margin: 10px;
}
p {
maring:0;
padding:0;
line-height:80px
}
<div id="container">
<div><p>A</p></div>
<div><p>B</p></div>
<div><p>C</p></div>
<div><p>D</p></div>
<div><p>E</p></div>
<div><p>F</p></div>
<div><p>G</p></div>
<div><p>H</p></div>
</div>
Here is my code.
So basically ... The first div to be always on the left, the 3rd to be always on the right and the one in the middle. But everytime you add a new div it should be added from left to right. Not left > right > middle.
Already have the correct answer from #Sofiene DJEBALI.
Add float:left; to your div in your css :
div {
width: 30%;
display: inline-block;
zoom: 1;
position: relative;
background-color:lightblue;
text-align:center;
color:red;
height:100px;
border:1px solid black;
margin: 10px;
float:left;
}
https://jsfiddle.net/bvgn46hu/28/
I have a post header div with height of 70px/4.375em
In it is a category h2 and a p timestamp
I want it to be aligned as demonstrated in this picture
(made in Photoshop)
Here is my HTML:
<div class="post-header" style="background-color:#9377d8">
<h2 class="category">Technology</h2>
<p class="timestamp">Saturday, today</p>
</div>
And my CSS:
.post-header{
height:4.375em;
width:25em;
margin-bottom:0;
float:left;
}
.category{
color:white;
display:inline;
float:left;
margin:0.625em;
}
.timestamp{
color:white;
display:inline;
float:right;
margin:0.625em;
}
Please help me with the CSS to get the design that I want
You can change your CSS as follows:
.post-header {
height:4.375em;
width:25em;
margin-bottom:0;
display:block;
}
.category {
color:white;
display:inline-block;
width:45%;
margin:0.625em;
}
.timestamp {
color:white;
display:inline-block;
text-align:right;
margin:0.625em;
width:40%;
}
This way, you get better control over your layout since you can specify both the width and the vertical align of the element. As we're at it, I'd use percentages for margins, but of course it goes on you
Visit fiddle to see it in action
You could try the following. Instead of floats, use inline-blocks with text-align: justify.
This only works if there at least two lines of text, so generate an extra blank line with the pseudo-element .post-header:after.
.post-header {
height: 4.375em;
width: 25em;
margin-bottom: 0;
float: left;
text-align: justify;
}
.category {
color: white;
margin: 0.625em;
display: inline-block;
}
.timestamp {
color: white;
margin: 0.625em;
display: inline-block;
text-align: right;
}
.post-header:after {
content: "";
display: inline-block;
width: 100%;
}
<div class="post-header" style="background-color:#9377d8">
<h2 class="category">Technology</h2>
<p class="timestamp">Saturday, today</p>
</div>
I am trying to do a vertical align for my texts. I also want to make sure the green background div need to cover from top to bottom inside the red color div. Currently the green color div only covers 90% of the red oolor div. I am not sure what happened in my case. Can anyone explain and help me out?
html
<div id='wrapper'>
<div class='head'></div>
<h2 class='title'>Warm-Up</h2>
</div>
css
.title{
display: inline;
padding-left: 15px;
vertical-align: middle;
margin: 0;
}
.head{
width: 30px;
height: 50px;
display: inline-block;
background-color: #A9D075;
}
#wrapper{
width:200px;
background-color: red;
}
http://jsfiddle.net/rmS2f/3/
Thanks.
Demo http://jsfiddle.net/rmS2f/6/
Your html structure will work but you need to change the styles:
.title {
display: inline-block;
padding-left: 45px;
vertical-align: middle;
margin: 0;
line-height:50px;
}
.head {
position:absolute;
left:0;
width: 30px;
height: 100%;
display: inline-block;
background-color: #A9D075;
}
#wrapper {
position:relative;
width:200px;
height:50px;
background-color: red;
}
I have this CSS Code:
html,body {
font-family:Arial;
font-weight:bold;
}
.container {
text-align:center;
}
.box {
width:475px;
display: inline-block;
margin:10px 20px 0 auto;
padding:12px;
border:1px solid black;
min-height:60px;
text-align: center;
float: left;
height: 200px;
}
.box h2 {
font-size:44px;
margin-top:4px;
margin-bottom:0;
}
.box p {
font-size:60px;
border:1px solid black;
margin-bottom:0;
}
but the divs with the two lines of text in make the values/numbers display lower than all the other divs with only one line of text.
how can i make all the values/numbers display in the same place inside the the .box divs?
Here is a fiddle with the full code: http://jsfiddle.net/8kex9/
With your code, you could do something like this: JS Fiddle
Giving them both absolute positions, will cause them to be positioned based on parent, rather than the sibling. You just can't add too many lines of text, or else it will run into each other.
.box h2 {
font-size:44px;
margin-top:4px;
margin-bottom:0;
position: absolute;
width: 465px;
}
.box p {
position: absolute;
font-size:60px;
border:1px solid black;
margin-top: 120px;
width: 465px;
}