We have outer div header which has 100% width of its container. The div is for heading text and button on right. The text have to be centered to the header and the button has to be on right. But if the text is long enough to overlap with button, then the text should fill left part of header and do not go on button.
header -> [ [CENTER TEXT] [button]]
OR (with wider CENTER TEXT)
header -> [[abcdabcdab CENTER TEXT cd][button]]
Tried with position: relative on header->text and position: absolute; left: 100%; on button when placing it as a child of center text. But this isn't exactly what is should look like. Any good way to do it with pure CSS?
Button should be placed with little margin to right border of header, rather not with margin to center text.
You can use this simple trick:
Use a span positioned inline after the text itself. Once the text is longer than the size of the header, the span drops a to the next line and the text can take all the space.
Notice that you need to set a fixed width to the button.
div.container {
border: 1px solid red;
height: 50px;
width: 100%;
position: relative;
}
h1 {
background: gray;
width: calc(100% - 100px);
float: left;
height: 50px;
line-height: 50px;
text-align: center;
margin: 0;
direction: rtl;
}
h1 span.text {
color: black;
background: lightyellow;
direction: ltr;
}
h1 span.padding-holder {
width: 100px;
display: inline-block;
position: relative;
}
button {
float: right;
width: 100px;
height: 50px;
}
<div class="container">
<h1>
<span class="text">Short Header</span>
<span class="padding-holder"></span>
</h1>
<button>Click Me</button>
</div>
<div class="container">
<h1>
<span class="text">Now a Medium Header</span>
<span class="padding-holder"></span>
</h1>
<button>Click Me</button>
</div>
<div class="container">
<h1>
<span class="text">And This is a Very Long Header</span>
<span class="padding-holder"></span>
</h1>
<button>Click Me</button>
</div>
Related
I want to horizontal align a div that consists of two other divs:
One from the left, that will contain an image.
Other from the right, that div will contain text, aligned to the left.
The alignment will be relative to a container, and the centered div should expand to a maximum width (and not take the entire container's width).
This pen describes what i tried to do using table layout
This is the HTML
<div class="container">
<div class="centered">
<div class="left">
left text
</div>
<div class="right">
very very very long right text
</div>
</div>
</diV>
And the CSS
.container {
width: 200px;
background-color: red;
}
.centered {
display: table;
margin: 0 auto;
max-width: 100px;
}
.left {
background-color: green;
display: table-cell;
vertical-align: middle;
}
.right {
background-color: blue;
display: table-cell;
vertical-align: middle;
}
As you can see, the space on the right of the blue area is part of the centered div (the green+blue area), but it makes the div's content not to be centered. What i would like is the blue area to take the width of the longest line in it
If you're wanting the blue area to dynamically grow with the text inside of it while retaining a centered area of content would the following be what you're looking for?
.container {
width: 200px;
background-color: red;
padding:0 20px;
}
.centered {
display: table;
margin: 0 auto;
max-width: 100px;
}
.left {
background-color: green;
display: table-cell;
vertical-align: middle;
color:#fff;
}
.right {
background-color: blue;
display: block;
vertical-align: middle;
width:inherit;
color:#fff;
}
<div class="container">
<div class="centered">
<div class="left">
left text
</div>
<div class="right">
very very very long right text very very very long right textvery very very long right textvery very very long right textvery very very long right textvery very very long right text
</div>
</div>
</div>
I have a grid of white divs with a green div behind
(like this).
I am trying to put text in each div, but for some reason, when I give a div text, that div is shifted down (like this). What css property/ HTML technique can I use to make the divs stay in place while still holding text?
The HTML consists of a div with this CSS:
#main {
top: 50px;
height: 280px;
width: 100%;
background-color: green;
position: fixed;
}
which contains divs with this CSS:
.small {
position: relative;
background-color: white;
border: 4px solid black;
width: 5%;
height: 80px;
display: inline-block;
margin-top: 2px;
}
and looks like this:
<div id="main">
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<!-- etc -->
</div>
They are aligned at their baseline. If there is text, that's the baseline of the text (i.e. of the last line of text), if not, it's near the bottom of the container.
Add
.small {
vertical-align: top;
}
to change this.
Here's a codepen: http://codepen.io/anon/pen/oxVajG
Here is fiddle https://jsfiddle.net/qyLjxwrv/1/.
As you can see left and right titles are aligned to screen corners, while I would like to align them to image corners. Trying it for 2hours without a success...
.wrapper {
position: absolute;
left: 0px;
right: 0px;
top: 0px;
background-color: rgba(213,213,213,.5);
text-align: center;
}
.info {
text-align: left;
}
.right-title {
float: right;
}
<div class="wrapper">
<div class="inner">
<div class="info">
<span class="left-title">Left</span>
<span class="right-title">Right</span>
</div>
<img src="http://i.imgur.com/CRcoPPS.jpg" />
</div>
</div>
Note that I don't want titles to appear inside image, but above it and aligned with left and right corner of image.
It seems that you can do it like this simply, and see the comments inline.
Updated Demo
.wrapper {
display: table; /*shrink-to-fit*/
margin: 0 auto; /*center*/
}
.wrapper img {
max-width: 100%; /*responsive*/
}
.left-title {
float: left; /*left*/
}
.right-title {
float: right; /*right*/
}
<div class="wrapper">
<div class="inner">
<div class="info">
<span class="left-title">Left</span>
<span class="right-title">Right</span>
</div>
<img src="http://i.imgur.com/CRcoPPS.jpg" />
</div>
</div>
Edit: Demos with the gray background color partly and full page.
I'd like to know how to get the following result:
Green is a container div 700 pixels wide. Blue is a title area which fills the green container width-wise with some title text centred in the middle. Red needs to float on the right without affecting the flow of the text in the title.
How do I achieve this? I've tried floating the red box on the right but it seems to push the text in the title to the left for some reason.
Edit - For the record, I hadn't posted an example because HTML and CSS isn't really my area of expertise and I'm struggling to get back to an example where the text didn't align (having tried half a dozen different methods I've been reading).
Here's roughly what I was trying: http://jsfiddle.net/3fgytw0u/
<!DOCTYPE html>
<head>
<title></title>
<style>
#Container {
width: 700px ;
margin-left: auto ;
margin-right: auto ;
}
#Title {
background-color: red;
text-align: center;
}
#GameGuidelines{
align:right;
width: 200px;
background-color: grey;
}
</style>
</head>
<body>
<div id="Container">
<div id="Title">
<h1>This</h1>
<h2>Is</h2>
<h2>A</h2>
<h2>Long</h2>
<h2>Title Title Title Title</h2>
</div>
<div id="GameGuidelines">
<ul>
<li>Some</li>
<li>Info</li>
<li>Here</li>
</ul>
</div>
<div id="Introduction">
<p>Rest of the page continues here</p>
</div>
</div>
</body>
</html>
Move the element up into the header, set it to position:absolute and give it a margin-left:500px;
http://jsfiddle.net/3fgytw0u/2/ <-- that one is right
Maybe it would help you: Link
#Container {
width: 700px ;
margin-left: auto ;
margin-right: auto ;
position: relative;
}
#Title {
background-color: red;
text-align: center;
}
#GameGuidelines{
text-align:right;
position: absolute;
right: 0;
top: 0;
width: 200px;
background-color: grey;
}
Alternative approach to positioning can be using negative margin-left equal to width of red area:
h2 {
padding: 10px;
background: #EEE;
text-align: center;
overflow: hidden;
}
h2 .right-block {
width: 50px;
height: 50px;
background: coral;
float: right;
margin-left: -50px;
}
<h2>
Some centered text
<div class="right-block"></div>
</h2>
Here is what you want.
<html>
<head></head>
<body>
<div style="border:green 1px solid;height:700px;" >
<div style="border:blue 1px solid;width:100%;height:200px;text-align:center">
Some Title Text
<div style="float:right;border:1px red solid;width:100px;height:100px;margin-right:5px;position:relative;">
Some text
</div>
</div>
</div>
</body>
</html>
Red div will float on the right inside the blue one.
That could be simply done by positioning the inner div as position: absolute and putting it right: 0px, but because you need to prevent that it does not start to be positioned with the main display instead, you put position: relative to the outer div too. Also make sure that while writing you put the red div first and then the div which has purple text, or you can just add top: 0px so that you don't care about that anymore.
Then it should work!
Here's a fiddle: http://jsfiddle.net/xg6rove7/
But be wary of the fact that any text in the red box can then overlap the text in the purple, as I have tried to show you in the fiddle. You might b better with using a padding for both sides equal to the box's width, or just use your plain old float: right
i'm attempting to create an header which is divided into 3 divs
they will all be set to display: inline-block
the left header part will contain a slogan and a logo which i wan't the slogan to be at
the right of the logo
the problem is my logo div and my slogan div are always placed one on top of the other .
to my understanding an inline element would be placed next to the last inline element
with in the same block , notice that the header-left div has 250px width and each of the
child div's have 100px width so why are they not placed one next to the other ?
my markup :
<div id="header">
<div id="header-left">
<div id="logo" />
<div id="slogan">
<span> Buy For U</span>
</div>
</div>
</div>
my css :
div#header
{
border: 1px solid black ;
height: 200px;
}
div#header div#header-left
{
display: inline-block;
height: 100%;
width: 250px;
}
div#header div#header-left div#logo
{
display: inline-block;
background: url("Google-Desktop-64.png") no-repeat center;
background-size: 25%;
height: inherit;
width: 100px;
}
div#header div#header-left div#slogan
{
display: inline-block;
height: inherit;
width:100px;
}
everything's fine. just close the logo's <div> properly. "self-closing" tags.
<div id="header">
<div id="header-left">
<div id="logo"></div>
<div id="slogan">
<span> Buy For U</span>
</div>
</div>
</div>
i also suggest using an <img> for the logo (and make it a link to homepage) rather than just a background. empty <div> tags are prone to errors during validation.
It is stange that your #header has a width of 200 pixels and the child #header-left 250 pixels, but apart from that I think it's better to use a float. This means that the two divs are next to each other:
div#header div#header-left div#logo
{
float: left;
background: url("Google-Desktop-64.png") no-repeat center;
background-size: 25%;
height: inherit;
width: 100px;
}
div#header div#header-left div#slogan
{
float: left;
height: inherit;
width:100px;
}
And you nead a clear in your html/css:
.clear_left { clear: left; }
And the html:
<div id="header">
<div id="header-left">
<div id="logo" />
<div id="slogan"><span> Buy For U</span></div>
<div class="clear_left"></div>
</div>
</div>