What happens if I define both the left and right properties for spans?

This is a class for span with the CSS properties

position:absolute; top:0px; left:0px; right:0px;

.textStyle1 {
  font-size:24px;
  font-family:'Arial';
  position:absolute;
  top:0px;
  left:0px;
  right:0px;
}

#div1 {
 position:absolute;
 left:100px;
 top:100px;
 width:200px;
 height:200px;
}

#div2 {
 position:absolute;
 left:100px;
 top:100px;
 width:200px;
 height:200px;
 writing-mode:tb-rl;
}

This class is used in spans placed inside divs

<div id="div2">
 <span class="textStyle1">Some text</span>
 <span class="textStyle1" style="left:2px; top:30px;">Another line of text</span>
</div>

<div id="div2">
 <span class="textStyle1">Some text</span>
 <span class="textStyle1" style="left:2px; top:30px;">Another line of text</span>
</div>

.textStyle1 {
  font-size: 24px;
  font-family: 'Arial';
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0px;
}

#div1 {
  position: absolute;
  left: 50px;
  top: 100px;
  width: 200px;
  height: 200px;
  background-color: Yellow;
}

#div2 {
  position: absolute;
  left: 270px;
  top: 100px;
  width: 200px;
  height: 200px;
  writing-mode: tb-rl;
  -webkit-writing-mode: vertical-rl;
  background-color: Cyan;
}
<div id="div1">
  <span class="textStyle1">Some text</span>
  <span class="textStyle1" style="left:2px; top:30px;">Another line of text</span>
</div>

<div id="div2">
  <span class="textStyle1">Some text</span>
  <span class="textStyle1" style="top:2px; right:30px;">Another line of text</span>
</div>

Although the inner elements are correctly positioned, there may be confusion regarding how the position is calculated when both left and right values are provided. It is unclear whether one value takes precedence based on the writing mode. This question specifically relates to span elements.

Answer №1

The span will expand to fill the entire width of the nearest parent element with position:relative enabled.

If there are no parent elements with position:relative set, the span will instead expand to the width of the window.

Answer №2

top and left take precedence over bottom and right when the browser aims to adhere to W3C standards, as outlined:

If all three of 'left', 'width', and 'right' are set to 'auto': Start by changing any 'auto' values for 'margin-left' and 'margin-right' to 0. Next, if the element's containing block with static positioning is 'ltr', set 'left' to the static position and follow rule number three below; otherwise, set 'right' to the static position and proceed with rule number one.

If none of the three attributes are set to 'auto': In a scenario where both 'margin-left' and 'margin-right' are 'auto', find a solution ensuring equal values for both margins unless this causes them to be negative. If the direction of the containing block is 'ltr' ('rtl'), change 'margin-left' ('margin-right') to zero and solve for 'margin-right' ('margin-left'). If either 'margin-left' or 'margin-right' is 'auto', work towards finding the value for that attribute. In case of conflicting values, prioritize 'left' (for 'rtl' direction) or 'right' (for 'ltr' direction) and resolve accordingly.

Otherwise, reset 'auto' values for 'margin-left' and 'margin-right' to 0, and apply one of the following six rules:

When 'left' and 'width' are 'auto' but 'right' is not, the width adjusts to fit the content, then solve for 'left'. When 'left' and 'right' are 'auto' and 'width' is fixed, set 'left' to the static position for an 'ltr' container block or 'right' for 'rtl'. Proceed to solve for 'left' in the case of 'rtl' or 'right' for 'ltr'. When 'width' and 'right' are 'auto' and 'left' is specified, adjust the width to fit the content and then solve for 'right'. If 'left' is 'auto' while 'width' and 'right' are defined, work on resolving 'left'. When 'width' is 'auto' and 'left' and 'right' are given, focus on finding the suitable width. Lastly, if 'right' is 'auto' but 'left' and 'width' are provided, determine the appropriate value for 'right'.

Source: http://www.w3.org/TR/CSS2/visudet.html

Answer №3

A similar result can be achieved by using width: 100%. For instance:

.box {
  height: 300px;
  width: 300px;
  position: relative;
}
.orange {
  position: absolute;
  top: 50px;
  left: 0;
  right: 0;
  bottom: 50px;
  background: rgba(255, 165, 0, .5);
}
.purple {
  position: absolute;
  top: 0;
  left: 80px;
  right: 80px;
  bottom: 0;
  background: rgba(128, 0, 128, .5);
}
.yellow {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 0, .5);
}
<div class="box">
  <div class="yellow">Full Size</div>
  <div class="orange">Full Width</div>
  <div class="purple">Full Height</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Replace all existing content on the webpage with an exciting Unity game upon clicking

In an interesting experiment, I am trying to hide a secret href that, once clicked, has the power to wipe out everything on the page, leaving it temporarily blank before replacing it with a captivating Unity game that takes over the entire page. Let me sh ...

Cannot Get jQuery .flip() to Work Automatically Every 2 Seconds

There are 3 words that I want to rotate on their x-axis every 2 seconds (repeating). Using jQuery: $(function () { count = 0; wordsArray = ["Innovation", "Creativity", "Success"]; setInterval(function () { count++; $("#words").text(wordsArray[co ...

Automatic keyboard suggestions not working for HTML search input using Ajax

I am currently working on a PHP web application that uses a MySql database. I have implemented a search suggestion box using ajax. The issue I am facing is that the suggestions can be selected with the mouse and auto-completed, but not when using the keybo ...

`Count the number of rows needed to accommodate text line breaks within a div element`

Is there a method to determine the number of lines that text breaks into using the CSS property word-break: break-all? For example, if I have a div like this: <div>Sample text to check how many lines the text is broken into</div> And the corr ...

Which specific html container or element can be found on the mymsn pages?

When accessing mymsn, users have the ability to personalize the content and layout of their webpage. I am curious about what type of container is being utilized for this customization - does it involve an html element, or perhaps javascript, or something e ...

What could be causing this Wordpress page to be unresponsive?

I am currently having trouble with the mobile display of a particular website. The site is not rendering correctly on mobile devices. Here is a snippet of the code from the issue: fiddle <div id="contactWrapper">. While the JSFiddle example shows a ...

What is the best way to determine if a radio button has been chosen, and proceed to the next radio button to display varied information?

The goal is to display the <div class="resp"> below each radio button when it is selected, with different content in each <div class="resp">. The previously selected <div class="resp"> should be hidden when a new radio button is chosen. O ...

Failure to connect HTML and CSS files

My HTML and CSS files are not linking even though they are in the same folder. Here is my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatibl ...

Increasing the upward motion of the matrix raining HTML canvas animation

Recently, I've been experimenting with the Matrix raining canvas animation here and I was intrigued by the idea of making it rain upwards instead of downwards. However, my attempts to achieve this using the rotate() method resulted in skewing and stre ...

Adaptive Design - Content Fragmentation

I am currently working on a webpage layout that has the following HTML structure: <div class="row"> <div class="col3">test 1</div> <div class="col3">test 2</div> <div class="col3">test 3</div> </div ...

Content not being hidden by Twitter Bootstrap tabs

I am encountering an issue with my code. When I click on an inactive tab, the content of the first tab does not hide and both tabs' content is displayed simultaneously. Is there a solution to this problem? Thank you for your help! <ul style="l ...

Interactive text animation triggered by a click

jQuery ( function ( $ ) { console.log(">>Testing animation"); $('a.loveit').on('click',function(event){ event.preventDefault(); var text = $(this).find('div.love-text'); ...

What is the trick to shortening paragraphs with dots...?

There is a p tag, <p> most iconic character in cinema</p> I need to truncate the text after 8 characters, and display dots afterwards. For example: most ic... Is there a way to achieve this using CSS? Any help with AngularJS would also be ...

Tips for choosing a particular list item using jQuery: retrieve the attribute value of 'value' and showcase it on the webpage

I have a task that requires the following: Implement an event listener so that when you click on any list item, the value of its "value" attribute will be shown next to this line. In my HTML, I have an ordered list with 8 list items. The values range fro ...

Listening for select elements that have remained unchanged

My current situation involves a select box with predetermined options. One of these options is preselected when the page loads: <select name="select" class="js-choice-select"> <option value="option-1">Option 1</option> <option ...

Issue with Javascript/Jquery functionality within a PHP script

I have been attempting to incorporate a multi-select feature (view at http://jsfiddle.net/eUDRV/318/) into my PHP webpage. While I am able to display the table as desired, pressing the buttons to move elements from one side to another does not trigger any ...

Engaging with JSON data inputs

Need help! I'm attempting to fetch JSON data using AJAX and load it into a select control. However, the process seems to get stuck at "Downloading the recipes....". Any insights on what might be causing this issue? (Tried a few fixes but nothing has w ...

I'm struggling to understand how to interpret this. The v-tab function seems to be generating a button with various properties, but I'm unsure which specific property is related to

The V-tab below generates the button known as the right one on the v-app-bar: https://i.stack.imgur.com/DzNmq.png <v-tab :to="'/demo'" active-class="text--primary" class=&quo ...

How can I ensure that Chakra UI MenuList items are always visible on the screen?

Currently, I am utilizing Chakra UI to design a menu and here is what I have so far: <Menu> <MenuButton>hover over this</MenuButton> <MenuList> <Flex>To show/hide this</Flex> </MenuList> </ ...

Is there a way to eliminate the border surrounding every cell in a table?

I'm facing an issue with a table on a page with a gray background. The challenge is to remove the borders of the cells in the table. <table width="510"> <tbody> <tr> <td style="background-color: #fff;" colspan="2" width="510"> ...