CSS- Centering child text wider than parent container - html

Here's a JSfiddle of what I have so far.
What I want to do is make it so that the text "Text With a Border" is centered inside of the parent div (I want to keep the double lined borders on either side of this text, so that the end result will be something like this: ==========Text With a Border==========. With the borders hanging off either side of the parent div.)
I would like to make it so that this solution works with containers of varying sizes with differing amounts of text.
I've found a few questions similar to this, about centering wide children inside of narrower parents, but for some reason I can't get them to work with this text.
CSS:
#container {
width:400px;
background:pink;
margin:0 auto;
padding: 10px 0;
}
.border {
display:block;
background:yellow;
}
.border span {
left:0;right:0;
white-space:nowrap;
margin: auto;
}
p.border span:after, p.border span:before {
content: " ";
display: inline-block;
width:100%;
height:5px;
border:solid gray;
border-width:1px 0;
}
HTML:
<div id="container">
<p class="border">
<span>Text With a Border</span>
</p>
</div>
Thanks for any help,

you could use negative margin and overflow.
Negative margin to virtually reduce width, space needed by pseudo-elements down to zero, :
http://jsfiddle.net/35waA/3/
#container {
width:400px;
background:pink;
margin:0 auto;
padding: 10px 0;
}
.border {
display:block;
background:yellow;
text-align:center;
overflow:hidden;
}
.border span {
white-space:nowrap;
margin: 0;
display:inline-block;
}
p.border span:after, p.border span:before {
content:" ";
display: inline-block;
width:200%;
height:5px;
border:solid gray;
border-width:1px 0;
}
p.border span:before {
margin-left:-200%;
}
p.border span:after {
margin-right:-200%;
}
Else , background-image /gradient will do too.

Related

CSS Horizontally align text over image

I don't know if it is possible, but is it possible for a <span> of variable text length to be horizontally aligned over an image? The one catch with this, is I'd like the <span> to have a background color and the background color block should overlap the image.
I've tried setting the <span> to display:inline-block but it doesn't seem to end up horizontally aligned. Here is the code if you don't want to look in the fiddle (the HTML here should remain the same if-at-all-possible)
The Code (https://jsfiddle.net/6c9gmvom/1/):
#wrapper {
width:100%;
}
.txt {
text-align:center;
margin:0 auto;
width:40px; /* ideally I would not want to use a fixed width here */
background-color:#ffffff;
}
img {
height:30px;
width:100%;
}
<div id="wrapper">
<img src="http://seedmagazine.com/slideshow/the_long_shot/img/8_the_long_shot_ss.jpg">
<div id="modulewrapper">
<span class="txt">hey</span>
<div id="module"></div>
</div>
</div>
You can align it with css. I've updated your fiddle, check it out to see the result https://jsfiddle.net/6c9gmvom/8/
The css I've added is:
#wrapper {
width:100%;
position: relative;
}
#modulewrapper {
text-align:center;
position: absolute;
top:0;
left:0;
right:0;
}
You can set the image as a background image... then set the span to display block.....
#wrapper {
width:100%;
}
.txt {
text-align:center;
margin: 10px auto;
background-color:#ffffff;
display: block;
text-align: center;
} /* ideally I would not want to use a fixed width here */
img {
height:30px;
width:100%;
}
#modulewrapper {
padding: 200px 0;
background: url(http://seedmagazine.com/slideshow/the_long_shot/img/8_the_long_shot_ss.jpg) no-repeat center center; }
<div id="wrapper">
<div id="modulewrapper">
<span class="txt">hey</span>
<div id="module"></div>
</div>
</div>
I'm not too sure I understand what it is you are asking.
If im not mistaken what you want is for the text to be on top of the image.
This can be done as seen https://jsfiddle.net/6c9gmvom/9/
#wrapper {width:100%;position:relative}
.txt {
position:absolute;
text-align:center;
margin:0 auto;
width:40px;
background-color:#ffffff;
z-index: 100;
width:100%;
}
img {
height:30px;width:100%;
}
.imageClass{
position:absolute;
left: 0;
top: 0;
}
.txt {
text-align:center;
margin:0 auto;
width:100%;
background-color:#ffffff;
position: fixed;
left: 0;
top: 15px;
}
img {
height:30px;
width:100%;
position: relatable;
}

Centering UL navigation horizontally using CSS sprite background

I have a 4 element horizontal list using a sprite image as the li background that I've been trying to get centered in the footer div. At this point I think I'm just running in circles randomly changing styles trying to get it. Here's the relevent CSS and HTML:
#footer-share-links {
width:400px;
text-align:center;
margin:10px auto;
background:#FF6666;
border: 1px solid red;
height:36px;
}
#footer-share-links ul {
padding:0;
position:relative;
list-style-type:none;
}
#footer-share-links li {
margin:0 auto;
display:inline;
float:left;
text-align:center;
position:absolute;
}
#footer-share-links li, #footer-share-links a {
height:36px;
}
#ftr_facebook {left:0px;width:25px;}
#ftr_facebook {background:url('sprites/spriteGlobal.2014-0001.png') 0 0;}
#ftr_twitter {left:30px;width:26px;}
#ftr_twitter {background:url('sprites/spriteGlobal.2014-0001.png') -25px 0;}
#ftr_gplus {left:61px;width:26px;}
#ftr_gplus {background:url('sprites/spriteGlobal.2014-0001.png') -77px 0;}
#ftr_linkedin {left:93px;width:26px;}
#ftr_linkedin {background:url('sprites/spriteGlobal.2014-0001.png') -51px 0;}
The background and border on the div are just to help me see the box. The HTML is:
<div id="footer-share-links">
<ul>
<li id="ftr_facebook"></li>
<li id="ftr_twitter"></li>
<li id="ftr_gplus"></li>
<li id="ftr_linkedin"></li>
</ul>
</div>
Using this I get a centered red box from the div, but the list elements are pushed to the left edge. If I add margin:0 auto; to the UL it stays the same. If I add margin-left:100px it does move it towards center, but I don't want to use a fixed value unless I absolutely have to.
Stripped out some of the competing properties to the bare minimum.
JSfiddle Demo
CSS
#footer-share-links {
width:400px;
text-align:center; /* this centers the inline-block list items */
margin:10px auto;
background:#FF6666;
border: 1px solid red;
height:36px;
}
#footer-share-links ul {
padding:0;
position:relative;
list-style-type:none;
margin: 0;
}
#footer-share-links li {
display:inline-block;
width:26px;
height:36px;
background-image: url(http://lorempixel.com/output/abstract-q-c-25-25-6.jpg);
background-position: center;
}
Here is one way of doing it, try the following CSS:
#footer-share-links {
width:400px;
text-align:center;
margin:10px auto;
background:#FF6666;
border: 1px solid red;
height:36px;
line-height: 36px;
}
#footer-share-links ul {
padding:0;
margin: 0;
list-style-type:none;
}
#footer-share-links li {
margin: 0;
display: inline-block;
width: 50px; /* demo only */
background-color: yellow; /* demo only */
}
#footer-share-links a {
display: block; /* or inline-block */
}
Demo: http://jsfiddle.net/audetwebdesign/yN5MP/
As suggested earlier, remove any floats and absolute positioning.
For the parent container #footer-share-links, set the line-height equal to the height value, that way the vertical centering takes care of itself.
On the ul element, make sure to zero out the margins.
Finally, on the li elements, use display: inline-block so that the width is recognized.
You can use your id names to specify width's on the individual li elements as needed.
On the a elements (links), use display: block to get make the link take up the width of the li element so that the link has enough active/control area.
What you need to do:
#footer-share-links li {
/*margin:0 auto;*/
display:inline-block; // change this to inline-block
/*float:left;*/
text-align:center;
/*position:absolute;*/
}
#footer-share-links ul {
...
...
margin: 0; // add this
}
Take out those I commented out, it works

How do I make a inline-block div occupy the rest of the space

I have a link and an inline div next to it (to the right). I want the div to occupy the rest of the space to the right. Is there a way to do that?
what<div style="display:inline-block;width:200px;border:1px solid red">hello</div>
If you can wrap a span around the div, like:
what<span><div>hello</div></span>
jsFiddle example
You can apply this CSS to get what you're after:
a {
background: #ccc;
float: left
}
span {
display: block;
overflow: hidden;
padding: 0 4px 0 6px
}
div {
width: 100%;
display:inline-block;
border:1px solid red
}
You could wrap everything within a div and give it table and table cell to children:
http://jsfiddle.net/T4Qcd/
.inner{
border:1px solid red;
display:table-cell;
width:100%;
}
a{
display:table-cell;
}
.wrapper{
display:table;
}
You can do so by applying width to both anchor tag as well as the div.
a{
width:10%;
}
div{
width:90%;
}
Your html would be.
what
<div style="display:inline-block;border:1px solid red">hello</div>

why my div is slightly moved to the right

So I'm just a begginer to this HTML and CSS stuff, and I tried to make my own webpage. The thing is, it looks like this:
While I would like to get the second div(#diary) centered, but I can't do it without screwing up the whole webpage. Which will be the correct code?
This is what I have:
HTML:
<div id="progress">
Blablabla
</div>
<div id="diary">
blablabla
</div>
CSS:
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin:auto;
width:30em;
display:inline-block;
}
#progress {
font-size:16px;
width:auto;
float:left;
display:inline-block;
margin-left:25px;
}
Thanks in advance ^^
You have mixed display: inline-block and float:left which makes no sense. Elements that float become display: block; by default. http://www.w3.org/TR/CSS2/visuren.html#float-position
There are two ways to solve your problem.
Way1 Go Inline-block all the way:
http://jsfiddle.net/fDx2U/
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin:auto;
width:30em;
display:inline-block;
vertical-align: top;
}
#progress {
font-size:16px;
width:auto;
vertical-align: top;
display:inline-block;
margin-left:25px;
}
How to the rid of the margin between the items: How to remove the space between inline-block elements?
Vital for this solution is the vertical-align:top; (your initial problem)
Way2 Go floating all the way:
http://jsfiddle.net/fDx2U/1/
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin-left: 100px;
}
#progress {
font-size:16px;
width:auto;
float:left;
margin-left:25px;
width: 100px;
}
Vital for this solution is that the width of .diary equals the margin-left of #progress
Try this
#diary {
margin:0 auto;
width:30em;
display:block;
}

How to set up width percentage basis?

I have the following problem:
I'd like to create a html page where a #sidebar spans a constant 27px and a #content spans the remaining part of the screen. The #content is divided into two areas splitting it at 40% - 60%.
<html>
<body>
<div id="sidebar">
</div>
<div id="content">
<div id="forty-percent">
</div>
<div id="sixty-percent">
</div>
</div>
</body>
</html>
I have tried to make the following css:
#sidebar{
width:27px;
}
#content{
position:absolute;
padding-left:27px;
width:100%;
}
But then I cannot divide the content into 40%-60%, because percentages are calculated from the width of the #content and not from its area inside.
What am I doing wrong? Can you please help?
UPDATE:
The demo of the NOT working version:
http://jsbin.com/iseqon/1/edit
Ideally the dashed boxes should be side-by-side, inside the blue box.
This may suit more your needs. If you want to have a better control of your #sidebar & #content vertical alignment, you must use inline-block to have a CSS only solution.
You can view it live here: http://codepen.io/jpsirois/pen/dvbmEy
* {
/* This prevent padding to be added on defined width */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-size: 0; /* Need to be set to 0 to properly use inline-block here */
color: white; /* For a better preview purpose only */
}
#sidebar,
#content {
display: inline-block; /* Allow vertical-align control (float didn’t) */
font-size: 16px; /* Reset font-size to normal */
vertical-align: middle; /* Demo of vertical-alignement */
}
#sidebar {
width: 27px;
background: darkred;
height: 50px; /* For a better preview purpose only */
margin-right: -27px; /* This allow #content to be inlined aside */
}
#content {
font-size: 0; /* Need to be set to 0 to properly use inline-block here */
width: 100%;
padding-left: 27px;
}
#forty-percent,
#sixty-percent {
height: 100px;/* For a better preview purpose only */
display: inline-block;
font-size: 16px; /* Reset font-size to normal */
}
#forty-percent {
width: 40%;
background: darkgreen;
}
#sixty-percent {
width: 60%;
background: darkblue;
}
You need this to float the #sidebar and give an equal margin-left to the #content, and also float the two inner boxes so they can sit side by side..
#sidebar {
width:27px;
float:left;
}
#content {
margin-left:27px;
overflow:auto;
}
#forty-percent {
width:40%;
float:left;
}
#sixty-percent {
width:60%;
float:left;
}
and also to not use the # char in the actual id
Demo at http://jsfiddle.net/gaby/8a7CN/
(your fixed jsbin demo at http://jsbin.com/iseqon/4/edit you need to keep in mind that borders add to the width so it cannot work with percentages very well)
how about having a parent div that would be relative and then having the div inside float right or left with absolute position within container. when the parent container is pos relative and the child is pos absolute, the children with position with respect to their container. In other words, something like that (untested but should give you the right idea)
#wrapper {
position:relative;
width:100%;
margin:50px;
}
#leftCol {
width:60%;
background-color:yellow;
}
#rightCol {
width:40%;
position:absolute;
right:0px;
top:0px;
height:100px;
background-color:red;
}
</style>
<div id='wrapper'>
<div='leftCol'>
</div>
<div id='rightCol'>
</div>
</div>
I am using your HTML only change the CSS.
My CSS is
#sidebar
{
width:27px;
min-width:27px;
float:left;
}
#content
{
float:right;
width:100%-28px;
min-width:100%-28px;
}
#forty-percent
{
width:40%;
float:left;
}
#sixty-percent
{
width:60%;
float:right;
}
Hope this will help you.Thanks.